|
| 1 | +require 'json' |
| 2 | +require 'tempfile' |
| 3 | +require 'open3' |
| 4 | +require 'bundler/inline' |
| 5 | +require 'optparse' |
| 6 | + |
| 7 | +gemfile do |
| 8 | + source 'https://rubygems.org' |
| 9 | + gem 'octokit' |
| 10 | +end |
| 11 | + |
| 12 | +class ScanPr |
| 13 | + def initialize |
| 14 | + @config_file = nil |
| 15 | + @github_token = ENV["GITHUB_TOKEN"] |
| 16 | + |
| 17 | + validate_token |
| 18 | + end |
| 19 | + |
| 20 | + def run(args) |
| 21 | + parse_options(args) |
| 22 | + repo_nwo, pr_number = extract_repo_and_pr(args) |
| 23 | + |
| 24 | + pr = fetch_pull_request(repo_nwo, pr_number) |
| 25 | + event_file = create_event_file(pr) |
| 26 | + |
| 27 | + execute_dependency_review(repo_nwo, event_file) |
| 28 | + ensure |
| 29 | + event_file&.unlink |
| 30 | + end |
| 31 | + |
| 32 | + private |
| 33 | + |
| 34 | + def validate_token |
| 35 | + if !@github_token || @github_token.empty? |
| 36 | + puts "Please set the GITHUB_TOKEN environment variable" |
| 37 | + exit -1 |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + def parse_options(args) |
| 42 | + op = OptionParser.new do |opts| |
| 43 | + usage = <<EOF |
| 44 | +Run Dependency Review on a repository. |
| 45 | +
|
| 46 | +\e[1mUsage:\e[22m |
| 47 | + scripts/scan_pr [options] <pr_url> |
| 48 | +
|
| 49 | +\e[1mExample:\e[22m |
| 50 | + scripts/scan_pr https://github.com/actions/dependency-review-action/pull/294 |
| 51 | +
|
| 52 | +EOF |
| 53 | + |
| 54 | + opts.banner = usage |
| 55 | + |
| 56 | + opts.on('-c', '--config-file <FILE>', 'Use an external configuration file') do |cf| |
| 57 | + @config_file = cf |
| 58 | + end |
| 59 | + |
| 60 | + opts.on("-h", "--help", "Prints this help") do |
| 61 | + puts opts |
| 62 | + exit |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + op.parse!(args) |
| 67 | + @option_parser = op |
| 68 | + end |
| 69 | + |
| 70 | + def extract_repo_and_pr(args) |
| 71 | + # make sure we have a NWO somewhere in the parameters |
| 72 | + arg = /(?<repo_nwo>[\w\-]+\/[\w\-]+)\/pull\/(?<pr_number>\d+)/.match(args.join(" ")) |
| 73 | + |
| 74 | + if arg.nil? |
| 75 | + puts @option_parser |
| 76 | + exit -1 |
| 77 | + end |
| 78 | + |
| 79 | + [arg[:repo_nwo], arg[:pr_number]] |
| 80 | + end |
| 81 | + |
| 82 | + def fetch_pull_request(repo_nwo, pr_number) |
| 83 | + octo = Octokit::Client.new(access_token: @github_token) |
| 84 | + octo.pull_request(repo_nwo, pr_number) |
| 85 | + end |
| 86 | + |
| 87 | + def create_event_file(pr) |
| 88 | + event_file = Tempfile.new |
| 89 | + event_file.write("{ \"pull_request\": #{pr.to_h.to_json}}") |
| 90 | + event_file.close |
| 91 | + event_file |
| 92 | + end |
| 93 | + |
| 94 | + def execute_dependency_review(repo_nwo, event_file) |
| 95 | + action_inputs = { |
| 96 | + "repo-token": @github_token, |
| 97 | + "config-file": @config_file |
| 98 | + } |
| 99 | + |
| 100 | + dev_cmd_env = { |
| 101 | + "GITHUB_REPOSITORY" => repo_nwo, |
| 102 | + "GITHUB_EVENT_NAME" => "pull_request", |
| 103 | + "GITHUB_EVENT_PATH" => event_file.path, |
| 104 | + "GITHUB_STEP_SUMMARY" => "/dev/null" |
| 105 | + } |
| 106 | + |
| 107 | + # Merge action inputs into environment, formatting keys as INPUT_... |
| 108 | + action_inputs_env = action_inputs.each_with_object({}) do |(name, value), h| |
| 109 | + h["INPUT_#{name.to_s.upcase}"] = value unless value.nil? |
| 110 | + end |
| 111 | + env = dev_cmd_env.merge(action_inputs_env) |
| 112 | + |
| 113 | + dev_cmd = [ |
| 114 | + "./node_modules/.bin/nodemon", |
| 115 | + "--exec", |
| 116 | + "node", |
| 117 | + "-r", |
| 118 | + "esbuild-register", |
| 119 | + "src/main.ts" |
| 120 | + ] |
| 121 | + |
| 122 | + Open3.popen2e(env, *dev_cmd) do |stdin, out| |
| 123 | + while line = out.gets |
| 124 | + puts line.gsub(@github_token, "<REDACTED>") |
| 125 | + end |
| 126 | + end |
| 127 | + end |
| 128 | +end |
0 commit comments