forked from thedave42/generate-dependencies-csv-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
183 lines (163 loc) · 5.46 KB
/
index.js
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
const core = require('@actions/core');
const github = require('@actions/github');
const fs = require('fs');
const repoToken = core.getInput('repo-token');
const org_name = core.getInput('org-name');
const repos = core.getInput('repo-names');
const trans_depth = parseInt(core.getInput('depth'));
const repoNames = repos.split(',');
const artifact = require('@actions/artifact');
const artifactClient = artifact.create();
const artifactName = `dependency-lists`;
let files = [];
let fileLines = [];
let pagination = null;
let checkedRepos = [];
let indent = [];
let firstIndent = false;
let depth = 0;
const rootDirectory = '.'; // Also possible to use __dirname
const options = {
continueOnError: false
};
let { graphql } = require('@octokit/graphql')
graphql = graphql.defaults({
headers: {
authorization: `token ${repoToken}`,
Accept: 'application/vnd.github.hawkgirl-preview+json'
}
});
const findDeps = async (org, repo) => {
const query =
`query ($org: String! $repo: String! $cursor: String){
repository(owner: $org name: $repo) {
name
dependencyGraphManifests(first: 100 after: $cursor) {
pageInfo {
hasNextPage
endCursor
}
nodes {
dependenciesCount
filename
dependencies {
nodes {
packageManager
packageName
requirements
hasDependencies
repository {
name
owner {
login
}
licenseInfo {
name
spdxId
url
}
}
}
}
}
}
}
}`
;
let hasNextPage = false;
do {
console.log(`${indent.join('')}${org}/${repo}: Finding dependencies...`);
if (checkedRepos.find(check => check.org == org && check.name == repo) != undefined) { // We've already checked this repo
console.log(`${indent.join('')}${org}/${repo}: Already checked.`)
return;
}
let getDepsResult = null;
try {
getDepsResult = await graphql({ query, org: org, repo: repo, cursor: pagination });
}
catch (e) {
console.log(`${indent.join('')}${org}/${repo}: GraphQL query failed: ${e.message}`);
return;
}
checkedRepos.push({
"org": org,
"name": repo
});
hasNextPage = getDepsResult.repository.dependencyGraphManifests.pageInfo.hasNextPage;
const repoDependencies = getDepsResult.repository.dependencyGraphManifests.nodes;
for (const repoDependency of repoDependencies) {
console.log(`${indent.join('')}${org}/${repo}: ${repoDependency.dependenciesCount} dependencies found in ${repoDependency.filename}.`)
for (const dep of repoDependency.dependencies.nodes) {
console.log(`${indent.join('')}${org}/${repo} [${depth}]: Adding ${dep.packageName}`);
fileLines.push(`${dep.packageName}\t${dep.requirements}\t${dep.packageManager}\t${repoDependency.filename}\t${org}/${repo}\t${(dep.repository != undefined && dep.repository.licenseInfo != undefined) ? dep.repository.licenseInfo.name : ''}\t${(dep.repository != undefined && dep.repository.licenseInfo != undefined) ? dep.repository.licenseInfo.spdxId : ''}\t${(dep.repository != undefined && dep.repository.licenseInfo != undefined) ? dep.repository.licenseInfo.url : ''}\t${dep.hasDependencies}`);
if (dep.hasDependencies && dep.repository != undefined && depth < trans_depth) {
try {
console.log(`${indent.join('')}${org}/${repo}: ${dep.packageName} also has dependencies. Looking up ${dep.repository.owner.login}/${dep.repository.name}...`);
if (firstIndent) {
indent.unshift(`|__[${depth}]: `);
}
else {
//indent.shift();
indent.unshift(` `);
indent.pop();
indent.push(`|__[${depth}]: `);
}
depth++;
firstIndent = false;
await findDeps(dep.repository.owner.login, dep.repository.name);
indent.shift();
depth--;
}
catch (e) {
console.log(`${indent.join('')}${org}/${repo}: Recusion request failed: ${e.message}`);
console.log(e);
depth--;
indent.shift();
}
}
}
}
if (hasNextPage) {
console.log('nextpage');
pagination = getDepsResult.repository.dependencyGraphManifests.pageInfo.endCursor;
}
} while (hasNextPage);
}
DumpDependencies();
async function DumpDependencies() {
console.log(`############################################# header-row-fix ######################################################`)
for (const repo of repoNames) {
//Begin get depencies for one repo
firstIndent = true;
indent = [];
depth = 0;
try {
const outfile = `./${org_name}-${repo}-dependency-list.csv`;
console.log(`${indent.join('')}${org_name}/${repo}: Saving dependencies to ${outfile}...`);
checkedRepos = [];
files.push(outfile);
fileLines = [];
headerRow = "packageName\tpackageVersion\tpackageEcosystem\tmanifestFilename\tmanifestOwner\tpackageLicenseName\tpackageLicenseId\tpackgeLicenseUrl\tpackageHasDependencies";
await findDeps(org_name, repo);
let sorted = fileLines.sort((a, b) => {
let packageA = a.split('\t')[4]; // manifest
let packageB = b.split('\t')[4];
if (packageA > packageB) {
return 1;
}
else if (packageA < packageB) {
return -1;
}
else {
return 0;
}
});
fs.writeFileSync(outfile, [headerRow, ...sorted].join('\n'));
console.log(`${indent.join('')}${org_name}/${repo}: ${fileLines.length-2} items saved to ${outfile}`);
// End get dependencies for one repo
} catch (error) {
console.log(`${indent.join('')}${org_name}/${repo}: Request failed:`, error.message);
}
}
const uploadResponse = await artifactClient.uploadArtifact(artifactName, files, rootDirectory, options);
}