-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversioner.js
More file actions
117 lines (108 loc) · 3.88 KB
/
versioner.js
File metadata and controls
117 lines (108 loc) · 3.88 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
const aws = require("aws-sdk");
const core = require("@actions/core");
/**
* Finds the "latest" version on the remote CodeArtifact service which matches a given prefix
*/
async function findOnCodeartifactByPrefix(domainName, format, packageName, repositoryName, prefix) {
const ca = new aws.CodeArtifact();
let nextToken = undefined;
do {
try {
const listVersionsResp = await ca
.listPackageVersions({
domain: domainName,
format: format,
package: packageName,
repository: repositoryName,
status: "Published",
sortBy: "PUBLISHED_TIME",
nextToken: nextToken,
})
.promise();
for (const index in listVersionsResp.versions) {
const version = listVersionsResp.versions[index];
core.debug(`Saw version ${version.version}`);
if (version.version.startsWith(prefix + ".")) {
return version.version;
}
}
nextToken = listVersionsResp.nextToken;
} catch (e) {
if (e.code === "ResourceNotFoundException") {
/**
* In the case the package cannot be found on code artifact, we return a null.
* This may happen because the package is not yet created in code artifact.
*/
return null
} else {
throw e
}
}
} while (nextToken !== undefined);
return null;
}
/**
* Takes a dot-separated version string, increments the last segment by `increment` and returns the new version string
* @param {string} version
* @param {number} increment
*/
function incrementVersionString(version, increment) {
const parts = version.split(".").map((s) => parseInt(s, 10));
parts.push(parts.pop() + increment);
return parts.join(".");
}
/**
* Main function with GitHub Actions abstracted away in run
* Tests us this as the main function
* @returns {Promise<string>}
*/
async function computeVersion(
domainName,
packageName,
repositoryName,
format,
prefix,
dieOnMissing,
increment,
) {
// Prefix ends up practically defining two modes. See README for more details
if (prefix) {
core.info(`Scanning CodeArtifact for latest package version for ${repositoryName}:${packageName} with prefix ${prefix}`);
const remoteVersion = await findOnCodeartifactByPrefix(domainName, format, packageName, repositoryName, prefix);
if (remoteVersion === null) {
if (dieOnMissing) {
throw new Error("No versions found matching prefix");
} else {
// Return a new "first version" under the prefix
return prefix + ".1";
}
}
return incrementVersionString(remoteVersion, increment);
} else {
core.info("No prefix provided, fetching defaultDisplayVersion");
// Do a list versions with minimum limit since we just want defaultDisplayVersion
const ca = new aws.CodeArtifact();
const listVersionsResp = await ca.listPackageVersions({
domain: domainName,
format,
package: packageName,
repository: repositoryName,
}).promise();
const defaultDisplayVersion = listVersionsResp.defaultDisplayVersion;
core.info(`defaultDisplayVersion: ${defaultDisplayVersion}`);
// Deal with missing
if (defaultDisplayVersion === undefined) {
if (dieOnMissing) {
throw new Error(`No defaultDisplayVersion found`);
} else {
return "1";
}
}
return incrementVersionString(defaultDisplayVersion, increment);
}
}
module.exports = {
findOnCodeartifactByPrefix,
incrementVersionString,
computeVersion,
}