Skip to content

Commit

Permalink
feat: add remove-type input.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jul 17, 2023
1 parent 5257a75 commit f54db6b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 41 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
original-markdown: true
filter: '[R|r]elease[d]\s+[v|V]\d(\.\d+){0,2}'

- run: echo "outputs.tag - ${{ steps.changelog.outputs.changelog }}"
- run: echo "outputs.changelog - ${{ steps.changelog.outputs.changelog }}"
- run: echo "outputs.tag - ${{ steps.changelog.outputs.tag }}"
- run: echo "outputs.branch - ${{ steps.changelog.outputs.branch }}"
- run: echo "outputs.compareurl - ${{ steps.changelog.outputs.compareurl }}"
Expand All @@ -76,8 +76,10 @@ jobs:
with:
filter: '[R|r]elease[d]\s+[v|V]\d(\.\d+){0,2}'
show-emoji: false
remove-type: true

- run: echo "outputs.tag - ${{ steps.changelogEmoji.outputs.changelog }}"
- name: Generate Changelog (outputs.changelog)
run: echo "${{ steps.changelogEmoji.outputs.changelog }}"

- name: Generate Changelog(main)
uses: jaywcjlove/changelog-generator@main
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ inputs:
template:
description: 'Define the log display template'
required: false
remove-type:
description: 'Remove type in commit'
required: false
path:
description: 'Only commits containing this file path will be returned..'
default: ''
Expand Down
100 changes: 62 additions & 38 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,47 @@ import { getVersion, getCommitLog, defaultTypes, formatStringCommit, getRegExp }

const regexp = /^[.A-Za-z0-9_-]*$/;

const getOptions = () => {
const myToken = getInput('token');
return {
...context.repo,
headRef: getInput('head-ref'),
baseRef: getInput('base-ref'),
myToken,
myPath: getInput('path'),
template: getInput('template'),
/** @example `type🆎,chore💄,fix🐞` Use commas to separate */
customEmoji: getInput('custom-emoji') || '',
showEmoji: getInput('show-emoji') === 'false' ? false : true,
removeType: getInput('remove-type') === 'false' ? false : true,
filterAuthor: getInput('filter-author'),
regExp: getInput('filter'),
ghPagesBranch: getInput('gh-pages') || 'gh-pages',
originalMarkdown: getInput('original-markdown'),
octokit: getOctokit(myToken),
types: defaultTypes,
}
}


