[Tracker-discuss] [issue600] Convert patches to GitHub Pull Request

Anish Shah metatracker at psf.upfronthosting.co.za
Sun Jun 19 14:03:31 EDT 2016


New submission from Anish Shah:

This is an initial patch to convert .patch/.diff files to GitHub Pull Requests.

----------
files: patch-to-pr.diff
messages: 3055
nosy: anish.shah, maciej.szulik
priority: feature
status: in-progress
title: Convert patches to GitHub Pull Request

_______________________________________________________
PSF Meta Tracker <metatracker at psf.upfronthosting.co.za>
<http://psf.upfronthosting.co.za/roundup/meta/issue600>
_______________________________________________________
-------------- next part --------------
diff --git a/detectors/patches.py b/detectors/patches.py
index 18d3db4..7a27d5f 100644
--- a/detectors/patches.py
+++ b/detectors/patches.py
@@ -5,16 +5,83 @@
 
 import posixpath
 import identify_patch
+import os
+import subprocess
+import string
+import random
+import json
+import requests
 
 patchtypes = ('.diff', '.patch')
 sourcetypes = ('.diff', '.patch', '.py')
 
+def create_pull_request(db, head, base, issue_id, issue_title):
+    endpoint = 'https://api.github.com/repos/AnishShah/cpython/pulls'
+    access_token = os.environ['ACCESS_TOKEN']
+    headers = {'Authorization': 'token {}'.format(access_token)}
+    data = json.dumps({
+            "title": "{}".format(issue_title),
+            "body": "fixes issue {}".format(issue_id),
+            "base": base, "head": head})
+    response = requests.post(endpoint, headers=headers, data=data)
+    if not response.ok:
+        raise Exception('Error generating pull request')
+    else:
+        response_body = response.json()
+        url = response_body['html_url']
+        state = response_body['state']
+        pr_id = db.github_pullrequest_url.create(url=url, state=state)
+        issue_pr = db.issue.get(issue_id, 'github_pullrequest_urls')
+        issue_pr.append(pr_id)
+        db.issue.set(issue_id, github_pullrequest_urls=issue_pr)
+        db.commit()
+
+def git_command(command, path):
+    process = subprocess.Popen(command, cwd=path, shell=True,
+                               stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+    out, err = process.communicate()
+    # err takes stdout
+    # if err:
+    #     raise Exception(err)
+
+def git_workflow(db, issue_id, file_id):
+    path = os.environ['GIT_PATH']
+    versions = map(lambda id: (db.version.get(id, 'name'), db.version.get(id, 'order')),
+                   db.issue.get(issue_id, 'versions'))
+    title = db.issue.get(issue_id, 'title')
+    commit_msg = 'Fixes issue {} : {}'.format(issue_id, title)
+    if len(versions) == 0:
+        parent_branch = "master"
+    else:
+        version = versions.pop()
+        if version[1] == 1:
+            parent_branch = "master"
+        else:
+            parent_branch = version[0].split()[1]
+    branch_name = ''.join([random.choice(string.ascii_letters) for i in range(random.choice(range(10, 20)))])
+    filename = db.file.get(file_id, 'name')
+    content = db.file.get(file_id, 'content')
+    fp = open(os.path.join(path, filename), 'wb')
+    fp.write(content)
+    fp.close()
+    git_command("git checkout {}".format(parent_branch), path)
+    git_command("git checkout -b {}".format(branch_name), path)
+    git_command("git apply {}".format(filename), path)
+    git_command("git add -A", path)
+    git_command("git commit -m \"{}\"".format(commit_msg), path)
+    git_command("git push origin {}".format(branch_name), path)
+    git_command("git checkout {}".format(parent_branch), path)
+    git_command("git branch -D {}".format(branch_name), path)
+    create_pull_request(db, branch_name, parent_branch, issue_id, title)
+
+
 def ispatch(file, types):
     return posixpath.splitext(file)[1] in types
 
 def patches_text_plain(db, cl, nodeid, newvalues):
     if ispatch(newvalues['name'], sourcetypes):
         newvalues['type'] = 'text/plain'
+        # git_workflow(newvalues)
 
 def patches_keyword(db, cl, nodeid, newvalues):
     # Check whether there are any new files
@@ -26,6 +93,7 @@ def patches_keyword(db, cl, nodeid, newvalues):
     for fileid in newfiles:
         if ispatch(db.file.get(fileid, 'name'), patchtypes):
             newpatch = True
+            git_workflow(db, nodeid, fileid)
             break
     if newpatch:
         # Add the patch keyword if its not already there
diff --git a/initial_data.py b/initial_data.py
index dc0ed4f..a9eee3a 100644
--- a/initial_data.py
+++ b/initial_data.py
@@ -49,13 +49,12 @@ component.create(name="Windows", order="18")
 component.create(name="XML", order="19")
 
 version = db.getclass('version')
-version.create(name='Python 3.1', order='1')
-version.create(name='Python 3.0', order='2')
-version.create(name='Python 2.7', order='3')
-version.create(name='Python 2.6', order='4')
-version.create(name='Python 2.5', order='5')
-version.create(name='Python 2.4', order='6')
-version.create(name='3rd party', order='7')
+version.create(name='Python 3.6', order='1')
+version.create(name='Python 3.5', order='2')
+version.create(name='Python 3.4', order='3')
+version.create(name='Python 3.3', order='4')
+version.create(name='Python 3.2', order='5')
+version.create(name='Python 2.7', order='6')
 
 
 severity = db.getclass('severity')


More information about the Tracker-discuss mailing list