|
1 | 1 | import { Context } from '@actions/github/lib/context';
|
2 | 2 |
|
3 | 3 | export async function createBranch(getOctokit: any, context: Context, branch: string, sha?: string) {
|
4 |
| - const toolkit = getOctokit(githubToken()); |
5 |
| - // Sometimes branch might come in with refs/heads already |
6 |
| - branch = branch.replace('refs/heads/', ''); |
7 |
| - const ref = `refs/heads/${branch}`; |
| 4 | + const toolkit = getOctokit(githubToken()); |
| 5 | + // Sometimes branch might come in with refs/heads already |
| 6 | + branch = branch.replace('refs/heads/', ''); |
| 7 | + const ref = `refs/heads/${branch}`; |
8 | 8 |
|
9 |
| - // throws HttpError if branch already exists. |
10 |
| - try { |
11 |
| - await toolkit.rest.repos.getBranch({ |
12 |
| - ...context.repo, |
13 |
| - branch, |
14 |
| - }); |
15 |
| - } catch (error: any) { |
16 |
| - if (error.name === 'HttpError' && error.status === 404) { |
17 |
| - const resp = await toolkit.rest.git.createRef({ |
18 |
| - ref, |
19 |
| - sha: sha || context.sha, |
20 |
| - ...context.repo, |
21 |
| - }); |
| 9 | + // throws HttpError if branch already exists. |
| 10 | + try { |
| 11 | + await toolkit.rest.repos.getBranch({ |
| 12 | + ...context.repo, |
| 13 | + branch, |
| 14 | + }); |
| 15 | + } catch (error: any) { |
| 16 | + if (error.name === 'HttpError' && error.status === 404) { |
| 17 | + const resp = await toolkit.rest.git.createRef({ |
| 18 | + ref, |
| 19 | + sha: sha || context.sha, |
| 20 | + ...context.repo, |
| 21 | + }); |
22 | 22 |
|
23 |
| - return resp?.data?.ref === ref; |
24 |
| - } else { |
25 |
| - throw Error(error); |
26 |
| - } |
27 |
| - } |
| 23 | + return resp?.data?.ref === ref; |
| 24 | + } else { |
| 25 | + throw Error(error); |
| 26 | + } |
| 27 | + } |
28 | 28 | }
|
29 | 29 |
|
30 | 30 | function githubToken(): string {
|
31 |
| - const token = process.env.GITHUB_TOKEN; |
32 |
| - if (!token) throw ReferenceError('No token defined in the environment variables'); |
33 |
| - return token; |
| 31 | + const token = process.env.GITHUB_TOKEN; |
| 32 | + if (!token) throw ReferenceError('No token defined in the environment variables'); |
| 33 | + return token; |
34 | 34 | }
|
0 commit comments