-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
106 lines (86 loc) · 3.29 KB
/
Copy pathscript.py
File metadata and controls
106 lines (86 loc) · 3.29 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
import xkcd2347
import json
from tomark import Tomark
import os
import requests
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Get GitHub API key
GITHUB_API_KEY = os.getenv('GITHUB_API_KEY')
# Exit if no key is found
if GITHUB_API_KEY is None:
print("No GitHub API key found. Please add it to your .env file")
exit()
# Create requests session
client = requests.Session()
client.headers.update({'Accept': 'application/vnd.github+json', 'Authorization': f'Bearer {GITHUB_API_KEY}', 'X-GitHub-Api-Version': '2022-11-28'})
# Create GitHub object
gh = xkcd2347.GitHub(key=GITHUB_API_KEY)
# Load config file
f = open('config.json')
data = json.load(f)
# Get config values
REPOS = data['repos']
ORGANIZATION = data['organization']
FETCH_METRICS = data['fetchMetrics']
dependencies = []
dependencyMap = {}
# Get dependencies for each repo
print(f"Getting dependencies for {len(REPOS)} repos of {ORGANIZATION}")
for repo in REPOS:
print(f"Getting dependencies for {repo}...")
for dep in gh.get_dependencies(ORGANIZATION, repo):
# Get the nameWithOwner of the repo if it exists
nameWithOwner = "No git repo found"
repository = dep.get('repository', None)
if repository is not None:
nameWithOwner = repository.get('nameWithOwner', None)
# Add to map if not already there
if nameWithOwner not in dependencyMap:
dependencyMap[nameWithOwner] = {
'package': dep['packageName'],
'github': f'[{nameWithOwner}](https://github.com/{nameWithOwner})',
'nameWithOwner': nameWithOwner,
'version': dep['requirements'],
'repos': []
}
# If not already added to repos
if repo not in dependencyMap[nameWithOwner]['repos']:
# Add repo to list
dependencyMap[nameWithOwner]['repos'].append(repo)
# Create a list of dependencies
for key in dependencyMap:
# Turn `repos` into a string. surround the repo names with backticks
repos = dependencyMap[key]['repos']
reposStr = ', '.join([f'`{r}`' for r in repos])
dependency = {
'Package': dependencyMap[key]['package'],
'Github Link': dependencyMap[key]['github'],
'Version': f"`{dependencyMap[key]['version']}`",
'Org Repos': reposStr
}
if nameWithOwner == "No git repo found" or not FETCH_METRICS:
# Add to list
dependencies.append(dependency)
continue
try:
# Get the repo details via HTTP request
response = client.get(f"https://api.github.com/repos/{dependencyMap[key]['nameWithOwner']}")
# Get created_at, updated_at, pushed_at, stargazers_count, and open_issues_count and add them to 'repo'
repoDetails = response.json()
dependency['Last Push'] = repoDetails.get('pushed_at', '-')[0:10]
dependency['Stars'] = repoDetails.get('stargazers_count', '-')
dependency['Open Issues'] = repoDetails.get('open_issues_count', '-')
except:
print(f"Error getting repo details for {nameWithOwner}")
# Add to list
dependencies.append(dependency)
# Sort dependencies by package name
sortedDeps = sorted(dependencies, key=lambda d: d['Package'])
# Create markdown table
markdown = Tomark.table(sortedDeps)
# Write markdown to file
f = open("deps.md", "w")
f.write(markdown)
f.close()