Skip to content

Commit 43a1092

Browse files
Merge pull request #9 from thoughtbot/nc/octokit
Use Octokit instead of calling `gh`
2 parents 244a9e0 + 5af7f12 commit 43a1092

14 files changed

Lines changed: 304 additions & 456 deletions

.github/workflows/integration.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ jobs:
5454
with:
5555
github-token: ${{ secrets.GITHUB_TOKEN }}
5656
dry-run: "true"
57-
working-directory: /tmp/test-app
57+
rails-root: /tmp/test-app
5858

5959
- name: Assert all outdated packages appear in dry-run output
6060
run: |

.tool-versions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ruby 4.0.1
1+
ruby 4.0.5

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
source "https://rubygems.org"
22
ruby ">= 3.2"
33

4+
gem "octokit"
5+
46
group :development, :test do
57
gem "rake"
68
gem "minitest"
9+
gem "minitest-mock", "~> 5.27"
710
end
811

912
group :development do

Gemfile.lock

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
11
GEM
22
remote: https://rubygems.org/
33
specs:
4+
addressable (2.9.0)
5+
public_suffix (>= 2.0.2, < 8.0)
46
ast (2.4.3)
57
drb (2.2.3)
8+
faraday (2.14.2)
9+
faraday-net_http (>= 2.0, < 3.5)
10+
json
11+
logger
12+
faraday-net_http (3.4.2)
13+
net-http (~> 0.5)
614
json (2.19.5)
715
language_server-protocol (3.17.0.5)
816
lint_roller (1.1.0)
17+
logger (1.7.0)
918
minitest (6.0.6)
1019
drb (~> 2.0)
1120
prism (~> 1.5)
21+
minitest-mock (5.27.0)
22+
net-http (0.9.1)
23+
uri (>= 0.11.1)
24+
octokit (10.0.0)
25+
faraday (>= 1, < 3)
26+
sawyer (~> 0.9)
1227
parallel (1.28.0)
1328
parser (3.3.11.1)
1429
ast (~> 2.4.1)
1530
racc
1631
prism (1.9.0)
32+
public_suffix (7.0.5)
1733
racc (1.8.1)
1834
rainbow (3.1.1)
1935
rake (13.4.2)
@@ -37,6 +53,9 @@ GEM
3753
rubocop (>= 1.75.0, < 2.0)
3854
rubocop-ast (>= 1.47.1, < 2.0)
3955
ruby-progressbar (1.13.0)
56+
sawyer (0.9.3)
57+
addressable (>= 2.3.5)
58+
faraday (>= 0.17.3, < 3)
4059
standard (1.54.0)
4160
language_server-protocol (~> 3.17.0.2)
4261
lint_roller (~> 1.0)
@@ -54,26 +73,37 @@ GEM
5473
unicode-display_width (3.2.0)
5574
unicode-emoji (~> 4.1)
5675
unicode-emoji (4.2.0)
76+
uri (1.1.1)
5777

5878
PLATFORMS
5979
arm64-darwin-25
6080
ruby
6181

6282
DEPENDENCIES
6383
minitest
84+
minitest-mock (~> 5.27)
85+
octokit
6486
rake
6587
standardrb
6688

