-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
135 lines (121 loc) · 5.05 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
import core from "@actions/core";
import github from "@actions/github";
// noinspection ES6UnusedImports
import {paginateRest, composePaginateRest} from "@octokit/plugin-paginate-rest"
function filterPackages(res, repo, nameMatcher) {
return res.data
.filter(d => {
if (!d.repository) return false;
return d.repository.name === repo
})
.filter(d => nameMatcher.test(d.name))
.map(d => {
return {name: d.name, count: d.version_count}
})
}
function filterVersions(res, pkgName, versionMatcher, tagMatcher, untagged) {
return res.data
.filter(d => versionMatcher.test(d.name))
.map(d => {
let tags = ""
if(d.metadata && d.metadata.container && d.metadata.container.tags) {
tags = d.metadata.container.tags.join(",")
}
return {name: pkgName, version: d.name, id: d.id, tags: tags}
})
.filter(d => {
if (untagged && !d.tags) {
return true;
} else if(untagged) {
return false;
}
if (tagMatcher) {
return tagMatcher.test(d.tags)
} else {
return true
}
})
}
async function main() {
const packageNamePattern = core.getInput('name') || process.env.PKG_NAME_PATTERN;
const packageVersionPattern = core.getInput('version') || process.env.PKG_VERSION_PATTERN;
const packageType = core.getInput('type') || process.env.PKG_TYPE;
const token = core.getInput('token') || process.env.GITHUB_TOKEN;
const tag = core.getInput('tag') || process.env.TAG;
const untagged = core.getBooleanInput('untagged', {required: false}) || process.env.UNTAGGED === 'true';
const {owner, repo} = github.context.repo
if (!packageVersionPattern) {
throw new Error(`No package version pattern specified. Please configure the 'version' input`)
}
if (!packageNamePattern) {
throw new Error(`No package name pattern specified. Please configure the 'name' input`)
}
if (!packageType) {
throw new Error(`No package type specified. Please configure the 'type' input`)
}
const packageNameMatcher = new RegExp(packageNamePattern)
const packageVersionMatcher = new RegExp(packageVersionPattern)
const tagMatcher = tag ? new RegExp(tag) : null
const octokit = github.getOctokit(token)
core.info(`Searching for packages in ${repo} owned by ${owner} that match the name-pattern: '${packageNamePattern}' and version-pattern: '${packageVersionPattern}' with package-type: '${packageType}'`)
// list packages for repo to find all packages that match the package name
// pattern and belong to this repo
let allPackages = await octokit.paginate('GET /orgs/{org}/packages',
{ org: owner, package_type: packageType },
(res) => filterPackages(res, repo, packageNameMatcher)
)
for (let allPackagesKey in allPackages) {
core.debug("Matched Package: " + allPackagesKey + " " + JSON.stringify(allPackages[allPackagesKey]))
}
core.info(`Found ${allPackages.length} packages that match '${packageNamePattern}' in repo ${repo}`)
// Find all the version in the packages that match the
// version pattern
let matchingVersions = []
for (let i = 0; i < allPackages.length; i++) {
let pkg = allPackages[i];
let versions = await octokit.paginate('GET /orgs/{org}/packages/{package_type}/{package_name}/versions',
{package_type: packageType, package_name: pkg.name, org: owner},
(res) => filterVersions(res, pkg.name, packageVersionMatcher, tagMatcher, untagged)
)
matchingVersions = matchingVersions.concat(versions)
}
matchingVersions.forEach(version => {
core.debug("Matched version " + JSON.stringify(version))
})
core.info(`Found ${matchingVersions.length} versions that match '${packageVersionPattern}' in repo ${repo} for ${allPackages.length} matched packages`)
let encounteredError = false;
//delete the versions that we matched
for (let i = 0; i < matchingVersions.length; i++) {
let v = matchingVersions[i]
core.info(`Deleting Name: ${v.name} Version: ${v.version} Id: ${v.id}`)
try {
await octokit.request('DELETE /orgs/{org}/packages/{package_type}/{package_name}/versions/{package_version_id}', {
package_type: packageType,
package_name: v.name,
package_version_id: v.id,
org: owner,
})
}catch (e) {
core.error(`'Error while trying to delete Name: ${v.name} Version: ${v.version} Id: ${v.id}: ${e.message}`)
if (e.message === "You cannot delete the last version of a package. You must delete the package instead.") {
core.info("Deleting package instead of just the last version")
try {
await octokit.request('DELETE /orgs/{org}/packages/{package_type}/{package_name}', {
package_type: packageType,
package_name: v.name,
org: owner,
})
}catch (e) {
core.error(`'Error while trying to deleting package: ${e.message}`)
encounteredError = true
}
} else {
encounteredError = true
}
}
}
if(encounteredError) {
throw new Error('An error occurred while deleting versions. Please check the log for details.')
}
}
main();