-
Notifications
You must be signed in to change notification settings - Fork 841
Expand file tree
/
Copy pathfetch-github-data.ts
More file actions
206 lines (178 loc) · 6.38 KB
/
Copy pathfetch-github-data.ts
File metadata and controls
206 lines (178 loc) · 6.38 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { config } from 'dotenv';
import { LibraryType } from '~/types';
import hasNativeCode from '~/util/hasNativeCode';
import { parseGitHubUrl } from '~/util/parseGitHubUrl';
import { processTopics, sleep, REQUEST_SLEEP, makeGraphqlQuery, getUpdatedUrl } from './helpers';
import GitHubLicensesQuery from './queries/GitHubLicensesQuery';
import GitHubRepositoryQuery from './queries/GitHubRepositoryQuery';
config();
const licenses = {
isc: {
name: 'ISC License',
url: 'https://www.isc.org/licenses/',
key: 'isc',
spdxId: 'ISC',
},
};
/**
* Fetch licenses from GitHub to be used later to parse licenses from npm
*/
export async function loadGitHubLicenses() {
const result = await makeGraphqlQuery(GitHubLicensesQuery);
result.data.licenses.forEach(license => {
licenses[license.key] = license;
});
}
export async function fetchGithubRateLimit() {
// Accurately fetch query rate limit and cost by making dummy request
// https://developer.github.com/v4/guides/resource-limitations/
const result = await makeGraphqlQuery(GitHubRepositoryQuery, {
repoOwner: 'react-native-directory',
repoName: 'website',
});
if (result.data) {
const { rateLimit } = result.data;
return {
apiLimit: rateLimit.limit,
apiLimitRemaining: rateLimit.remaining,
apiLimitCost: rateLimit.cost,
};
}
if (result.errors) {
console.log('[GH] GraphQL API error:', result.errors);
}
return {};
}
export async function fetchGithubData(data: LibraryType, retries = 2) {
if (retries < 0) {
console.error(`[GH] ERROR fetching ${data.githubUrl} - OUT OF RETRIES`);
return data;
}
try {
const url = data.githubUrl;
const { isMonorepo, repoOwner, repoName, packagePath } = parseGitHubUrl(url);
const fullName = `${repoOwner}/${repoName}`;
const result = await makeGraphqlQuery(GitHubRepositoryQuery, {
repoOwner,
repoName,
packagePath,
packageFilesPath: packagePath === '.' ? 'HEAD:' : `HEAD:${packagePath}`,
packageJsonPath: `HEAD:${packagePath === '.' ? '' : `${packagePath}/`}package.json`,
});
if (result.errors) {
if (result.errors[0].type === 'NOT_FOUND') {
const newUrl = await getUpdatedUrl(url);
if (newUrl !== url) {
console.warn(`[GH] Repository ${fullName} has moved to ${newUrl}`);
data.githubUrl = newUrl;
} else {
console.warn(`[GH] Repository ${fullName} not found`);
}
} else {
console.warn(`[GH] Data fetch error for ${fullName}`, result.errors);
}
console.log(`[GH] Retrying fetch for ${data.githubUrl} due to error result`);
await sleep(REQUEST_SLEEP, REQUEST_SLEEP * 2);
return await fetchGithubData(data, retries - 1);
}
if (!result?.data?.repository) {
console.log(
`[GH] Retrying fetch for ${data.githubUrl} due to ${result?.message?.toLowerCase() ?? 'missing data'} (status: ${result?.status ?? 'Unknown'})`
);
await sleep(REQUEST_SLEEP, REQUEST_SLEEP * 2);
return await fetchGithubData(data, retries - 1);
}
const github = createRepoDataWithResponse(result.data.repository, isMonorepo);
return {
...data,
github,
};
} catch (error) {
console.log(`[GH] Retrying fetch for ${data.githubUrl} due to an error`, error);
await sleep(REQUEST_SLEEP, REQUEST_SLEEP * 2);
return await fetchGithubData(data, retries - 1);
}
}
// Get the GitHub license spec from the npm string
function getLicenseFromPackageJson(packageJson: Record<string, string | object>) {
if (packageJson.license && typeof packageJson.license === 'string') {
return licenses[packageJson.license.toLowerCase()];
}
}
function createRepoDataWithResponse(json, monorepo: boolean) {
if (json.packageJson) {
try {
const packageJson = JSON.parse(json.packageJson.text);
json.newArchitecture = Boolean(packageJson.codegenConfig);
json.name = packageJson.name;
json.isPackagePrivate = packageJson.private ?? false;
json.registry = packageJson?.publishConfig?.registry ?? undefined;
json.dependenciesCount = packageJson.dependencies
? Object.keys(packageJson.dependencies).length
: 0;
if (monorepo) {
json.homepageUrl = packageJson.homepage;
json.topics = processTopics(packageJson.keywords);
json.description = packageJson.description;
json.licenseInfo = getLicenseFromPackageJson(packageJson);
}
if (!monorepo) {
json.topics = [
...new Set([
...processTopics(packageJson.keywords),
...processTopics(json.repositoryTopics.nodes.map(({ topic }) => topic.name)),
]),
];
json.description ??= packageJson.description;
if (!json.licenseInfo || (json.licenseInfo && json.licenseInfo.key === 'other')) {
json.licenseInfo = getLicenseFromPackageJson(packageJson) ?? json.licenseInfo;
}
}
if (packageJson.types || packageJson.typings) {
json.types = true;
}
} catch (e) {
console.error(`Unable to parse ${json.name} package.json file!`);
console.error(e);
}
}
if (!monorepo) {
json.lastRelease =
json.releases && json.releases.nodes && json.releases.nodes.length
? json.releases.nodes[0]
: undefined;
}
const lastCommitAt = json.defaultBranchRef.target.history.nodes[0].committedDate;
return {
urls: {
repo: json.url,
homepage: json?.homepageUrl?.length > 0 ? json.homepageUrl : null,
},
stats: {
hasIssues: json.hasIssuesEnabled,
hasWiki: json.hasWikiEnabled,
hasSponsorships: json.hasSponsorshipsEnabled,
hasDiscussions: json.hasDiscussionsEnabled,
hasTopics: json.topics && json.topics.length > 0,
updatedAt: lastCommitAt,
createdAt: json.createdAt,
pushedAt: lastCommitAt,
forks: json.forks.totalCount,
issues: json.issues.totalCount,
subscribers: json.watchers.totalCount,
stars: json.stargazers.totalCount,
dependencies: json.dependenciesCount,
},
name: json.name,
fullName: json.nameWithOwner,
isPrivate: json.isPackagePrivate,
registry: json.registry,
description: json.description,
topics: json.topics,
license: json.licenseInfo,
hasTypes: json.types ?? false,
newArchitecture: json.newArchitecture,
isArchived: json.isArchived,
hasNativeCode: hasNativeCode(json.files),
};
}