Copilot commented on code in PR #55484: URL: https://github.com/apache/arrow/pull/55484#discussion_r4613131648
########## dev/merged_pr.py: ########## @@ -0,0 +1,215 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +"""Post-merge issue metadata and release credits for apache/arrow.""" + +from collections import Counter +import json +import os +import re +import subprocess +import sys + + +REPO = 'apache/arrow' +# Keep in sync with verifyTitles in .github/boring-cyborg.yml. +TITLE = re.compile(r'^(GH-([1-9][0-9]*)|MINOR): (?:\[[^\[\]\r\n]+\])+ \S.*$') +SUFFIX = re.compile(r' \(#([0-9]+)\)$') + + +def gh(*args): + return subprocess.check_output(['gh', 'api', *args], text=True) + + +def api(path, *args): + return json.loads(gh(f'repos/{REPO}/{path}', *args)) + + +def items(path): + pages = api(path, '--paginate', '--slurp') + return (item for page in pages for item in page) + + +def merged_pr(sha, branch=None): + if not re.fullmatch('[a-f0-9]{40}', sha): + raise ValueError('Expected a full merge commit SHA') + matches = [pr for pr in items(f'commits/{sha}/pulls') + if pr['merged_at'] and pr['merge_commit_sha'] == sha + and pr['base']['repo']['full_name'] == REPO + and (branch is None or pr['base']['ref'] == branch)] + if not matches: + return None # A direct push, not a squash merge. + pr, = matches # Do not guess if the association is ambiguous. + return pr + + +def warn_on_pr(number, message): + print(message, file=sys.stderr) + path = f'issues/{number}/comments' + for comment in items(path): + author = comment['user'] or {} + if author.get('login') == 'github-actions[bot]' and comment['body'] == message: + return + api(path, '-f', f'body={message}') + + +def default_milestone(): + maintenance = {ref['ref'] for ref in items('git/matching-refs/heads/maint-')} + candidates = [(int(m['title'].split('.')[0]), m['number']) + for m in items('milestones?state=open&per_page=100') + if re.fullmatch(r'[0-9]+\.0\.0', m['title']) + and 'refs/heads/maint-' + m['title'] not in maintenance] + return min(candidates)[1] if candidates else None + + +def update(sha, message=None): + pr = merged_pr(sha, 'main') + if pr is None: + print(f'{sha}: no merged PR targeting main') + return + if message is None: # Manual recovery: read the immutable commit, not a PR ref. + message = api(f'commits/{sha}')['commit']['message'] + subject = SUFFIX.sub('', message.partition('\n')[0].rstrip('\r')) + commit_title, pr_title = TITLE.fullmatch(subject), TITLE.fullmatch(pr['title']) + if not commit_title or not pr_title or commit_title[1] != pr_title[1]: + warn_on_pr(pr['number'], f'Issue metadata not updated for {sha}: the squash ' + 'commit and PR must both have valid titles naming the same issue ' + '(or both be MINOR). Please check the issue, assignee and milestone ' + 'manually; do not rewrite main.') + return + number = pr_title[2] + body = pr['body'] or '' + if not number or re.search(r'^\* GitHub Issue: Refs\b', body, re.M | re.I): + return # MINOR or an explicitly partial fix. + fixes_line = rf'^\* GitHub Issue: Fixes #{number}[ \t]*\r?$' + if not re.search(fixes_line, body, re.M | re.I): + warn_on_pr(pr['number'], f'Expected `GitHub Issue: Fixes #{number}` in the PR ' + 'description. No metadata was changed. Please check closure, ' + 'assignee and milestone on the intended issue.') + return + path = f'issues/{number}' + issue = api(path) + if 'pull_request' in issue: + warn_on_pr(pr['number'], f'Issue metadata not updated: #{number} is a pull ' + 'request. Please update the intended issue manually.') + return + if not issue['assignees'] and pr['user']: + api(f'{path}/assignees', '-f', 'assignees[]=' + pr['user']['login']) + if issue['milestone'] is None: + milestone = default_milestone() + if milestone is not None: + api(path, '-X', 'PATCH', '-F', f'milestone={milestone}') + else: + warn_on_pr(pr['number'], 'No open major release milestone without a ' + f'maintenance branch was found. Please set one on #{number}.') + # GitHub handles closure and links the PR in the issue timeline via Fixes #N. + + +def update_event(event): + if 'inputs' in event: + update(event['inputs']['sha']) + return + commits = event['commits'] + # Push webhooks cap the list of commits; the compare API supports pagination. + if len(commits) >= 2048 or len(commits) < event.get('size', 0): + path = f"compare/{event['before']}...{event['after']}?per_page=100" + pages = api(path, '--paginate', '--slurp') + commits = [{'id': c['sha'], 'message': c['commit']['message']} + for page in pages for c in page['commits']] + failed = [] + for commit in commits: + try: + update(commit['id'], commit['message']) + except (subprocess.CalledProcessError, ValueError) as exc: + print(f"{commit['id']}: {exc}", file=sys.stderr) + failed.append(commit['id']) + if failed: + sys.exit('Failed merge SHA(s) to retry: ' + ', '.join(failed)) + + +def batches(keys, field): + keys = list(keys) + for start in range(0, len(keys), 50): + fields = ' '.join(f'k{k}:{field(k)}' for k in keys[start:start + 50]) + query = 'query {repository(owner:"apache",name:"arrow") {' + fields + '}}' + # check_output raises on *any* failed batch; never publish a partial tally. + yield json.loads(gh('graphql', '-f', 'query=' + query))['data']['repository'] + + +def list_committers(git_range): + log = subprocess.check_output(['git', 'log', '--format=%H %s', git_range], + text=True) + commits = dict(line.split(' ', 1) for line in log.splitlines()) + numbers, possible_picks = set(), {} + + def associations(sha): + return (f'object(oid:"{sha}") {{ ... on Commit {{ associatedPullRequests(' + 'first:10) {pageInfo {hasNextPage} nodes {number mergeCommit {oid} ' + 'repository {nameWithOwner}}} }}') + + for batch in batches(commits, associations): + for key, obj in batch.items(): + sha = key[1:] + prs = obj['associatedPullRequests'] + if prs['pageInfo']['hasNextPage']: + pr = merged_pr(sha) # Paginated lookup for an unusually busy commit. + else: + matches = [p for p in prs['nodes'] + if p['repository']['nameWithOwner'] == REPO + and (p['mergeCommit'] or {}).get('oid') == sha] + pr, = matches or [None] + if pr: + numbers.add(pr['number']) + elif suffix := SUFFIX.search(commits[sha]): + # Maintenance cherry-picks retain the merge subject, not its SHA. + possible_picks.setdefault(int(suffix[1]), set()).add(commits[sha]) + else: + print(f'{sha}: no associated merge to credit', file=sys.stderr) Review Comment: A maintenance cherry-pick of a suffixless squash commit is omitted from Patch Committers, even when GitHub associates it with the original PR. Its new SHA fails the exact-SHA check and this fallback requires a suffix, so the helper only warns and lets the website publish an incomplete tally. Add a subject-validated fallback through associated PR candidates and test suffixless cherry-picks. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
