Last active
May 21, 2023 12:05
-
-
Save guysmoilov/956a066a71ba0677eeabf307af5d01e1 to your computer and use it in GitHub Desktop.
Python script to consolidate Github PRs into a single PR, based on labels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Put this under .github/workflows/consolidate-prs.yml | |
name: Consolidate PRs | |
on: | |
pull_request: | |
types: | |
- opened | |
- synchronize | |
branches: | |
- master | |
jobs: | |
consolidate_prs: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Set up Python | |
uses: actions/setup-python@v2 | |
with: | |
python-version: 3.x | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install PyGithub | |
- name: Consolidate PRs | |
run: | | |
python consolidate_prs.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generated using chat GPT - requires fixes. DO NOT USE WITHOUT FIXING | |
import os | |
import shutil | |
import tempfile | |
from github import Github | |
# GitHub authentication token | |
token = os.environ.get('GITHUB_TOKEN') | |
# Repository information | |
repo_owner = 'your-repo-owner' | |
repo_name = 'your-repo-name' | |
# Labels to filter the pull requests | |
label_names = ['label1', 'label2'] | |
# Create a GitHub instance using the token | |
github = Github(token) | |
# Get the repository | |
repo = github.get_repo(f'{repo_owner}/{repo_name}') | |
# Fetch pull requests with the specified labels | |
pull_requests = repo.get_pulls(state='open', sort='created', base='master') | |
filtered_prs = [pr for pr in pull_requests if any(label in label_names for label in pr.labels)] | |
# Create a temporary directory to store the consolidated branch | |
temp_dir = tempfile.mkdtemp() | |
try: | |
consolidated_branch_name = 'consolidated-pr' | |
consolidated_branch_ref = f'refs/heads/{consolidated_branch_name}' | |
# Create the consolidated branch | |
repo.create_git_ref(ref=consolidated_branch_ref, sha=repo.get_branch('master').commit.sha) | |
consolidated_commits = [] | |
consolidated_title = 'Consolidated PR' | |
consolidated_body = '' | |
for pr in filtered_prs: | |
# Retrieve the pull request's branch commit and add it to the consolidated branch | |
pr_branch_name = f'pull-request-{pr.number}' | |
pr_branch = repo.get_branch(pr.head.ref) | |
pr_commit = repo.get_commit(pr_branch.commit.sha) | |
# Cherry-pick the commit to the consolidated branch | |
repo.create_git_ref(ref=f'refs/heads/{pr_branch_name}', sha=pr_commit.sha) | |
repo.git.cherry_pick(pr_commit.sha) | |
consolidated_commits.append(pr_commit.sha) | |
# Consolidate the pull request title and body | |
consolidated_body += f'## {pr.title}\n\n{pr.body}\n\n' | |
# Push the consolidated branch to the repository | |
repo.git.push('origin', consolidated_branch_name) | |
# Create the consolidated pull request | |
consolidated_pr = repo.create_pull( | |
title=consolidated_title, | |
body=consolidated_body, | |
base='master', | |
head=consolidated_branch_name | |
) | |
# Add a comment with the individual pull request numbers for reference | |
pr_numbers = ', '.join(str(pr.number) for pr in filtered_prs) | |
comment_body = f'This PR consolidates the following pull requests: {pr_numbers}' | |
consolidated_pr.create_issue_comment(comment_body) | |
print(f'Consolidated PR created: #{consolidated_pr.number}') | |
finally: | |
# Clean up the temporary directory | |
shutil.rmtree(temp_dir) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment