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 script and GitHub action to close orphaned PRs #952

Merged
merged 1 commit into from
Feb 28, 2025
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
22 changes: 22 additions & 0 deletions .github/workflows/close_prs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
name: Close PRs

on:
schedule:
- cron: "0 0 * * 0"
workflow_dispatch:

jobs:
close-prs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: "ruby"
timeout-minutes: 30
- name: Close PRs
env:
GITHUB_API_TOKEN: "${{ secrets.PR_TOKEN }}"
run: bin/close_prs
58 changes: 58 additions & 0 deletions bin/close_prs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env ruby

require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "colorize"
gem "octokit"
gem "optimist"
end

opts = Optimist.options do
opt :dry_run, "Execute without making changes", :default => false
end

def close_pull_request(repo, number, dry_run:, **_)
if dry_run
puts "** dry_run: github.close_pull_request(#{repo.inspect}, #{number.inspect})".light_black
else
github.close_pull_request(repo, number)
end
end

def delete_branch(repo, branch, dry_run:, **_)
if dry_run
puts "** dry_run: github.delete_branch(#{repo.inspect}, #{branch.inspect})".light_black
else
github.delete_branch(repo, branch)
end
end

def github
@github ||= Octokit::Client.new(
:access_token => ENV.fetch("GITHUB_API_TOKEN"),
:auto_paginate => true
)
end

CRT_REPO = "ManageIQ/manageiq-cross_repo-tests".freeze

github.pull_requests(CRT_REPO, :state => "open").each do |crt_pr|
target = crt_pr.title.split(" ").last
next unless target.match?(%r{^ManageIQ/[^#]+#\d+$})

repo, number = target.split("#")
pr = github.pull_request(repo, number)

crt_pr_slug = "#{CRT_REPO}##{crt_pr.number}"
pr_slug = "#{repo}##{number}"

if pr.state == "closed"
puts "Closing #{crt_pr_slug} since #{pr_slug} is closed"
close_pull_request(CRT_REPO, crt_pr.number, **opts)
puts "Deleting branch #{crt_pr.head.ref}"
delete_branch(CRT_REPO, crt_pr.head.ref, **opts)
else
puts "Skipping #{crt_pr_slug} since #{pr_slug} is open"
end
end
Loading