-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathselect.js
106 lines (89 loc) · 3.65 KB
/
select.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
const core = require('@actions/core');
const github = require('@actions/github');
require('isomorphic-fetch');
try {
const owner = core.getInput('owner');
const repositoryName = core.getInput('repository').split("/").slice(-1);
const packageName = core.getInput('package').toLowerCase();
const filterString = core.getInput('filter');
const keep = core.getInput('keep');
const token = core.getInput('github-token');
function filterVersions(json) {
try {
// filter for package in repository
let packages = json.data.repository.packages.edges.filter(element => {
return element.node.name === packageName;
});
// exit if no matching packages found
if (packages.length == 0) {
console.log("No matching packages found in this repository.");
process.exit(0);
}
// filter for specific version names containing in package versions
let versions = packages[0].node.versions.edges.filter(element => {
return element.node.version.indexOf(filterString) > -1;
});
// exit if no maching versions found
if (versions.length == 0) {
console.log("No matching versions for \"" + filterString + "\" found for this package.");
process.exit(0);
}
console.log("\nMatching versions for \"" + filterString + "\":");
// collect ids from filtered versions
let versionIds = versions.map(element => {
console.log(element.node.version);
return element.node.id;
});
// calculate number of versions that can be selected
let sliceNumber = versionIds.length - keep;
console.log("\nKeep latest " + keep + " versions...");
console.log("Select the oldest " + sliceNumber + " versions...");
if ( sliceNumber > 0 ) {
// select the oldest versions
let selectedIds = versionIds.slice(0, sliceNumber);
console.log(selectedIds);
// return the selected versions as comma separated string
return selectedIds.join();
}
// return empty string if no versions are selected
return "";
} catch(e) {
console.log(e);
return process.exit(-1);
}
}
fetch('https://api.github.com/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/vnd.github.packages-preview+json',
'authorization': `token ${token}`,
},
body: JSON.stringify({
query: `{
repository(owner: "${owner}", name: "${repositoryName}") {
packages(first: 1, names: ["${packageName}"]) {
edges {
node {
name
versions(last: 100, orderBy: {field: CREATED_AT, direction: ASC}) {
edges {
node {
id
version
}
}
}
}
}
}
}
}`
}),
})
.then(res => res.json())
.then(resJson => filterVersions(resJson))
.then(versionIds => core.setOutput("versionids", versionIds));
} catch (error) {
core.setFailed(error.message);
}