Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | import { getInput, getBooleanInput, setFailed, startGroup, info, endGroup, setOutput } from '@actions/core';
import { context, getOctokit } from '@actions/github';
import { parseCustomEmojis } from './utils';
import { defaultTypes, formatStringCommit } from './utils';
export type ActionOptions = ReturnType<typeof getOptions>;
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: getBooleanInput('show-emoji') !== false,
removeType: getBooleanInput('remove-type') !== false,
filterAuthor: getInput('filter-author'),
regExp: getInput('filter'),
ghPagesBranch: getInput('gh-pages') || 'gh-pages',
originalMarkdown: getBooleanInput('original-markdown'),
octokit: getOctokit(myToken),
types: parseCustomEmojis(getInput('custom-emoji'), defaultTypes) as typeof defaultTypes,
tagRef: '',
};
};
export const getTagRef = async (options: ActionOptions) => {
const { octokit, owner, repo, headRef } = options;
let tagRef = '';
if (!options.tagRef) {
const listTags = await octokit.rest.repos.listTags({ owner, repo });
if (listTags.status !== 200) {
setFailed(`Failed to get tag lists (status=${listTags.status})`);
return '';
}
tagRef = listTags.data[0]?.name || '';
} else {
tagRef = options.tagRef;
}
return tagRef;
};
export const handleNoBaseRef = async (options: ActionOptions) => {
const { octokit, owner, repo } = options;
// Get the latest release
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 || ''}`);
} else {
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();
return latestRelease.data.tag_name;
}
};
export const handleBranchData = async (options: ActionOptions) => {
const { octokit, ghPagesBranch } = options;
try {
const branchData = await octokit.request('GET /repos/{owner}/{repo}/branches', { ...context.repo });
const ghPagesData = branchData.data.find((item: any) => item.name === ghPagesBranch);
startGroup(`\x1b[34mGet Branch \x1b[0m`);
info(`Branch Data: ${JSON.stringify(branchData.data, null, 2)}`);
if (ghPagesData) {
info(`ghPages Data: ${ghPagesBranch}, ${ghPagesData.commit.sha}, ${JSON.stringify(ghPagesData, null, 2)}`);
setOutput('gh-pages-hash', ghPagesData.commit.sha);
setOutput('gh-pages-short-hash', ghPagesData.commit.sha.substring(0, 7));
}
endGroup();
} catch (error) {
if (error instanceof Error) {
info(`Get Branch: \x1b[33m${error.message}\x1b[0m`);
}
}
};
export const fetchCommits = async (options: ActionOptions) => {
const { octokit, myPath, baseRef, headRef } = options;
let resultData = [] as any[];
if (myPath) {
info(`path: ${myPath}`);
const commitsData = await octokit.request('GET /repos/{owner}/{repo}/commits', {
...context.repo,
path: myPath,
});
if (commitsData && commitsData.status !== 200) {
setFailed(`There are no releases on ${options.owner}/${options.repo}. Tags are not releases. (status=${commitsData.status}) ${(commitsData.data as any).message || ''}`);
} else {
resultData = commitsData.data;
}
startGroup(`Compare Path Commits Result Data: \x1b[32m${commitsData.status || '-'}\x1b[0m \x1b[32m${baseRef}\x1b[0m...\x1b[32m${headRef}\x1b[0m`);
info(`${JSON.stringify(commitsData.data, null, 2)}`);
endGroup();
} else {
const commitsData = await octokit.rest.repos.compareCommitsWithBasehead({
...context.repo,
basehead: `${baseRef}...${headRef}`,
});
if (commitsData && commitsData.status !== 200) {
setFailed(`There are no releases on ${options.owner}/${options.repo}. Tags are not releases. (status=${commitsData.status}) ${(commitsData.data as any).message || ''}`);
} else {
resultData = commitsData.data.commits;
}
startGroup(`Compare Commits Result Data: \x1b[32m${commitsData.status || '-'}\x1b[0m \x1b[32m${baseRef}\x1b[0m...\x1b[32m${headRef}\x1b[0m`);
info(`${JSON.stringify(commitsData, null, 2)}`);
endGroup();
}
return resultData;
};
export const processCommits = (resultData: any[], options: ActionOptions) => {
const { order, owner, repo, originalMarkdown, regExp, filterAuthor, types } = options;
let commitLog = [] as string[];
info(`ResultData Length: ${resultData.length} - ${order}`);
resultData.forEach((data) => {
const message = data.commit.message.split('\n\n')[0];
const author = data.author || data.committer || { login: '-' };
startGroup(`Commit: \x1b[34m${message}\x1b[0m \x1b[34m${(data.commit.author || {}).name}(${author.login})\x1b[0m ${data.sha}`);
info(`${JSON.stringify(data, null, 2)}`);
endGroup();
commitLog.push(formatStringCommit(message, `${owner}/${repo}`, {
originalMarkdown,
regExp,
shortHash: data.sha.slice(0, 7),
filterAuthor,
hash: data.sha,
login: author.login,
}));
});
return order === 'asc' ? commitLog : commitLog.reverse();
};
|