Add auth setup for private registry with scope (#16)

feat: add auth setup to private registry with scope
This commit is contained in:
Vitor Gomes 2023-11-01 06:17:17 -03:00 committed by GitHub
parent 6be87460e3
commit 830e319e28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 23030 additions and 3503 deletions

View file

@ -27,11 +27,15 @@ setup({
version:
readVersionFromPackageJson() || action.getInput("bun-version") || undefined,
customUrl: action.getInput("bun-download-url") || undefined,
registryUrl: action.getInput("registry-url") || undefined,
scope: action.getInput("scope") || undefined,
})
.then(({ version, revision, cacheHit }) => {
.then(({ version, revision, cacheHit, registryUrl, scope }) => {
action.setOutput("bun-version", version);
action.setOutput("bun-revision", revision);
action.setOutput("cache-hit", cacheHit);
action.setOutput("registry-url", registryUrl);
action.setOutput("scope", scope);
})
.catch((error) => {
action.setFailed(error);

57
src/auth.ts Normal file
View file

@ -0,0 +1,57 @@
import { EOL } from "node:os";
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { resolve } from "node:path";
import * as core from "@actions/core";
export function configureAuthentication(registryUrl: string, scope: string) {
const bunfigPath = resolve(process.cwd(), "bunfig.toml");
if (!registryUrl.endsWith("/")) {
registryUrl += "/";
}
writeRegistryToConfigFile({ registryUrl, fileLocation: bunfigPath, scope });
}
type WriteRegistryToConfigFile = {
registryUrl: string;
fileLocation: string;
scope: string;
};
function writeRegistryToConfigFile({
registryUrl,
fileLocation,
scope,
}: WriteRegistryToConfigFile) {
if (scope && scope[0] !== "@") {
scope = "@" + scope;
}
if (scope) {
scope = scope.toLocaleLowerCase();
}
core.info(`Setting auth in ${fileLocation}`);
let newContents = "";
if (existsSync(fileLocation)) {
const curContents = readFileSync(fileLocation, "utf8");
curContents.split(EOL).forEach((line: string) => {
// Add current contents unless they are setting the registry
if (!line.toLowerCase().startsWith(scope)) {
newContents += line + EOL;
}
});
newContents += EOL;
}
const bunRegistryString = `'${scope}' = { token = "$BUN_AUTH_TOKEN", url = "${registryUrl}"}`;
newContents += `[install.scopes]${EOL}${EOL}${bunRegistryString}${EOL}`;
writeFileSync("./bunfig.toml", newContents);
}

View file

@ -2,19 +2,23 @@ import { homedir } from "node:os";
import { join } from "node:path";
import { readdir, symlink } from "node:fs/promises";
import * as action from "@actions/core";
import { downloadTool, extractZip } from "@actions/tool-cache";
import * as cache from "@actions/cache";
import { restoreCache, saveCache } from "@actions/cache";
import { downloadTool, extractZip } from "@actions/tool-cache";
import { cp, mkdirP, rmRF } from "@actions/io";
import { getExecOutput } from "@actions/exec";
import { configureAuthentication } from "./auth";
export default async (options?: {
version?: string;
customUrl?: string;
scope?: string;
registryUrl?: string;
}): Promise<{
version: string;
revision: string;
cacheHit: boolean;
scope?: string;
registryUrl?: string;
}> => {
const { url, cacheKey } = getDownloadUrl(options);
const cacheEnabled = cacheKey && cache.isFeatureAvailable();
@ -24,7 +28,7 @@ export default async (options?: {
let revision: string | undefined;
let cacheHit = false;
if (cacheEnabled) {
const cacheRestored = await restoreCache([path], cacheKey);
const cacheRestored = await cache.restoreCache([path], cacheKey);
if (cacheRestored) {
revision = await verifyBun(path);
if (revision) {
@ -61,16 +65,25 @@ export default async (options?: {
}
if (cacheEnabled) {
try {
await saveCache([path], cacheKey);
await cache.saveCache([path], cacheKey);
} catch (error) {
action.warning("Failed to save Bun to cache.");
}
}
const [version] = revision.split("+");
const { registryUrl, scope } = options;
if (!!registryUrl && !!scope) {
configureAuthentication(registryUrl, scope);
}
return {
version,
revision,
cacheHit,
registryUrl,
scope,
};
};