Get GH token using expected variable - #323
Conversation
| # "REPOS_PATH=infrastructure GH_ACCESS_TOKEN=abc123 COCINA_LEVEL2= ./merge-all.rb" | ||
| BRANCH_NAME = ENV.fetch('BRANCH_NAME', 'update-dependencies') | ||
| COCINA_LEVEL2_BRANCH_NAME = 'cocina-level2-updates' | ||
| # To generate update-dependencies PRs for all repos in <REPOS_PATH>/projects.yml: |
There was a problem hiding this comment.
I've never felt that having quotes around the command hints was useful because it makes copy/paste a tad more difficult. Remove the quotes. (I could also be convinced to remove the dollar signs.)
Improve clarity of code comments.
| # NOTE: The above variables may also be set as environment variables instead, in | ||
| # which case they will be picked up by the script without needing to be | ||
| # passed in on the command line. |
There was a problem hiding this comment.
Clarify that env vars may either be set in the shell session or provided on the command-line.
| require 'highline/import' | ||
| require 'octokit' | ||
| require 'yaml' |
There was a problem hiding this comment.
Co-locate all require statements.
There was a problem hiding this comment.
Pull request overview
Updates the merge-all.rb script to use a more standard GitHub token environment variable and refactors parts of the script’s repo loading and PR status handling.
Changes:
- Updates usage instructions and token lookup to prefer
GITHUB_TOKEN(with fallback toGH_ACCESS_TOKEN). - Refactors projects loading / filtering logic and PR enumeration (uses
filter_map). - Adjusts PR status handling and exit codes for “no PRs found” / “checks failing” cases.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # To generate update-dependencies PRs for all repos in <REPOS_PATH>/projects.yml: | ||
| # | ||
| # $ REPOS_PATH=infrastructure GITHUB_TOKEN=abc123 ./merge-all.rb | ||
| # | ||
| # To generate PRs only for projects that rely on Cocina updates: |
|
|
||
| def access_token | ||
| @access_token = ENV['GITHUB_TOKEN'] || ENV['GH_ACCESS_TOKEN'] | ||
| raise 'GITHUB_TOKEN variable must be set' if @access_token.nil? || @access_token.empty? |
|
|
||
| require 'octokit' | ||
| client = Octokit::Client.new(access_token: access_token) | ||
| client = Octokit::Client.new(access_token:) |
| def status_from(statuses, checks) | ||
| # GitHub API marks PRs with 0 statuses as "pending", we cast that to success | ||
| return 'success' if (statuses.state == 'success' || statuses.total_count.zero?) && | ||
| checks.check_runs.map(&:conclusion).all? { |status| status == 'success' } | ||
|
|
||
| 'failure' | ||
| end | ||
|
|
||
| def access_token | ||
| ENV['GH_ACCESS_TOKEN'] | ||
| end | ||
|
|
||
| # anything other than nil or false is true here, just as Ruby intended | ||
| def cocina_level2 | ||
| ENV['COCINA_LEVEL2'] | ||
| (statuses.state == 'success' || statuses.total_count.zero?) && | ||
| checks.check_runs.map(&:conclusion).all? { |status| status == 'success' } |
| unless pr_list.all? { |pr| pr[:status] } | ||
| puts '*No* PRs were merged because these PRs are not passing: ' | ||
| pr_list.filter { |pr| pr[:status] != 'success' }.each do |pr| | ||
| pr_list.reject { |pr| pr[:status] }.each do |pr| | ||
| puts "#{pr[:status]} - #{pr[:url]}" | ||
| end |
| COCINA_LEVEL2_BRANCH_NAME = ENV.fetch('COCINA_LEVEL2_BRANCH_NAME', 'cocina-level2-updates') | ||
|
|
||
| def access_token | ||
| @access_token = ENV['GITHUB_TOKEN'] || ENV['GH_ACCESS_TOKEN'] |
There was a problem hiding this comment.
AFAICT, GITHUB_TOKEN is the recommended and most often used env var for this token, so allow that one to work, but don't break anyone who's been relying on the bespoke (and/or outdated) env var this codebase has been using.
If this is only a minor change to the scripts, please 🔪 kill the Jenkins build. 🔪
Navigate from SUL CI ➡️ Stanford University Digital Library ➡️ access-update-scripts ➡️ Branches / main ➡️ Build History ➡️ Cancel build button (🆇)