Skip to content

Commit

Permalink
change tabs back to spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
alehechka authored and peterjgrainger committed Mar 16, 2022
1 parent bc4ea13 commit 1e7a168
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 50 deletions.
16 changes: 0 additions & 16 deletions .github/workflows/test-run.yml

This file was deleted.

50 changes: 25 additions & 25 deletions src/create-branch.ts
Original file line number Diff line number Diff line change
@@ -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;
}
18 changes: 9 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

0 comments on commit 1e7a168

Please sign in to comment.