File: hijack_pr.py

package info (click to toggle)
smart-open 7.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 980 kB
  • sloc: python: 8,054; sh: 90; makefile: 14
file content (34 lines) | stat: -rwxr-xr-x 1,001 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python
"""Hijack a PR to add commits as a maintainer.

This is a two-step process:

    1. Add a git remote that points to the contributor's repo
    2. Check out the actual contribution by reference

As a maintainer, you can add changes by making new commits and pushing them
back to the remote.
"""
import json
import subprocess
import sys

import smart_open

prid = int(sys.argv[1])
url = f"https://api.github.com/repos/RaRe-Technologies/smart_open/pulls/{prid}"
with smart_open.open(url) as fin:
    prinfo = json.load(fin)

user = prinfo['head']['user']['login']
ssh_url = prinfo['head']['repo']['ssh_url']

remotes = subprocess.check_output(['git', 'remote']).strip().decode('utf-8').split('\n')
if user not in remotes:
    subprocess.check_call(['git', 'remote', 'add', user, ssh_url])

subprocess.check_call(['git', 'fetch', user])

ref = prinfo['head']['ref']
subprocess.check_call(['git', 'checkout', f'{user}/{ref}'])
subprocess.check_call(['git', 'switch', '-c', f'{ref}'])