Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add 'update_tag' option #293

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -181,6 +181,7 @@ The following are optional as `step.with` keys
| `discussion_category_name` | String | If specified, a discussion of the specified category is created and linked to the release. The value must be a category that already exists in the repository. For more information, see ["Managing categories for discussions in your repository."](https://docs.github.com/en/discussions/managing-discussions-for-your-community/managing-categories-for-discussions-in-your-repository) |
| `generate_release_notes` | Boolean | Whether to automatically generate the name and body for this release. If name is specified, the specified name will be used; otherwise, a name will be automatically generated. If body is specified, the body will be pre-pended to the automatically generated notes. See the [GitHub docs for this feature](https://docs.github.com/en/repositories/releasing-projects-on-github/automatically-generated-release-notes) for more information |
| `append_body` | Boolean | Append to existing body instead of overwriting it |
| `update_tag` | Boolean | Update the tag of the release to the current commit. This will also update the release time. Default is false |

💡 When providing a `body` and `body_path` at the same time, `body_path` will be
attempted first, then falling back on `body` if the path can not be read from.
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Expand Up @@ -46,6 +46,9 @@ inputs:
append_body:
description: "Append to existing body instead of overwriting it. Default is false."
required: false
update_tag:
description: "Update the tag of the release to the current commit. This will also update the release time."
required: false
env:
"GITHUB_TOKEN": "As provided by Github Actions"
outputs:
Expand Down
4 changes: 2 additions & 2 deletions dist/index.js

Large diffs are not rendered by default.

195 changes: 128 additions & 67 deletions src/github.ts
Expand Up @@ -65,6 +65,19 @@ export interface Releaser {
owner: string;
repo: string;
}): AsyncIterableIterator<{ data: Release[] }>;

createRef(params: {
owner: string;
repo: string;
ref: string;
sha: string;
}) : Promise<any>;

deleteRef(params: {
owner: string;
repo: string;
ref: string;
}) : Promise<any>;
}

export class GitHubReleaser implements Releaser {
Expand Down Expand Up @@ -121,6 +134,23 @@ export class GitHubReleaser implements Releaser {
this.github.rest.repos.listReleases.endpoint.merge(updatedParams)
);
}

createRef(params: {
owner: string;
repo: string;
ref: string;
sha: string;
}) : Promise<any> {
return this.github.rest.git.createRef(params);
}

deleteRef(params: {
owner: string;
repo: string;
ref: string;
}) : Promise<any> {
return this.github.rest.git.deleteRef(params);
}
}

