ci(reward): keep reward tags off Latest and out of git describe by FenjuFu · Pull Request #920 · iflytek/skillhub · GitHub
Skip to content
Merged
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
125 changes: 125 additions & 0 deletions .github/scripts/count-reward.test.ts
29 changes: 25 additions & 4 deletions .github/scripts/count-reward.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
import { $, YAML } from "npm:zx";

import { createDetachedCommit } from "./git.ts";
import { Reward } from "./type.ts";

$.verbose = true;

const tagName = `statistic-${new Date().toJSON().slice(0, 7)}`;
const existingTag = await $`git show-ref --verify --quiet ${`refs/tags/${tagName}`}`.nothrow();
if (existingTag.exitCode === 0) {
const release = await $`gh api ${`repos/{owner}/{repo}/releases/tags/${tagName}`} --include`.nothrow();
if (release.exitCode === 0) process.exit(0);
if (!/^HTTP\/[\d.]+ 404\b/m.test(release.stdout)) {
throw new Error(`Could not check the existing statistic release: ${release.stderr}`);
}

// Recover from a previous push that succeeded before release creation failed.
// The published tag is the source of truth, not a new calculation of rewards.
const tagType = (await $`git cat-file -t ${`refs/tags/${tagName}`}`).stdout.trim();
if (tagType !== "tag") throw new Error("The existing statistic tag is not annotated.");
const notes = (await $`git tag -l --format="%(contents)" ${tagName}`).stdout.trimEnd();
if (!notes.trim()) throw new Error("The existing statistic tag has no summary.");
await $`gh release create ${tagName} --verify-tag --latest=false --notes ${notes}`;
process.exit(0);
}
if (existingTag.exitCode !== 1) throw new Error("Could not check the statistic tag.");

const rawTags =
await $`git tag --list "reward-*" --format="%(refname:short) %(creatordate:short)"`;

Expand Down Expand Up @@ -52,15 +73,15 @@ const summaryText = YAML.stringify(summaryList);

console.log(summaryText);

const tagName = `statistic-${new Date().toJSON().slice(0, 7)}`;

await $`git config user.name "github-actions[bot]"`;
await $`git config user.email "github-actions[bot]@users.noreply.github.com"`;

await $`git tag -a ${tagName} $(git rev-parse HEAD) -m ${summaryText}`;
const tagTarget = await createDetachedCommit(tagName);
await $`git tag -a ${tagName} ${tagTarget} -m ${summaryText}`;
await $`git push origin --tags --no-verify`;

await $`git config unset user.name`;
await $`git config unset user.email`;

await $`gh release create ${tagName} --notes ${summaryText}`;
// Keep "Latest" pointing at the newest product release
await $`gh release create ${tagName} --latest=false --notes ${summaryText}`;
15 changes: 15 additions & 0 deletions .github/scripts/git.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { $ } from "npm:zx";

/**
* Create a parentless commit with an empty tree to carry reward/statistic tags.
*
* Tags pointing at product commits become the nearest tag in `git describe`,
* so downstream builds would report versions like `reward-42-3-gabc1234`
* instead of the latest release tag. Tags on a detached commit are never
* reachable from any branch and keep release tags authoritative.
*/
export async function createDetachedCommit(message: string) {
const emptyTree = (await $`git hash-object -w -t tree /dev/null`).stdout.trim();

return (await $`git commit-tree ${emptyTree} -m ${message}`).stdout.trim();
}
6 changes: 5 additions & 1 deletion .github/scripts/share-reward.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import "npm:array-unique-proposal";
import { components } from "npm:@octokit/openapi-types";
import { $, argv, YAML } from "npm:zx";

import { createDetachedCommit } from "./git.ts";
import { Reward } from "./type.ts";

$.verbose = true;
Expand Down Expand Up @@ -110,7 +111,10 @@ console.log(listText);
await $`git config user.name "github-actions[bot]"`;
await $`git config user.email "github-actions[bot]@users.noreply.github.com"`;

await $`git tag -a "reward-${issueNumber}" ${mergeCommitSha} -m ${listText}`;
const tagTarget = await createDetachedCommit(
`reward-${issueNumber}: merged in ${mergeCommitSha}`,
);
await $`git tag -a "reward-${issueNumber}" ${tagTarget} -m ${listText}`;
await $`git push origin --tags --no-verify`;

await $`git config unset user.name`;
Expand Down
20 changes: 1 addition & 19 deletions .github/workflows/statistic-member-reward.yml
Loading