Merge pull request #46 from Spikatrix/master

Allow setting an explicit target_commitish
This commit is contained in:
Sven-Hendrik Haase 2023-07-28 18:28:50 +02:00 committed by GitHub
commit 80d7a7e41c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 1337 additions and 1303 deletions

View File

@ -21,6 +21,7 @@ Optional Arguments
- `prerelease`: Mark the release as a pre-release (Default: `false`). - `prerelease`: Mark the release as a pre-release (Default: `false`).
- `make_latest`: Mark the release as the latest release for the repository (Default: `true`). - `make_latest`: Mark the release as the latest release for the repository (Default: `true`).
- `release_name`: Explicitly set a release name. (Defaults: implicitly same as `tag` via GitHub API). - `release_name`: Explicitly set a release name. (Defaults: implicitly same as `tag` via GitHub API).
- `target_commit`: Sets the commit hash or branch for the tag to be based on (Default: the default branch, usually `main`).
- `body`: Content of the release text (Default: `""`). - `body`: Content of the release text (Default: `""`).
- `repo_name`: Specify the name of the GitHub repository in which the GitHub release will be created, edited, and deleted. If the repository is other than the current, it is required to create a personal access token with `repo`, `user`, `admin:repo_hook` scopes to the foreign repository and add it as a secret. (Default: current repository). - `repo_name`: Specify the name of the GitHub repository in which the GitHub release will be created, edited, and deleted. If the repository is other than the current, it is required to create a personal access token with `repo`, `user`, `admin:repo_hook` scopes to the foreign repository and add it as a secret. (Default: current repository).

View File

@ -32,6 +32,8 @@ inputs:
description: 'Explicitly set a release name. Defaults to empty which will cause the release to take the tag as name on GitHub.' description: 'Explicitly set a release name. Defaults to empty which will cause the release to take the tag as name on GitHub.'
body: body:
description: 'Content of the release text. Empty by default.' description: 'Content of the release text. Empty by default.'
target_commit:
description: 'Specifies the commitish value that determines where the Git tag is created from. Can be any branch or commit SHA. Unused if the Git tag already exists. Default: the repository\"s default branch (usually `main`).'
repo_name: repo_name:
description: 'Specify the name of the GitHub repository in which the GitHub release will be created, edited, and deleted. If the repository is other than the current, it is required to create a personal access token with `repo`, `user`, `admin:repo_hook` scopes to the foreign repository and add it as a secret. Defaults to the current repository' description: 'Specify the name of the GitHub repository in which the GitHub release will be created, edited, and deleted. If the repository is other than the current, it is required to create a personal access token with `repo`, `user`, `admin:repo_hook` scopes to the foreign repository and add it as a secret. Defaults to the current repository'
outputs: outputs:

19
dist/index.js vendored
View File

