Skip to content

Commit

Permalink
Merge pull request #8 from affrae/affrae-v1.5-output-directory
Browse files Browse the repository at this point in the history
added suppport for -d --dir to output reports to a particular directory
  • Loading branch information
affrae committed May 5, 2021
2 parents a4b5e31 + b250573 commit 5c9e545
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,25 @@ to get the last report on that table
```

### Downloading reports

#### Output directory

By default the reports will be downloaded to the directory you are calling the script **_from_** - **_not_** the directory the script is stored in.
So for example if you issued the command:

``` zsh
cd myreports; ghasrd.rb -o myowner -r myrepo -g 5876671
```

the report(s) will be downloaded to the `myreports` directory

The `-d [DIRECTORY]` or `--dir [DIRECTORY]` option allows you to set where the reports will be downloaded to. The following command:

``` zsh
mkdir ~/myreports; ghasrd.rb -o myowner -r myrepo -g 5876671 -d ~/myreports
```

will download the report(s) to the `myreports` directory in your home directory - no matter where you call the script from.
#### By analysis ID

If you know the ID (or multiple IDs) for an analyis (you can get a list of IDs using the `-l` option), you can use the following command to download the report for each ID:
Expand Down
22 changes: 20 additions & 2 deletions ghasrd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# frozen_string_literal: true

require 'terminal-table'
require 'pathname'
require 'octokit'
require 'optparse'
require 'ostruct'
Expand All @@ -26,12 +27,21 @@ def self.parse(args)
options.extraVerbose = false
options.api = 'https://api.github.com'
options.hostname = 'github.com'
options.directory = Pathname.new(Dir.pwd)

opt_parser = OptionParser.new do |opts|
opts.banner = "Usage: #{$PROGRAM_NAME} [options]"

opts.separator ''
opts.separator 'Mandatory options:'
opts.on('-d', '--dir DIRECTORY', 'The directory to write the reports to') do |directory|
path = Pathname.new(directory)
raise 'Directory does not exist' unless path.exist?

raise 'Path given is not a directory' unless path.directory?

puts "Output directory is #{path.expand_path}"
options.directory = path.expand_path
end

opts.on('-o', '--owner OWNER', 'The owner of the repository') do |owner|
unless owner.match('^([a-z0-9])(?!.*--)([a-z0-9-])*([a-z0-9])$')
Expand Down Expand Up @@ -196,7 +206,11 @@ def get_report(options, report, file_name)
return
end

File.open(file_name, 'w') { |f| f.write(response.body); puts " Report Downloaded to #{file_name}" }
path = options.directory + file_name
path.open('w') do |f|
f.write(response.body)
puts " Report Downloaded to #{file_name}"
end
end

# Main
Expand Down Expand Up @@ -263,6 +277,8 @@ def get_report(options, report, file_name)

when 'get'
puts 'Getting reports...'
puts " Writing output to: #{options.directory}"

options.report_list.each do |report|
get_report(options, report, "analysis_#{report}.sarif")
end
Expand All @@ -271,6 +287,7 @@ def get_report(options, report, file_name)
when 'pr'
options.pr_list.each do |pr_id|
puts "Getting SARIF report(s) for PR ##{pr_id} in https://#{options.hostname}/#{options.owner}/#{options.repo}:"
puts " Writing output to: #{options.directory}"
pr_info = client.pull_request("#{options.owner}/#{options.repo}", pr_id.to_s)
puts " HEAD is #{pr_info.head.sha}"
reports = client.get("/repos/#{options.owner}/#{options.repo}/code-scanning/analyses")
Expand All @@ -292,6 +309,7 @@ def get_report(options, report, file_name)

when 'sha'
puts 'Getting reports...'
puts " Writing output to: #{options.directory}"
options.sha_list.each do |sha|
begin
commit_info = client.get("/repos/#{options.owner}/#{options.repo}/commits/#{sha}")
Expand Down

0 comments on commit 5c9e545

Please sign in to comment.