Skip to content

Commit

Permalink
Add JF_RELEASES_REPO to GitHub actions (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
omerzi authored Jun 15, 2023
1 parent da2dd24 commit 723a97d
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 16 deletions.
35 changes: 32 additions & 3 deletions action/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const os = __importStar(require("os"));
const path = __importStar(require("path"));
class Utils {
static addToPath() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
let fileName = Utils.getExecutableName();
let version = core.getInput(Utils.VERSION_ARG);
Expand All @@ -57,13 +58,31 @@ class Utils {
}
}
// Download Frogbot
let url = Utils.getCliUrl(major, version, fileName);
const releasesRepo = (_a = process.env.JF_RELEASES_REPO) !== null && _a !== void 0 ? _a : '';
let url = Utils.getCliUrl(major, version, fileName, releasesRepo);
core.debug('Downloading Frogbot from ' + url);
let downloadDir = yield toolCache.downloadTool(url);
let auth = this.generateAuthString(releasesRepo);
let downloadDir = yield toolCache.downloadTool(url, '', auth);
// Cache 'frogbot' executable
yield this.cacheAndAddPath(downloadDir, version, fileName);
});
}
static generateAuthString(releasesRepo) {
var _a, _b, _c;
if (!releasesRepo) {
return '';
}
let accessToken = (_a = process.env.JF_ACCESS_TOKEN) !== null && _a !== void 0 ? _a : '';
let username = (_b = process.env.JF_USER) !== null && _b !== void 0 ? _b : '';
let password = (_c = process.env.JF_PASSWORD) !== null && _c !== void 0 ? _c : '';
if (accessToken) {
return 'Bearer ' + Buffer.from(accessToken).toString();
}
else if (username && password) {
return 'Basic ' + Buffer.from(username + ':' + password).toString('base64');
}
return '';
}
static setFrogbotEnv() {
core.exportVariable('JF_GIT_PROVIDER', 'github');
core.exportVariable('JF_GIT_OWNER', github.context.repo.owner);
Expand Down Expand Up @@ -126,8 +145,18 @@ class Utils {
core.addPath(cliDir);
});
}
static getCliUrl(major, version, fileName) {
static getCliUrl(major, version, fileName, releasesRepo) {
var _a;
let architecture = 'frogbot-' + Utils.getArchitecture();
if (releasesRepo) {
let platformUrl = (_a = process.env.JF_URL) !== null && _a !== void 0 ? _a : '';
if (!platformUrl) {
throw new Error('Failed while downloading Frogbot from Artifactory, JF_URL must be set');
}
// Remove trailing slash if exists
platformUrl = platformUrl.replace(/\/$/, '');
return `${platformUrl}/artifactory/${releasesRepo}/artifactory/frogbot/v${major}/${version}/${architecture}/${fileName}`;
}
return `https://releases.jfrog.io/artifactory/frogbot/v${major}/${version}/${architecture}/${fileName}`;
}
static getArchitecture() {
Expand Down
14 changes: 7 additions & 7 deletions action/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion action/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"devDependencies": {
"@types/jest": "^27.5.0",
"@types/node": "^18.0.3",
"@types/node": "^18.16.18",
"@typescript-eslint/eslint-plugin": "^5.15.0",
"@typescript-eslint/parser": "^5.15.0",
"eslint": "^8.11.0",
Expand Down
33 changes: 29 additions & 4 deletions action/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,30 @@ export class Utils {
}

// Download Frogbot
let url: string = Utils.getCliUrl(major, version, fileName);
const releasesRepo: string = process.env.JF_RELEASES_REPO ?? '';
let url: string = Utils.getCliUrl(major, version, fileName, releasesRepo);
core.debug('Downloading Frogbot from ' + url);
let downloadDir: string = await toolCache.downloadTool(url);

let auth: string = this.generateAuthString(releasesRepo);
let downloadDir: string = await toolCache.downloadTool(url, '', auth);
// Cache 'frogbot' executable
await this.cacheAndAddPath(downloadDir, version, fileName);
}

public static generateAuthString(releasesRepo: string): string {
if (!releasesRepo) {
return ''
}
let accessToken: string = process.env.JF_ACCESS_TOKEN ?? '';
let username: string = process.env.JF_USER ?? '';
let password: string = process.env.JF_PASSWORD ?? '';
if (accessToken) {
return 'Bearer ' + Buffer.from(accessToken).toString();
} else if (username && password) {
return 'Basic ' + Buffer.from(username + ':' + password).toString('base64');
}
return '';
}

public static setFrogbotEnv() {
core.exportVariable('JF_GIT_PROVIDER', 'github');
core.exportVariable('JF_GIT_OWNER', github.context.repo.owner);
Expand Down Expand Up @@ -96,8 +112,17 @@ export class Utils {
core.addPath(cliDir);
}

public static getCliUrl(major: string, version: string, fileName: string): string {
public static getCliUrl(major: string, version: string, fileName: string, releasesRepo: string): string {
let architecture: string = 'frogbot-' + Utils.getArchitecture();
if (releasesRepo) {
let platformUrl: string = process.env.JF_URL ?? '';
if (!platformUrl) {
throw new Error('Failed while downloading Frogbot from Artifactory, JF_URL must be set');
}
// Remove trailing slash if exists
platformUrl = platformUrl.replace(/\/$/, '');
return `${platformUrl}/artifactory/${releasesRepo}/artifactory/frogbot/v${major}/${version}/${architecture}/${fileName}`;
}
return `https://releases.jfrog.io/artifactory/frogbot/v${major}/${version}/${architecture}/${fileName}`;
}

Expand Down
66 changes: 65 additions & 1 deletion action/test/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import { Utils } from '../src/utils';
jest.mock('os');

describe('Frogbot Action Tests', () => {
afterEach(() => {
delete process.env.JF_ACCESS_TOKEN;
delete process.env.JF_USER;
delete process.env.PASSWORD;
delete process.env.JF_GIT_PROVIDER;
delete process.env.JF_GIT_OWNER;
delete process.env.GITHUB_REPOSITORY_OWNER;
delete process.env.GITHUB_REPOSITORY;
})

describe('Frogbot URL Tests', () => {
const myOs: jest.Mocked<typeof os> = os as any;
let cases: string[][] = [
Expand All @@ -25,11 +35,65 @@ describe('Frogbot Action Tests', () => {
test.each(cases)('CLI Url for %s-%s', (platform, arch, fileName, expectedUrl) => {
myOs.platform.mockImplementation(() => <NodeJS.Platform>platform);
myOs.arch.mockImplementation(() => arch);
let cliUrl: string = Utils.getCliUrl('1', '1.2.3', fileName);
let cliUrl: string = Utils.getCliUrl('1', '1.2.3', fileName, '');
expect(cliUrl).toBe(expectedUrl);
});
});

describe('Frogbot URL Tests With Remote Artifactory', () => {
const myOs: jest.Mocked<typeof os> = os as any;
const releasesRepo: string = 'frogbot-remote';
process.env['JF_URL'] = 'https://myfrogbot.com/';
process.env['JF_ACCESS_TOKEN'] = 'access_token1';
let cases: string[][] = [
[
'win32' as NodeJS.Platform,
'amd64',
'jfrog.exe',
'https://myfrogbot.com/artifactory/frogbot-remote/artifactory/frogbot/v2/2.8.7/frogbot-windows-amd64/jfrog.exe',
],
['darwin' as NodeJS.Platform, 'amd64', 'jfrog', 'https://myfrogbot.com/artifactory/frogbot-remote/artifactory/frogbot/v2/2.8.7/frogbot-mac-386/jfrog'],
['linux' as NodeJS.Platform, 'amd64', 'jfrog', 'https://myfrogbot.com/artifactory/frogbot-remote/artifactory/frogbot/v2/2.8.7/frogbot-linux-amd64/jfrog'],
['linux' as NodeJS.Platform, 'arm64', 'jfrog', 'https://myfrogbot.com/artifactory/frogbot-remote/artifactory/frogbot/v2/2.8.7/frogbot-linux-arm64/jfrog'],
['linux' as NodeJS.Platform, '386', 'jfrog', 'https://myfrogbot.com/artifactory/frogbot-remote/artifactory/frogbot/v2/2.8.7/frogbot-linux-386/jfrog'],
['linux' as NodeJS.Platform, 'arm', 'jfrog', 'https://myfrogbot.com/artifactory/frogbot-remote/artifactory/frogbot/v2/2.8.7/frogbot-linux-arm/jfrog'],
['linux' as NodeJS.Platform, 'ppc64', 'jfrog', 'https://myfrogbot.com/artifactory/frogbot-remote/artifactory/frogbot/v2/2.8.7/frogbot-linux-ppc64/jfrog'],
['linux' as NodeJS.Platform, 'ppc64le', 'jfrog', 'https://myfrogbot.com/artifactory/frogbot-remote/artifactory/frogbot/v2/2.8.7/frogbot-linux-ppc64le/jfrog'],
];

test.each(cases)('Remote CLI Url for %s-%s', (platform, arch, fileName, expectedUrl) => {
myOs.platform.mockImplementation(() => <NodeJS.Platform>platform);
myOs.arch.mockImplementation(() => arch);
let cliUrl: string = Utils.getCliUrl('2', '2.8.7', fileName, releasesRepo);
expect(cliUrl).toBe(expectedUrl);
});
});

describe('Generate auth string', () => {
it('Should return an empty string if releasesRepo is falsy', () => {
const result = Utils.generateAuthString('');
expect(result).toBe('');
});

it('Should generate a Bearer token if accessToken is provided', () => {
process.env.JF_ACCESS_TOKEN = 'yourAccessToken';
const result = Utils.generateAuthString('yourReleasesRepo');
expect(result).toBe('Bearer yourAccessToken');
});

it('Should generate a Basic token if username and password are provided', () => {
process.env.JF_USER = 'yourUsername';
process.env.JF_PASSWORD = 'yourPassword';
const result = Utils.generateAuthString('yourReleasesRepo');
expect(result).toBe('Basic eW91clVzZXJuYW1lOnlvdXJQYXNzd29yZA==');
});

it('Should return an empty string if no credentials are provided', () => {
const result = Utils.generateAuthString('yourReleasesRepo');
expect(result).toBe('');
});
});

it('Repository env tests', () => {
process.env['GITHUB_REPOSITORY_OWNER'] = 'jfrog';
process.env['GITHUB_REPOSITORY'] = 'jfrog/frogbot';
Expand Down

0 comments on commit 723a97d

Please sign in to comment.