Skip to content

Commit f80ffa1

Browse files
authored
Merge pull request #952 from Fryguy/auto-close-prs
Add script and GitHub action to close orphaned PRs
2 parents 569110d + be5d4d3 commit f80ffa1

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

.github/workflows/close_prs.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
name: Close PRs
3+
4+
on:
5+
schedule:
6+
- cron: "0 0 * * 0"
7+
workflow_dispatch:
8+
9+
jobs:
10+
close-prs:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- name: Set up Ruby
15+
uses: ruby/setup-ruby@v1
16+
with:
17+
ruby-version: "ruby"
18+
timeout-minutes: 30
19+
- name: Close PRs
20+
env:
21+
GITHUB_API_TOKEN: "${{ secrets.PR_TOKEN }}"
22+
run: bin/close_prs

bin/close_prs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env ruby
2+
3+
require "bundler/inline"
4+
gemfile do
5+
source "https://rubygems.org"
6+
gem "colorize"
7+
gem "octokit"
8+
gem "optimist"
9+
end
10+
11+
opts = Optimist.options do
12+
opt :dry_run, "Execute without making changes", :default => false
13+
end
14+
15+
def close_pull_request(repo, number, dry_run:, **_)
16+
if dry_run
17+
puts "** dry_run: github.close_pull_request(#{repo.inspect}, #{number.inspect})".light_black
18+
else
19+
github.close_pull_request(repo, number)
20+
end
21+
end
22+
23+
def delete_branch(repo, branch, dry_run:, **_)
24+
if dry_run
25+
puts "** dry_run: github.delete_branch(#{repo.inspect}, #{branch.inspect})".light_black
26+
else
27+
github.delete_branch(repo, branch)
28+
end
29+
end
30+
31+
def github
32+
@github ||= Octokit::Client.new(
33+
:access_token => ENV.fetch("GITHUB_API_TOKEN"),
34+
:auto_paginate => true
35+
)
36+
end
37+
38+
CRT_REPO = "ManageIQ/manageiq-cross_repo-tests".freeze
39+
40+
github.pull_requests(CRT_REPO, :state => "open").each do |crt_pr|
41+
target = crt_pr.title.split(" ").last
42+
next unless target.match?(%r{^ManageIQ/[^#]+#\d+$})
43+
44+
repo, number = target.split("#")
45+
pr = github.pull_request(repo, number)
46+
47+
crt_pr_slug = "#{CRT_REPO}##{crt_pr.number}"
48+
pr_slug = "#{repo}##{number}"
49+
50+
if pr.state == "closed"
51+
puts "Closing #{crt_pr_slug} since #{pr_slug} is closed"
52+
close_pull_request(CRT_REPO, crt_pr.number, **opts)
53+
puts "Deleting branch #{crt_pr.head.ref}"
54+
delete_branch(CRT_REPO, crt_pr.head.ref, **opts)
55+
else
56+
puts "Skipping #{crt_pr_slug} since #{pr_slug} is open"
57+
end
58+
end

0 commit comments

Comments
 (0)