-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnew_github_issue_url.py
117 lines (86 loc) · 3.56 KB
/
new_github_issue_url.py
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
from json import dumps
try:
from urllib import urlencode, unquote
from urlparse import urlparse, parse_qsl, ParseResult
except ImportError:
# Python 3 fallback
from urllib.parse import (
urlencode, unquote, urlparse, parse_qsl, ParseResult
)
import webbrowser
class IssueUrl:
def __init__(self, options):
repoUrl = None
self.opts = options
if "repoUrl" in self.opts:
repoUrl = self.opts["repoUrl"]
try:
del self.opts["user"]
del self.opts["repo"]
except:
pass
elif "user" in self.opts and "repo" in options:
try:
del self.opts["repoUrl"]
except:
pass
repoUrl = "https://github.com/{0}/{1}".format(self.opts["user"], self.opts["repo"])
else:
raise KeyError('You need to specify either the `repoUrl` option or both the `user` and `repo` options')
self.url = "{0}/issues/new".format(repoUrl)
self.types = [
'body', 'title', 'labels', 'template', 'milestone', 'assignee', 'projects'
]
def add_url_params(self, url, params):
""" Add GET params to provided URL being aware of existing.
:param url: string of target URL
:param params: dict containing requested params to be added
:return: string with updated URL
>> url = 'http://stackoverflow.com/test?answers=true'
>> new_params = {'answers': False, 'data': ['some','values']}
>> add_url_params(url, new_params)
'http://stackoverflow.com/test?data=some&data=values&answers=false'
Source: https://stackoverflow.com/a/25580545/3821823
"""
# Unquoting URL first so we don't loose existing args
url = unquote(url)
# Extracting url info
parsed_url = urlparse(url)
# Extracting URL arguments from parsed URL
get_args = parsed_url.query
# Converting URL arguments to dict
parsed_get_args = dict(parse_qsl(get_args))
# Merging URL arguments dict with new params
parsed_get_args.update(params)
# Bool and Dict values should be converted to json-friendly values
# you may throw this part away if you don't like it :)
parsed_get_args.update(
{k: dumps(v) for k, v in parsed_get_args.items()
if isinstance(v, (bool, dict))}
)
# Converting URL argument to proper query string
encoded_get_args = urlencode(parsed_get_args, doseq=True)
# Creating new parsed result object based on provided with new
# URL arguments. Same thing happens inside of urlparse.
new_url = ParseResult(
parsed_url.scheme, parsed_url.netloc, parsed_url.path,
parsed_url.params, encoded_get_args, parsed_url.fragment
).geturl()
return new_url
def get_url(self):
url = self.url
for type in self.types:
try:
value = self.opts[type]
except:
continue
if type == "labels" or type == "projects":
if not isinstance(value, list):
err = "The {0} option should be an array".format(type)
raise TypeError(err)
value = ",".join(map(str, value))
self.opts[type] = value
self.url = self.add_url_params(url, self.opts)
return self.url
def opn(self):
webbrowser.open(self.get_url(), 1)