| 1 | import click |
| 2 | import requests |
| 3 | import json |
| 4 | from git import Repo |
| 5 | |
| 6 | COMMITTER_EMAIL = 'noreply@microsoft.com' |
| 7 | REPO = 'microsoft/wsl' |
| 8 | |
| 9 | @click.command() |
| 10 | @click.argument('repo_path', required=True) |
| 11 | @click.argument('token', required=True) |
| 12 | @click.argument('committer', required=True) |
| 13 | @click.argument('message', required=True) |
| 14 | @click.argument('branch', required=True) |
| 15 | @click.argument('target_branch', required=True) |
| 16 | @click.option('--debug', default=False, is_flag=True) |
| 17 | def main(repo_path: str, token: str, committer: str, message: str, branch: str, target_branch: str, debug: bool): |
| 18 | try: |
| 19 | repo = Repo(repo_path) |
| 20 | |
| 21 | modified_files = [e.a_path for e in repo.index.diff(None)] |
| 22 | untracked_files = list(repo.untracked_files) |
| 23 | changed_files = modified_files + untracked_files |
| 24 | |
| 25 | if not changed_files: |
| 26 | print('No files changed, skipping') |
| 27 | return |
| 28 | |
| 29 | print(f'Changed files: {",".join(changed_files)}') |
| 30 | |
| 31 | |
| 32 | repo.create_head(branch).checkout() |
| 33 | |
| 34 | with repo.config_writer() as config: |
| 35 | config.set_value("user", "email", COMMITTER_EMAIL) |
| 36 | config.set_value("user", "name", committer) |
| 37 | |
| 38 | # 'git add -A' so newly created files in new directories are staged too. |
| 39 | repo.git.add(A=True) |
| 40 | repo.git.commit(m=message) |
| 41 | repo.git.push('origin', branch) |
| 42 | |
| 43 | headers = {'Accept': 'application/vnd.github+json', 'Authorization': 'Bearer ' + token} |
| 44 | |
| 45 | body = { |
| 46 | 'title': message, |
| 47 | 'description': 'Automated change', |
| 48 | 'head': branch, |
| 49 | 'base': target_branch |
| 50 | } |
| 51 | |
| 52 | response = requests.post(f'https://api.github.com/repos/{REPO}/pulls', headers=headers, data=json.dumps(body), timeout=30) |
| 53 | response.raise_for_status() |
| 54 | |
| 55 | print(f'Created pull request: {response.json()["html_url"]}') |
| 56 | |
| 57 | except: |
| 58 | if debug: |
| 59 | import pdb |
| 60 | import traceback |
| 61 | traceback.print_exc() |
| 62 | pdb.post_mortem() |
| 63 | |
| 64 | raise |
| 65 | |
| 66 | if __name__ == '__main__': |
| 67 | main() |