generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc4ea13
commit 1e7a168
Showing
3 changed files
with
34 additions
and
50 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters