-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
184 lines (162 loc) · 4.9 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
184
const core = require('@actions/core');
const toolCache = require('@actions/tool-cache');
const process = require('process');
const childProcess = require('child_process');
const https = require('https');
async function getReleases() {
return new Promise((resolve, reject) => {
https
.get(
"https://api.github.com/repos/applanga/applanga-cli/releases",
{
headers: { "User-Agent": "Node.js" },
},
(res) => {
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
if (res.statusCode === 200) {
resolve(JSON.parse(data));
} else {
reject(new Error(`Failed to get releases: ${res.statusCode}`));
}
});
}
)
.on("error", (err) => {
reject(err);
});
});
}
function compareVersions(v1, v2) {
const v1Parts = v1.split(".").map(Number);
const v2Parts = v2.split(".").map(Number);
for (let i = 0; i < v1Parts.length; i++) {
if (v1Parts[i] > v2Parts[i]) return 1;
if (v1Parts[i] < v2Parts[i]) return -1;
}
return 0;
}
async function getLatestVersion(version) {
const releases = await getReleases();
// Regex patterns for the expected wildcard formats
const majorMinorPattern = /^(\d+)\.(\d+)\.\*$/; // match x.y.*
const majorPattern = /^(\d+)\.\*\.\*$/; // match x.*.*
const anyVersionPattern = /^\*$/; // match *
let latestVersion = null;
switch (true) {
case majorMinorPattern.test(version):
// Major.Minor.* pattern match
const [_, major, minor] = version.match(majorMinorPattern);
for (const release of releases) {
const tagName = release.tag_name;
if (
tagName.startsWith(`${major}.${minor}.`) &&
!release.prerelease &&
!release.draft &&
(!latestVersion || compareVersions(tagName, latestVersion) > 0)
) {
latestVersion = tagName;
}
}
break;
case majorPattern.test(version):
// Major.*.* pattern match
const [__, majorOnly] = version.match(majorPattern);
for (const release of releases) {
const tagName = release.tag_name;
if (
tagName.startsWith(`${majorOnly}.`) &&
!release.prerelease &&
!release.draft &&
(!latestVersion || compareVersions(tagName, latestVersion) > 0)
) {
latestVersion = tagName;
}
}
break;
case anyVersionPattern.test(version):
// * pattern match
for (const release of releases) {
const tagName = release.tag_name;
if (
!release.prerelease &&
!release.draft &&
(!latestVersion || compareVersions(tagName, latestVersion) > 0)
) {
latestVersion = tagName;
}
}
break;
default:
throw new Error(
`Invalid version pattern: ${version}. Accepted patterns: *, x.*.*, x.y.*`
);
}
return latestVersion;
}
async function run() {
try {
const applanga = 'applanga'
const osPlatform = process.platform;
var url = '', extractedFile = '';
//getting version from workflow input
let version = core.getInput('version');
// Resolve version using wildcard
if (version.includes('*')) {
version = await getLatestVersion(version);
if (!version) {
core.setFailed(`No matching version found for: ${core.getInput('version')}`);
return;
}
}
//checking if the file was alredy downloaded
//if so, no need to redownload
const cachePath = toolCache.find(applanga, version);
if (cachePath && cachePath !== '') {
core.addPath(cachePath);
return;
}
//figuring out teh download path
switch (osPlatform) {
case 'win32':
url = 'https://github.com/applanga/applanga-cli/releases/download/' + version + '/applanga_windows.zip';
break;
case 'darwin':
url = 'https://github.com/applanga/applanga-cli/releases/download/' + version + '/applanga_osx.tar.gz';
break;
case 'linux':
url = 'https://github.com/applanga/applanga-cli/releases/download/' + version + '/applanga_linux.tar.gz';
break;
default:
core.setFailed('Unsupported OS system');
return;
}
const downloaded = await toolCache.downloadTool(url);
//unpacking the downloaded file
switch (osPlatform) {
case 'win32':
extractedFile = await toolCache.extractZip(downloaded, 'extracted');
break;
case 'darwin':
case 'linux':
extractedFile = await toolCache.extractTar(downloaded, 'extracted');
break;
default:
core.setFailed('Unsupported OS system');
return;
}
//adding teh path to teh enviroment
const finalPath = await toolCache.cacheDir(extractedFile, applanga, version);
core.addPath(finalPath);
//setting rights to run the command
if (osPlatform !== 'win32') {
childProcess.exec(`chmod +x ${finalPath}/${applanga}`);
}
} catch (err) {
core.setFailed(err);
}
}
run();