Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Make an issue on github using API V3 and Python
import json
import requests
# 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, assignee=None, milestone=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(auth=(USERNAME, PASSWORD))
# Create our issue
issue = {'title': title,
'body': body,
'assignee': assignee,
'milestone': milestone,
'labels': labels}
# Add the issue to our repository
r = session.post(url, json.dumps(issue))
if r.status_code == 201:
print 'Successfully created Issue "%s"' % title
else:
print 'Could not create Issue "%s"' % title
print 'Response:', r.content
make_github_issue('Issue Title', 'Body text', 'assigned_user', 3, ['bug'])
@nikhilgupta10
Copy link

nikhilgupta10 commented Aug 24, 2016

Hi, Do you have an updated version of it?
For example: session = requests.session(auth=(USERNAME, PASSWORD)) is not working with an error: TypeError: session() takes no arguments (1 given)

I changed it to "session = requests.Session(auth=(USERNAME, PASSWORD)) " and still it fails with an error: TypeError: init() got an unexpected keyword argument 'auth'

any help is much appreciated.

@nikhilgupta10
Copy link

nikhilgupta10 commented Aug 24, 2016

Fixed the problem:
session = requests.Session()
session.auth = (USERNAME, PASSWORD)

@alexanderrichtertd
Copy link

alexanderrichtertd commented Oct 16, 2016

Works great with the fix by nikhilgupta10 and I needed to set milestone to None
milestone=None

@mutekinootoko
Copy link

mutekinootoko commented May 17, 2017

also 'labels': []

@zixan
Copy link

zixan commented Aug 10, 2017

assignee has been deprecated in the latest release of API. This is the updated code that worked for me:

import os
import json
import requests

# Authentication for user filing issue (must have read/write access to
# repository to add issue to)
USERNAME = 'billkarma-bot'
PASSWORD = os.environ['BILLKARMA_BOT_GITHUB_PASSWORD']

# The repository to add this issue to
REPO_OWNER = 'footbits'
REPO_NAME = 'billkarma-ios'

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 "%s"' % title
    else:
        print 'Could not create Issue "%s"' % title
        print 'Response:', r.content

make_github_issue('Issue Title', 'Body text', ['bug'])

@youjung-hong
Copy link

youjung-hong commented Oct 11, 2017

print format is changed on python3. This works for me.

import os
import json
import requests

# 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))
    else:
        print ('Could not create Issue {0:s}'.format(title))
        print ('Response:', r.content)

@rapekas
Copy link

rapekas commented Apr 17, 2018

Using bulk import of issues, you may cause tons of notifications as well as ban from GitHub. Use new API described at https://gist.github.com/jonmagic/5282384165e0f86ef105. Known limitation - you cannot assign more than one person to issue. New API supports issue timestamps.

# -*- coding: utf-8 -*-

# Uses Python 2.7

import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import requests
import json

# Authentication for user filing issue (must have read/write access to
# repository to add issue to)
USERNAME = os.environ['GITHUB_USER']
TOKEN = os.environ['GITHUB_TOKEN']

# The repository to add this issue to
REPO_OWNER = USERNAME
REPO_NAME = 'reponame'

def make_github_issue(title, body=None, created_at=None, closed_at=None, updated_at=None, assignee=None, milestone=None, closed=None, labels=None):
    # Create an issue on github.com using the given parameters
    # Url to create issues via POST
    url = 'https://api.github.com/repos/%s/%s/import/issues' % (REPO_OWNER, REPO_NAME)
    
    # Headers
    headers = {
        "Authorization": "token %s" % TOKEN,
        "Accept": "application/vnd.github.golden-comet-preview+json"
    }
    
    # Create our issue
    data = {'issue': {'title': title,
                      'body': body,
                      'created_at': created_at,
                      'closed_at': closed_at,
                      'updated_at': updated_at,
                      'assignee': assignee,
                      'milestone': milestone,
                      'closed': closed,
                      'labels': labels}}

    payload = json.dumps(data)

    # Add the issue to our repository
    response = requests.request("POST", url, data=payload, headers=headers)
    if response.status_code == 202:
        print 'Successfully created Issue "%s"' % title
    else:
        print 'Could not create Issue "%s"' % title
        print 'Response:', response.content

title = 'Pretty title'
body = 'Beautiful body'
created_at = "2014-01-01T12:34:58Z"
closed_at = "2014-01-02T12:24:56Z"
updated_at = "2014-01-03T11:34:53Z"
assignee = 'username'
milestone = 1
closed = False
labels = [
    "bug", "low", "energy"
]

make_github_issue(title, body, created_at, closed_at, updated_at, assignee, milestone, closed, labels)

@ezzra
Copy link

ezzra commented Jun 9, 2020

in case someone still stumbles in this google high ranked code, there is an official github cli tool, still in beta but pretty: https://github.com/cli/cli/tree/v0.9.0

@Vudum
Copy link

Vudum commented Oct 28, 2020

How do I get the bug ID shown immediately in the log when a bug is created? At the moment in the log, we can find "Successfully created Issue "%s"' % title". But how to see the newly created bug ID in the log?

@ADI10HERO
Copy link

ADI10HERO commented Jan 11, 2021

Can we "update" issue title using github API? or modifying the above code?
If yes, then can I get an example snippet to do the same?

@hingoman25
Copy link

hingoman25 commented Jan 21, 2021

I tried using @rapekas way of posting issues but was having some trouble. I receive the 202 code which means that the request was successful but for some reason the issue does not show up when I check my account. Has anyone dealt with this?

@DerBla
Copy link

DerBla commented Dec 27, 2021

@ADI10HERO, this is late but, you would need to change the URL to add the issue number and only add the values you want to update in the body. Also, it would be a patch instead of a post request. Docs for reference https://docs.github.com/en/rest/reference/issues#create-an-issue

REPO_OWNER = 'UserName'
REPO_NAME = 'RepoName'
ISSUE_NUMBER= 'IssueNumber'
AUTH_TOKEN= 'PersonalAccessToken'

def update_issue(ISSUE_NUMBER):
    url = 'https://api.github.com/repos/%s/%s/issues/%s' % (
        REPO_OWNER, REPO_NAME, ISSUE_NUMBER)

    # Create an authenticated session to create the issue
    session = requests.Session()
    # Create our issue and headers to authenticate/format
    headers = {'Authorization': 'token %s' % AUTH_TOKEN,
               'Accept': 'application/vnd.github.v3+json'}
    # Put the values you want to modify 
    issue= {
        'title': 'New issue'
        #  'created_at': created_at,
        #  'closed_at': closed_at,
        #  'updated_at': updated_at,
        #  'assignee': assignee,
        #  'milestone': milestone,
        #  'closed': closed
    }

    # Perform the change in your repository
    r = session.patch(url, data=json.dumps(issue), headers=headers)
    if r.status_code == 200:
        print('Successfully changed Issue #{0}: {1}'.format(
            r.json().get("number"), r.json().get("title")))
    else:
        print('Could not modify Issue #{0}'.format(ISSUE_NUMBER))
        print('Response:', r.content)

@ADI10HERO
Copy link

ADI10HERO commented Jan 3, 2022

Thanks a lot @DerBla

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment