diff --git a/.github/workflows/test-run.yml b/.github/workflows/test-run.yml deleted file mode 100644 index 5e5e044..0000000 --- a/.github/workflows/test-run.yml +++ /dev/null @@ -1,16 +0,0 @@ -name: 'Test Run' -on: - - push - -jobs: - test_run: - 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/src/create-branch.ts b/src/create-branch.ts index cf65981..118c0fd 100644 --- a/src/create-branch.ts +++ b/src/create-branch.ts @@ -1,34 +1,34 @@ import { Context } from '@actions/github/lib/context'; export async function createBranch(getOctokit: any, context: Context, branch: string, sha?: string) { - const toolkit = getOctokit(githubToken()); - // Sometimes branch might come in with refs/heads already - branch = branch.replace('refs/heads/', ''); - const ref = `refs/heads/${branch}`; + const toolkit = getOctokit(githubToken()); + // Sometimes branch might come in with refs/heads already + branch = branch.replace('refs/heads/', ''); + const ref = `refs/heads/${branch}`; - // throws HttpError if branch already exists. - try { - await toolkit.rest.repos.getBranch({ - ...context.repo, - branch, - }); - } catch (error: any) { - if (error.name === 'HttpError' && error.status === 404) { - const resp = await toolkit.rest.git.createRef({ - ref, - sha: sha || context.sha, - ...context.repo, - }); + // throws HttpError if branch already exists. + try { + await toolkit.rest.repos.getBranch({ + ...context.repo, + branch, + }); + } catch (error: any) { + if (error.name === 'HttpError' && error.status === 404) { + const resp = await toolkit.rest.git.createRef({ + ref, + sha: sha || context.sha, + ...context.repo, + }); - return resp?.data?.ref === ref; - } else { - throw Error(error); - } - } + return resp?.data?.ref === ref; + } else { + throw Error(error); + } + } } function githubToken(): string { - const token = process.env.GITHUB_TOKEN; - if (!token) throw ReferenceError('No token defined in the environment variables'); - return token; + const token = process.env.GITHUB_TOKEN; + if (!token) throw ReferenceError('No token defined in the environment variables'); + return token; } diff --git a/src/main.ts b/src/main.ts index 99eca8e..dfe4853 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,14 +3,14 @@ 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}`); - const isCreated = await createBranch(getOctokit, context, branch, sha); - core.setOutput('created', Boolean(isCreated)); - } 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();