forked from HarshCasper/Rotten-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaster_fetch_script.py
96 lines (77 loc) · 2.93 KB
/
master_fetch_script.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
from github import Github
import json
import sys
from decouple import config
def RetriveIssueData(storeHere, repoName):
"""The funtion retrives the data of issues and returns an array"""
issues = repoName.get_issues(state="all")
for i in issues:
issue = {}
issue["Title"] = i.title
issue["Creator"] = i.user.login
issue["Assignees"] = [name.login for name in i.assignees]
issue["OpenedAt"] = i.created_at.strftime("%d-%m-%G")
if i.closed_at is not None:
issue["ClosedAt"] = i.closed_at.strftime("%d-%m-%G")
else:
issue["ClosedAt"] = None
if i.pull_request is not None:
issue["PullRequest"] = i.pull_request.html_url
else:
issue["PullRequest"] = None
issue["Body"] = i.body
issue["Comments"] = [[j.id, j.user.login] for j in i.get_comments()]
storeHere.append(issue)
return storeHere
def RetrivePullRequestData(storeHere, repoName):
"""The funtion retrives the data of PRs and returns an array"""
PRs = repoName.get_pulls(state="all")
for i in PRs:
pr = {}
pr["ID"] = i.id
pr["Creator"] = i.user.login
pr["State"] = i.state
pr["OpenedAt"] = i.created_at.strftime("%d-%m-%G")
if i.merged_at is not None:
pr["MergedAt"] = i.merged_at.strftime("%d-%m-%G")
else:
pr["MergedAt"] = None
if i.closed_at is not None:
pr["ClosedAt"] = i.closed_at.strftime("%d-%m-%G")
else:
pr["ClosedAt"] = None
rews = []
for rew in i.get_review_requests():
for k in rew:
rews.append([k.id, k.login])
pr["Body"] = i.body
pr["Reviewers"] = rews
pr["FilesChanged"] = i.changed_files
if i.merged_by is not None:
pr["MergedBy"] = i.merged_by.login
else:
pr["MergedBy"] = None
storeHere.append(pr)
return storeHere
def WriteToJSON(data, fileName):
"""This function converts the array of dicts to json and stores it to .json file"""
with open(str(fileName) + ".json", "w") as file:
json.dump(data, file, indent=6)
file.close()
print("Successfully created " + str(fileName) + ".json")
if __name__ == "__main__":
# Setup your github token before running the script
# and set the variable in .env file
token = config("GITHUBTOKEN")
g = Github(token)
# Final info is stored in the following datastructures
IssuesInfo = []
PullRequestsInfo = []
# Getting the repo based on the info from command line
repo = g.get_repo(str(sys.argv[1]) + "/" + str(sys.argv[2]))
print("The script may take some time .....")
# Calling the funtions
issueData = RetriveIssueData(IssuesInfo, repo)
WriteToJSON(issueData, "AllIssuesInfo")
PRData = RetrivePullRequestData(PullRequestsInfo, repo)
WriteToJSON(PRData, "AllPullRequestsInfo")