async function run() {
try {
var headRef = getInput('head-ref');
var baseRef = getInput('base-ref');
const myToken = getInput('token');
const myPath = getInput('path');
const template = getInput('template');
/** @example `type🆎,chore💄,fix🐞` Use commas to separate */
const customEmoji = getInput('custom-emoji') || '';
const showEmoji = getInput('show-emoji') === 'false' ? false : true;
const filterAuthor = getInput('filter-author');
const regExp = getInput('filter');
const ghPagesBranch = getInput('gh-pages') || 'gh-pages';
const originalMarkdown = getInput('original-markdown');
const { owner, repo } = context.repo;
const octokit = getOctokit(myToken);
const types = defaultTypes;
const options = getOptions();
const {
myPath,
template,
customEmoji,
removeType,
showEmoji,
filterAuthor,
regExp,
ghPagesBranch,
originalMarkdown,
owner,
repo,
octokit,
types,
} = options || {};

const customEmojiData = customEmoji.split(',')
if (customEmoji && customEmojiData.length) {
Expand All @@ -33,22 +57,22 @@ async function run() {
});
}

if (!baseRef) {
if (!options.baseRef) {
const latestRelease = await octokit.rest.repos.getLatestRelease({ ...context.repo });
if (latestRelease.status !== 200) {
setFailed(
`There are no releases on ${owner}/${repo}. Tags are not releases. (status=${latestRelease.status}) ${(latestRelease.data as any).message || ''}`
);
}
baseRef = latestRelease.data.tag_name;
options.baseRef = latestRelease.data.tag_name;
startGroup(
`Latest Release Result Data: \x1b[32m${latestRelease.status || '-'}\x1b[0m \x1b[32m${latestRelease.data.tag_name}\x1b[0m`
)
info(`${JSON.stringify(latestRelease, null, 2)}`)
endGroup()
}
if (!headRef) {
headRef = context.sha;
if (!options.headRef) {
options.headRef = context.sha;
}

info(`Commit Content: \x1b[34m${owner}/${repo}\x1b[0m`)
Expand All @@ -66,7 +90,7 @@ async function run() {
setOutput('branch', branch);
info(`Branch: \x1b[34m${branch}\x1b[0m`);
}
info(`Ref: baseRef(\x1b[32m${baseRef}\x1b[0m), headRef(\x1b[32m${headRef}\x1b[0m), tagRef(\x1b[32m${tagRef}\x1b[0m)`);
info(`Ref: baseRef(\x1b[32m${options.baseRef}\x1b[0m), options.headRef(\x1b[32m${options.headRef}\x1b[0m), tagRef(\x1b[32m${tagRef}\x1b[0m)`);

try {
const branchData = await octokit.request('GET /repos/{owner}/{repo}/branches', { ...context.repo });
Expand All @@ -87,18 +111,18 @@ async function run() {
}
}

if ((baseRef || '').replace(/^[vV]/, '') === headRef) {
setOutput('tag', baseRef);
setOutput('version', baseRef.replace(/^[vV]/, ''));
info(`Done: baseRef(\x1b[33m${baseRef}\x1b[0m) === headRef(\x1b[32m${headRef}\x1b[0m)`);
if ((options.baseRef || '').replace(/^[vV]/, '') === options.headRef) {
setOutput('tag', options.baseRef);
setOutput('version', options.baseRef.replace(/^[vV]/, ''));
info(`Done: baseRef(\x1b[33m${options.baseRef}\x1b[0m) === headRef(\x1b[32m${options.headRef}\x1b[0m)`);
return;
}

if (
!!headRef &&
!!baseRef &&
regexp.test(headRef) &&
regexp.test(baseRef)
!!options.headRef &&
!!options.baseRef &&
regexp.test(options.headRef) &&
regexp.test(options.baseRef)
) {
let resultData = [] as Commits[]
if (myPath) {
Expand All @@ -116,15 +140,15 @@ async function run() {
resultData = commitsData.data as unknown as Commits[];
}
startGroup(
`Compare Path Commits Result Data: \x1b[32m${commitsData.status || '-'}\x1b[0m \x1b[32m${baseRef}\x1b[0m...\x1b[32m${headRef}\x1b[0m`
`Compare Path Commits Result Data: \x1b[32m${commitsData.status || '-'}\x1b[0m \x1b[32m${options.baseRef}\x1b[0m...\x1b[32m${options.headRef}\x1b[0m`
)
info(`${JSON.stringify(commitsData.data, null, 2)}`)
endGroup()
} else {
const commitsData = await octokit.rest.repos.compareCommits({
...context.repo,
base: baseRef,
head: headRef,
base: options.baseRef,
head: options.headRef,
});

if (commitsData && commitsData.status !== 200) {
Expand All @@ -135,7 +159,7 @@ async function run() {
resultData = commitsData.data.commits as unknown as Commits[]
}
startGroup(
`Compare Commits Result Data: \x1b[32m${commitsData.status || '-'}\x1b[0m \x1b[32m${baseRef}\x1b[0m...\x1b[32m${headRef}\x1b[0m`
`Compare Commits Result Data: \x1b[32m${commitsData.status || '-'}\x1b[0m \x1b[32m${options.baseRef}\x1b[0m...\x1b[32m${options.headRef}\x1b[0m`
)
info(`${JSON.stringify(commitsData, null, 2)}`)
endGroup()
Expand Down Expand Up @@ -167,7 +191,7 @@ async function run() {
tagRef = listTags.data[0] && listTags.data[0].name ? listTags.data[0].name : '';
}

const { changelog, changelogContent } = getCommitLog(commitLog, { types, showEmoji, template });
const { changelog, changelogContent } = getCommitLog(commitLog, { types, showEmoji, removeType, template });
startGroup('Result Changelog');
info(`${changelog.join('\n')}`);
endGroup();
Expand All @@ -176,11 +200,11 @@ async function run() {
info(`Tag: \x1b[34m${tagRef}\x1b[0m`);
setOutput('tag', tagRef);

info(`Tag: \x1b[34m${tagRef || headRef || '-'}\x1b[0m`);
info(`Input head-ref: \x1b[34m${headRef}\x1b[0m`);
info(`Input base-ref: \x1b[34m${baseRef}\x1b[0m`);
setOutput('compareurl', `https://github.com/${owner}/${repo}/compare/${baseRef}...${tagRef || headRef}`);
setOutput('version', getVersion(tagRef || headRef || '').replace(/^v/, ''));
info(`Tag: \x1b[34m${tagRef || options.headRef || '-'}\x1b[0m`);
info(`Input head-ref: \x1b[34m${options.headRef}\x1b[0m`);
info(`Input base-ref: \x1b[34m${options.baseRef}\x1b[0m`);
setOutput('compareurl', `https://github.com/${owner}/${repo}/compare/${options.baseRef}...${tagRef || options.headRef}`);
setOutput('version', getVersion(tagRef || options.headRef || '').replace(/^v/, ''));
} else {
setFailed(
'Branch names must contain only numbers, strings, underscores, periods, and dashes.'
Expand Down
6 changes: 5 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ type Options = {
types: typeof defaultTypes;
category?: Partial<Record<keyof typeof defaultTypes, string[]>>;
showEmoji: boolean;
removeType: boolean;
template: string;
}

export function getCommitLog(log: string[], options = {} as Options) {
const { types, category = {}, showEmoji, template } = options;
const { types, category = {}, showEmoji, template, removeType = false } = options;
if (!Array.isArray(category['__unknown__'])) category['__unknown__'] = [];
log = log.map((commit) => {
(Object.keys(types || {}) as Array<keyof typeof defaultTypes>).forEach((name) => {
Expand All @@ -75,6 +76,9 @@ export function getCommitLog(log: string[], options = {} as Options) {
commit = showEmoji ? `- ${types['__unknown__']} ${commit}` : `- ${commit}`;
category['__unknown__']!.push(commit);
}
if (removeType) {
commit = commit.replace(/(^-\s+?\w+(\s+)?\((.*?)\):\s+)|(^-\s+?\w+(\s+)?:\s+)/, '- ');
}
return commit
}).filter(Boolean);

Expand Down

0 comments on commit f54db6b

Please sign in to comment.