Skip to content

Commit d1356e0

Browse files
alehechkapeterjgrainger
authored andcommitted
output created flag
1 parent 299aa7c commit d1356e0

File tree

5 files changed

+23
-13
lines changed

5 files changed

+23
-13
lines changed

.github/workflows/test-run.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ jobs:
77
runs-on: ubuntu-latest
88
steps:
99
- uses: alehechka/action-create-branch@feature/output-create-status
10+
id: branch
1011
env:
1112
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1213
with:
1314
branch: test/output
15+
16+
- run: echo "${{ steps.branch.outputs.created }}"

action.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ name: 'Create Branch'
22
description: 'Creates a branch'
33
author: 'Peter Grainger (peter@grainger)'
44
branding:
5-
icon: 'git-branch'
5+
icon: 'git-branch'
66
color: 'green'
77
inputs:
88
branch:
99
description: 'The branch to create'
1010
default: 'release-candidate'
1111
sha:
1212
description: 'The SHA1 value for the branch reference'
13+
outputs:
14+
created:
15+
description: 'Boolean value representing whether or not a new branch was created.'
1316
runs:
1417
using: 'node12'
1518
main: 'dist/index.js'

dist/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,8 @@ function run() {
368368
const branch = core.getInput('branch');
369369
const sha = core.getInput('sha');
370370
core.debug(`Creating branch ${branch}`);
371-
yield (0, create_branch_1.createBranch)(github_1.getOctokit, github_1.context, branch, sha);
371+
const isCreated = yield (0, create_branch_1.createBranch)(github_1.getOctokit, github_1.context, branch, sha);
372+
core.setOutput('created', Boolean(isCreated));
372373
}
373374
catch (error) {
374375
core.setFailed(error.message);
@@ -5743,6 +5744,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
57435744
Object.defineProperty(exports, "__esModule", { value: true });
57445745
exports.createBranch = void 0;
57455746
function createBranch(getOctokit, context, branch, sha) {
5747+
var _a;
57465748
return __awaiter(this, void 0, void 0, function* () {
57475749
const toolkit = getOctokit(githubToken());
57485750
// Sometimes branch might come in with refs/heads already
@@ -5754,7 +5756,7 @@ function createBranch(getOctokit, context, branch, sha) {
57545756
catch (error) {
57555757
if (error.name === 'HttpError' && error.status === 404) {
57565758
const resp = yield toolkit.rest.git.createRef(Object.assign({ ref: `refs/heads/${branch}`, sha: sha || context.sha }, context.repo));
5757-
console.log('Create Ref Response:', JSON.stringify(resp));
5759+
return ((_a = resp === null || resp === void 0 ? void 0 : resp.data) === null || _a === void 0 ? void 0 : _a.ref) === branch;
57585760
}
57595761
else {
57605762
throw Error(error);

src/create-branch.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export async function createBranch(getOctokit: any, context: Context, branch: st
1818
sha: sha || context.sha,
1919
...context.repo,
2020
});
21-
console.log('Create Ref Response:', JSON.stringify(resp));
21+
22+
return resp?.data?.ref === branch;
2223
} else {
2324
throw Error(error);
2425
}

src/main.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import * as core from '@actions/core';
2-
import { getOctokit, context} from '@actions/github'
2+
import { getOctokit, context } from '@actions/github';
33
import { createBranch } from './create-branch';
44

55
async function run() {
6-
try {
7-
const branch = core.getInput('branch');
8-
const sha = core.getInput('sha');
9-
core.debug(`Creating branch ${branch}`);
10-
await createBranch(getOctokit, context, branch, sha)
11-
} catch (error: any) {
12-
core.setFailed(error.message);
13-
}
6+
try {
7+
const branch = core.getInput('branch');
8+
const sha = core.getInput('sha');
9+
core.debug(`Creating branch ${branch}`);
10+
const isCreated = await createBranch(getOctokit, context, branch, sha);
11+
core.setOutput('created', Boolean(isCreated));
12+
} catch (error: any) {
13+
core.setFailed(error.message);
14+
}
1415
}
1516
run();

0 commit comments

Comments
 (0)