@ -45,13 +45,14 @@ const github = __importStar(__nccwpck_require__(5438));
const path = __importStar(__nccwpck_require__(1017)); const path = __importStar(__nccwpck_require__(1017));
const glob = __importStar(__nccwpck_require__(7106)); const glob = __importStar(__nccwpck_require__(7106));
const attempt_1 = __nccwpck_require__(6494); const attempt_1 = __nccwpck_require__(6494);
const getRef = 'GET /repos/{owner}/{repo}/git/ref/{ref}';
const releaseByTag = 'GET /repos/{owner}/{repo}/releases/tags/{tag}'; const releaseByTag = 'GET /repos/{owner}/{repo}/releases/tags/{tag}';
const createRelease = 'POST /repos/{owner}/{repo}/releases'; const createRelease = 'POST /repos/{owner}/{repo}/releases';
const updateRelease = 'PATCH /repos/{owner}/{repo}/releases/{release_id}'; const updateRelease = 'PATCH /repos/{owner}/{repo}/releases/{release_id}';
const repoAssets = 'GET /repos/{owner}/{repo}/releases/{release_id}/assets'; const repoAssets = 'GET /repos/{owner}/{repo}/releases/{release_id}/assets';
const uploadAssets = 'POST {origin}/repos/{owner}/{repo}/releases/{release_id}/assets{?name,label}'; const uploadAssets = 'POST {origin}/repos/{owner}/{repo}/releases/{release_id}/assets{?name,label}';
const deleteAssets = 'DELETE /repos/{owner}/{repo}/releases/assets/{asset_id}'; const deleteAssets = 'DELETE /repos/{owner}/{repo}/releases/assets/{asset_id}';
function get_release_by_tag(tag, prerelease, make_latest, release_name, body, octokit, overwrite, promote) { function get_release_by_tag(tag, prerelease, make_latest, release_name, body, octokit, overwrite, promote, target_commit) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
let release; let release;
try { try {
@ -62,7 +63,18 @@ function get_release_by_tag(tag, prerelease, make_latest, release_name, body, oc
// If this returns 404, we need to create the release first. // If this returns 404, we need to create the release first.
if (error.status === 404) { if (error.status === 404) {
core.debug(`Release for tag ${tag} doesn't exist yet so we'll create it now.`); core.debug(`Release for tag ${tag} doesn't exist yet so we'll create it now.`);
return yield octokit.request(createRelease, Object.assign(Object.assign({}, repo()), { tag_name: tag, prerelease: prerelease, make_latest: make_latest ? 'true' : 'false', name: release_name, body: body })); if (target_commit) {
try {
yield octokit.request(getRef, Object.assign(Object.assign({}, repo()), { ref: `tags/${tag}` }));
core.warning(`Ignoring target_commit as the tag ${tag} already exists`);
}
catch (tagError) {
if (tagError.status !== 404) {
throw tagError
}
}
}
return yield octokit.request(createRelease, Object.assign(Object.assign({}, repo()), { tag_name: tag, prerelease: prerelease, make_latest: make_latest ? 'true' : 'false', name: release_name, body: body, target_commitish: target_commit }));
} }
else { else {
throw error; throw error;
@ -167,13 +179,14 @@ function run() {
const prerelease = core.getInput('prerelease') == 'true' ? true : false; const prerelease = core.getInput('prerelease') == 'true' ? true : false;
const make_latest = core.getInput('make_latest') != 'false' ? true : false; const make_latest = core.getInput('make_latest') != 'false' ? true : false;
const release_name = core.getInput('release_name'); const release_name = core.getInput('release_name');
const target_commit = core.getInput('target_commit');
const body = core const body = core
.getInput('body') .getInput('body')
.replace(/%0A/gi, '\n') .replace(/%0A/gi, '\n')
.replace(/%0D/gi, '\r') .replace(/%0D/gi, '\r')
.replace(/%25/g, '%'); .replace(/%25/g, '%');
const octokit = github.getOctokit(token); const octokit = github.getOctokit(token);
const release = yield get_release_by_tag(tag, prerelease, make_latest, release_name, body, octokit, overwrite, promote); const release = yield get_release_by_tag(tag, prerelease, make_latest, release_name, body, octokit, overwrite, promote, target_commit);
if (file_glob) { if (file_glob) {
const files = glob.sync(file); const files = glob.sync(file);
if (files.length > 0) { if (files.length > 0) {

View File

@ -7,6 +7,7 @@ import * as path from 'path'
import * as glob from 'glob' import * as glob from 'glob'
import {retry} from '@lifeomic/attempt' import {retry} from '@lifeomic/attempt'
const getRef = 'GET /repos/{owner}/{repo}/git/ref/{ref}' as const
const releaseByTag = 'GET /repos/{owner}/{repo}/releases/tags/{tag}' as const const releaseByTag = 'GET /repos/{owner}/{repo}/releases/tags/{tag}' as const
const createRelease = 'POST /repos/{owner}/{repo}/releases' as const const createRelease = 'POST /repos/{owner}/{repo}/releases' as const
const updateRelease = const updateRelease =
@ -33,7 +34,8 @@ async function get_release_by_tag(
body: string, body: string,
octokit: Octokit, octokit: Octokit,
overwrite: boolean, overwrite: boolean,
promote: boolean promote: boolean,
target_commit: string
): Promise<ReleaseByTagResp | CreateReleaseResp | UpdateReleaseResp> { ): Promise<ReleaseByTagResp | CreateReleaseResp | UpdateReleaseResp> {
let release: ReleaseByTagResp let release: ReleaseByTagResp
try { try {
@ -48,13 +50,27 @@ async function get_release_by_tag(
core.debug( core.debug(
`Release for tag ${tag} doesn't exist yet so we'll create it now.` `Release for tag ${tag} doesn't exist yet so we'll create it now.`
) )
if (target_commit) {
try {
await octokit.request(getRef, {
...repo(),
ref: `tags/${tag}`
})
core.warning(`Ignoring target_commit as the tag ${tag} already exists`)
} catch (tagError: any) {
if (tagError.status !== 404) {
throw tagError
}
}
}
return await octokit.request(createRelease, { return await octokit.request(createRelease, {
...repo(), ...repo(),
tag_name: tag, tag_name: tag,
prerelease: prerelease, prerelease: prerelease,
make_latest: make_latest ? 'true' : 'false', make_latest: make_latest ? 'true' : 'false',
name: release_name, name: release_name,
body: body body: body,
target_commitish: target_commit
}) })
} else { } else {
throw error throw error
@ -194,6 +210,7 @@ async function run(): Promise<void> {
const prerelease = core.getInput('prerelease') == 'true' ? true : false const prerelease = core.getInput('prerelease') == 'true' ? true : false
const make_latest = core.getInput('make_latest') != 'false' ? true : false const make_latest = core.getInput('make_latest') != 'false' ? true : false
const release_name = core.getInput('release_name') const release_name = core.getInput('release_name')
const target_commit = core.getInput('target_commit')
const body = core const body = core
.getInput('body') .getInput('body')
.replace(/%0A/gi, '\n') .replace(/%0A/gi, '\n')
@ -209,7 +226,8 @@ async function run(): Promise<void> {
body, body,
octokit, octokit,
overwrite, overwrite,
promote promote,
target_commit
) )
if (file_glob) { if (file_glob) {