Skip to content

Commit

Permalink
refactor: based on env
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos A Becker <[email protected]>
  • Loading branch information
caarlos0 committed May 27, 2021
1 parent 67b75fc commit bbda928
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 72 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,11 @@ jobs:
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
# either 'goreleaser' (default) or 'goreleaser-pro'
distribution: goreleaser
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Your GoReleaser Pro key, if you are using the 'goreleaser-pro' distribution
# Your GoReleaser Pro key, if you want to use the pro version
# GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }}
```

Expand Down Expand Up @@ -171,7 +169,6 @@ Following inputs can be used as `step.with` keys

| Name | Type | Default | Description |
|------------------|---------|--------------|------------------------------------------------------------------|
| `distribution` | String | `goreleaser` | GoReleaser distribution, either `goreleaser` or `goreleaser-pro` |
| `version`**¹** | String | `latest` | GoReleaser version |
| `args` | String | | Arguments to pass to GoReleaser |
| `workdir` | String | `.` | Working directory (below repository root) |
Expand All @@ -186,7 +183,7 @@ Following environment variables can be used as `step.env` keys
| Name | Description |
|------------------|---------------------------------------|
| `GITHUB_TOKEN` | [GITHUB_TOKEN](https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token) as provided by `secrets` |
| `GORELEASER_KEY` | Your [GoReleaser Pro](https://goreleaser.com/pro) License Key, in case you are using the `goreleaser-pro` distribution |
| `GORELEASER_KEY` | Your [GoReleaser Pro](https://goreleaser.com/pro) License Key, if you want to use the Pro version |

## Limitation

Expand Down
24 changes: 12 additions & 12 deletions __tests__/github.test.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
import * as github from '../src/github';

describe('github', () => {
it('suffixes pro distribution', async () => {
expect(github.suffix('goreleaser-pro')).toEqual('-pro');
});
it('does not suffix oss distribution', async () => {
expect(github.suffix('goreleaser')).toEqual('');
});
it('returns latest GoReleaser GitHub release', async () => {
const release = await github.getRelease('goreleaser', 'latest');
process.env.GORELEASER_CURRENT_TAG = '';
const release = await github.getRelease('latest');
expect(release).not.toBeNull();
expect(release?.tag_name).not.toEqual('');
});
it('returns v0.117.0 GoReleaser GitHub release', async () => {
const release = await github.getRelease('goreleaser', 'v0.117.0');
process.env.GORELEASER_CURRENT_TAG = '';
const release = await github.getRelease('v0.117.0');
expect(release).not.toBeNull();
expect(release?.tag_name).toEqual('v0.117.0');
});
it('returns v0.132.1 GoReleaser GitHub release', async () => {
const release = await github.getRelease('goreleaser', '~> 0.132');
process.env.GORELEASER_CURRENT_TAG = '';
const release = await github.getRelease('~> 0.132');
expect(release).not.toBeNull();
expect(release?.tag_name).toEqual('v0.132.1');
});
it('returns latest GoReleaser Pro GitHub release', async () => {
const release = await github.getRelease('goreleaser-pro', 'latest');
process.env.GORELEASER_CURRENT_TAG = 'key';
const release = await github.getRelease('latest');
expect(release).not.toBeNull();
expect(release?.tag_name).not.toEqual('');
});
it('returns v0.166.0-pro GoReleaser Pro GitHub release', async () => {
const release = await github.getRelease('goreleaser-pro', 'v0.166.0-pro');
process.env.GORELEASER_CURRENT_TAG = 'key';
const release = await github.getRelease('v0.166.0-pro');
expect(release).not.toBeNull();
expect(release?.tag_name).toEqual('v0.166.0-pro');
});
it('returns v0.166.0-pro GoReleaser Pro GitHub release when using semver', async () => {
const release = await github.getRelease('goreleaser-pro', '~> 0.166');
process.env.GORELEASER_CURRENT_TAG = 'key';
const release = await github.getRelease('~> 0.166');
expect(release).not.toBeNull();
expect(release?.tag_name).toEqual('v0.166.0-pro');
});
Expand Down
12 changes: 8 additions & 4 deletions __tests__/installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@ import * as installer from '../src/installer';

describe('installer', () => {
it('acquires v0.117.0 version of GoReleaser', async () => {
const goreleaser = await installer.getGoReleaser('goreleaser', 'v0.117.0');
process.env.GORELEASER_CURRENT_TAG = '';
const goreleaser = await installer.getGoReleaser('v0.117.0');
expect(fs.existsSync(goreleaser)).toBe(true);
}, 100000);

it('acquires latest version of GoReleaser', async () => {
const goreleaser = await installer.getGoReleaser('goreleaser', 'latest');
process.env.GORELEASER_CURRENT_TAG = '';
const goreleaser = await installer.getGoReleaser('latest');
expect(fs.existsSync(goreleaser)).toBe(true);
}, 100000);

it('acquires v0.166.0-pro version of GoReleaser Pro', async () => {
const goreleaser = await installer.getGoReleaser('goreleaser-pro', 'v0.166.0-pro');
process.env.GORELEASER_CURRENT_TAG = 'key';
const goreleaser = await installer.getGoReleaser('v0.166.0-pro');
expect(fs.existsSync(goreleaser)).toBe(true);
}, 100000);

it('acquires latest version of GoReleaser Pro', async () => {
const goreleaser = await installer.getGoReleaser('goreleaser-pro', 'latest');
process.env.GORELEASER_CURRENT_TAG = 'key';
const goreleaser = await installer.getGoReleaser('latest');
expect(fs.existsSync(goreleaser)).toBe(true);
}, 100000);
});
20 changes: 20 additions & 0 deletions __tests__/pro.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as pro from '../src/pro';

describe('pro', () => {
it('suffixes pro distribution', async () => {
process.env.GORELEASER_CURRENT_TAG = 'fake-key';
expect(pro.suffix()).toEqual('-pro');
});
it('does not suffix oss distribution', async () => {
process.env.GORELEASER_CURRENT_TAG = '';
expect(pro.suffix()).toEqual('');
});
it('gets pro distribution', async () => {
process.env.GORELEASER_CURRENT_TAG = 'fake-key';
expect(pro.distribution()).toEqual('goreleaser-pro');
});
it('gets oss distribution', async () => {
process.env.GORELEASER_CURRENT_TAG = '';
expect(pro.distribution()).toEqual('goreleaser');
});
});
66 changes: 41 additions & 25 deletions dist/index.js

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

29 changes: 11 additions & 18 deletions src/github.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
import * as httpm from '@actions/http-client';
import * as core from '@actions/core';
import * as semver from 'semver';
import * as pro from './pro';

export interface GitHubRelease {
id: number;
tag_name: string;
}

export const getRelease = async (distribution: string, version: string): Promise<GitHubRelease | null> => {
const resolvedVersion: string = (await resolveVersion(distribution, version)) || version;
const url: string = `https://github.com/goreleaser/${distribution}/releases/${resolvedVersion}`;
export const getRelease = async (version: string): Promise<GitHubRelease | null> => {
const resolvedVersion: string = (await resolveVersion(version)) || version;
const url: string = `https://github.com/goreleaser/${pro.distribution()}/releases/${resolvedVersion}`;
const http: httpm.HttpClient = new httpm.HttpClient('goreleaser-action');
return (await http.getJson<GitHubRelease>(url)).result;
};

