Forked from dasevilla/github-issue-to-phab-task.py
Last active
August 10, 2022 19:31
-
-
Save zcourts/e3035c55e32394dab071ce214a59d5d5 to your computer and use it in GitHub Desktop.
Copy GitHub issues to Phabricator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import os | |
import sys | |
import requests | |
import envoy | |
GITHUB_TOKEN = os.environ.get('GITHUB_TOKEN') | |
if GITHUB_TOKEN is None: | |
raise Exception('Missing GITHUB_TOKEN from os.environ') | |
DEFAULT_PHAB_PROJ = 'PHID-PROJ-gyqye2irfypgoibyk4gj' | |
# Get project ID from Conduit API using option 2 from http://stackoverflow.com/questions/25753749/how-do-you-find-the-phid-of-a-phabricator-object | |
GITHUB_LABEL_TO_PHAB_PROJ_MAP = { | |
# 'ios sdk': 'PHID-PROJ-tbul5vwnukmnurumxaz' | |
} | |
def get_issue(owner, repo, issue_number): | |
auth = (GITHUB_TOKEN, 'x-oauth-basic') | |
headers = { | |
'User-Agent': 'GitHub-Issue-To-Phab-Task' | |
} | |
url = 'https://api.github.com/repos/%s/%s/issues/%s' % ( | |
owner, | |
repo, | |
issue_number | |
) | |
r = requests.get( | |
url=url, | |
headers=headers, | |
auth=auth, | |
) | |
r.raise_for_status() | |
return r.json() | |
def github_issue_to_phab_json(github_issue): | |
task_description = "Original report: %s\n\n---\n%s\n" % ( | |
github_issue['html_url'], | |
github_issue['body'] | |
) | |
phab_projects = [ | |
DEFAULT_PHAB_PROJ, | |
] | |
for label in github_issue['labels']: | |
phab_project = GITHUB_LABEL_TO_PHAB_PROJ_MAP.get(label['name']) | |
if phab_project is not None: | |
phab_projects.append(phab_project) | |
phab_task = { | |
'title': github_issue['title'], | |
'description': task_description, | |
'projectPHIDs': phab_projects | |
} | |
return json.dumps(phab_task, indent=2, sort_keys=True) | |
def main(owner, repo, from_issue, to_issue): | |
print "Generating issues for %s/%s from %s to %s" % (owner, repo, from_issue, to_issue) | |
for issue_number in range(from_issue, to_issue + 1): #number of issues in repo | |
github_issue = get_issue(owner, repo, issue_number) | |
out = github_issue_to_phab_json(github_issue) | |
r = envoy.run('arc call-conduit maniphest.createtask', data=out, timeout=10) | |
print r.std_out | |
print r.std_err | |
if __name__ == '__main__': | |
if len(sys.argv) != 5 : | |
print """ | |
Imports a range of Github issues from a given repo for some user | |
Example usage:: | |
$ python gh-importer.py username repo start-issue end-issue | |
""" | |
sys.exit(1) | |
main(sys.argv[1], sys.argv[2], int(sys.argv[3]), int(sys.argv[4])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment