Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close linked issues when a PR is merged #777

Open
xmo-odoo opened this issue Jun 16, 2023 · 2 comments
Open

Close linked issues when a PR is merged #777

xmo-odoo opened this issue Jun 16, 2023 · 2 comments
Labels

Comments

@xmo-odoo
Copy link
Collaborator

xmo-odoo commented Jun 16, 2023

Github has features which close a linked issue when a PR is merged (possibly just to the main branch): special tags in commits (closes, fixes, ...) or manually linked issues (to test how this behaves).

In the context of the mergebot, and odoo's workflow, this is a problem:

  1. the commit tags only work from the main branch, as Odoo PRs are mostly not merged in the main branch a lot of fixes will never land there
  2. linked issues only work off of merging the PR which is impossible for external tools to do if they need to do any modification (rebase, rewrites, ...) unless they can and want to smash that back in the PR itself, Explicit "mark as merged" option in pull request UI isaacs/github#2 https://github.com/orgs/community/discussions/12437 https://github.com/orgs/community/discussions/24641

It's possible to retrieve linked PRs but only in the v4 API via PullRequest.closingIssuesReferences. Though at least that looks pre-filtered by applicable issues (aka only those this PR will close). This means on merging a PR we could retrieve this list and close all the linked issues. A bit of a pain because GQL but it'd be done via a cron anyway (as we'd need to be sure the PR was successfully merged before even checking) so should be feasible via an ad-hoc GQL query.

@xmo-odoo xmo-odoo modified the milestone: saas-13.2 Jun 16, 2023
@xmo-odoo xmo-odoo moved this to accepted in Mergebot May 29, 2024
@xmo-odoo xmo-odoo moved this from accepted to ideas in Mergebot Jun 4, 2024
@xmo-odoo
Copy link
Collaborator Author

xmo-odoo commented Jun 4, 2024

Pulling back because I want to do it, but

  • it's hardly urgent (more of a nice to have to compensate for a few gh deficiencies)
  • it requires a fair amount of research on how gh populates "issues references"
    • option 1 is back-reference from commits ("closes", "fixes", "resolves" strictures)
    • option 2 is explicitly linking to an issue from a PR, or a PR from an issue, via the sidebar ("Development" section), but it doesn't look like there's any API to do that...
  • it definitely requires additions to dummy central's currently anemic issues support, and likely adding actual gql support for closingIssuesReferences (so plugging in async-graphql)

@xmo-odoo xmo-odoo moved this from ideas to accepted in Mergebot Jul 10, 2024
@xmo-odoo xmo-odoo moved this from accepted to done in Mergebot Aug 5, 2024
@xmo-odoo xmo-odoo moved this from done to ideas in Mergebot Aug 5, 2024
@xmo-odoo
Copy link
Collaborator Author

xmo-odoo commented Oct 25, 2024

Demo script:

import json
import requests
import psycopg2

s = requests.Session()
s.headers['Authorization'] = 

with psycopg2.connect(dbname="mig") as cn:
    with cn.cursor() as cr:
        cr.execute("""
        SELECT r.name, p.number
        FROM forwardport_branch_remover br
        LEFT JOIN runbot_merge_pull_requests p on p.id = br.pr_id
        LEFT JOIN runbot_merge_repository r on r.id = p.repository
        """)
        prs = [
            (*n.split('/'), p)
            for n, p in cr
        ]


body = "".join(
"""
    _%d: repository(owner: %s, name: %s) {
        pullRequest(number: %d) {
            closingIssuesReferences(last: 100) {
                nodes {
                    number
                }
            }
        }
    }
""" % (i, json.dumps(o), json.dumps(r), n)
    for i, (o, r, n) in enumerate(prs)
)

r = s.post(
    'https://api.github.com/graphql',
    json={'query': f"query {{\n{body}\n}}"}
)

print(r.headers['x-ratelimit-remaining'], '/', r.headers['x-ratelimit-limit'], r.headers['x-ratelimit-resource'])

for (owner, name, number), entry in zip(prs, r.json()['data'].values()):
    if not entry['pullRequest']:
        continue

    refs = [
        r['number']
        for r in entry['pullRequest']['closingIssuesReferences'].get('nodes', ())
    ]
    if refs:
        print(f"{owner}/{name}#{number} =>")
    for ref in refs:
        r = s.get(f'https://api.github.com/repos/{owner}/{name}/issues/{ref}')
        if r.ok:
            i = r.json()
            print(f'\t#{ref} {i["state"]} {i["state_reason"]}')
        print('\t', r.headers['x-ratelimit-remaining'], '/', r.headers['x-ratelimit-limit'], r.headers['x-ratelimit-resource'])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: accepted
Development

No branches or pull requests

1 participant