export const asset = (path: string): ReleaseAsset => {
Expand Down Expand Up @@ -198,48 +228,101 @@ export const release = async (

const discussion_category_name = config.input_discussion_category_name;
const generate_release_notes = config.input_generate_release_notes;
try {

if (config.input_draft) {
// you can't get a an existing draft by tag
// so we must find one in the list of all releases
if (config.input_draft) {
for await (const response of releaser.allReleases({
owner,
repo,
})) {
let release = response.data.find((release) => release.tag_name === tag);
if (release) {
return release;
}
for await (const response of releaser.allReleases({
owner,
repo,
})) {
let release = response.data.find((release) => release.tag_name === tag);
if (release) {
return release;
}
}
let existingRelease = await releaser.getReleaseByTag({
}


let existingRelease: Release|null = null;
try {
existingRelease = (await releaser.getReleaseByTag({
owner,
repo,
tag,
});

const release_id = existingRelease.data.id;
})).data;
console.log(`Found a release with tag ${tag} !`);
}catch(e){
if(e.status === 404){
console.log(`No release with tag ${tag} found`);
}else{
console.log(`An error occured while fetching the release for the tag ${tag} !`);
throw e;
}
}
if(existingRelease==null){
const tag_name = tag;
const name = config.input_name || tag;
const body = releaseBody(config);
const draft = config.input_draft;
const prerelease = config.input_prerelease;
const target_commitish = config.input_target_commitish;
let commitMessage: string = "";
if (target_commitish) {
commitMessage = ` using commit "${target_commitish}"`;
}
console.log(
`👩‍🏭 Creating new GitHub release for tag ${tag_name}${commitMessage}...`
);
try {
let release = await releaser.createRelease({
owner,
repo,
tag_name,
name,
body,
draft,
prerelease,
target_commitish,
discussion_category_name,
generate_release_notes,
});
return release.data;
} catch (error) {
// presume a race with competing metrix runs
console.log(
`⚠️ GitHub release failed with status: ${
error.status
}\n${JSON.stringify(error.response.data.errors)}\nretrying... (${
maxRetries - 1
} retries remaining)`
);
return release(config, releaser, maxRetries - 1);
}
}else{
console.log(`Updating release with tag ${tag}..`);
const release_id = existingRelease.id;
let target_commitish: string;
if (
config.input_target_commitish &&
config.input_target_commitish !== existingRelease.data.target_commitish
config.input_target_commitish !== existingRelease.target_commitish
) {
console.log(
`Updating commit from "${existingRelease.data.target_commitish}" to "${config.input_target_commitish}"`
`Updating commit from "${existingRelease.target_commitish}" to "${config.input_target_commitish}"`
);
target_commitish = config.input_target_commitish;
} else {
target_commitish = existingRelease.data.target_commitish;
target_commitish = existingRelease.target_commitish;
}

const tag_name = tag;
const name = config.input_name || existingRelease.data.name || tag;
const name = config.input_name || existingRelease.name || tag;
// revisit: support a new body-concat-strategy input for accumulating
// body parts as a release gets updated. some users will likely want this while
// others won't previously this was duplicating content for most which
// no one wants
const workflowBody = releaseBody(config) || "";
const existingReleaseBody = existingRelease.data.body || "";
const existingReleaseBody = existingRelease.body || "";
let body: string;
if (config.input_append_body && workflowBody && existingReleaseBody) {
body = existingReleaseBody + "\n" + workflowBody;
Expand All @@ -250,12 +333,32 @@ export const release = async (
const draft =
config.input_draft !== undefined
? config.input_draft
: existingRelease.data.draft;
: existingRelease.draft;
const prerelease =
config.input_prerelease !== undefined
? config.input_prerelease
: existingRelease.data.prerelease;
: existingRelease.prerelease;

if(config.input_update_tag){
await releaser.deleteRef({
owner,
repo,
ref: "tags/"+existingRelease.tag_name,
});
await releaser.createRef({
owner,
repo,
ref: "refs/tags/"+existingRelease.tag_name,
sha: config.github_sha
})

console.log(`Updated ref/tags/${existingRelease.tag_name} to ${config.github_sha}`);

// give github the time to draft the release before updating it
// Else, I think we would have a race condition with github to update the release
await sleep(2000);
}

const release = await releaser.updateRelease({
owner,
repo,
Expand All @@ -270,51 +373,9 @@ export const release = async (
generate_release_notes,
});
return release.data;
} catch (error) {
if (error.status === 404) {
const tag_name = tag;
const name = config.input_name || tag;
const body = releaseBody(config);
const draft = config.input_draft;
const prerelease = config.input_prerelease;
const target_commitish = config.input_target_commitish;
let commitMessage: string = "";
if (target_commitish) {
commitMessage = ` using commit "${target_commitish}"`;
}
console.log(
`👩‍🏭 Creating new GitHub release for tag ${tag_name}${commitMessage}...`
);
try {
let release = await releaser.createRelease({
owner,
repo,
tag_name,
name,
body,
draft,
prerelease,
target_commitish,
discussion_category_name,
generate_release_notes,
});
return release.data;
} catch (error) {
// presume a race with competing metrix runs
console.log(
`⚠️ GitHub release failed with status: ${
error.status
}\n${JSON.stringify(error.response.data.errors)}\nretrying... (${
maxRetries - 1
} retries remaining)`
);
return release(config, releaser, maxRetries - 1);
}
} else {
console.log(
`⚠️ Unexpected error fetching GitHub release for tag ${config.github_ref}: ${error}`
);
throw error;
}
}
};

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
4 changes: 4 additions & 0 deletions src/util.ts
Expand Up @@ -4,6 +4,7 @@ import { statSync, readFileSync } from "fs";
export interface Config {
github_token: string;
github_ref: string;
github_sha: string;
github_repository: string;
// user provided
input_name?: string;
Expand All @@ -19,6 +20,7 @@ export interface Config {
input_discussion_category_name?: string;
input_generate_release_notes?: boolean;
input_append_body?: boolean;
input_update_tag?: string;
}

export const uploadUrl = (url: string): string => {
Expand Down Expand Up @@ -54,6 +56,7 @@ export const parseConfig = (env: Env): Config => {
return {
github_token: env.GITHUB_TOKEN || env.INPUT_TOKEN || "",
github_ref: env.GITHUB_REF || "",
github_sha: env.GITHUB_SHA || "",
github_repository: env.INPUT_REPOSITORY || env.GITHUB_REPOSITORY || "",
input_name: env.INPUT_NAME,
input_tag_name: env.INPUT_TAG_NAME?.trim(),
Expand All @@ -70,6 +73,7 @@ export const parseConfig = (env: Env): Config => {
env.INPUT_DISCUSSION_CATEGORY_NAME || undefined,
input_generate_release_notes: env.INPUT_GENERATE_RELEASE_NOTES == "true",
input_append_body: env.INPUT_APPEND_BODY == "true",
input_update_tag: env.INPUT_UPDATE_TAG,
};
};

Expand Down