diff --git a/README.md b/README.md index 230e2a3..754890e 100644 --- a/README.md +++ b/README.md @@ -141,10 +141,7 @@ jobs: - uses: engineerd/configurator@v0.0.8 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 @@ -152,6 +149,27 @@ jobs: 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/configurator@v0.0.8 + 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 diff --git a/src/configurator.ts b/src/configurator.ts index 6ad3e62..a8489da 100644 --- a/src/configurator.ts +++ b/src/configurator.ts @@ -19,6 +19,8 @@ const Version: string = "version"; const IncludePrereleases: string = "includePrereleases"; const URLTemplate: string = "urlTemplate"; +const GitHubReleasesRegex: RegExp = /^https:\/\/github\.com\/(?.*\/.*)\/releases\/download\//i; + export function getConfig(): Configurator { return new Configurator( core.getInput(NameInput), @@ -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() { diff --git a/test/action.ts b/test/action.ts index 7b45290..73b12df 100644 --- a/test/action.ts +++ b/test/action.ts @@ -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= + 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= + 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,