-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
133 lines (111 loc) · 5.62 KB
/
main.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
"""A small Python script to fetch GitHub contributions by organization members and
open-source contributors of a GitHub organization. It places the contributions data
in two CSV files: one for organization members and another for open-source contributors.
"""
import argparse
import csv
import os
import requests
# Read the GitHub access token from the environment variable.
ACCESS_TOKEN = os.environ.get("GITHUB_ACCESS_TOKEN")
# Set up headers with your access token
HEADERS = {
"Authorization": f"Bearer {ACCESS_TOKEN}",
}
if __name__ == "__main__":
# Throw an error if the organization name or access token is not provided.
if not ACCESS_TOKEN:
raise ValueError("Please provide a GitHub personal access token")
# Fetch Organization name from command line arguments.
parser = argparse.ArgumentParser()
parser.add_argument(
"--org",
help="GitHub organization name",
type=str,
required=True,
)
args = parser.parse_args()
org_name = args.org
# Make a GET request to fetch organization repositories.
print(f"Fetching repositories in {org_name}...")
response = requests.get(
f"https://api.github.com/orgs/{org_name}/repos", headers=HEADERS
)
# Retrieve the repositories data if the request is successful.
if response.status_code == 200:
org_repos_data = response.json()
# Fetch the list of organization members.
members_url = f"https://api.github.com/orgs/{org_name}/members"
members_response = requests.get(members_url, headers=HEADERS)
# Retrieve the members data if the request is successful.
if members_response.status_code == 200:
print("Fetching organization members...\n")
members_data = members_response.json()
# Create a set of organization member usernames for faster lookup.
org_members = {member["login"] for member in members_data}
# Create dictionaries to store contributions information.
org_member_contributions = {}
open_source_contributions = {}
# Loop through the repositories in the organization.
for repo in org_repos_data:
print(f"Fetching contributors data in {repo['name']}...")
repo_name = repo["name"]
# Define the REST API endpoint URL for contributors in the repository.
repo_contributors_url = (
f"https://api.github.com/repos/{org_name}/{repo_name}/contributors"
)
# Make a GET request to fetch contributors in the repository
contributors_response = requests.get(
repo_contributors_url, headers=HEADERS
)
# Retrieve the contributions data if the request is successful.
if contributors_response.status_code == 200:
contributors_data = contributors_response.json()
# Loop through the contributors in the repository.
for contributor in contributors_data:
contributor_name = contributor["login"]
contributions = contributor["contributions"]
# Filter data based on organization membership.
if contributor_name in org_members:
if contributor_name in org_member_contributions:
org_member_contributions[
contributor_name
] += contributions
else:
org_member_contributions[
contributor_name
] = contributions
else:
if contributor_name in open_source_contributions:
open_source_contributions[
contributor_name
] += contributions
else:
open_source_contributions[
contributor_name
] = contributions
# Sort contributors by contributions and write to CSV files
org_member_contributions_sorted = sorted(
org_member_contributions.items(), key=lambda x: x[1], reverse=True
)
open_source_contributions_sorted = sorted(
open_source_contributions.items(), key=lambda x: x[1], reverse=True
)
# Define the CSV file names.
print("\nSaving contributions overview to CSV files...")
org_member_csv_file = "org_member_contributions_overview.csv"
open_source_csv_file = "open_source_contributions_overview.csv"
# Write organization member contributions to a CSV file.
with open(org_member_csv_file, mode="w", newline="") as org_member_file:
writer = csv.writer(org_member_file)
writer.writerow(["Username", "Contributions"])
writer.writerows(org_member_contributions_sorted)
# Write open-source contributions to a CSV file.
with open(open_source_csv_file, mode="w", newline="") as open_source_file:
writer = csv.writer(open_source_file)
writer.writerow(["Username", "Contributions"])
writer.writerows(open_source_contributions_sorted)
print(f"Organization member contributions saved to {org_member_csv_file}")
print(f"Open-source contributions saved to {open_source_csv_file}")
else:
print(f"Request failed with status code {response.status_code}")