Skip to content

Instantly share code, notes, and snippets.

@youjung-hong
Created October 11, 2017 00:10
Show Gist options
  • Save youjung-hong/1c29f51442657b77550d0b6d24f33ed6 to your computer and use it in GitHub Desktop.
Save youjung-hong/1c29f51442657b77550d0b6d24f33ed6 to your computer and use it in GitHub Desktop.
create and lock an issue on github
# this code is from https://gist.github.com/JeffPaine/3145490
import os
import json
import requests
import sys
# Authentication for user filing issue (must have read/write access to
# repository to add issue to)
USERNAME = 'CHANGEME'
PASSWORD = 'CHANGEME'
# The repository to add this issue to
REPO_OWNER = 'CHANGEME'
REPO_NAME = 'CHANGEME'
def make_github_issue(title, body=None, labels=None):
'''Create an issue on github.com using the given parameters.'''
# Our url to create issues via POST
url = 'https://api.github.com/repos/%s/%s/issues' % (REPO_OWNER, REPO_NAME)
# Create an authenticated session to create the issue
session = requests.Session()
session.auth = (USERNAME, PASSWORD)
# Create our issue
issue = {'title': title,
'body': body,
'labels': labels}
# Add the issue to our repository
r = session.post(url, json.dumps(issue))
if r.status_code == 201:
print ('Successfully created Issue {0:s}'.format(title))
response = json.loads(r.content.decode('utf-8'))
return response.get('number')
else:
print ('Could not create Issue {0:s}'.format(title))
print ('Response:', r.content)
return 0
def lock_github_issue(number):
'''lock an issue on github.com using the given parameters.'''
# Our url to create issues via POST
url = 'https://api.github.com/repos/%s/%s/issues/%d/lock' % (REPO_OWNER, REPO_NAME, number)
# Create an authenticated session to create the issue
session = requests.Session()
session.auth = (USERNAME, PASSWORD)
# lock our issue
r = session.put(url)
if r.status_code == 204:
print ('Successfully locked Issue {0:d}'.format(number))
else:
print ('Could not lock Issue {0:d}'.format(number))
print ('Response:', r.content)
def close_github_issue(number):
'''lock an issue on github.com using the given parameters.'''
# Our url to create issues via POST
url = 'https://github.com/%s/%s/issue_comments' % (REPO_OWNER, REPO_NAME)
referer = 'https://github.com/%s/%s/issues/%d' % (REPO_OWNER, REPO_NAME, number)
# Create an authenticated session to create the issue
session = requests.Session()
session.auth = (USERNAME, PASSWORD)
# set a header
header = {'referer': referer, 'host': 'github.com'}
session.headers.update(header)
# lock our issue
r = session.post(url)
if r.status_code == 200:
print ('Successfully closed Issue {0:d}'.format(number))
else:
print ('Could not close Issue {0:d}'.format(number))
print ('Response:', r.content)
max = int(sys.argv[1]) if len(sys.argv) > 1 else 1
for i in range(0, max):
issue_number = make_github_issue('Issue Title', 'Body text', ['BUGS'])
# close_github_issue(issue_number) # not working yet
lock_github_issue(issue_number)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment