-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-manager.py
More file actions
executable file
·165 lines (124 loc) · 3.57 KB
/
github-manager.py
File metadata and controls
executable file
·165 lines (124 loc) · 3.57 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import argparse
import json
import logging
import requests
class githubPullRequests:
def __init__(
self,
arguments
):
self.state = arguments.state
self.type = arguments.type
self.reponame = arguments.reponame
self.owner = arguments.owner
self.get_pr_response = None
self.github_url = "https://api.github.com/repos/{}/{}/pulls".format(
self.owner,
self.reponame
)
def get_pull_requests(self):
"""
https://developer.github.com/v3/pulls/#list-pull-requests
GET /repos/:owner/:repo/pulls
"""
args = {
"state" : self.state,
"type" : self.type
}
try:
self.get_pr_response = requests.get(
self.github_url,
params = args
)
except Exception as get_pr_exception:
raise get_pr_exception
def display_pull_requests(self):
logging.info(
"""
GitHub Pull Requests Summary
{} repository has {} {} PR's
""".format(
self.reponame,
self.state,
len(json.loads(self.get_pr_response.text))
)
)
for pr_details in json.loads(self.get_pr_response.text):
pr_info = """
"Id" : {}
"Title" : {}
"Url" : {}
"State" : {}
"Last Updated" : {}
"Closed at" : {}
"User" : {}
""".format(
pr_details.get('id'),
pr_details.get('title'),
pr_details.get('html_url'),
pr_details.get('state'),
pr_details.get('updated_at'),
pr_details.get('closed_at'),
pr_details.get('user').get('login')
)
logging.info(pr_info)
def get_arguments():
parser = argparse.ArgumentParser(
description = """
Github Manager
Supported features:
Get list of Pull Requests based on thier state.
"""
)
parser.add_argument(
"-r",
"--reponame",
required = True,
help = "Name of the GitHub repository"
)
parser.add_argument(
"-s",
"--state",
required = True,
help = "State of the PR",
default = "open",
choices = ["open", "closed", "all"]
)
parser.add_argument(
"-t",
"--type",
help = "Type of issue",
default = "pr",
choices = ["pr"]
)
parser.add_argument(
"-o",
"--owner",
help = "Owner of the repository",
required = True
)
parser.add_argument(
"-l",
"--loglevel",
help = "Loglevel for logs",
default = "info",
choices = ["info", "debug"]
)
arguments = parser.parse_args()
return arguments
def log_config(loglevel):
logging.basicConfig(
format = '%(levelname)s:%(message)s',
level = getattr( logging, loglevel.upper())
)
return logging.getLogger()
def main():
arguments = get_arguments()
log_config(arguments.loglevel)
classObj = githubPullRequests(
arguments
)
classObj.get_pull_requests()
classObj.display_pull_requests()
if __name__ == "__main__":
main()