Skip to content

Commit

Permalink
Merge pull request #106 from regevbr/body_update
Browse files Browse the repository at this point in the history
fix #42 - overwrite body and name as well
  • Loading branch information
svenstaro authored May 23, 2023
2 parents d8e2a64 + febd7d0 commit 42c6380
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
25 changes: 22 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
42 changes: 37 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -20,18 +22,22 @@ 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,
prerelease: boolean,
make_latest: boolean,
release_name: string,
body: string,
octokit: Octokit
): Promise<ReleaseByTagResp | CreateReleaseResp> {
octokit: Octokit,
overwrite: boolean
): Promise<ReleaseByTagResp | CreateReleaseResp | UpdateReleaseResp> {
let release: ReleaseByTagResp
try {
core.debug(`Getting release by tag ${tag}.`)
return await octokit.request(releaseByTag, {
release = await octokit.request(releaseByTag, {
...repo(),
tag: tag
})
Expand All @@ -53,10 +59,35 @@ async function get_release_by_tag(
throw error
}
}
let updateObject: Partial<UpdateReleaseParams> | 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,
Expand Down Expand Up @@ -169,7 +200,8 @@ async function run(): Promise<void> {
make_latest,
release_name,
body,
octokit
octokit,
overwrite
)

if (file_glob) {
Expand Down

0 comments on commit 42c6380

Please sign in to comment.