6789
CHECKSUMS
90+
addressable (2.9.0) sha256=7fdf6ac3660f7f4e867a0838be3f6cf722ace541dd97767fa42bc6cfa980c7af
6891
ast (2.4.3) sha256=954615157c1d6a382bc27d690d973195e79db7f55e9765ac7c481c60bdb4d383
6992
drb (2.2.3) sha256=0b00d6fdb50995fe4a45dea13663493c841112e4068656854646f418fda13373
93+
faraday (2.14.2) sha256=73ccb9994a9e8648f010e32eca2ae82e41c57860aa10932cda29418b9e0223ad
94+
faraday-net_http (3.4.2) sha256=f147758260d3526939bf57ecf911682f94926a3666502e24c69992765875906c
7095
json (2.19.5) sha256=218a18553e4801d579ca7e0f5bc72bafd776d7397238a1fb4e74db5b0a812c59
7196
language_server-protocol (3.17.0.5) sha256=fd1e39a51a28bf3eec959379985a72e296e9f9acfce46f6a79d31ca8760803cc
7297
lint_roller (1.1.0) sha256=2c0c845b632a7d172cb849cc90c1bce937a28c5c8ccccb50dfd46a485003cc87
98+
logger (1.7.0) sha256=196edec7cc44b66cfb40f9755ce11b392f21f7967696af15d274dde7edff0203
7399
minitest (6.0.6) sha256=153ea36d1d987a62942382b61075745042a2b3123b1cd48f4c3675af9cc7d6f1
100+
minitest-mock (5.27.0) sha256=7040ed7185417a966920987eaa6eaf1be4ea1fc5b25bb03ff4703f98564a55b0
101+
net-http (0.9.1) sha256=25ba0b67c63e89df626ed8fac771d0ad24ad151a858af2cc8e6a716ca4336996
102+
octokit (10.0.0) sha256=82e99a539b7637b7e905e6d277bb0c1a4bed56735935cc33db6da7eae49a24e8
74103
parallel (1.28.0) sha256=33e6de1484baf2524792d178b0913fc8eb94c628d6cfe45599ad4458c638c970
75104
parser (3.3.11.1) sha256=d17ace7aabe3e72c3cc94043714be27cc6f852f104d81aa284c2281aecc65d54
76105
prism (1.9.0) sha256=7b530c6a9f92c24300014919c9dcbc055bf4cdf51ec30aed099b06cd6674ef85
106+
public_suffix (7.0.5) sha256=1a8bb08f1bbea19228d3bed6e5ed908d1cb4f7c2726d18bd9cadf60bc676f623
77107
racc (1.8.1) sha256=4a7f6929691dbec8b5209a0b373bc2614882b55fc5d2e447a21aaa691303d62f
78108
rainbow (3.1.1) sha256=039491aa3a89f42efa1d6dec2fc4e62ede96eb6acd95e52f1ad581182b79bc6a
79109
rake (13.4.2) sha256=cb825b2bd5f1f8e91ca37bddb4b9aaf345551b4731da62949be002fa89283701
@@ -82,12 +112,14 @@ CHECKSUMS
82112
rubocop-ast (1.49.1) sha256=4412f3ee70f6fe4546cc489548e0f6fcf76cafcfa80fa03af67098ffed755035
83113
rubocop-performance (1.26.1) sha256=cd19b936ff196df85829d264b522fd4f98b6c89ad271fa52744a8c11b8f71834
84114
ruby-progressbar (1.13.0) sha256=80fc9c47a9b640d6834e0dc7b3c94c9df37f08cb072b7761e4a71e22cff29b33
115+
sawyer (0.9.3) sha256=0d0f19298408047037638639fe62f4794483fb04320269169bd41af2bdcf5e41
85116
standard (1.54.0) sha256=7a4b08f83d9893083c8f03bc486f0feeb6a84d48233b40829c03ef4767ea0100
86117
standard-custom (1.0.2) sha256=424adc84179a074f1a2a309bb9cf7cd6bfdb2b6541f20c6bf9436c0ba22a652b
87118
standard-performance (1.9.0) sha256=49483d31be448292951d80e5e67cdcb576c2502103c7b40aec6f1b6e9c88e3f2
88119
standardrb (1.0.1) sha256=7a1328be429f4e97a97e357e2446f3509e80164a59ff00bc6a4daa78e3351f2c
89120
unicode-display_width (3.2.0) sha256=0cdd96b5681a5949cdbc2c55e7b420facae74c4aaf9a9815eee1087cb1853c42
90121
unicode-emoji (4.2.0) sha256=519e69150f75652e40bf736106cfbc8f0f73aa3fb6a65afe62fefa7f80b0f80f
122+
uri (1.1.1) sha256=379fa58d27ffb1387eaada68c749d1426738bd0f654d812fcc07e7568f5c57c6
91123

92124
RUBY VERSION
93125
ruby 4.0.1

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Another option is to use a dedicated GitHub token for this action, which can be
2020
set as a secret in the repository's settings. This token should have the `repo`
2121
scope enabled.
2222

23-
Then, add a workflow file:
23+
Then, add a workflow file that sets up your Rails environment and runs the action:
2424

