From d1356e0a9b96f3547176be609f38c1f0e97e49a7 Mon Sep 17 00:00:00 2001 From: Adam Lehechka <42357034+alehechka@users.noreply.github.com> Date: Sun, 20 Feb 2022 11:26:06 -0600 Subject: [PATCH] output created flag --- .github/workflows/test-run.yml | 3 +++ action.yml | 5 ++++- dist/index.js | 6 ++++-- src/create-branch.ts | 3 ++- src/main.ts | 19 ++++++++++--------- 5 files changed, 23 insertions(+), 13 deletions(-) diff --git a/.github/workflows/test-run.yml b/.github/workflows/test-run.yml index 1d93295..5e5e044 100644 --- a/.github/workflows/test-run.yml +++ b/.github/workflows/test-run.yml @@ -7,7 +7,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: alehechka/action-create-branch@feature/output-create-status + id: branch env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: branch: test/output + + - run: echo "${{ steps.branch.outputs.created }}" diff --git a/action.yml b/action.yml index 0e911cb..1289ac7 100644 --- a/action.yml +++ b/action.yml @@ -2,7 +2,7 @@ name: 'Create Branch' description: 'Creates a branch' author: 'Peter Grainger (peter@grainger)' branding: - icon: 'git-branch' + icon: 'git-branch' color: 'green' inputs: branch: @@ -10,6 +10,9 @@ inputs: default: 'release-candidate' sha: description: 'The SHA1 value for the branch reference' +outputs: + created: + description: 'Boolean value representing whether or not a new branch was created.' runs: using: 'node12' main: 'dist/index.js' diff --git a/dist/index.js b/dist/index.js index 6d74993..28a67b4 100644 --- a/dist/index.js +++ b/dist/index.js @@ -368,7 +368,8 @@ function run() { const branch = core.getInput('branch'); const sha = core.getInput('sha'); core.debug(`Creating branch ${branch}`); - yield (0, create_branch_1.createBranch)(github_1.getOctokit, github_1.context, branch, sha); + const isCreated = yield (0, create_branch_1.createBranch)(github_1.getOctokit, github_1.context, branch, sha); + core.setOutput('created', Boolean(isCreated)); } catch (error) { core.setFailed(error.message); @@ -5743,6 +5744,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge Object.defineProperty(exports, "__esModule", { value: true }); exports.createBranch = void 0; function createBranch(getOctokit, context, branch, sha) { + var _a; return __awaiter(this, void 0, void 0, function* () { const toolkit = getOctokit(githubToken()); // Sometimes branch might come in with refs/heads already @@ -5754,7 +5756,7 @@ function createBranch(getOctokit, context, branch, sha) { catch (error) { if (error.name === 'HttpError' && error.status === 404) { const resp = yield toolkit.rest.git.createRef(Object.assign({ ref: `refs/heads/${branch}`, sha: sha || context.sha }, context.repo)); - console.log('Create Ref Response:', JSON.stringify(resp)); + return ((_a = resp === null || resp === void 0 ? void 0 : resp.data) === null || _a === void 0 ? void 0 : _a.ref) === branch; } else { throw Error(error); diff --git a/src/create-branch.ts b/src/create-branch.ts index 0d77798..1fb42b9 100644 --- a/src/create-branch.ts +++ b/src/create-branch.ts @@ -18,7 +18,8 @@ export async function createBranch(getOctokit: any, context: Context, branch: st sha: sha || context.sha, ...context.repo, }); - console.log('Create Ref Response:', JSON.stringify(resp)); + + return resp?.data?.ref === branch; } else { throw Error(error); } diff --git a/src/main.ts b/src/main.ts index e06b4f3..99eca8e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,15 +1,16 @@ import * as core from '@actions/core'; -import { getOctokit, context} from '@actions/github' +import { getOctokit, context } from '@actions/github'; import { createBranch } from './create-branch'; async function run() { - try { - const branch = core.getInput('branch'); - const sha = core.getInput('sha'); - core.debug(`Creating branch ${branch}`); - await createBranch(getOctokit, context, branch, sha) - } catch (error: any) { - core.setFailed(error.message); - } + try { + const branch = core.getInput('branch'); + const sha = core.getInput('sha'); + core.debug(`Creating branch ${branch}`); + const isCreated = await createBranch(getOctokit, context, branch, sha); + core.setOutput('created', Boolean(isCreated)); + } catch (error: any) { + core.setFailed(error.message); + } } run();