Skip to content

Instantly share code, notes, and snippets.

@timmc-edx
Created June 25, 2021 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timmc-edx/750f7166c4aa76b3998d34980e03b13e to your computer and use it in GitHub Desktop.
Save timmc-edx/750f7166c4aa76b3998d34980e03b13e to your computer and use it in GitHub Desktop.
ARCHBOM-1819
#!/usr/bin/env python3
# This script records an action we took to leave a comment on (almost)
# every open edx-platform PR to let people know that they needed to
# rebase onto master (or merge it into their branch) to avoid breaking
# the build.
#
# The script requires a Github access token and finds all open PRs
# against master on edx-platform which do not have a specific commit
# in their ancestors, then leaves a comment on each one. This should
# be done from a bot account; the bot will then be subscribed to all
# those threads, and should be manually unsubscribed.
#
# The list of PRs this was run against was added to the skip-list in
# case we needed to run it again for some reason; during testing we
# limited it to just run against a few PRs and then added those to the
# skip-list for the next iteration.
#
# Requirements:
# - github3.py
# - click
import sys
import click
import github3
# The oldest commit that all new branches should have as their
# ancestor in order to contain all the changes to the quality checks.
WORKING_QUALITY_COMMIT = '2e335653203668bc2e3f22ea33ded5b8d1ffe25e'
def process_pr(gh, repo, pr):
# Skip any PRs that have the commit as an ancestor
comparison = repo.compare_commits(WORKING_QUALITY_COMMIT, pr.head.sha)
if comparison.behind_by == 0:
return
# PRs we already handled during testing
if str(pr.number) in ['27935', '27933', '27932', '27929', '27925', '27923', '27917', '27937', '27914', '27913', '27905', '27904', '27900', '27894', '27887', '27880', '27879', '27875', '27870', '27867', '27861', '27858', '27857', '27850', '27849', '27836', '27830', '27829', '27825', '27818', '27817', '27815', '27806', '27803', '27799', '27796', '27788', '27786', '27782', '27760', '27743', '27741', '27719', '27715', '27712', '27696', '27690', '27689', '27683', '27680', '27644', '27630', '27620', '27608', '27599', '27569', '27565', '27557', '27553', '27542', '27529', '27510', '27466', '27411', '27372', '27278', '27270', '27269', '27263', '27234', '27223', '27212', '27197', '27193', '27165', '27157', '27095', '27089', '27068', '27046', '27043', '27037', '27033', '27022', '27011', '26999', '26985', '26982', '26948', '26855', '26827', '26825', '26813', '26812', '26811', '26791', '26760', '26683', '26640', '26633', '26616', '26612', '26605', '26602', '26509', '26508', '26485', '26474', '26458', '26395', '26333', '26140', '26061', '25968', '25876', '25850', '25818', '25689', '25641', '25639', '25606', '25551', '25475', '25426', '25420', '25297', '25289', '25187', '25158', '25081', '24924', '24914', '24910', '24821', '24759', '24758', '24683', '24647', '24641', '24638', '24552', '24531', '24464', '24314', '24232', '24049', '24038', '23903', '23879', '23812', '23759', '23467', '23278', '23179', '23171', '23092', '23087', '22959', '22930', '22928', '22883', '22881', '22615', '22529', '22476', '22440', '22417', '22406', '22388', '22386', '21767', '21725', '21715', '21615', '21584', '21334', '21260', '21256', '21157', '21152', '21130', '20948', '20874', '20828', '20718', '20714', '20452', '20349', '20336', '20147', '20130', '20120', '20109', '20021', '19789', '19756', '19669', '19664', '19640', '19593', '19586', '19569', '19556', '19473', '19433', '19073', '19030', '18974', '18955', '18908', '18896', '18895', '18884', '18580', '18530', '18473', '18394', '18331', '18216', '17982', '17932', '17859', '17798', '17774', '17506', '17373', '17365', '17319', '17118', '16745', '16527']:
return
print(f"PR #{pr.number} (branch {pr.head.ref}) is behind by {comparison.behind_by}")
message = f"""
📣 💥 Heads-up: You must either **rebase onto master** or **merge master into your branch** to avoid breaking the build.
We recently removed diff-quality and introduced lint-amnesty. This means that the automated quality check that has run on your branch doesn't work the same way it will on master. If you have introduced any quality failures, they might pass on the PR but then break the build on master.
This branch has been detected to not have commit {WORKING_QUALITY_COMMIT[:8]} as an ancestor. Here's how to see for yourself:
```
git merge-base --is-ancestor {WORKING_QUALITY_COMMIT[:8]} {pr.head.ref} && echo "You're all set" || echo "Please rebase onto master or merge master to your branch"
```
If you have any questions, please reach out to the Architecture team (either #edx-shared-architecture on Open edX Slack or #architecture on edX internal).
"""
pr.create_comment(message)
@click.command()
@click.option(
'--github-username',
help="Username corresponding to the Github access token"
)
@click.option(
'--github-token',
help="Github token with repo and org read scope on applicable orgs."
)
@click.option(
'--github-repo',
help="Github repo in org/short-name format"
)
def main(github_username, github_token, github_repo):
gh = github3.login(github_username, github_token)
(repo_owner, repo_name) = github_repo.split('/', 2)
repo = gh.repository(repo_owner, repo_name)
prs = list(repo.pull_requests(state='open', base='master'))
print(f"Found {len(prs)} open PRs against master")
for pr in prs:
process_pr(gh, repo, pr)
if __name__ == '__main__':
main(auto_envvar_prefix='MSG_PRS')
@nedbat
Copy link

nedbat commented Oct 31, 2022

Here's my version: https://gist.github.com/nedbat/8138c4cda17d4537b9d3df86564359f1 It uses a cutoff date so it doesn't comment on years-stale pull requests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment