From 61b1aa624c0d69df18fc156be16a65b778a0aa13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfredo=20Espa=C3=B1a?= Date: Thu, 29 Sep 2022 17:13:54 -0600 Subject: [PATCH] Fix logic to get branch name (#16) --- README.md | 4 ++-- action.yml | 3 +++ dist/index.js | 11 +++++------ dist/userInput.js | 5 ++++- src/index.ts | 4 ++-- src/userInput.ts | 9 +++++++-- 6 files changed, 23 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 74f57a1..8b6f889 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,6 @@ Add the following snippet to the script section of your github actions file: jobs: build: runs-on: ubuntu-latest - env: - VERSION: ${{ github.event.release.tag_name }} steps: - uses: actions/checkout@v3 - uses: AzBuilder/terrakube-action-github@1.0.0 @@ -20,6 +18,7 @@ jobs: TERRAKUBE_TEMPLATE: "vulnerability-snyk" TERRAKUBE_REPOSITORY: "https://github.com/AzBuilder/terraform-sample-repository.git" TERRAKUBE_ENDPOINT: "https://terrakube.interal/service" + TERRAKUBE_BRANCH: ${{ github.event.pull_request.base.ref }} GITHUB_TOKEN: "xxxx" ``` @@ -31,6 +30,7 @@ jobs: | TERRAKUBE_REPOSITORY (*) | Terrakube git repository | | TERRAKUBE_TEMPLATE (*) | Terrakube template name | | TERRAKUBE_ENDPOINT (*) | Terrakbue api endpoint | +| TERRAKUBE_BRANCH (*) | Github Branch when running a job | | GITHUB_TOKEN (*) | Github Token | _(*) = required variable._ diff --git a/action.yml b/action.yml index 9eb2393..3daf05a 100644 --- a/action.yml +++ b/action.yml @@ -16,6 +16,9 @@ inputs: TERRAKUBE_TEMPLATE: description: 'Terrakube Template' required: true + TERRAKUBE_BRANCH: + description: 'Branch to run the job' + required: true TOKEN: description: 'Github Token' required: true diff --git a/dist/index.js b/dist/index.js index db8ab15..258733b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -53,7 +53,6 @@ const terrakube_1 = require("./terrakube"); const promises_1 = require("fs/promises"); function run() { var e_1, _a; - var _b, _c; return __awaiter(this, void 0, void 0, function* () { try { const githubActionInput = yield (0, userInput_1.getActionInput)(); @@ -63,8 +62,8 @@ function run() { const currentDirectory = yield getCurrentDirectory(); console.debug(`Processing: ${currentDirectory}`); try { - for (var _d = __asyncValues(globber.globGenerator()), _e; _e = yield _d.next(), !_e.done;) { - const file = _e.value; + for (var _b = __asyncValues(globber.globGenerator()), _c; _c = yield _b.next(), !_c.done;) { + const file = _c.value; const terrakubeData = JSON.parse(yield (0, promises_1.readFile)(`${file}`, "utf8")); core.startGroup(`Execute Workspace ${terrakubeData.workspace}`); console.debug(`Processing: ${file}`); @@ -75,8 +74,8 @@ function run() { core.info(`Workspace: ${terrakubeData.workspace}`); core.info(`Folder: ${terrakubeData.folder}`); core.info(`Action branch: ${process.env.GITHUB_REF}`); - core.info(`Branch: ${(_b = process.env.GITHUB_REF) === null || _b === void 0 ? void 0 : _b.toString().split("/")[2]}`); - terrakubeData.branch = (_c = process.env.GITHUB_REF) === null || _c === void 0 ? void 0 : _c.toString().split("/")[2]; + core.info(`Branch: ${githubActionInput.branch}`); + terrakubeData.branch = githubActionInput.branch; //Object.keys(terrakubeData.variables).forEach(key => { // console.log('Key : ' + key + ', Value : ' + terrakubeData.variables[key]) //}) @@ -131,7 +130,7 @@ function run() { catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { - if (_e && !_e.done && (_a = _d.return)) yield _a.call(_d); + if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b); } finally { if (e_1) throw e_1.error; } } diff --git a/dist/userInput.js b/dist/userInput.js index aaabf2e..2b04e21 100644 --- a/dist/userInput.js +++ b/dist/userInput.js @@ -44,13 +44,16 @@ function getActionInput() { core.debug(`Terrakube Repository: ${terrakubeRepository}`); const terrakubeTemplate = core.getInput('terrakube_template', { required: true }); core.debug(`Terrakube Template: ${terrakubeTemplate}`); + const terrakubeBranch = core.getInput('terrakube_branch', { required: true }); + core.debug(`Terrakube Branch: ${terrakubeTemplate}`); const githubToken = core.getInput('token', { required: true }); const terrakubeActionInput = { token: terrakubeToken, terrakubeEndpoint: terrakubeEndpoint, terrakubeRepository: terrakubeRepository, terrakubeTemplate: terrakubeTemplate, - githubToken: githubToken + githubToken: githubToken, + branch: terrakubeBranch }; return terrakubeActionInput; }); diff --git a/src/index.ts b/src/index.ts index 4ea53f7..7dd7efc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -32,8 +32,8 @@ async function run(): Promise { core.info(`Workspace: ${terrakubeData.workspace}`) core.info(`Folder: ${terrakubeData.folder}`) core.info(`Action branch: ${process.env.GITHUB_REF}`) - core.info(`Branch: ${process.env.GITHUB_REF?.toString().split("/")[2]}`) - terrakubeData.branch = process.env.GITHUB_REF?.toString().split("/")[2]; + core.info(`Branch: ${githubActionInput.branch}`) + terrakubeData.branch = githubActionInput.branch //Object.keys(terrakubeData.variables).forEach(key => { // console.log('Key : ' + key + ', Value : ' + terrakubeData.variables[key]) diff --git a/src/userInput.ts b/src/userInput.ts index a8b9ee3..993f792 100644 --- a/src/userInput.ts +++ b/src/userInput.ts @@ -5,7 +5,8 @@ export interface GitHubActionInput{ terrakubeEndpoint: string, terrakubeRepository: string, terrakubeTemplate: string, - githubToken: string + githubToken: string, + branch: string } export async function getActionInput(): Promise{ @@ -21,6 +22,9 @@ export async function getActionInput(): Promise{ const terrakubeTemplate: string = core.getInput('terrakube_template', { required: true }) core.debug(`Terrakube Template: ${terrakubeTemplate}`) + const terrakubeBranch: string = core.getInput('terrakube_branch', { required: true }) + core.debug(`Terrakube Branch: ${terrakubeBranch}`) + const githubToken: string = core.getInput('token', { required: true }) const terrakubeActionInput:GitHubActionInput = { @@ -28,7 +32,8 @@ export async function getActionInput(): Promise{ terrakubeEndpoint: terrakubeEndpoint, terrakubeRepository: terrakubeRepository, terrakubeTemplate: terrakubeTemplate, - githubToken: githubToken + githubToken: githubToken, + branch: terrakubeBranch } return terrakubeActionInput