Skip to content

Commit

Permalink
Merge pull request #105 from DeLaGuardo/rate-limit-cli-fix
Browse files Browse the repository at this point in the history
Fix wrong use of auth token
  • Loading branch information
DeLaGuardo committed Feb 11, 2024
2 parents 9d39ec9 + f688ff1 commit bc7570e
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 56 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
java-version: '8'

- name: Install clojure tools
uses: DeLaGuardo/setup-clojure@12.4
uses: DeLaGuardo/setup-clojure@12.5
with:
# Install just one or all simultaneously
# The value must indicate a particular version of the tool, or use 'latest'
Expand Down
6 changes: 4 additions & 2 deletions __tests__/entrypoint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,20 @@ describe('setup-clojure', () => {

it('sets up Clojure CLI tools from deprecated `tools-deps` option', async () => {
inputs['tools-deps'] = '1.2.3'
inputs['github-token'] = 'auth token'

await main()

expect(cli.setup).toHaveBeenCalledWith('1.2.3')
expect(cli.setup).toHaveBeenCalledWith('1.2.3', 'Bearer auth token')
})

it('sets up Clojure CLI tools', async () => {
inputs['cli'] = '1.2.3'
inputs['github-token'] = 'auth token'

await main()

expect(cli.setup).toHaveBeenCalledWith('1.2.3')
expect(cli.setup).toHaveBeenCalledWith('1.2.3', 'Bearer auth token')
})

it('sets up Babashka', async () => {
Expand Down
12 changes: 6 additions & 6 deletions __tests__/tdeps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,19 @@ describe('tdeps tests', () => {
it('Throws if invalid version', async () => {
const msg = 'Unexpected HTTP response: 403'
tc.downloadTool.mockRejectedValueOnce(new Error(msg))
await expect(tdeps.setup('1000')).rejects.toThrow(msg)
await expect(tdeps.setup('1000', 'auth token')).rejects.toThrow(msg)
})

it('Install clojure tools deps with normal version', async () => {
tc.downloadTool.mockResolvedValueOnce(downloadPath)
tc.cacheDir.mockResolvedValueOnce(cachePath)

await tdeps.setup('1.10.1.469')
await tdeps.setup('1.10.1.469', 'auth token')

expect(tc.downloadTool).toHaveBeenCalledWith(
'https://download.clojure.org/install/linux-install-1.10.1.469.sh',
undefined,
undefined
'auth token'
)
expect(io.mkdirP).toHaveBeenCalledWith('/tmp/usr/local/opt/ClojureTools')
expect(exec.exec).toHaveBeenCalledWith('bash', [
Expand All @@ -106,12 +106,12 @@ describe('tdeps tests', () => {
tc.downloadTool.mockResolvedValueOnce(downloadPath)
tc.cacheDir.mockResolvedValueOnce(cachePath)

await tdeps.setup('latest')
await tdeps.setup('latest', 'auth token')

expect(tc.downloadTool).toHaveBeenCalledWith(
'https://download.clojure.org/install/linux-install-1.2.3.sh',
undefined,
undefined
'auth token'
)
expect(io.mkdirP).toHaveBeenCalledWith('/tmp/usr/local/opt/ClojureTools')
expect(exec.exec).toHaveBeenCalledWith('bash', [
Expand Down Expand Up @@ -177,7 +177,7 @@ describe('tdeps tests', () => {
it('Uses version of clojure tools-deps installed in cache', async () => {
tc.find.mockReturnValue(cachePath)

await tdeps.setup('1.10.1.469')
await tdeps.setup('1.10.1.469', 'auth token')

expect(core.exportVariable).toHaveBeenCalledWith(
'CLOJURE_INSTALL_DIR',
Expand Down
45 changes: 25 additions & 20 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ function toolVersion(version, githubAuth) {
return __awaiter(this, void 0, void 0, function* () {
core.debug('=== Check tool version');
if (version === 'latest') {
const res = yield client.getJson('https://api.github.com/repos/clojure/brew-install/releases/latest', githubAuth ? { Authorization: githubAuth } : undefined);
const res = yield client.getJson('https://api.github.com/repos/clojure/brew-install/releases/latest', { Authorization: githubAuth });
const versionString = (_a = res.result) === null || _a === void 0 ? void 0 : _a.tag_name;
if (versionString) {
return versionString;
Expand All @@ -427,7 +427,9 @@ function getUrls(tag, githubAuth) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
core.debug('=== Get download URLs');
const res = yield client.getJson(`https://api.github.com/repos/clojure/brew-install/releases/tags/${tag}`, githubAuth ? { Authorization: githubAuth } : undefined);
const res = yield client.getJson(`https://api.github.com/repos/clojure/brew-install/releases/tags/${tag}`, {
Authorization: githubAuth
});
const posix_install_url = `https://github.com/clojure/brew-install/releases/download/${tag}/posix-install.sh`;
const assets = (_a = res.result) === null || _a === void 0 ? void 0 : _a.assets;
if (assets && isResourceProvided(posix_install_url, assets)) {
Expand All @@ -445,10 +447,10 @@ function getUrls(tag, githubAuth) {
}
});
}
function setup(requestedVersion, githubToken) {
function setup(requestedVersion, githubAuth) {
return __awaiter(this, void 0, void 0, function* () {
core.debug('=== Run setup');
const version = yield toolVersion(requestedVersion, githubToken);
const version = yield toolVersion(requestedVersion, githubAuth);
const installDir = utils.isWindows()
? 'C:\\Program Files\\WindowsPowerShell\\Modules'
: '/tmp/usr/local/opt';
Expand All @@ -461,7 +463,7 @@ function setup(requestedVersion, githubToken) {
});
}
else {
const { linux, posix, windows } = yield getUrls(version, githubToken);
const { linux, posix, windows } = yield getUrls(version, githubAuth);
if (utils.isWindows()) {
yield exec.exec(`powershell -c "iwr -useb ${windows} | iex"`, [], {
// Install to a modules location common to powershell/pwsh
Expand All @@ -475,15 +477,15 @@ function setup(requestedVersion, githubToken) {
let clojureInstallScript;
if (utils.isMacOS()) {
if (posix) {
clojureInstallScript = yield tc.downloadTool(posix, undefined, githubToken);
clojureInstallScript = yield tc.downloadTool(posix, undefined, githubAuth);
}
else {
clojureInstallScript = yield tc.downloadTool(linux, undefined, githubToken);
yield MacOSDeps(clojureInstallScript, githubToken);
clojureInstallScript = yield tc.downloadTool(linux, undefined, githubAuth);
yield MacOSDeps(clojureInstallScript, githubAuth);
}
}
else {
clojureInstallScript = yield tc.downloadTool(linux, undefined, githubToken);
clojureInstallScript = yield tc.downloadTool(linux, undefined, githubAuth);
}
const clojureToolsDir = yield runLinuxInstall(clojureInstallScript, path.join(installDir, 'ClojureTools'));
core.debug(`clojure tools deps installed to ${clojureToolsDir}`);
Expand All @@ -506,23 +508,26 @@ function runLinuxInstall(installScript, destinationFolder) {
return destinationFolder;
});
}
function MacOSDeps(file, githubToken) {
function MacOSDeps(file, githubAuth) {
return __awaiter(this, void 0, void 0, function* () {
core.debug('=== Install extra deps for MacOS');
const data = yield fs.readFile(file, 'utf-8');
const newValue = data.replace(/install -D/gim, '$(brew --prefix coreutils)/bin/ginstall -D');
yield fs.writeFile(file, newValue, 'utf-8');
const env = githubToken
? { env: { HOMEBREW_GITHUB_API_TOKEN: githubToken.substring(7) } }
: undefined;
yield exec.exec('brew', ['install', 'coreutils'], env);
yield exec.exec('brew', ['install', 'coreutils'], {
env: {
HOMEBREW_GITHUB_API_TOKEN: githubAuth.substring(7),
HOMEBREW_NO_INSTALL_CLEANUP: 'true',
HOME: process.env['HOME'] || ''
}
});
});
}
function getLatestDepsClj(githubAuth) {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
core.debug('=== Fetch latest version of deps clj');
const res = yield client.getJson(`https://api.github.com/repos/borkdude/deps.clj/releases/latest`, githubAuth ? { Authorization: githubAuth } : undefined);
const res = yield client.getJson(`https://api.github.com/repos/borkdude/deps.clj/releases/latest`, { Authorization: githubAuth });
const result = (_b = (_a = res.result) === null || _a === void 0 ? void 0 : _a.tag_name) === null || _b === void 0 ? void 0 : _b.replace(/^v/, '');
if (result) {
return result;
Expand Down Expand Up @@ -898,8 +903,8 @@ function main() {
try {
const { LEIN_VERSION, BOOT_VERSION, TDEPS_VERSION, CLI_VERSION, BB_VERSION, CLJ_KONDO_VERSION, CLJFMT_VERSION, CLJSTYLE_VERSION, ZPRINT_VERSION } = getTools();
const tools = [];
const githubToken = core.getInput('github-token');
const githubAuth = githubToken ? `Bearer ${githubToken}` : undefined;
const githubToken = core.getInput('github-token', { required: true });
const githubAuth = `Bearer ${githubToken}`;
const IS_WINDOWS = utils.isWindows();
if (LEIN_VERSION) {
tools.push(lein.setup(LEIN_VERSION, githubAuth));
Expand All @@ -908,10 +913,10 @@ function main() {
tools.push(boot.setup(BOOT_VERSION, githubAuth));
}
if (CLI_VERSION) {
tools.push(cli.setup(CLI_VERSION));
tools.push(cli.setup(CLI_VERSION, githubAuth));
}
if (TDEPS_VERSION && !CLI_VERSION) {
tools.push(cli.setup(TDEPS_VERSION));
tools.push(cli.setup(TDEPS_VERSION, githubAuth));
}
if (BB_VERSION) {
tools.push(bb.setup(BB_VERSION, githubAuth));
Expand Down Expand Up @@ -1327,7 +1332,7 @@ exports.isMacOS = isMacOS;

Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.VERSION = void 0;
exports.VERSION = '12-4';
exports.VERSION = '12-5';


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

Large diffs are not rendered by default.

44 changes: 23 additions & 21 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ const client = new http.HttpClient('actions/setup-clojure', undefined, {

async function toolVersion(
version: string,
githubAuth?: string
githubAuth: string
): Promise<string> {
core.debug('=== Check tool version')
if (version === 'latest') {
const res = await client.getJson<{tag_name: string}>(
'https://api.github.com/repos/clojure/brew-install/releases/latest',
githubAuth ? {Authorization: githubAuth} : undefined
{Authorization: githubAuth}
)
const versionString = res.result?.tag_name
if (versionString) {
Expand All @@ -50,15 +50,14 @@ function isResourceProvided(

async function getUrls(
tag: string,
githubAuth?: string
githubAuth: string
): Promise<{posix?: string; linux: string; windows: string}> {
core.debug('=== Get download URLs')
const res = await client.getJson<{
assets: {browser_download_url: string}[]
}>(
`https://api.github.com/repos/clojure/brew-install/releases/tags/${tag}`,
githubAuth ? {Authorization: githubAuth} : undefined
)
}>(`https://api.github.com/repos/clojure/brew-install/releases/tags/${tag}`, {
Authorization: githubAuth
})
const posix_install_url = `https://github.com/clojure/brew-install/releases/download/${tag}/posix-install.sh`

const assets = res.result?.assets
Expand All @@ -78,10 +77,10 @@ async function getUrls(

export async function setup(
requestedVersion: string,
githubToken?: string
githubAuth: string
): Promise<void> {
core.debug('=== Run setup')
const version = await toolVersion(requestedVersion, githubToken)
const version = await toolVersion(requestedVersion, githubAuth)
const installDir = utils.isWindows()
? 'C:\\Program Files\\WindowsPowerShell\\Modules'
: '/tmp/usr/local/opt'
Expand All @@ -98,7 +97,7 @@ export async function setup(
recursive: true
})
} else {
const {linux, posix, windows} = await getUrls(version, githubToken)
const {linux, posix, windows} = await getUrls(version, githubAuth)

if (utils.isWindows()) {
await exec.exec(`powershell -c "iwr -useb ${windows} | iex"`, [], {
Expand Down Expand Up @@ -126,21 +125,21 @@ export async function setup(
clojureInstallScript = await tc.downloadTool(
posix,
undefined,
githubToken
githubAuth
)
} else {
clojureInstallScript = await tc.downloadTool(
linux,
undefined,
githubToken
githubAuth
)
await MacOSDeps(clojureInstallScript, githubToken)
await MacOSDeps(clojureInstallScript, githubAuth)
}
} else {
clojureInstallScript = await tc.downloadTool(
linux,
undefined,
githubToken
githubAuth
)
}

Expand Down Expand Up @@ -179,25 +178,28 @@ async function runLinuxInstall(
return destinationFolder
}

async function MacOSDeps(file: string, githubToken?: string): Promise<void> {
async function MacOSDeps(file: string, githubAuth: string): Promise<void> {
core.debug('=== Install extra deps for MacOS')
const data = await fs.readFile(file, 'utf-8')
const newValue = data.replace(
/install -D/gim,
'$(brew --prefix coreutils)/bin/ginstall -D'
)
await fs.writeFile(file, newValue, 'utf-8')
const env = githubToken
? {env: {HOMEBREW_GITHUB_API_TOKEN: githubToken.substring(7)}}
: undefined
await exec.exec('brew', ['install', 'coreutils'], env)
await exec.exec('brew', ['install', 'coreutils'], {
env: {
HOMEBREW_GITHUB_API_TOKEN: githubAuth.substring(7),
HOMEBREW_NO_INSTALL_CLEANUP: 'true',
HOME: process.env['HOME'] || ''
}
})
}

export async function getLatestDepsClj(githubAuth?: string): Promise<string> {
export async function getLatestDepsClj(githubAuth: string): Promise<string> {
core.debug('=== Fetch latest version of deps clj')
const res = await client.getJson<{tag_name: string}>(
`https://api.github.com/repos/borkdude/deps.clj/releases/latest`,
githubAuth ? {Authorization: githubAuth} : undefined
{Authorization: githubAuth}
)

const result = res.result?.tag_name?.replace(/^v/, '')
Expand Down
8 changes: 4 additions & 4 deletions src/entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export async function main(): Promise<void> {

const tools = []

const githubToken = core.getInput('github-token')
const githubAuth = githubToken ? `Bearer ${githubToken}` : undefined
const githubToken = core.getInput('github-token', {required: true})
const githubAuth = `Bearer ${githubToken}`
const IS_WINDOWS = utils.isWindows()

if (LEIN_VERSION) {
Expand All @@ -39,11 +39,11 @@ export async function main(): Promise<void> {
}

if (CLI_VERSION) {
tools.push(cli.setup(CLI_VERSION))
tools.push(cli.setup(CLI_VERSION, githubAuth))
}

if (TDEPS_VERSION && !CLI_VERSION) {
tools.push(cli.setup(TDEPS_VERSION))
tools.push(cli.setup(TDEPS_VERSION, githubAuth))
}

if (BB_VERSION) {
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 = '12-4'
export const VERSION = '12-5'

0 comments on commit bc7570e

Please sign in to comment.