Skip to content

Commit

Permalink
output created flag
Browse files Browse the repository at this point in the history
  • Loading branch information
alehechka authored and peterjgrainger committed Mar 16, 2022
1 parent 299aa7c commit d1356e0
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test-run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ name: 'Create Branch'
description: 'Creates a branch'
author: 'Peter Grainger (peter@grainger)'
branding:
icon: 'git-branch'
icon: 'git-branch'
color: 'green'
inputs:
branch:
description: 'The branch to create'
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'
6 changes: 4 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/create-branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
19 changes: 10 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit d1356e0

Please sign in to comment.