Skip to content

Commit

Permalink
Pull request merge script
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Jan 16, 2021
1 parent 44aa32c commit be51b0d
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 540 deletions.
47 changes: 0 additions & 47 deletions bin/changes-since-last-release.pl

This file was deleted.

76 changes: 0 additions & 76 deletions bin/ckan-build-info.py

This file was deleted.

20 changes: 0 additions & 20 deletions bin/ckan-build.py

This file was deleted.

86 changes: 86 additions & 0 deletions bin/ckan-merge-pr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/python3

# https://github.com/KSP-CKAN/CKAN/wiki/Releasing-a-new-CKAN-client#when-merging-pull-requests

import sys
from pathlib import Path
from subprocess import run
from exitstatus import ExitStatus
import click
from click import command, option, argument
from git import Repo
from github import Github

@command()
@option('--repo-path', type=click.Path(exists=True, file_okay=False),
default='.', help='Path to CKAN working copy')
@option('--token', required=False, envvar='GITHUB_TOKEN')
@argument('pr_num', type=click.INT)
def merge_pr(repo_path: str, token: str, pr_num: int) -> None:
r = Repo(repo_path)
pr = Github(token).get_repo('KSP-CKAN/CKAN').get_pull(pr_num)
# Make sure master is checked out
if r.head.ref.name != 'master':
print('Not on master branch!')
# Bail and let user get his or her repo in order
sys.exit(ExitStatus.failure)
# Make sure master is up to date
master_remote = r.remotes[r.head.ref.tracking_branch().remote_name]
print(f'Fetching {master_remote.name}...')
master_remote.fetch()
remote_master = r.head.ref.tracking_branch()
if r.head.commit.hexsha != remote_master.commit.hexsha:
print(f'master branch is not up to date!')
sys.exit(ExitStatus.failure)
# Make sure PR's branch exists
local_branch = pr.head.ref
branch = r.heads[local_branch]
# Fetch remote to make sure we aren't missing changes
tracking = branch.tracking_branch()
if not tracking:
print(f'Upstream {pr.head.label} missing!')
sys.exit(ExitStatus.failure)
remote = r.remotes[tracking.remote_name]
if remote.name != master_remote.name:
print(f'Fetching {remote.name}...')
remote.fetch()
# Make sure branch is identical to its upstream
if branch.commit.hexsha != pr.head.sha:
print(f'Branch {local_branch} not up to date!')
# Bail and let the user figure it out, no way we can safely cover all possibilities here
sys.exit(ExitStatus.failure)
# Get reviewers from PR
reviewers = [rvw.user.login for rvw in pr.get_reviews() if rvw.state == 'APPROVED']
if not reviewers:
print(f'PR #{pr_num} is not approved!')
sys.exit(ExitStatus.failure)
# Get title from PR
pr_title = pr.title
# Get author from PR
author = pr.user.login
# Merge the branch with no-commit and no-ff
base = r.merge_base(branch, r.head)
r.index.merge_tree(branch, base=base)
# Update the working copy
r.index.checkout(force=True)
# Print line to add to CHANGELOG.md at top of file, user needs to move it to the right spot
changelog_path = Path(repo_path) / 'CHANGELOG.md'
with open(changelog_path, 'r+') as changelog:
lines = [f'- [UNKNOWN] {pr_title} (#{pr_num} by: {author}; reviewed: {", ".join(reviewers)})\n',
*changelog.readlines()]
changelog.seek(0)
changelog.writelines(lines)
# Edit CHANGELOG.md
editor=r.config_reader().get('core', 'editor')
run([editor, changelog_path])
# Stage change log
r.index.add([changelog_path.as_posix()])
# Commit
r.index.commit(f'Merge #{pr_num} {pr_title}',
parent_commits=(r.head.commit, branch.commit))

# Don't push, let the user inspect and decide
sys.exit(ExitStatus.success)

if __name__ == '__main__':
merge_pr()
64 changes: 0 additions & 64 deletions bin/ckan-release-maker.py

This file was deleted.

100 changes: 0 additions & 100 deletions bin/ckan-release-promoter.py

This file was deleted.

Empty file modified bin/ckan-validate.py
100644 → 100755
Empty file.
Loading

0 comments on commit be51b0d

Please sign in to comment.