Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: infer github release values #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,35 @@ jobs:
- uses: engineerd/[email protected]
with:
name: "kind"
fromGitHubReleases: "true"
repo: "kubernetes-sigs/kind"
urlTemplate: "https://github.com/kubernetes-sigs/kind/releases/download/{{version}}/kind-linux-amd64"
version: "latest"
token: ${{ secrets.GITHUB_TOKEN }}

- name: Testing
run: |
kind --help
```

Note that when the `urlTemplate` is a GitHub Releases Download URL, `fromGitHubReleases` will be infered to be `true`, the `repo` will be pulled from the URL, and `version` will be infered to be latest. You can override any of these by explicitly providing a value.

For example, if you wanted to pin the version to to a specific range:

```yaml
jobs:
kind:
runs-on: ubuntu-latest
steps:
- uses: engineerd/[email protected]
with:
name: "kind"
urlTemplate: "https://github.com/kubernetes-sigs/kind/releases/download/{{version}}/kind-linux-amd64"
token: ${{ secrets.GITHUB_TOKEN }}
version: "^v0.11.1"

- name: Testing
run: |
kind --help
```

## Other examples

- download an executable from a given URL and move it to a folder in path with
Expand Down
18 changes: 18 additions & 0 deletions src/configurator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const Version: string = "version";
const IncludePrereleases: string = "includePrereleases";
const URLTemplate: string = "urlTemplate";

const GitHubReleasesRegex: RegExp = /^https:\/\/github\.com\/(?<repo>.*\/.*)\/releases\/download\//i;

export function getConfig(): Configurator {
return new Configurator(
core.getInput(NameInput),
Expand Down Expand Up @@ -67,6 +69,22 @@ export class Configurator {
this.version = version;
this.includePrereleases = includePrereleases == "true";
this.urlTemplate = urlTemplate;

if (!!urlTemplate && GitHubReleasesRegex.test(this.urlTemplate)) {
const [,urlRepo] = GitHubReleasesRegex.exec(this.urlTemplate)!;

if (!fromGitHubRelease) {
this.fromGitHubReleases = true;
}

if (!repo) {
this.repo = urlRepo;
}

if (!version) {
this.version = "latest";
}
}
}

async configure() {
Expand Down
35 changes: 35 additions & 0 deletions test/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,41 @@ describe("test GitHub release download", async () => {
await c.configure();
assert.equal(fs.existsSync(path.join(cfg.binPath(), c.name)), true);
});

it("correctly overrides infered configuration based on GitHub release input", async () => {
const input = {
INPUT_NAME: "kind",
// before running this test, run export GITHUB_TOKEN=<github-token>
INPUT_TOKEN: process.env["GITHUB_TOKEN"],
INPUT_URLTEMPLATE:
"https://github.com/kubernetes-sigs/kind/releases/download/{{version}}/kind-linux-amd64",
};

for (const key in input) process.env[key] = input[key];

let c = cfg.getConfig();
assert.equal(c.fromGitHubReleases, true);
assert.equal(c.repo, "kubernetes-sigs/kind");
assert.equal(c.version, "latest");
});

it("correctly infers configuration based on GitHub release input", async () => {
const input = {
INPUT_NAME: "kind",
// before running this test, run export GITHUB_TOKEN=<github-token>
INPUT_TOKEN: process.env["GITHUB_TOKEN"],
INPUT_VERSION: "^v0.11.1",
INPUT_URLTEMPLATE:
"https://github.com/kubernetes-sigs/kind/releases/download/{{version}}/kind-linux-amd64",
};

for (const key in input) process.env[key] = input[key];

let c = cfg.getConfig();
assert.equal(c.fromGitHubReleases, true);
assert.equal(c.repo, "kubernetes-sigs/kind");
assert.equal(c.version, "^v0.11.1");
});
});

// given that `getTag` returns the latest version that satisfies certain constraints,
Expand Down