const resolveVersion = async (distribution: string, version: string): Promise<string | null> => {
const allTags: Array<string> | null = await getAllTags(distribution);
const resolveVersion = async (version: string): Promise<string | null> => {
const allTags: Array<string> | null = await getAllTags();
if (!allTags) {
throw new Error(`Cannot find GoReleaser tags`);
}
core.debug(`Found ${allTags.length} tags in total`);

if (version === 'latest' || !isPro(distribution)) {
if (version === 'latest' || !pro.isPro()) {
return semver.maxSatisfying(allTags, version);
}

const cleanTags: Array<string> = allTags.map(tag => cleanTag(tag));
const cleanVersion: string = cleanTag(version);
return semver.maxSatisfying(cleanTags, cleanVersion) + suffix(distribution);
return semver.maxSatisfying(cleanTags, cleanVersion) + pro.suffix();
};

interface GitHubTag {
tag_name: string;
}

const getAllTags = async (distribution: string): Promise<Array<string>> => {
const getAllTags = async (): Promise<Array<string>> => {
const http: httpm.HttpClient = new httpm.HttpClient('goreleaser-action');
const pro: string = suffix(distribution);
const url: string = `https://goreleaser.com/static/releases${pro}.json`;
const suffix: string = pro.suffix();
const url: string = `https://goreleaser.com/static/releases${suffix}.json`;
const getTags = http.getJson<Array<GitHubTag>>(url);

return getTags.then(response => {
Expand All @@ -49,14 +50,6 @@ const getAllTags = async (distribution: string): Promise<Array<string>> => {
});
};

export const suffix = (distribution: string): string => {
return isPro(distribution) ? '-pro' : '';
};

const isPro = (distribution: string): boolean => {
return distribution === 'goreleaser-pro';
};

const cleanTag = (tag: string): string => {
return tag.replace(/-pro$/, '');
};
14 changes: 8 additions & 6 deletions src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ import * as os from 'os';
import * as path from 'path';
import * as util from 'util';
import * as github from './github';
import * as pro from './pro';
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';

const osPlat: string = os.platform();
const osArch: string = os.arch();

export async function getGoReleaser(distribution: string, version: string): Promise<string> {
const release: github.GitHubRelease | null = await github.getRelease(distribution, version);
export async function getGoReleaser(version: string): Promise<string> {
const release: github.GitHubRelease | null = await github.getRelease(version);
if (!release) {
throw new Error(`Cannot find GoReleaser ${version} release`);
}

core.info(`✅ GoReleaser version found: ${release.tag_name}`);
const filename = getFilename(distribution);
const filename = getFilename();
const distribution = pro.distribution();
const downloadUrl = util.format(
'https://github.com/goreleaser/%s/releases/download/%s/%s',
distribution,
Expand Down Expand Up @@ -45,10 +47,10 @@ export async function getGoReleaser(distribution: string, version: string): Prom
return exePath;
}

const getFilename = (distribution: string): string => {
const getFilename = (): string => {
const platform: string = osPlat == 'win32' ? 'Windows' : osPlat == 'darwin' ? 'Darwin' : 'Linux';
const arch: string = osArch == 'x64' ? 'x86_64' : 'i386';
const ext: string = osPlat == 'win32' ? 'zip' : 'tar.gz';
const pro: string = github.suffix(distribution);
return util.format('goreleaser%s_%s_%s.%s', pro, platform, arch, ext);
const suffix: string = pro.suffix();
return util.format('goreleaser%s_%s_%s.%s', suffix, platform, arch, ext);
};
3 changes: 1 addition & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import {dirname} from 'path';

async function run(): Promise<void> {
try {
const distribution = core.getInput('distribution') || 'goreleaser';
const version = core.getInput('version') || 'latest';
const args = core.getInput('args');
const workdir = core.getInput('workdir') || '.';
const isInstallOnly = /^true$/i.test(core.getInput('install-only'));
const goreleaser = await installer.getGoReleaser(distribution, version);
const goreleaser = await installer.getGoReleaser(version);
core.info(`✅ GoReleaser installed successfully`);

if (isInstallOnly) {
Expand Down
Loading

0 comments on commit bbda928

Please sign in to comment.