-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub_create_issue.py
More file actions
executable file
·126 lines (103 loc) · 3.77 KB
/
github_create_issue.py
File metadata and controls
executable file
·126 lines (103 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
import json
import requests
import argparse
from argparse import RawTextHelpFormatter
import os
import sys
USAGE = """Github create issue.
This script allows to create issue in github for given project.
Usage:
# export env vars
export GITHUB_USER=nvtkaszpir
export GITHUB_PASS=<token>
export REPO_OWNER=nvtkaszpir
export REPO_NAME=vagrant-killingfloorstats
# set title, body and two labels
./create_issue.py --title="Test" --body="Hello And Die" -l bug -l jenkins
Based on https://gist.github.com/JeffPaine/3145490 with tweaks
"""
# Authentication for user filing issue (must have read/write access to
# repository to add issue to)
GITHUB_USER = os.environ.get('GITHUB_USER')
GITHUB_PASS = os.environ.get('GITHUB_PASS')
# The repository to add this issue to
REPO_OWNER = os.environ.get('REPO_OWNER')
REPO_NAME = os.environ.get('REPO_NAME')
def check_env_vars():
"""Check env vars"""
if not GITHUB_USER:
print 'Missing GITHUB_USER env var'
return 1
if not GITHUB_PASS:
print 'Missing GITHUB_PASS env var'
return 1
if not REPO_OWNER:
print 'Missing REPO_OWNER env var'
return 1
if not REPO_NAME:
print 'Missing REPO_NAME env var'
return 1
return 0
def make_github_issue(title, body=None, assignee=None, milestone=None, labels=None, dry=None):
'''Create an issue on github.com using the given parameters.'''
if check_env_vars():
print 'Missing env vars, aborting'
return 1
if labels is None:
labels = []
# Create our issue
issue = {
'title': title,
'body': body,
'assignee': assignee,
'milestone': milestone,
'labels': labels
}
if dry:
print 'Would send to github:'
print "REPO_OWNER: %s" % REPO_OWNER
print "REPO_NAME: %s" % REPO_NAME
print "GITHUB_USER: %s" % GITHUB_USER
print "GITHUB_PASS: %s" % "***masked***"
print "title: %s" % args.title
print "body: %s" % args.body
print "assignee: %s" % args.assignee
print "milestone: %s" % args.milestone
print "labels: %s" % args.labels
print "issue: %s" % issue
return 0
# 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 = (GITHUB_USER, GITHUB_PASS)
# Add the issue to our repository
response = session.post(url, json.dumps(issue))
if response.status_code == 201:
print 'Successfully created Issue "%s"' % title
resp_obj = json.loads(response.content)
print 'Response: ', resp_obj['url']
return 0
else:
print 'Could not create Issue "%s"' % title
print 'Response: ', response.content
return 1
# make_github_issue('Issue Title', 'Body text', 'assigned_user', 3, ['bug'])
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=USAGE, formatter_class=RawTextHelpFormatter)
parser.add_argument("-t", "--title", help="github issue title")
parser.add_argument("-b", "--body", help="github issue body text")
parser.add_argument("-a", "--assignee", help="assignee for github issue (optional)", default=None)
parser.add_argument("-m", "--milestone", help="milestone, optional", default=None)
parser.add_argument("-l", "--labels", action="append", help="optional labels to assign")
parser.add_argument("-d", "--dry", action='store_true', help="do not send to github")
args = parser.parse_args()
result = make_github_issue(
title=args.title,
body=args.body,
assignee=args.assignee,
milestone=args.milestone,
labels=args.labels
)
sys.exit(result)