Skip to content

Instantly share code, notes, and snippets.

@metalivedev
Last active December 28, 2015 09:19
Show Gist options
  • Save metalivedev/7478553 to your computer and use it in GitHub Desktop.
Save metalivedev/7478553 to your computer and use it in GitHub Desktop.
Quick hack to check the Docker project pull requests for requests that have docs in them. This lists them and sets the label to "doc" so I can later find them easily in the github UI. Also makes a nice list of the docs being modified so I can check them for conflicts.
import requests
import json
mytoken = 'SECRET_PERSONAL_TOKEN_SEE:https://github.com/settings/applications'
url = 'https://api.github.com/repos/dotcloud/docker'
addlabels = {}
labelarray = [ '/project/doc' ] #start with the docs label, and then any others for code
addlabels['docs'] = json.dumps( labelarray )
# We're not going to add a second lable for code anymore
# since the core team will be watching all PRs.
addlabels['both'] = addlabels['docs']
headers = { 'Authorization' : 'token ' + mytoken }
# pull requests that contain docs (and possibly code)
docprs = {}
docprs['docs'] = set()
docprs['both'] = set()
docprs['new'] = set()
# dictionary of documents in the PRs, with a list of the PRs they occur in.
prdocs = {}
def postlabels(pr, labels):
print "Adding %s to PR %s response=" % (addlabels[labels], pr),
docurl = "%s/issues/%s/labels" % (url, pr)
r = requests.post(docurl, headers=headers, data=addlabels[labels])
print r.status_code
#-------- MAIN ----------
# Get a list of all the open pull requests
listresp=requests.get('%s/pulls' % url, headers=headers)
# And make a list of their IDs. Aren't list comprehensions fun?
ids=[p['number'] for p in listresp.json()]
# And get any additional pages!
while "next" in listresp.links and "url" in listresp.links["next"] and len(listresp.links["next"]["url"]):
listresp = requests.get(listresp.links["next"]["url"], headers=headers)
ids.extend([p['number'] for p in listresp.json()])
totalPRs = len(ids)
countPRs = 1
print "Found %s open PRs." % totalPRs
print "Getting files for each..."
# Go through each PR and get the files, then check for docs.
for i in ids:
# Special case for a PR that has like a zillion files in it
if i == 3408:
print "Ignoring 3408 because it is too noisy."
continue
prString = "\r[{count}/{total}] {pr}".format(count=countPRs, total=totalPRs, pr=i)
countPRs = countPRs + 1
sys.stdout.write(prString)
sys.stdout.flush()
filesresp = requests.get('%s/pulls/%s/files' % (url, i), headers=headers)
bHasCode = False
for f in filesresp.json():
fname = f['filename']
if fname.startswith('docs/'):
docprs['docs'].add(i)
if fname in prdocs:
prdocs[fname].add(i)
else:
prdocs[fname] = set()
prdocs[fname].add(i)
else:
bHasCode = True
if bHasCode and i in docprs['docs']:
docprs['both'].add(i)
print "\nDone."
docsonly = docprs['docs'] - docprs['both']
print "Checking labels for the %s doc PRs:" % len(docprs['docs'])
# Add the doc label to doc PRs. PRs numbers are also issues.
for d in docprs['docs']:
labelresp = requests.get('%s/issues/%s/labels' % (url, d), headers=headers)
issuelabels = [i['name'] for i in labelresp.json()]
hasDocLabel = labelarray[0] in issuelabels
hasCodeLabel = True # HACK to remove second 'code' in issuelabels
if d in docsonly:
if hasDocLabel:
print "Issue %s already labeled as %s." % (d, labelarray[0])
else:
postlabels(d, 'docs')
docprs['new'].add(d)
else: # Needs both labels
if hasDocLabel and hasCodeLabel:
print "Issue %s already has labels: %s" % (d, labelarray)
else:
postlabels(d, 'both')
docprs['new'].add(d)
for file in prdocs:
print file, prdocs[file]
print "docprs: ", docprs['docs']
print "Docs only: ", docsonly
print "Both: ", docprs['both']
print "NEW: ", docprs['new']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment