Skip to content

Commit 5403e90

Browse files
committed
Add fallback to download zip as jar
For some reason releases before 2.9.2 contains zip instead of jar.
1 parent cf36221 commit 5403e90

File tree

4 files changed

+139
-14
lines changed

4 files changed

+139
-14
lines changed

__tests__/leiningen.test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@ const toolPath = join(__dirname, 'runner', 'tools', 'leiningen')
1111
const tempPath = join(__dirname, 'runner', 'temp', 'leiningen')
1212
const downloadPath = join(__dirname, 'runner', 'download')
1313
const jarDownloadPath = join(__dirname, 'runner', 'download', 'leiningen.jar')
14+
const zipDownloadPath = join(__dirname, 'runner', 'download', 'leiningen.zip')
1415
const cachePath = join(__dirname, 'runner', 'cache')
1516

1617
import * as leiningen from '../src/leiningen'
1718

19+
function httpError(statusCode: number): Error & {httpStatusCode: number} {
20+
return Object.assign(new Error(`Unexpected HTTP response: ${statusCode}`), {
21+
httpStatusCode: statusCode
22+
})
23+
}
24+
1825
jest.mock('@actions/core')
1926
const core: jest.Mocked<typeof _core> = _core as never
2027

@@ -45,6 +52,7 @@ describe('leiningen tests', () => {
4552
afterEach(async () => {
4653
jest.spyOn(global.Math, 'random').mockRestore()
4754
jest.resetAllMocks()
55+
global.fetch = undefined as never
4856
delete process.env['RUNNER_TOOL_CACHE']
4957
delete process.env['RUNNER_TEMP']
5058
})
@@ -175,6 +183,66 @@ describe('leiningen tests', () => {
175183
expect(core.addPath).toHaveBeenCalledWith(join(cachePath, 'bin'))
176184
})
177185

186+
it('Falls back to zip artifact when jar returns 404', async () => {
187+
tc.downloadTool.mockResolvedValueOnce(downloadPath)
188+
tc.downloadTool.mockRejectedValueOnce(httpError(404))
189+
tc.downloadTool.mockResolvedValueOnce(zipDownloadPath)
190+
fs.stat.mockResolvedValueOnce({isFile: () => true} as never)
191+
tc.cacheDir.mockResolvedValueOnce(cachePath)
192+
193+
await leiningen.setup('2.9.1')
194+
195+
expect(tc.downloadTool).toHaveBeenNthCalledWith(
196+
2,
197+
'https://github.com/technomancy/leiningen/releases/download/2.9.1/leiningen-2.9.1-standalone.jar',
198+
join(tempPath, 'leiningen-2.9.1-standalone.jar'),
199+
undefined
200+
)
201+
expect(tc.downloadTool).toHaveBeenNthCalledWith(
202+
3,
203+
'https://github.com/technomancy/leiningen/releases/download/2.9.1/leiningen-2.9.1-standalone.zip',
204+
join(tempPath, 'leiningen-2.9.1-standalone.zip'),
205+
undefined
206+
)
207+
expect(io.mv).toHaveBeenCalledWith(
208+
zipDownloadPath,
209+
join(tempPath, 'leiningen-2.9.1-standalone.jar')
210+
)
211+
expect(io.mv).toHaveBeenCalledWith(
212+
join(tempPath, 'leiningen-2.9.1-standalone.jar'),
213+
join(
214+
tempPath,
215+
'temp_2000000000',
216+
'leiningen',
217+
'self-installs',
218+
'leiningen-2.9.1-standalone.jar'
219+
)
220+
)
221+
})
222+
223+
it('Fails when both jar and zip artifacts return 404', async () => {
224+
tc.downloadTool.mockResolvedValueOnce(downloadPath)
225+
tc.downloadTool.mockRejectedValueOnce(httpError(404))
226+
tc.downloadTool.mockRejectedValueOnce(httpError(404))
227+
228+
await expect(leiningen.setup('2.9.1')).rejects.toThrow(
229+
'Unexpected HTTP response: 404'
230+
)
231+
232+
expect(tc.downloadTool).toHaveBeenNthCalledWith(
233+
2,
234+
'https://github.com/technomancy/leiningen/releases/download/2.9.1/leiningen-2.9.1-standalone.jar',
235+
join(tempPath, 'leiningen-2.9.1-standalone.jar'),
236+
undefined
237+
)
238+
expect(tc.downloadTool).toHaveBeenNthCalledWith(
239+
3,
240+
'https://github.com/technomancy/leiningen/releases/download/2.9.1/leiningen-2.9.1-standalone.zip',
241+
join(tempPath, 'leiningen-2.9.1-standalone.zip'),
242+
undefined
243+
)
244+
})
245+
178246
it('Uses version of leiningen installed in cache', async () => {
179247
tc.find.mockReturnValue(cachePath)
180248
await leiningen.setup('2.9.1')

dist/index.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,35 @@ const fs = __importStar(__nccwpck_require__(2621));
11241124
const utils = __importStar(__nccwpck_require__(9277));
11251125
exports.identifier = 'Leiningen';
11261126
const LEIN_JAR_BASE_URL = 'https://github.com/technomancy/leiningen/releases/download';
1127+
function is404Error(error) {
1128+
return (typeof error === 'object' &&
1129+
error !== null &&
1130+
'httpStatusCode' in error &&
1131+
error.httpStatusCode === 404);
1132+
}
1133+
function downloadStandaloneJar(version, githubAuth) {
1134+
return __awaiter(this, void 0, void 0, function* () {
1135+
const jarFileName = `leiningen-${version}-standalone.jar`;
1136+
const jarPath = path.join(utils.getTempDir(), jarFileName);
1137+
const jarUrl = `${LEIN_JAR_BASE_URL}/${version}/${jarFileName}`;
1138+
core.info(`Downloading Leiningen JAR from ${jarUrl}`);
1139+
try {
1140+
return yield tc.downloadTool(jarUrl, jarPath, githubAuth);
1141+
}
1142+
catch (error) {
1143+
if (!is404Error(error)) {
1144+
throw error;
1145+
}
1146+
const zipFileName = jarFileName.replace(/\.jar$/, '.zip');
1147+
const zipUrl = `${LEIN_JAR_BASE_URL}/${version}/${zipFileName}`;
1148+
const zipPath = path.join(utils.getTempDir(), zipFileName);
1149+
core.info(`Leiningen JAR returned 404, retrying ZIP artifact from ${zipUrl}`);
1150+
const downloadedZipPath = yield tc.downloadTool(zipUrl, zipPath, githubAuth);
1151+
yield io.mv(downloadedZipPath, jarPath);
1152+
return jarPath;
1153+
}
1154+
});
1155+
}
11271156
function setup(version, githubAuth) {
11281157
return __awaiter(this, void 0, void 0, function* () {
11291158
let toolPath = tc.find(exports.identifier, utils.getCacheVersionString(version), os.arch());
@@ -1142,10 +1171,7 @@ function setup(version, githubAuth) {
11421171
else {
11431172
binScripts.push(yield tc.downloadTool(`https://raw.githubusercontent.com/technomancy/leiningen/${version === 'latest' ? 'stable' : version}/bin/lein`, path.join(utils.getTempDir(), 'lein'), githubAuth));
11441173
}
1145-
const jarFileName = `leiningen-${resolvedVersion}-standalone.jar`;
1146-
const jarUrl = `${LEIN_JAR_BASE_URL}/${resolvedVersion}/${jarFileName}`;
1147-
core.info(`Downloading Leiningen JAR from ${jarUrl}`);
1148-
const jarPath = yield tc.downloadTool(jarUrl, path.join(utils.getTempDir(), jarFileName), githubAuth);
1174+
const jarPath = yield downloadStandaloneJar(resolvedVersion, githubAuth);
11491175
const tempDir = path.join(utils.getTempDir(), `temp_${Math.floor(Math.random() * 2000000000)}`);
11501176
const leiningenDir = yield installLeiningen(binScripts, jarPath, resolvedVersion, tempDir);
11511177
core.debug(`Leiningen installed to ${leiningenDir}`);

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/leiningen.ts

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,45 @@ export const identifier = 'Leiningen'
1212
const LEIN_JAR_BASE_URL =
1313
'https://github.com/technomancy/leiningen/releases/download'
1414

15+
function is404Error(error: unknown): boolean {
16+
return (
17+
typeof error === 'object' &&
18+
error !== null &&
19+
'httpStatusCode' in error &&
20+
error.httpStatusCode === 404
21+
)
22+
}
23+
24+
async function downloadStandaloneJar(
25+
version: string,
26+
githubAuth?: string
27+
): Promise<string> {
28+
const jarFileName = `leiningen-${version}-standalone.jar`
29+
const jarPath = path.join(utils.getTempDir(), jarFileName)
30+
const jarUrl = `${LEIN_JAR_BASE_URL}/${version}/${jarFileName}`
31+
core.info(`Downloading Leiningen JAR from ${jarUrl}`)
32+
33+
try {
34+
return await tc.downloadTool(jarUrl, jarPath, githubAuth)
35+
} catch (error) {
36+
if (!is404Error(error)) {
37+
throw error
38+
}
39+
40+
const zipFileName = jarFileName.replace(/\.jar$/, '.zip')
41+
const zipUrl = `${LEIN_JAR_BASE_URL}/${version}/${zipFileName}`
42+
const zipPath = path.join(utils.getTempDir(), zipFileName)
43+
44+
core.info(
45+
`Leiningen JAR returned 404, retrying ZIP artifact from ${zipUrl}`
46+
)
47+
48+
const downloadedZipPath = await tc.downloadTool(zipUrl, zipPath, githubAuth)
49+
await io.mv(downloadedZipPath, jarPath)
50+
return jarPath
51+
}
52+
}
53+
1554
export async function setup(
1655
version: string,
1756
githubAuth?: string
@@ -54,15 +93,7 @@ export async function setup(
5493
)
5594
}
5695

57-
const jarFileName = `leiningen-${resolvedVersion}-standalone.jar`
58-
const jarUrl = `${LEIN_JAR_BASE_URL}/${resolvedVersion}/${jarFileName}`
59-
core.info(`Downloading Leiningen JAR from ${jarUrl}`)
60-
61-
const jarPath = await tc.downloadTool(
62-
jarUrl,
63-
path.join(utils.getTempDir(), jarFileName),
64-
githubAuth
65-
)
96+
const jarPath = await downloadStandaloneJar(resolvedVersion, githubAuth)
6697

6798
const tempDir: string = path.join(
6899
utils.getTempDir(),

0 commit comments

Comments
 (0)