Skip to content

Commit

Permalink
fix: release log with previous tag with sorting by version
Browse files Browse the repository at this point in the history
  • Loading branch information
syi0808 committed Oct 12, 2024
1 parent aa2c0f2 commit b470fe3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
25 changes: 25 additions & 0 deletions src/git.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import semver from 'semver';
import { exec } from 'tinyexec';
import { AbstractError } from './error.js';

Expand Down Expand Up @@ -32,6 +33,30 @@ export class Git {
}
}

async tags() {
try {
return (await this.git(['tag', '-l']))
.trim()
.split('\n')
.map((v) => v.slice(1))
.sort(semver.compareIdentifiers);
} catch (error) {
throw new GitError('Failed to run `git config --get user.name`', {
cause: error,
});
}
}

async previousTag(tag: string) {
try {
const tags = await this.tags();

return tags.at(tags.findIndex((t) => t === tag) - 1);
} catch {
return null;
}
}

async dryFetch() {
try {
return await this.git(['fetch', '--dry-run']);
Expand Down
16 changes: 8 additions & 8 deletions src/tasks/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,15 @@ export interface Ctx extends ResolvedOptions {
promptEnabled: boolean;
npmOnly: boolean;
jsrOnly: boolean;
lastRev: string;
cleanWorkingTree: boolean;
}

export async function run(options: ResolvedOptions) {
const git = new Git();

const ctx = <Ctx>{
...options,
promptEnabled: !isCI && process.stdin.isTTY,
npmOnly: options.registries.every((registry) => registry !== 'jsr'),
jsrOnly: options.registries.every((registry) => registry === 'jsr'),
lastRev: (await git.latestTag()) || (await git.firstCommit()),
};

try {
Expand Down Expand Up @@ -196,20 +192,24 @@ export async function run(options: ResolvedOptions) {

const repositoryUrl = await git.repository();

const latestTag = `${await git.latestTag()}`;

const lastRev =
(await git.previousTag(latestTag)) ||
(await git.firstCommit());

const commits = (
await git.commits(ctx.lastRev, `${await git.latestTag()}`)
await git.commits(lastRev, `${latestTag}`)
).slice(1);

const latestTag = await git.latestTag();

let body = commits
.map(
({ id, message }) =>
`- ${message.replace('#', `${repositoryUrl}/issues/`)} ${repositoryUrl}/commit/${id}`,
)
.join('\n');

body += `\n\n${repositoryUrl}/compare/${ctx.lastRev}...${latestTag}`;
body += `\n\n${repositoryUrl}/compare/${lastRev}...${latestTag}`;

const releaseDraftUrl = new URL(
`${repositoryUrl}/releases/new`,
Expand Down

0 comments on commit b470fe3

Please sign in to comment.