Skip to content

Commit 7b18a23

Browse files
committed
Make sure we use a PAT with the right permissions for GitHub API calls in the workflow
1 parent 374add6 commit 7b18a23

3 files changed

Lines changed: 13 additions & 9 deletions

File tree

.github/workflows/check-ruby-versions.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
- name: Check for new versions and create PR
2727
id: check-and-create
2828
env:
29-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
29+
GITHUB_TOKEN: ${{ secrets.RUBY_AUTOMATION_PAT }}
3030
run: bin/check-and-create-pr
3131

3232
- name: Annotate new versions added
@@ -42,7 +42,7 @@ jobs:
4242
- name: Enable auto-merge
4343
if: steps.check-and-create.outputs.pr_created == 'true'
4444
env:
45-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
45+
GH_TOKEN: ${{ secrets.RUBY_AUTOMATION_PAT }}
4646
run: |
4747
echo "Enabling auto-merge for PR #${{ steps.check-and-create.outputs.pr_number }}..."
4848
gh pr merge ${{ steps.check-and-create.outputs.pr_number }} --auto --squash

lib/ruby_version_pr_creator.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,15 @@ def validate_prerequisites!
115115
raise Error, "Git is not installed or not in PATH."
116116
end
117117

118-
# Verify we can authenticate with GitHub
118+
# Verify we can authenticate with GitHub and access this repository
119119
begin
120-
github_client.user
120+
github_client.repository(repository)
121121
rescue Octokit::Unauthorized
122122
raise Error, "GitHub API authentication failed. Check your token."
123+
rescue Octokit::Forbidden
124+
raise Error, "GitHub API access forbidden for repository #{repository}. Check token permissions."
125+
rescue Octokit::NotFound
126+
raise Error, "GitHub repository not found or inaccessible: #{repository}."
123127
rescue Octokit::Error => e
124128
raise Error, "GitHub API error: #{e.message}"
125129
end

test/ruby_version_pr_creator_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ def stub_git_operations(creator)
162162
end
163163

164164
def stub_github_api_success
165-
stub_request(:get, "https://api.github.com/user")
166-
.to_return(status: 200, body: JSON.generate({ login: "test-user" }), headers: json_headers)
165+
stub_request(:get, "https://api.github.com/repos/rails/devcontainer")
166+
.to_return(status: 200, body: JSON.generate({ full_name: "rails/devcontainer" }), headers: json_headers)
167167

168168
stub_request(:get, "https://api.github.com/repos/rails/devcontainer/pulls?state=open")
169169
.to_return(status: 200, body: JSON.generate([]), headers: json_headers)
@@ -180,12 +180,12 @@ def stub_github_api_success
180180
end
181181

182182
def stub_github_api_unauthorized
183-
stub_request(:get, "https://api.github.com/user").to_return(status: 401)
183+
stub_request(:get, "https://api.github.com/repos/rails/devcontainer").to_return(status: 401)
184184
end
185185

186186
def stub_github_api_with_existing_pr
187-
stub_request(:get, "https://api.github.com/user")
188-
.to_return(status: 200, body: JSON.generate({ login: "test-user" }), headers: json_headers)
187+
stub_request(:get, "https://api.github.com/repos/rails/devcontainer")
188+
.to_return(status: 200, body: JSON.generate({ full_name: "rails/devcontainer" }), headers: json_headers)
189189

190190
stub_request(:get, "https://api.github.com/repos/rails/devcontainer/pulls?state=open")
191191
.to_return(

0 commit comments

Comments
 (0)