Skip to content

Commit

Permalink
fix: fix tagRef issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Aug 2, 2024
1 parent 4e0b790 commit 24ec136
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 35 deletions.
37 changes: 7 additions & 30 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
import { getInput, setFailed, startGroup, info, endGroup, setOutput } from '@actions/core';
import { context, getOctokit, } from '@actions/github';
import { getVersion, getCommitLog, defaultTypes, handleBranchData, processCommits, parseCustomEmojis, getTagRef, fetchCommits, handleNoBaseRef } from './utils';
import { setFailed, startGroup, info, endGroup, setOutput } from '@actions/core';
import { context } from '@actions/github';
import { getVersion, getOptions, getCommitLog, handleBranchData, processCommits, getTagRef, fetchCommits, handleNoBaseRef } from './utils';

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'),
order: getInput('order') as 'asc' | 'desc',
template: getInput('template'),
/** @example `type🆎,chore💄,fix🐞` Use commas to separate */
customEmoji: getInput('custom-emoji') || '',
showEmoji: getInput('show-emoji') !== 'false',
removeType: getInput('remove-type') !== 'false',
filterAuthor: getInput('filter-author'),
regExp: getInput('filter'),
ghPagesBranch: getInput('gh-pages') || 'gh-pages',
originalMarkdown: getInput('original-markdown'),
octokit: getOctokit(myToken),
types: parseCustomEmojis(getInput('custom-emoji'), defaultTypes),
};
};

async function run() {
try {
const options = getOptions();
Expand All @@ -46,9 +23,8 @@ async function run() {
info(`${JSON.stringify(context, null, 2)}`);
endGroup();

let tagRef = '';
if ((context.ref || '').startsWith('refs/tags/')) {
tagRef = getVersion(context.ref);
options.tagRef = getVersion(context.ref);
}

if ((context.ref || '').startsWith('refs/heads/')) {
Expand All @@ -57,7 +33,7 @@ async function run() {
info(`Branch: \x1b[34m${branch}\x1b[0m`);
}

info(`Ref: baseRef(\x1b[32m${baseRef}\x1b[0m), options.headRef(\x1b[32m${headRef}\x1b[0m), tagRef(\x1b[32m${tagRef}\x1b[0m)`);
info(`Ref: baseRef(\x1b[32m${baseRef}\x1b[0m), options.headRef(\x1b[32m${headRef}\x1b[0m), tagRef(\x1b[32m${options.tagRef}\x1b[0m)`);

await handleBranchData(options);

Expand All @@ -71,7 +47,8 @@ async function run() {
if (regexp.test(headRef) && regexp.test(baseRef)) {
const resultData = await fetchCommits(options);
const commitLog = processCommits(resultData, options);
tagRef = await getTagRef(options);

const tagRef = await getTagRef(options);
const { changelog, changelogContent } = getCommitLog(commitLog, { types, showEmoji, removeType, template });
startGroup('Result Changelog');
info(`${changelog.join('\n')}`);
Expand Down
37 changes: 32 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
import { getInput, setFailed, startGroup, info, endGroup, setOutput } from '@actions/core';
import { context, getOctokit } from '@actions/github';


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

export type ActionOptions = ReturnType<typeof getOptions>;

export const getVersion = (ver: string = '') => {
let currentVersion = ''
ver.replace(/([v|V]\d(\.\d+){0,2})/i, (str) => {
Expand Down Expand Up @@ -45,7 +72,7 @@ export const parseCustomEmojis = (customEmoji: string, defaultTypes: Record<stri
};


export const handleNoBaseRef = async (options: any) => {
export const handleNoBaseRef = async (options: ActionOptions) => {
const { octokit, owner, repo } = options;
const latestRelease = await octokit.rest.repos.getLatestRelease({ ...context.repo });
if (latestRelease.status !== 200) {
Expand All @@ -57,7 +84,7 @@ export const handleNoBaseRef = async (options: any) => {
endGroup();
};

export const handleBranchData = async (options: any) => {
export const handleBranchData = async (options: ActionOptions) => {
const { octokit, ghPagesBranch } = options;
try {
const branchData = await octokit.request('GET /repos/{owner}/{repo}/branches', { ...context.repo });
Expand All @@ -77,7 +104,7 @@ export const handleBranchData = async (options: any) => {
}
};

export const fetchCommits = async (options: any) => {
export const fetchCommits = async (options: ActionOptions) => {
const { octokit, myPath, baseRef, headRef } = options;
let resultData = [] as any[];

Expand Down Expand Up @@ -115,7 +142,7 @@ export const fetchCommits = async (options: any) => {
return resultData;
};

export const processCommits = (resultData: any[], options: any) => {
export const processCommits = (resultData: any[], options: ActionOptions) => {
const { order, owner, repo, originalMarkdown, regExp, filterAuthor, types } = options;
let commitLog = [] as string[];

Expand All @@ -139,7 +166,7 @@ export const processCommits = (resultData: any[], options: any) => {
return order === 'asc' ? commitLog : commitLog.reverse();
};

export const getTagRef = async (options: any) => {
export const getTagRef = async (options: ActionOptions) => {
const { octokit, owner, repo, headRef } = options;
let tagRef = '';

Expand Down

0 comments on commit 24ec136

Please sign in to comment.