-
-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#!/usr/bin/python3 | ||
|
||
# https://github.com/KSP-CKAN/CKAN/wiki/Releasing-a-new-CKAN-client#when-merging-pull-requests | ||
|
||
import sys | ||
from os import environ | ||
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() |
Empty file.
Empty file.
Empty file.