Skip to content

Commit

Permalink
fix: changelog links
Browse files Browse the repository at this point in the history
  • Loading branch information
sylc committed Jun 2, 2022
1 parent 7444342 commit dde99a9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion plugins/changelog/mod.ts
Expand Up @@ -42,7 +42,7 @@ const plugin: ReleasePlugin = {
for (let i = 0; i < tags.length; i++) {
const tag = tags[i];
const belonging = commits.filter((_) => _.belongs?.hash === tag.hash);
pushTag(doc, belonging, filters, tag);
pushTag(doc, repo, belonging, filters, tag, "md");
}

const md = render(doc);
Expand Down
2 changes: 1 addition & 1 deletion plugins/github/mod.ts
Expand Up @@ -65,7 +65,7 @@ const plugin: ReleasePlugin<GithubConfig> = {

const latest = tags[0];
const belonging = commits.filter((_) => _.belongs?.hash === latest.hash);
pushTag(doc, belonging, filters, latest);
pushTag(doc, repo, belonging, filters, latest, "github");

if (!config.options.dry && config.github.release) {
const token = Deno.env.get(GITHUB_TOKEN)!;
Expand Down
35 changes: 31 additions & 4 deletions src/changelog.ts
Expand Up @@ -12,6 +12,7 @@ export interface Document {
links: string[];
}

// links definition for markdown (they are not inline)
export function fmtLink(name: string, to: string): string {
return `[${name}]: ${to}`;
}
Expand All @@ -24,37 +25,63 @@ All notable changes to this project will be documented in this file.`);

export function pushChanges(
doc: Document,
repo: Repo,
title: string,
commits: Commit[],
style: "github" | "md",
): void {
doc.sections.push(`### ${title}`);
const list: string[] = [];
for (const commit of commits) {
const { hash } = commit;
const { subject } = commit.cc;
const { header } = commit.cc;
const shortid = `\`${hash.slice(0, 7)}\``;
console.log("sub", header);
if (repo.remote && repo.remote.github && style === "md") {
const { user, name } = repo.remote.github;
let url = `https://github.com/${user}/${name}/`;
url = `${url}commit/${hash}`;

list.push(`- ${subject} (${shortid})`);
list.push(`- ${header} ([${shortid}])`);
doc.links.push(fmtLink(shortid, url));
} else {
// on github release we do not need to use url
list.push(`- ${header} (${shortid})`);
}
}
doc.sections.push(list.join("\n"));
}

export function pushTag(
doc: Document,
repo: Repo,
commits: Commit[],
filters: Filter[],
tag: Tag,
style: "github" | "md",
parent?: Tag,
): void {
const year = tag.date.getUTCFullYear();
const month = String(tag.date.getUTCMonth() + 1).padStart(2, "0");
const day = String(tag.date.getUTCDate()).padStart(2, "0");

doc.sections.push(`## [${tag.version}] - ${year}-${month}-${day}`);
if (repo.remote && repo.remote.github && style === "md") {
const { user, name } = repo.remote.github;
let url = `https://github.com/${user}/${name}/`;

url = parent
? `${url}compare/${parent.version}...${tag.version}`
: `${url}compare/${tag.version}`;
doc.links.push(fmtLink(tag.version, url));
doc.sections.push(`## [${tag.version}] - ${year}-${month}-${day}`);
} else {
doc.sections.push(`## ${tag.version} - ${year}-${month}-${day}`);
}

for (const filter of filters) {
const filtered = commits.filter((_) => _.cc.type === filter.type);
if (filtered.length > 0) {
pushChanges(doc, filter.title, filtered);
pushChanges(doc, repo, filter.title, filtered, style);
}
}
}
Expand Down

0 comments on commit dde99a9

Please sign in to comment.