diff --git a/dist/index.js b/dist/index.js index 8ab52584..245c5ff5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -47,14 +47,16 @@ const glob = __importStar(__nccwpck_require__(7106)); const attempt_1 = __nccwpck_require__(6494); const releaseByTag = 'GET /repos/{owner}/{repo}/releases/tags/{tag}'; const createRelease = 'POST /repos/{owner}/{repo}/releases'; +const updateRelease = 'PATCH /repos/{owner}/{repo}/releases/{release_id}'; const repoAssets = 'GET /repos/{owner}/{repo}/releases/{release_id}/assets'; const uploadAssets = 'POST {origin}/repos/{owner}/{repo}/releases/{release_id}/assets{?name,label}'; const deleteAssets = 'DELETE /repos/{owner}/{repo}/releases/assets/{asset_id}'; -function get_release_by_tag(tag, prerelease, make_latest, release_name, body, octokit) { +function get_release_by_tag(tag, prerelease, make_latest, release_name, body, octokit, overwrite) { return __awaiter(this, void 0, void 0, function* () { + let release; try { core.debug(`Getting release by tag ${tag}.`); - return yield octokit.request(releaseByTag, Object.assign(Object.assign({}, repo()), { tag: tag })); + release = yield octokit.request(releaseByTag, Object.assign(Object.assign({}, repo()), { tag: tag })); } catch (error) { // If this returns 404, we need to create the release first. @@ -66,6 +68,23 @@ function get_release_by_tag(tag, prerelease, make_latest, release_name, body, oc throw error; } } + let updateObject; + if (overwrite) { + if (release.data.name !== release_name) { + core.debug(`The ${tag} release already exists with a different name ${release.data.name} so we'll overwrite it.`); + updateObject = updateObject || {}; + updateObject.name = release_name; + } + if (release.data.body !== body) { + core.debug(`The ${tag} release already exists with a different body ${release.data.body} so we'll overwrite it.`); + updateObject = updateObject || {}; + updateObject.body = body; + } + } + if (updateObject) { + return octokit.request(updateRelease, Object.assign(Object.assign(Object.assign({}, repo()), updateObject), { release_id: release.data.id })); + } + return release; }); } function upload_to_release(release, file, asset_name, tag, overwrite, octokit) { @@ -148,7 +167,7 @@ function run() { .replace(/%0D/gi, '\r') .replace(/%25/g, '%'); const octokit = github.getOctokit(token); - const release = yield get_release_by_tag(tag, prerelease, make_latest, release_name, body, octokit); + const release = yield get_release_by_tag(tag, prerelease, make_latest, release_name, body, octokit, overwrite); if (file_glob) { const files = glob.sync(file); if (files.length > 0) { diff --git a/src/main.ts b/src/main.ts index f0270bc9..5d3f5fc2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,6 +9,8 @@ import {retry} from '@lifeomic/attempt' const releaseByTag = 'GET /repos/{owner}/{repo}/releases/tags/{tag}' as const const createRelease = 'POST /repos/{owner}/{repo}/releases' as const +const updateRelease = + 'PATCH /repos/{owner}/{repo}/releases/{release_id}' as const const repoAssets = 'GET /repos/{owner}/{repo}/releases/{release_id}/assets' as const const uploadAssets = @@ -20,6 +22,8 @@ type ReleaseByTagResp = Endpoints[typeof releaseByTag]['response'] type CreateReleaseResp = Endpoints[typeof createRelease]['response'] type RepoAssetsResp = Endpoints[typeof repoAssets]['response']['data'] type UploadAssetResp = Endpoints[typeof uploadAssets]['response'] +type UpdateReleaseResp = Endpoints[typeof updateRelease]['response'] +type UpdateReleaseParams = Endpoints[typeof updateRelease]['parameters'] async function get_release_by_tag( tag: string, @@ -27,11 +31,13 @@ async function get_release_by_tag( make_latest: boolean, release_name: string, body: string, - octokit: Octokit -): Promise { + octokit: Octokit, + overwrite: boolean +): Promise { + let release: ReleaseByTagResp try { core.debug(`Getting release by tag ${tag}.`) - return await octokit.request(releaseByTag, { + release = await octokit.request(releaseByTag, { ...repo(), tag: tag }) @@ -53,10 +59,35 @@ async function get_release_by_tag( throw error } } + let updateObject: Partial | undefined + if (overwrite) { + if (release.data.name !== release_name) { + core.debug( + `The ${tag} release already exists with a different name ${release.data.name} so we'll overwrite it.` + ) + updateObject = updateObject || {} + updateObject.name = release_name + } + if (release.data.body !== body) { + core.debug( + `The ${tag} release already exists with a different body ${release.data.body} so we'll overwrite it.` + ) + updateObject = updateObject || {} + updateObject.body = body + } + } + if (updateObject) { + return octokit.request(updateRelease, { + ...repo(), + ...updateObject, + release_id: release.data.id + }) + } + return release } async function upload_to_release( - release: ReleaseByTagResp | CreateReleaseResp, + release: ReleaseByTagResp | CreateReleaseResp | UpdateReleaseResp, file: string, asset_name: string, tag: string, @@ -169,7 +200,8 @@ async function run(): Promise { make_latest, release_name, body, - octokit + octokit, + overwrite ) if (file_glob) {