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 2 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ The following are optional as `step.with` keys
| `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 |
| `make_latest` | String | Specifies whether this release should be set as the latest release for the repository. Drafts and prereleases cannot be set as latest. Can be `true`, `false`, or `legacy`. Uses GitHub api defaults if not provided |
| `update_tag` | Boolean | Update the tag of the release to the current commit. This will also update the release time. Default is false |
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some reservations about this. Action gh release is an action for managing GitHub releases, the product feature

the concern is that with these changes the scope is increasing to also include managing git state something that feels possible with git itself external to this action.

Would it be possible to do that as a separate workflow step or possibly trigger that then triggers the workflow that manages gh release data?


💡 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
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ inputs:
make_latest:
description: "Specifies whether this release should be set as the latest release for the repository. Drafts and prereleases cannot be set as latest. Can be `true`, `false`, or `legacy`. Uses GitHub api default if not provided"
required: false
update_tag:
description: "Update the tag of the release to the current commit. This will also update the release time."
env:
GITHUB_TOKEN: "As provided by Github Actions"
outputs:
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

102 changes: 95 additions & 7 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ 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>;

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

export class GitHubReleaser implements Releaser {
Expand Down Expand Up @@ -138,6 +157,31 @@ 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);
}

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

export const asset = (path: string): ReleaseAsset => {
Expand Down Expand Up @@ -224,18 +268,18 @@ export const release = async (
// so we must find one in the list of all releases
let _release: Release | undefined = undefined;
if (config.input_draft) {
for await (const response of releaser.allReleases({
owner,
repo,
})) {
for await (const response of releaser.allReleases({
owner,
repo,
})) {
_release = response.data.find((release) => release.tag_name === tag);
}
} else {
_release = (
await releaser.getReleaseByTag({
owner,
repo,
tag,
owner,
repo,
tag,
})
).data;
}
Expand Down Expand Up @@ -297,6 +341,29 @@ export const release = async (

const make_latest = config.input_make_latest;

if(config.input_update_tag){

let newCommitSha = await getTargetCommit(target_commitish, releaser, owner, repo);

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

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

// 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);
Copy link
Owner

@softprops softprops Jul 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is something inherently unreliable and inefficient about relying on time as a synchronization primitive

It’s possible that sleeping 2 seconds isn’t long enough resulting in a noneffective approach and it is also possible that is is way to long resulting in wasted gh action time.

Is it possible to restructure this without so that it’s not time dependent?

}

const release = await releaser.updateRelease({
owner,
repo,
Expand Down Expand Up @@ -397,3 +464,24 @@ async function createRelease(
return release(config, releaser, maxRetries - 1);
}
}

async function getTargetCommit(target_commitish: string, releaser: Releaser, owner: string, repo: string) : Promise<string> {
if (target_commitish.length == 40) { // sha1 size
return target_commitish;
} else {
// assume it is a branch
let resp = await releaser.getRef({
owner: owner,
repo: repo,
ref: "heads/"+target_commitish,
})
if (resp.status == 404) {
throw new Error(`Branch ${target_commitish} not found`);
}
return resp.data.object.sha;
}
}

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
2 changes: 2 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface Config {
input_generate_release_notes?: boolean;
input_append_body?: boolean;
input_make_latest: "true" | "false" | "legacy" | undefined;
input_update_tag?: string;
}

export const uploadUrl = (url: string): string => {
Expand Down Expand Up @@ -72,6 +73,7 @@ export const parseConfig = (env: Env): Config => {
input_generate_release_notes: env.INPUT_GENERATE_RELEASE_NOTES == "true",
input_append_body: env.INPUT_APPEND_BODY == "true",
input_make_latest: parseMakeLatest(env.INPUT_MAKE_LATEST),
input_update_tag: env.INPUT_UPDATE_TAG,
};
};

Expand Down