Skip to content
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

Add "PR doctor" script #33

Merged
merged 3 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ gem "httparty"
gem "octokit"

group :development, :test do
gem "byebug"
gem "rspec"
gem "rubocop-govuk", require: false
gem "timecop"
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ GEM
ast (2.4.2)
base64 (0.1.1)
bigdecimal (3.1.4)
byebug (11.1.3)
concurrent-ruby (1.2.2)
connection_pool (2.4.1)
crack (0.4.5)
Expand Down Expand Up @@ -111,6 +112,7 @@ PLATFORMS
ruby

DEPENDENCIES
byebug
httparty
octokit
rspec
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ To run the tests:
bundle exec rspec
```

### Using the merger locally

The repo expects an `AUTO_MERGE_TOKEN` environment variable to be defined. This should be a GitHub API token [with sufficient scope](./docs/adr/03-access-token-scope.md).

You can then run the merger with:

```
bundle exec ruby bin/merge_dependabot_prs.rb
```

The repo also ships with a "doctor" script to help you to debug individual PRs and why they did or did not auto-merge.

```
bundle exec ruby bin/pr_doctor.rb https://github.com/alphagov/content-data-api/pull/1996
```

### Further documentation

- [ADR 1: Limited team access to avoid privilege escalation](./docs/adr/01-limited-team-access.md)
Expand Down
7 changes: 7 additions & 0 deletions bin/pr_doctor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require_relative "../lib/auto_merger"

raise "Expecting exactly one argument, e.g. `ruby bin/pr_doctor.rb https://github.com/alphagov/content-data-api/pull/1996`" unless ARGV.count == 1

pr_url = ARGV.first

AutoMerger.analyse_dependabot_pr(pr_url)
10 changes: 10 additions & 0 deletions lib/auto_merger.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative "./bank_holiday_checker"
require_relative "./repo"
require_relative "./repos"

class AutoMerger
Expand Down Expand Up @@ -29,4 +30,13 @@ def merge_dependabot_prs
end
end
end

def self.analyse_dependabot_pr(url)
puts "Analysing #{url}..."
_, repo_name, pr_number = url.match(/alphagov\/(.+)\/pull\/(.+)$/).to_a
pr = Repo.new(repo_name).dependabot_pull_request(pr_number)

puts pr.is_auto_mergeable? ? "PR is considered auto-mergeable." : "PR is not considered auto-mergeable."
puts 'Add `require "byebug"; byebug` inside the `is_auto_mergeable?` method to find out more.'
end
end
4 changes: 4 additions & 0 deletions lib/repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ def dependabot_pull_requests
.select { |api_response| api_response.user.login == "dependabot[bot]" }
.map { |api_response| PullRequest.new(api_response) }
end

def dependabot_pull_request(pr_number)
PullRequest.new(GitHubClient.instance.pull_request("alphagov/#{@repo_name}", pr_number))
end
end