-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.js
executable file
·77 lines (63 loc) · 2.54 KB
/
bin.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
#!/usr/bin/env node
"use strict";
import { requestSnykAndGithubTokens, getSnykOrgs, requestSnykOrganisationsToTidy, getSnykProjectsFromOrganisation, findGitHubProjectsScannedBySnyk, isGitHubRepositoryArchivedOrDeleted, requestToDeleteTheSnykProject } from "./index.js";
// These are used to minimise the amount API calls we make to GitHub
// We check if a repository first exists in one of these arrays
// If it does then we don't need to make another API call to GitHub
let globalActive = [];
let globalArchivedRepos = [];
// Used to inform the user how many projects they tidied up
let globalDeactivationCount = 0;
const { githubToken, snykToken } = await requestSnykAndGithubTokens();
const allAvailableSnykOrganisations = await getSnykOrgs(snykToken);
const { chosenSnykOrganisations } = await requestSnykOrganisationsToTidy(allAvailableSnykOrganisations)
for (const organisation of chosenSnykOrganisations) {
const name = organisation.name;
const slug = organisation.slug;
console.log(`Scanning snyk org named ${name}`);
const projects = await getSnykProjectsFromOrganisation(
snykToken,
organisation.id
);
const repositories = findGitHubProjectsScannedBySnyk(projects);
console.log(`Found ${repositories.length} GitHub repositories being scanned in ${name}`);
let archivedRepos = [];
let deactivationCount = 0;
for (const repository of repositories) {
// If we already know the repository is active then we can skip past it
if (globalActive.find((repo) => repo.name === repository.name)) {
continue;
}
const archivedOrDeleted = globalArchivedRepos.find((repo) => repo.name === repository.name) || await isGitHubRepositoryArchivedOrDeleted(
githubToken,
repository.name
);
if (archivedOrDeleted) {
archivedRepos.push(repository);
globalArchivedRepos.push(repository);
await requestToDeleteTheSnykProject(
repository,
organisation,
slug,
snykToken
);
deactivationCount += 1;
} else {
globalActive.push(repository);
}
}
globalDeactivationCount += deactivationCount
if (archivedRepos.length === 0) {
console.log(
`All of the GitHub repositories being scanned in ${name} are unarchived - there is nothing to tidy up - nice work!`
);
} else {
console.log(
`You tidied up ${deactivationCount} out of a possible ${archivedRepos.length} projects`
);
}
console.log();
}
console.log(
`All done -- you tidied up ${globalDeactivationCount} out of a possible ${globalArchivedRepos.length} projects`
);