Skip to content

Commit

Permalink
feat!: Do not filter commits by defaults (#23)
Browse files Browse the repository at this point in the history
* do not filter commits by defaults

* x

* allow ! in commit expression

* fmt

* lint

* x
  • Loading branch information
sylc committed Mar 6, 2023
1 parent 40a8238 commit e7caf2d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Most changes are optionals and configurable.
# Installation

```
$ deno install -A -f --no-check -n release_up https://deno.land/x/release_up@0.6.0/cli.ts
$ deno install -A -f -n release_up https://deno.land/x/release_up@0.6.0/cli.ts
```

# Usage
Expand Down
10 changes: 3 additions & 7 deletions plugins/changelog/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { join } from "./deps.ts";
import type { ReleasePlugin } from "../../plugin.ts";
import {
Document,
filters,
polyfillVersion,
pushHeader,
pushTag,
render,
significantCommits,
} from "../../src/changelog.ts";

const plugin: ReleasePlugin = {
Expand All @@ -29,12 +29,8 @@ const plugin: ReleasePlugin = {
const tag = tags[i];
const parent = i < tags.length - 1 ? tags[i + 1] : undefined;
const belonging = commits.filter((_) => _.belongs?.hash === tag.hash);
const filteredTypes = filters.map((f) => f.type);
const filteredCommits = belonging.filter((_) =>
filteredTypes.includes(_.cc.type || "")
);
if (filteredCommits.length) {
pushTag(doc, repo, belonging, filters, tag, "md", parent);
if (belonging.length) {
pushTag(doc, repo, belonging, significantCommits, tag, "md", parent);
}
}

Expand Down
4 changes: 2 additions & 2 deletions plugins/github/mod.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ReleasePlugin } from "../../plugin.ts";
import {
Document,
filters,
polyfillVersion,
pushTag,
render,
significantCommits,
} from "../../src/changelog.ts";

import * as gh from "./api.ts";
Expand Down Expand Up @@ -53,7 +53,7 @@ const plugin: ReleasePlugin<GithubConfig> = {
const latest = tags[0];
const belonging = commits.filter((_) => _.belongs?.hash === latest.hash);
const parent = tags.length > 0 ? tags[1] : undefined;
pushTag(doc, repo, belonging, filters, latest, "github", parent);
pushTag(doc, repo, belonging, significantCommits, latest, "github", parent);

if (!config.options.dry) {
const token = Deno.env.get(GITHUB_TOKEN)!;
Expand Down
63 changes: 44 additions & 19 deletions src/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ export interface Filter {
title: string;
}

export const filters: Filter[] = [
{
type: "breaking",
title: "Breaking",
},
/**
* Default list of commit type
*/
export const significantCommits: Filter[] = [
{
type: "feat",
title: "Features",
Expand All @@ -20,14 +19,6 @@ export const filters: Filter[] = [
type: "fix",
title: "Bug Fixes",
},
{
type: "docs",
title: "Docs",
},
{
type: "core",
title: "Core",
},
];

export interface Document {
Expand All @@ -46,14 +37,19 @@ export function pushHeader(doc: Document): void {
All notable changes to this project will be documented in this file.`);
}

/**
* Push a list of commits in the section with a title
* @param title tile of the section in the document
* @param style Style formatting of the document. Github releases have links different from the changelog
*/
export function pushChanges(
doc: Document,
repo: Repo,
title: string,
commits: Commit[],
style: "github" | "md",
): void {
doc.sections.push(`### ${title}`);
if (title !== "") doc.sections.push(`### ${title}`);
const list: string[] = [];
for (const commit of commits) {
const { hash } = commit;
Expand All @@ -79,7 +75,8 @@ export function pushTag(
doc: Document,
repo: Repo,
commits: Commit[],
filters: Filter[],
// TODO: remove this parameter
_filters: Filter[],
tag: Tag,
style: "github" | "md",
parent?: Tag,
Expand All @@ -100,12 +97,40 @@ export function pushTag(
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, repo, filter.title, filtered, style);
const breaking: Commit[] = [];
const sections: { [key: string]: Commit[] } = {};
const others: Commit[] = [];

let hasConventionalCommit = false;
for (const commit of commits) {
const type = commit.cc.type;
if (type && commit.cc.header?.includes(`!:`)) {
breaking.push(commit);
hasConventionalCommit = true;
} else if (type && significantCommits.map((t) => t.type).includes(type)) {
if (!sections[type]) sections[type] = [];
sections[type].push(commit);
hasConventionalCommit = true;
} else {
others.push(commit);
}
}

if (breaking.length) pushChanges(doc, repo, "Breaking", breaking, style);
for (const significantCommit of significantCommits) {
if (sections[significantCommit.type]?.length) {
pushChanges(
doc,
repo,
significantCommit.title,
sections[significantCommit.type],
style,
);
}
}
const othersTitle = hasConventionalCommit ? "Others" : "";
if (others.length) pushChanges(doc, repo, othersTitle, others, style);

if (repo.remote && repo.remote.github && parent) {
const linkName = `${parent.version}...${tag.version}`;
doc.sections.push(`Full Changelog: [${linkName}]`);
Expand Down
7 changes: 6 additions & 1 deletion src/commits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ export async function fetchRawCommits(
const author = details[2];

const body = `${title}\n${description}`;
const cc = ccparse(body);
const cc = ccparse(body, {
// Allow for ! after the scope
headerPattern: new RegExp(
/^(\w*)(?:\(([\w\$\.\-\* ]*)\))?(?:\:|!\:) (.*)$/,
),
});

return {
hash,
Expand Down

0 comments on commit e7caf2d

Please sign in to comment.