From 24ec13681d256d4dd0a09799fc23775d023839e7 Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Fri, 2 Aug 2024 23:52:10 +0800 Subject: [PATCH] fix: fix tagRef issue. --- src/index.ts | 37 +++++++------------------------------ src/utils.ts | 37 ++++++++++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/index.ts b/src/index.ts index b354ccd..2ffafbc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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(); @@ -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/')) { @@ -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); @@ -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')}`); diff --git a/src/utils.ts b/src/utils.ts index 30d2fc3..3b0b819 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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; + export const getVersion = (ver: string = '') => { let currentVersion = '' ver.replace(/([v|V]\d(\.\d+){0,2})/i, (str) => { @@ -45,7 +72,7 @@ export const parseCustomEmojis = (customEmoji: string, defaultTypes: Record { +export const handleNoBaseRef = async (options: ActionOptions) => { const { octokit, owner, repo } = options; const latestRelease = await octokit.rest.repos.getLatestRelease({ ...context.repo }); if (latestRelease.status !== 200) { @@ -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 }); @@ -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[]; @@ -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[]; @@ -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 = '';