|
1 |
| -/** |
2 |
| - * The entrypoint for the action. |
3 |
| - */ |
4 |
| -import { run } from './main' |
| 1 | +import { getInput, setFailed, setOutput } from '@actions/core'; |
| 2 | +import { context, getOctokit } from '@actions/github'; |
| 3 | +import { readFile } from 'fs/promises'; |
5 | 4 |
|
6 |
| -// eslint-disable-next-line @typescript-eslint/no-floating-promises |
7 |
| -run() |
| 5 | +type ActionInput = { |
| 6 | + tag: string; |
| 7 | + releaseName: string; |
| 8 | + body: string; |
| 9 | + draft: boolean; |
| 10 | + prerelease: boolean; |
| 11 | + commitish: string; |
| 12 | + bodyPath: string; |
| 13 | + owner: string; |
| 14 | + repo: string; |
| 15 | +}; |
| 16 | + |
| 17 | +export async function run(): Promise<void> { |
| 18 | + try { |
| 19 | + const githubToken = process.env.GITHUB_TOKEN; |
| 20 | + |
| 21 | + if (!githubToken) { |
| 22 | + setFailed('No Github Token was provided'); |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + const octokit = getOctokit(githubToken); |
| 27 | + const { tag, releaseName, body, draft, prerelease, commitish, bodyPath, owner, repo } = getInputs(); |
| 28 | + const bodyFileContent = await getBodyFileContent(bodyPath); |
| 29 | + |
| 30 | + const createReleaseResponse = await octokit.request(`POST /repos/${owner}/${repo}/releases`, { |
| 31 | + owner, |
| 32 | + repo, |
| 33 | + tag_name: tag, |
| 34 | + name: releaseName, |
| 35 | + body: bodyFileContent || body, |
| 36 | + draft, |
| 37 | + prerelease, |
| 38 | + target_commitish: commitish |
| 39 | + }); |
| 40 | + |
| 41 | + const { |
| 42 | + data: { id: releaseId, html_url: htmlUrl, upload_url: uploadUrl } |
| 43 | + } = createReleaseResponse; |
| 44 | + |
| 45 | + console.count('test'); |
| 46 | + |
| 47 | + setOutput('id', releaseId); |
| 48 | + setOutput('html_url', htmlUrl); |
| 49 | + setOutput('upload_url', uploadUrl); |
| 50 | + } catch (error) { |
| 51 | + if (error instanceof Error) setFailed(error.message); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +run(); |
| 56 | + |
| 57 | +function getInputs(): ActionInput { |
| 58 | + const { owner: currentOwner, repo: currentRepo } = context.repo; |
| 59 | + |
| 60 | + const tagName = getInput('tag_name', { required: true }); |
| 61 | + |
| 62 | + // This removes the 'refs/tags' portion of the string, i.e. from 'refs/tags/v1.10.15' to 'v1.10.15' |
| 63 | + const tag = tagName.replace('refs/tags/', ''); |
| 64 | + const releaseName = getInput('release_name', { required: false }).replace('refs/tags/', ''); |
| 65 | + |
| 66 | + const body = getInput('body', { required: false }); |
| 67 | + const draft = getInput('draft', { required: false }) === 'true'; |
| 68 | + const prerelease = getInput('prerelease', { required: false }) === 'true'; |
| 69 | + const commitish = getInput('commitish', { required: false }) || context.sha; |
| 70 | + |
| 71 | + const bodyPath = getInput('body_path', { required: false }); |
| 72 | + const owner = getInput('owner', { required: false }) || currentOwner; |
| 73 | + const repo = getInput('repo', { required: false }) || currentRepo; |
| 74 | + |
| 75 | + return { tag, releaseName, body, draft, prerelease, commitish, bodyPath, owner, repo }; |
| 76 | +} |
| 77 | + |
| 78 | +async function getBodyFileContent(bodyPath: string): Promise<string | null> { |
| 79 | + if (!bodyPath) return null; |
| 80 | + |
| 81 | + try { |
| 82 | + const bodyFileContent = await readFile(bodyPath, { encoding: 'utf8' }); |
| 83 | + return bodyFileContent; |
| 84 | + } catch (error) { |
| 85 | + if (error instanceof Error) setFailed(error.message); |
| 86 | + return null; |
| 87 | + } |
| 88 | +} |
0 commit comments