Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion __tests__/entrypoint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,21 @@ describe('setup-clojure', () => {

await main()

expect(lein.setup).toHaveBeenCalledWith('1.2.3', 'Bearer abc')
expect(lein.setup).toHaveBeenCalledWith('1.2.3', 'Bearer abc', undefined)
})

it('forwards the codeberg token to Leiningen setup', async () => {
inputs['lein'] = '2.13.0'
inputs['github-token'] = 'abc'
inputs['codeberg-token'] = 'cbtoken'

await main()

expect(lein.setup).toHaveBeenCalledWith(
'2.13.0',
'Bearer abc',
'token cbtoken'
)
})

it('sets up Boot', async () => {
Expand Down
53 changes: 53 additions & 0 deletions __tests__/leiningen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ describe('leiningen tests', () => {

await leiningen.setup('2.9.1')

// Versions before 2.13.0 fetch the lein script from GitHub raw
expect(tc.downloadTool).toHaveBeenNthCalledWith(
1,
'https://raw.githubusercontent.com/technomancy/leiningen/2.9.1/bin/lein',
join(tempPath, 'lein'),
undefined
)

// Verify JAR was downloaded from GitHub releases
expect(tc.downloadTool).toHaveBeenCalledWith(
'https://github.com/technomancy/leiningen/releases/download/2.9.1/leiningen-2.9.1-standalone.jar',
Expand Down Expand Up @@ -243,6 +251,51 @@ describe('leiningen tests', () => {
)
})

it('Fetches lein 2.13.0 script from the Codeberg API', async () => {
tc.downloadTool.mockResolvedValueOnce(downloadPath)
tc.downloadTool.mockResolvedValueOnce(jarDownloadPath)
fs.stat.mockResolvedValueOnce({isFile: () => true} as never)
tc.cacheDir.mockResolvedValueOnce(cachePath)

await leiningen.setup('2.13.0', 'Bearer gh', 'token cb')

// 2.13.0+ fetch the lein script from the Codeberg API with the codeberg auth
expect(tc.downloadTool).toHaveBeenNthCalledWith(
1,
'https://codeberg.org/api/v1/repos/leiningen/leiningen/raw/bin/lein?ref=2.13.0',
join(tempPath, 'lein'),
'token cb'
)

// JAR still comes from GitHub releases, using the github auth
expect(tc.downloadTool).toHaveBeenNthCalledWith(
2,
'https://github.com/technomancy/leiningen/releases/download/2.13.0/leiningen-2.13.0-standalone.jar',
join(tempPath, 'leiningen-2.13.0-standalone.jar'),
'Bearer gh'
)
})

it('Uses Codeberg for latest when it resolves to 2.13.0+', async () => {
global.fetch = jest.fn().mockResolvedValue({
ok: true,
json: jest.fn().mockResolvedValue({tag_name: '2.13.0'})
})
tc.downloadTool.mockResolvedValueOnce(downloadPath)
tc.downloadTool.mockResolvedValueOnce(jarDownloadPath)
fs.stat.mockResolvedValueOnce({isFile: () => true} as never)
tc.cacheDir.mockResolvedValueOnce(cachePath)

await leiningen.setup('latest', 'Bearer gh', 'token cb')

expect(tc.downloadTool).toHaveBeenNthCalledWith(
1,
'https://codeberg.org/api/v1/repos/leiningen/leiningen/raw/bin/lein?ref=2.13.0',
join(tempPath, 'lein'),
'token cb'
)
})

it('Uses version of leiningen installed in cache', async () => {
tc.find.mockReturnValue(cachePath)
await leiningen.setup('2.9.1')
Expand Down
7 changes: 7 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ inputs:
More info: https://docs.github.com/en/actions/security-guides/automatic-token-authentication
default: ${{ github.token }}
required: false
codeberg-token:
description: >+
Optional Codeberg API token. Leiningen 2.13.0 and newer fetch their `lein`
scripts from the Codeberg API; providing a token raises the rate limit
from per-IP to per-user. Generate one at
https://codeberg.org/user/settings/applications
required: false
invalidate-cache:
description: >+
Set to `true` to fix problems related to wrongly populated tool cache
Expand Down
34 changes: 29 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -943,9 +943,11 @@ function main() {
const invalidateCache = core.getBooleanInput('invalidate-cache');
const githubToken = core.getInput('github-token');
const githubAuthToken = (githubToken === null || githubToken === void 0 ? void 0 : githubToken.length) > 0 ? `Bearer ${githubToken}` : undefined;
const codebergToken = core.getInput('codeberg-token');
const codebergAuthToken = (codebergToken === null || codebergToken === void 0 ? void 0 : codebergToken.length) > 0 ? `token ${codebergToken}` : undefined;
try {
if (LEIN_VERSION) {
tools.push(setupTool(lein.identifier, LEIN_VERSION, invalidateCache, lein.setup.bind(null, LEIN_VERSION, githubAuthToken)));
tools.push(setupTool(lein.identifier, LEIN_VERSION, invalidateCache, lein.setup.bind(null, LEIN_VERSION, githubAuthToken, codebergAuthToken)));
}
if (BOOT_VERSION) {
tools.push(setupTool(boot.identifier, BOOT_VERSION, invalidateCache, boot.setup.bind(null, BOOT_VERSION, githubAuthToken)));
Expand Down Expand Up @@ -1153,7 +1155,13 @@ function downloadStandaloneJar(version, githubAuth) {
}
});
}
function setup(version, githubAuth) {
const CODEBERG_SCRIPT_VERSION = '2.13.0';
function binScriptUrl(version, file, useCodeberg) {
return useCodeberg
? `https://codeberg.org/api/v1/repos/leiningen/leiningen/raw/bin/${file}?ref=${version}`
: `https://raw.githubusercontent.com/technomancy/leiningen/${version}/bin/${file}`;
}
function setup(version, githubAuth, codebergAuth) {
return __awaiter(this, void 0, void 0, function* () {
let toolPath = tc.find(exports.identifier, utils.getCacheVersionString(version), os.arch());
if (toolPath && version !== 'latest') {
Expand All @@ -1162,14 +1170,16 @@ function setup(version, githubAuth) {
else {
// Resolve 'latest' to actual version number
const resolvedVersion = version === 'latest' ? yield getLatestVersion(githubAuth) : version;
const useCodeberg = utils.versionGte(resolvedVersion, CODEBERG_SCRIPT_VERSION);
const binAuth = useCodeberg ? codebergAuth : githubAuth;
const binScripts = [];
if (utils.isWindows()) {
for (const ext of ['ps1', 'bat']) {
binScripts.push(yield tc.downloadTool(`https://raw.githubusercontent.com/technomancy/leiningen/${version === 'latest' ? 'stable' : version}/bin/lein.${ext}`, path.join(utils.getTempDir(), `lein.${ext}`), githubAuth));
binScripts.push(yield tc.downloadTool(binScriptUrl(resolvedVersion, `lein.${ext}`, useCodeberg), path.join(utils.getTempDir(), `lein.${ext}`), binAuth));
}
}
else {
binScripts.push(yield tc.downloadTool(`https://raw.githubusercontent.com/technomancy/leiningen/${version === 'latest' ? 'stable' : version}/bin/lein`, path.join(utils.getTempDir(), 'lein'), githubAuth));
binScripts.push(yield tc.downloadTool(binScriptUrl(resolvedVersion, 'lein', useCodeberg), path.join(utils.getTempDir(), 'lein'), binAuth));
}
const jarPath = yield downloadStandaloneJar(resolvedVersion, githubAuth);
const tempDir = path.join(utils.getTempDir(), `temp_${Math.floor(Math.random() * 2000000000)}`);
Expand Down Expand Up @@ -1324,6 +1334,7 @@ var __importStar = (this && this.__importStar) || (function () {
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getCacheVersionString = getCacheVersionString;
exports.getTempDir = getTempDir;
exports.versionGte = versionGte;
exports.isWindows = isWindows;
exports.isMacOS = isMacOS;
const path = __importStar(__nccwpck_require__(16928));
Expand Down Expand Up @@ -1356,6 +1367,19 @@ function getTempDir() {
}
return tempDirectory;
}
function versionGte(a, b) {
var _a, _b;
const parse = (v) => v.split('.').map(segment => parseInt(segment, 10) || 0);
const pa = parse(a);
const pb = parse(b);
for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
const x = (_a = pa[i]) !== null && _a !== void 0 ? _a : 0;
const y = (_b = pb[i]) !== null && _b !== void 0 ? _b : 0;
if (x !== y)
return x > y;
}
return true;
}
function isWindows() {
return core_1.platform.isWindows;
}
Expand All @@ -1373,7 +1397,7 @@ function isMacOS() {

Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.VERSION = void 0;
exports.VERSION = '13-6';
exports.VERSION = '13-8';


/***/ }),
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion src/entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,23 @@ export async function main(): Promise<void> {
const githubAuthToken =
githubToken?.length > 0 ? `Bearer ${githubToken}` : undefined

const codebergToken = core.getInput('codeberg-token')
const codebergAuthToken =
codebergToken?.length > 0 ? `token ${codebergToken}` : undefined

try {
if (LEIN_VERSION) {
tools.push(
setupTool(
lein.identifier,
LEIN_VERSION,
invalidateCache,
lein.setup.bind(null, LEIN_VERSION, githubAuthToken)
lein.setup.bind(
null,
LEIN_VERSION,
githubAuthToken,
codebergAuthToken
)
)
)
}
Expand Down
33 changes: 24 additions & 9 deletions src/leiningen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,22 @@ async function downloadStandaloneJar(
}
}

const CODEBERG_SCRIPT_VERSION = '2.13.0'

function binScriptUrl(
version: string,
file: string,
useCodeberg: boolean
): string {
return useCodeberg
? `https://codeberg.org/api/v1/repos/leiningen/leiningen/raw/bin/${file}?ref=${version}`
: `https://raw.githubusercontent.com/technomancy/leiningen/${version}/bin/${file}`
}

export async function setup(
version: string,
githubAuth?: string
githubAuth?: string,
codebergAuth?: string
): Promise<void> {
let toolPath = tc.find(
identifier,
Expand All @@ -68,27 +81,29 @@ export async function setup(
const resolvedVersion =
version === 'latest' ? await getLatestVersion(githubAuth) : version

const useCodeberg = utils.versionGte(
resolvedVersion,
CODEBERG_SCRIPT_VERSION
)
const binAuth = useCodeberg ? codebergAuth : githubAuth

const binScripts = []
if (utils.isWindows()) {
for (const ext of ['ps1', 'bat']) {
binScripts.push(
await tc.downloadTool(
`https://raw.githubusercontent.com/technomancy/leiningen/${
version === 'latest' ? 'stable' : version
}/bin/lein.${ext}`,
binScriptUrl(resolvedVersion, `lein.${ext}`, useCodeberg),
path.join(utils.getTempDir(), `lein.${ext}`),
githubAuth
binAuth
)
)
}
} else {
binScripts.push(
await tc.downloadTool(
`https://raw.githubusercontent.com/technomancy/leiningen/${
version === 'latest' ? 'stable' : version
}/bin/lein`,
binScriptUrl(resolvedVersion, 'lein', useCodeberg),
path.join(utils.getTempDir(), 'lein'),
githubAuth
binAuth
)
)
}
Expand Down
13 changes: 13 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ export function getTempDir(): string {
return tempDirectory
}

export function versionGte(a: string, b: string): boolean {
const parse = (v: string): number[] =>
v.split('.').map(segment => parseInt(segment, 10) || 0)
const pa = parse(a)
const pb = parse(b)
for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
const x = pa[i] ?? 0
const y = pb[i] ?? 0
if (x !== y) return x > y
}
return true
}

export function isWindows(): boolean {
return platform.isWindows
}
Expand Down
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const VERSION = '13-6'
export const VERSION = '13-8'
Loading