-
Notifications
You must be signed in to change notification settings - Fork 158
Make Ruby Code Scannable #978
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,87 +1,8 @@ | ||
| #!/usr/bin/env ruby | ||
| require 'json' | ||
| require 'tempfile' | ||
| require 'open3' | ||
| require 'bundler/inline' | ||
| require 'optparse' | ||
|
|
||
| gemfile do | ||
| source 'https://rubygems.org' | ||
| gem 'octokit' | ||
| end | ||
| # Load the scan_pr library | ||
| require_relative 'scan_pr_lib' | ||
|
|
||
| config_file = nil | ||
| github_token = ENV["GITHUB_TOKEN"] | ||
|
|
||
| if !github_token || github_token.empty? | ||
| puts "Please set the GITHUB_TOKEN environment variable" | ||
| exit -1 | ||
| end | ||
|
|
||
| op = OptionParser.new do |opts| | ||
| usage = <<EOF | ||
| Run Dependency Review on a repository. | ||
|
|
||
| \e[1mUsage:\e[22m | ||
| scripts/scan_pr [options] <pr_url> | ||
|
|
||
| \e[1mExample:\e[22m | ||
| scripts/scan_pr https://github.com/actions/dependency-review-action/pull/294 | ||
|
|
||
| EOF | ||
|
|
||
| opts.banner = usage | ||
|
|
||
| opts.on('-c', '--config-file <FILE>', 'Use an external configuration file') do |cf| | ||
| config_file = cf | ||
| end | ||
|
|
||
| opts.on("-h", "--help", "Prints this help") do | ||
| puts opts | ||
| exit | ||
| end | ||
| end | ||
|
|
||
| op.parse! | ||
|
|
||
| # make sure we have a NWO somewhere in the parameters | ||
| arg = /(?<repo_nwo>[\w\-]+\/[\w\-]+)\/pull\/(?<pr_number>\d+)/.match(ARGV.join(" ")) | ||
|
|
||
| if arg.nil? | ||
| puts op | ||
| exit -1 | ||
| end | ||
|
|
||
| repo_nwo = arg[:repo_nwo] | ||
| pr_number = arg[:pr_number] | ||
|
|
||
| octo = Octokit::Client.new(access_token: github_token) | ||
| pr = octo.pull_request(repo_nwo, pr_number) | ||
|
|
||
| event_file = Tempfile.new | ||
| event_file.write("{ \"pull_request\": #{pr.to_h.to_json}}") | ||
| event_file.close | ||
|
|
||
| action_inputs = { | ||
| "repo-token": github_token, | ||
| "config-file": config_file | ||
| } | ||
|
|
||
| dev_cmd_env = { | ||
| "GITHUB_REPOSITORY" => repo_nwo, | ||
| "GITHUB_EVENT_NAME" => "pull_request", | ||
| "GITHUB_EVENT_PATH" => event_file.path, | ||
| "GITHUB_STEP_SUMMARY" => "/dev/null" | ||
| } | ||
|
|
||
| # bash does not like variable names with dashes like the ones Actions | ||
| # uses (e.g. INPUT_REPO-TOKEN). Passing them through `env` instead of | ||
| # manually setting them does the job. | ||
| action_inputs_env_str = action_inputs.map { |name, value| "\"INPUT_#{name.upcase}=#{value}\"" }.join(" ") | ||
| dev_cmd = "./node_modules/.bin/nodemon --exec \"env #{action_inputs_env_str} node -r esbuild-register\" src/main.ts" | ||
|
|
||
| Open3.popen2e(dev_cmd_env, dev_cmd) do |stdin, out| | ||
| while line = out.gets | ||
| puts line.gsub(github_token, "<REDACTED>") | ||
| end | ||
| end | ||
| # Create and run the scanner | ||
| scanner = ScanPr.new | ||
| scanner.run(ARGV) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| require 'json' | ||
|
||
| require 'tempfile' | ||
| require 'open3' | ||
| require 'bundler/inline' | ||
| require 'optparse' | ||
|
|
||
| gemfile do | ||
| source 'https://rubygems.org' | ||
| gem 'octokit' | ||
| end | ||
|
|
||
| class ScanPr | ||
| def initialize | ||
| @config_file = nil | ||
| @github_token = ENV["GITHUB_TOKEN"] | ||
|
|
||
| validate_token | ||
| end | ||
|
|
||
| def run(args) | ||
| parse_options(args) | ||
| repo_nwo, pr_number = extract_repo_and_pr(args) | ||
|
|
||
| pr = fetch_pull_request(repo_nwo, pr_number) | ||
| event_file = create_event_file(pr) | ||
|
|
||
| execute_dependency_review(repo_nwo, event_file) | ||
| ensure | ||
| event_file&.unlink | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def validate_token | ||
| if !@github_token || @github_token.empty? | ||
| puts "Please set the GITHUB_TOKEN environment variable" | ||
| exit -1 | ||
| end | ||
| end | ||
|
|
||
| def parse_options(args) | ||
| op = OptionParser.new do |opts| | ||
| usage = <<EOF | ||
| Run Dependency Review on a repository. | ||
|
|
||
| \e[1mUsage:\e[22m | ||
| scripts/scan_pr [options] <pr_url> | ||
|
|
||
| \e[1mExample:\e[22m | ||
| scripts/scan_pr https://github.com/actions/dependency-review-action/pull/294 | ||
|
|
||
| EOF | ||
|
|
||
| opts.banner = usage | ||
|
|
||
| opts.on('-c', '--config-file <FILE>', 'Use an external configuration file') do |cf| | ||
| @config_file = cf | ||
| end | ||
|
|
||
| opts.on("-h", "--help", "Prints this help") do | ||
| puts opts | ||
| exit | ||
| end | ||
| end | ||
|
|
||
| op.parse!(args) | ||
| @option_parser = op | ||
| end | ||
|
|
||
| def extract_repo_and_pr(args) | ||
| # make sure we have a NWO somewhere in the parameters | ||
| arg = /(?<repo_nwo>[\w\-]+\/[\w\-]+)\/pull\/(?<pr_number>\d+)/.match(args.join(" ")) | ||
|
|
||
| if arg.nil? | ||
| puts @option_parser | ||
| exit -1 | ||
| end | ||
|
|
||
| [arg[:repo_nwo], arg[:pr_number]] | ||
| end | ||
|
|
||
| def fetch_pull_request(repo_nwo, pr_number) | ||
| octo = Octokit::Client.new(access_token: @github_token) | ||
| octo.pull_request(repo_nwo, pr_number) | ||
| end | ||
|
|
||
| def create_event_file(pr) | ||
| event_file = Tempfile.new | ||
| event_file.write("{ \"pull_request\": #{pr.to_h.to_json}}") | ||
| event_file.close | ||
| event_file | ||
| end | ||
|
|
||
| def execute_dependency_review(repo_nwo, event_file) | ||
| action_inputs = { | ||
| "repo-token": @github_token, | ||
| "config-file": @config_file | ||
| } | ||
|
|
||
| dev_cmd_env = { | ||
| "GITHUB_REPOSITORY" => repo_nwo, | ||
| "GITHUB_EVENT_NAME" => "pull_request", | ||
| "GITHUB_EVENT_PATH" => event_file.path, | ||
| "GITHUB_STEP_SUMMARY" => "/dev/null" | ||
| } | ||
|
|
||
| # Merge action inputs into environment, formatting keys as INPUT_... | ||
| action_inputs_env = action_inputs.each_with_object({}) do |(name, value), h| | ||
| h["INPUT_#{name.to_s.upcase}"] = value unless value.nil? | ||
| end | ||
| env = dev_cmd_env.merge(action_inputs_env) | ||
|
|
||
| dev_cmd = [ | ||
| "./node_modules/.bin/nodemon", | ||
| "--exec", | ||
| "node", | ||
| "-r", | ||
| "esbuild-register", | ||
| "src/main.ts" | ||
| ] | ||
|
|
||
| Open3.popen2e(env, *dev_cmd) do |stdin, out| | ||
| while line = out.gets | ||
| puts line.gsub(@github_token, "<REDACTED>") | ||
| end | ||
| end | ||
| end | ||
| end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot detected a code snippet with 2 occurrences. See search results for more details.
Matched Code Snippet