-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathfetch_contributions.py
180 lines (151 loc) · 7.36 KB
/
fetch_contributions.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import argparse
import pandas as pd
import requests
from bs4 import BeautifulSoup
from lxml import html
from tabulate import tabulate
class Fetch_PullRequests:
"""
Fetches the pull requests of a user in a organization.
"""
def __init__(self, username, organization, filename):
"""
:param username: github user
:param organization: Organisation name
:param filename: filename, it's optional
"""
self.ORG_URL = f"https://github.com/orgs/{organization}/repositories"
self.URL = f"https://github.com/{organization}"
self.organization = organization
self.username = username
self.filename = filename
def _list_of_repositories(self):
"""
Function lists the repositories of the organisation.
Returns
-------
list
lists the repositories
"""
page = requests.get(self.ORG_URL)
tree = html.fromstring(page.content)
number_of_pages = tree.xpath(
'//*[@id="org-repositories"]/div/div/div[2]/div/em/@data-total-pages')
Repositories = []
if len(number_of_pages) == 0:
Repositories.extend(tree.xpath(
'//*[contains(concat( " ", @class, " " ), concat( " ", "wb-break-all", " " ))]//*[contains(concat( " ", @class, " " ), concat( " ", "d-inline-block", " " ))]/text()'))
else:
for number in range(1, int(number_of_pages[0]) + 1):
page_ = requests.get(self.ORG_URL + f"?page={number}")
tree = html.fromstring(page_.content)
Repositories.extend(tree.xpath(
'//*[contains(concat( " ", @class, " " ), concat( " ", "wb-break-all", " " ))]//*[contains(concat( " ", @class, " " ), concat( " ", "d-inline-block", " " ))]/text()'))
return list(pd.Series(list(set(Repositories))).str.strip().values)
def _extract_pullrequests(self, repo):
"""
Function fetches the pull request of a repo.
Parameters
----------
repo: str
repository name
Returns
-------
pandas dataframe
dataframe consists of columns - "Title to PR", "Link of PR", "Status(Merged/Closed/Open)"
"""
# initializing the lists to store the title, link and status of the pull request
Title = []
Link = []
Status = []
URL = self.URL + f"/{repo}/pulls?q=is%3Apr+author%3A{self.username}"
page = requests.get(URL)
tree = html.fromstring(page.content)
# to determine the number of pages
number_of_pages = tree.xpath(
'//*[@id="repo-content-pjax-container"]/div/div[6]/div/em/@data-total-pages')
if len(number_of_pages) == 0:
# Title.extend(tree.xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "markdown-title", " " ))]/text()'))
soup = BeautifulSoup(page.text, 'html.parser')
# "Title may contain text in <code> tags. So,to handle it we use beautiful soup.
for tag in soup.find_all('a', attrs={'class': 'markdown-title'}):
Title.append(tag.text.strip())
Link.extend(
tree.xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "markdown-title", " " ))]/@href'))
Status.extend(tree.xpath(
'//*[contains(concat( " ", @class, " " ), concat( " ", "pl-3", " " ))]/span/@aria-label'))
else:
for number in range(1, int(number_of_pages[0]) + 1):
URL = self.URL + \
f"/{repo}/pulls?page={number}&q=is%3Apr+author%3A{self.username}"
page = requests.get(URL)
tree = html.fromstring(page.content)
# Title.extend(tree.xpath('//*[contains(concat( " ", @class, " " ), concat( " ", "markdown-title", " " ))]/text()'))
soup = BeautifulSoup(page.text, 'html.parser')
# Names = tree.xpath(
# '//*[contains(concat( " ", @class, " " ), concat( " ", "opened-by", " " ))]//*[contains(concat( " ", @class, " " ), concat( " ", "Link--muted", " " ))]/text()')
for tag in soup.find_all('a', attrs={'class': 'markdown-title'}):
Title.append(tag.text.strip())
Link.extend(tree.xpath(
'//*[contains(concat( " ", @class, " " ), concat( " ", "markdown-title", " " ))]/@href'))
Status.extend(tree.xpath(
'//*[contains(concat( " ", @class, " " ), concat( " ", "pl-3", " " ))]/span/@aria-label'))
Data = {
"Title to PR": Title,
"Link of PR": Link,
"Status(Merged/Closed/Open)": Status
}
# creating a dataframe with the above dictionary
dataframe = pd.DataFrame.from_dict(Data)
# dataframe.head()
# make necessary changes to the columns of dataframe before returning it
dataframe['Status(Merged/Closed/Open)'] = dataframe['Status(Merged/Closed/Open)'].astype(str).str.replace(
" pull request",
"", regex=False)
if dataframe['Link of PR'].dtype != 'O':
dataframe['Link of PR'] = dataframe['Link of PR'].astype(str)
dataframe['Link of PR'] = 'https://github.com' + \
dataframe['Link of PR']
return dataframe
def get_pullrequests(self):
"""
Function pass the repo parameter to the "_extract_pullrequests" to fetch the pull requests of the particular repo.
Returns
-------
str
return str saying that the file is stored if markdown is not empty.
"""
dataframe = pd.DataFrame()
for repo in self._list_of_repositories():
dataframe = dataframe.append(
self._extract_pullrequests(repo), ignore_index=True)
markdown = dataframe.to_markdown()
if len(markdown) > 0:
# creating a markdown file
# markdown_file = open(f"{self.filename}.md", "w")
with open(f"{self.filename}.md", "w") as markdown_file:
markdown_file.write(markdown)
return "Markdown File is successfully stored"
return "No pull requests found !!"
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-u", "--username", action="store_true")
parser.add_argument(
"user", type=str, help="The name of the user to get the pull requests")
parser.add_argument("-o", "--organization", action="store_true")
parser.add_argument("organization_name", type=str,
help="the organisation where user made the pull requests")
parser.add_argument("-f", "--file", nargs="?")
parser.add_argument("filename", type=str, nargs="?",
help="filename to store the markdown table")
args = parser.parse_args()
if args.filename:
file_name = args.filename
else:
file_name = "Markdown_file"
if args.username and args.organization:
response = Fetch_PullRequests(
args.user, args.organization_name, file_name)
print(response.get_pullrequests())
else:
print("Please pass atleast two arguments: '--username', '--organisation'")