2525
```yaml
2626
# .github/workflows/importmap-updates.yml
@@ -38,7 +38,11 @@ jobs:
3838
update:
3939
runs-on: ubuntu-latest
4040
steps:
41-
- uses: actions/checkout@v4
41+
- uses: actions/checkout@v6
42+
- uses: ruby/setup-ruby@v1
43+
with:
44+
bundler-cache: true
45+
4246
- uses: thoughtbot/importmap-update@v1
4347
with:
4448
github-token: ${{ secrets.GITHUB_TOKEN }}

action.yml

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ inputs:
1111
required: false
1212
default: ".github/importmap-updates.yml"
1313
github-token:
14-
description: "Token used by gh to list, open, edit, and close PRs."
14+
description: "Token used by Octokit to list, open, edit, and close PRs."
1515
required: true
1616
base-branch:
1717
description: "Branch to base PRs against."
@@ -32,30 +32,27 @@ inputs:
3232
description: "Git author email for commits."
3333
required: false
3434
default: "github-actions[bot]@users.noreply.github.com"
35-
working-directory:
35+
rails-root:
3636
description: "Directory containing the Rails app to run against. Useful when the app lives in a subdirectory or for integration testing."
3737
required: false
3838
default: "."
3939

4040
runs:
4141
using: "composite"
4242
steps:
43-
- name: Set up Ruby
44-
uses: ruby/setup-ruby@v1
45-
with:
46-
bundler-cache: true
47-
ruby-version: ${{ inputs.ruby-version }}
48-
4943
- name: Run importmap-update
5044
shell: bash
51-
working-directory: ${{ inputs.working-directory }}
45+
working-directory: ${{ github.action_path }}
5246
env:
53-
GH_TOKEN: ${{ inputs.github-token }}
47+
BUNDLE_GEMFILE: ${{ github.action_path }}/Gemfile
48+
RAILS_ROOT: ${{ inputs.rails-root }}
5449
GITHUB_TOKEN: ${{ inputs.github-token }}
5550
GITHUB_REPOSITORY: ${{ github.repository }}
5651
INPUT_CONFIG_FILE: ${{ inputs.config-file }}
5752
IMPORTMAP_BASE_BRANCH: ${{ inputs.base-branch }}
5853
IMPORTMAP_DRY_RUN: ${{ inputs.dry-run }}
5954
IMPORTMAP_AUTHOR_NAME: ${{ inputs.author-name }}
6055
IMPORTMAP_AUTHOR_EMAIL: ${{ inputs.author-email }}
61-
run: ${{ github.action_path }}/exe/importmap-update 2>&1 | tee "${IMPORTMAP_RUN_LOG:-/dev/null}"
56+
run: |
57+
bundle install
58+
bundle exec exe/importmap-update 2>&1 | tee "${IMPORTMAP_RUN_LOG:-/dev/null}"

exe/importmap-update

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# INPUT_CONFIG_FILE Path to the YAML config (default
1818
# .github/importmap-updates.yml).
1919
# GITHUB_REPOSITORY OWNER/REPO of the consuming repo.
20-
# GITHUB_TOKEN / GH_TOKEN Auth for gh (the action.yml passes this in).
20+
# GITHUB_TOKEN / GH_TOKEN Auth token passed to Octokit (action.yml passes this in).
2121
# IMPORTMAP_BASE_BRANCH Base branch for PRs (default `main`).
2222
# IMPORTMAP_DRY_RUN Set to "true" for a no-side-effects run.
2323
# IMPORTMAP_AUTHOR_NAME Git author name for commits.
@@ -31,7 +31,7 @@ require "config"
3131
require "planner"
3232
require "reconciler"
3333
require "executor"
34-
require "gh_client"
34+
require "github_client"
3535
require "git_client"
3636
require "commands"
3737
require "parsers/outdated_parser"
@@ -156,8 +156,14 @@ when :run
156156
exit 2
157157
end
158158

159-
runner = Importmap::Update::Commands::ShellRunner.new
160-
gh = Importmap::Update::GhClient.new(repo: repo, runner: runner)
159+
token = ENV["GITHUB_TOKEN"] || ENV["GH_TOKEN"]
160+
if token.nil? || token.empty?
161+
warn "GITHUB_TOKEN (or GH_TOKEN) is not set; refusing to run."
162+
exit 2
163+
end
164+
165+
runner = Importmap::Update::Commands::ShellRunner.new(cwd: ENV.fetch("RAILS_ROOT", "."))
166+
gh = Importmap::Update::GitHubClient.new(repo: repo, token: token)
161167
git = Importmap::Update::GitClient.new(
162168
runner: runner,
163169
author_name: ENV.fetch("IMPORTMAP_AUTHOR_NAME", "github-actions[bot]"),

lib/commands.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ def initialize(cwd: nil, env: nil)
4747
def run(*argv)
4848
opts = {}
4949
opts[:chdir] = @cwd if @cwd
50-
stdout, stderr, status = Open3.capture3(@env, *argv, **opts)
51-
Result.new(stdout: stdout, stderr: stderr, exit_code: status.exitstatus)
50+
Bundler.with_unbundled_env do
51+
stdout, stderr, status = Open3.capture3(@env, *argv, opts)
52+
Result.new(stdout: stdout, stderr: stderr, exit_code: status.exitstatus)
53+
end
5254
end
5355

5456
# Raises on non-zero exit. Use when you have no recovery strategy

lib/executor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
require_relative "commands"
4-
require_relative "gh_client"
4+
require_relative "github_client"
55
require_relative "git_client"
66
require_relative "metadata"
77

0 commit comments

Comments
 (0)