Skip to content

Commit

Permalink
feat: add tag match pattern (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
srheaume authored Aug 13, 2024
1 parent 94f78b0 commit c5d854f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 27 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ variety of programming languages and conventions.
| ignore-files | null | Regex list to ignore files (comma separated) e.g. '/\.txt$/, /\.doc$/' |
| branch-fallback | 'main' | Fallback if no branch_default could be found |
| tag-fallback | null | Fallback if no tag could be found |
| tag-match-pattern | null | Pattern, using glob(7) syntax, to match tags in the repository |
| fallback-commit-type | '' | Fallback for commits without type (Conventional Commits) |
| fallback-commit-scope | '' | Fallback for commits without scope (Conventional Commits) |
| commit-msg-with-footer | false | Include footer from commit messages (Conventional Commits) |
Expand Down
14 changes: 9 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ try {
let ignoreFilesStr = core.getInput('ignore-files') || null;
let branchFallback = core.getInput('branch-fallback') || null;
let tagFallback = core.getInput('tag-fallback') || null;
let tagMatchPattern = core.getInput('tag-match-pattern') || null;
let fallbackCommitType = core.getInput('fallback-commit-type') || null;
let fallbackCommitScope = core.getInput('fallback-commit-scope') || null;
let commitMsgWithFooter = core.getInput('commit-msg-with-footer') || null;
Expand All @@ -553,7 +554,7 @@ try {
workDir = getWorkingDirectory(workspace);
}
let ignoreFiles = (0, common_processing_1.isEmpty)(ignoreFilesStr) ? new Set() : ignoreFilesStr.split(',');
let result = run(github.context, workDir, ignoreFiles, branchFallback, tagFallback, !(0, common_processing_1.isEmpty)(fallbackCommitType) ? fallbackCommitType : "", !(0, common_processing_1.isEmpty)(fallbackCommitScope) ? fallbackCommitScope : "", !(0, common_processing_1.isEmpty)(commitMsgWithFooter) ? commitMsgWithFooter.toLowerCase() === 'true' : false, !(0, common_processing_1.isEmpty)(maxChangeLogLength) ? parseInt(maxChangeLogLength) || -1 : -1, !(0, common_processing_1.isEmpty)(nullToEmpty) ? nullToEmpty.toLowerCase() === 'true' : true);
let result = run(github.context, workDir, ignoreFiles, branchFallback, tagFallback, tagMatchPattern, !(0, common_processing_1.isEmpty)(fallbackCommitType) ? fallbackCommitType : "", !(0, common_processing_1.isEmpty)(fallbackCommitScope) ? fallbackCommitScope : "", !(0, common_processing_1.isEmpty)(commitMsgWithFooter) ? commitMsgWithFooter.toLowerCase() === 'true' : false, !(0, common_processing_1.isEmpty)(maxChangeLogLength) ? parseInt(maxChangeLogLength) || -1 : -1, !(0, common_processing_1.isEmpty)(nullToEmpty) ? nullToEmpty.toLowerCase() === 'true' : true);
result.set('GITHUB_WORKSPACE', workspace || null);
console.log(JSON.stringify(Object.fromEntries((0, common_processing_1.sortMap)(result)), null, 4));
result.forEach((value, key) => {
Expand All @@ -568,7 +569,7 @@ catch (e) {
core.setFailed(e.message);
}
}
function run(context, workDir, ignoreFiles, branchFallback, tagFallback, fallbackCommitType, fallbackCommitScope, commitMsgWithFooter, maxChangelogLength, nullToEmpty) {
function run(context, workDir, ignoreFiles, branchFallback, tagFallback, tagMatchPattern, fallbackCommitType, fallbackCommitScope, commitMsgWithFooter, maxChangelogLength, nullToEmpty) {
var _a, _b;
//DEFAULTS
let result = new Map();
Expand All @@ -578,6 +579,7 @@ function run(context, workDir, ignoreFiles, branchFallback, tagFallback, fallbac
result.set('ignore-files', Array.from(ignoreFiles).join(", ") || null);
result.set('branch-fallback', branchFallback);
result.set('tag-fallback', tagFallback);
result.set('tag-match-pattern', tagMatchPattern);
result.set('fallback-commit-type', fallbackCommitType);
result.set('fallback-commit-scope', fallbackCommitScope);
result.set('commit-msg-with-footer', commitMsgWithFooter);
Expand All @@ -593,7 +595,7 @@ function run(context, workDir, ignoreFiles, branchFallback, tagFallback, fallbac
result.set('branch_default', ((_b = context === null || context === void 0 ? void 0 : context.repository) === null || _b === void 0 ? void 0 : _b.default_branch) || getDefaultBranch(workDir, branchFallback));
result.set('is_default_branch', result.get('branch') === result.get('branch_default') && result.get('branch') !== null);
result.set('sha_latest', (0, common_processing_1.cmd)(workDir, 'git rev-parse HEAD'));
result = setLatestTag(workDir, result, tagFallback);
result = setLatestTag(workDir, result, tagFallback, tagMatchPattern);
result.set('has_changes', result.get('sha_latest') !== result.get('sha_latest_tag'));
addChanges(ignoreFiles, workDir, result);
addAheadBehind(workDir, result);
Expand Down Expand Up @@ -697,8 +699,10 @@ function hasFileEnding(fileNames, fileEndings) {
return fileEndings.some(ending => fileName.toLowerCase().endsWith(ending.toLowerCase()));
});
}
function setLatestTag(workDir, result, tagFallback) {
let latestTag = (0, common_processing_1.cmd)(workDir, 'git describe --tags --abbrev=0');
function setLatestTag(workDir, result, tagFallback, tagMatchPattern) {
const baseCommand = 'git describe --tags --abbrev=0';
const command = tagMatchPattern ? `${baseCommand} --match ${tagMatchPattern}` : baseCommand;
let latestTag = (0, common_processing_1.cmd)(workDir, command);
if (!(0, common_processing_1.isEmpty)(latestTag)) {
result.set('tag_latest', latestTag);
result.set('sha_latest_tag', (0, common_processing_1.cmd)(workDir, 'git rev-list -n 1 ' + latestTag));
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ try {
let ignoreFilesStr = core.getInput('ignore-files') || null;
let branchFallback = core.getInput('branch-fallback') || null;
let tagFallback = core.getInput('tag-fallback') || null;
let tagMatchPattern = core.getInput('tag-match-pattern') || null;
let fallbackCommitType = core.getInput('fallback-commit-type') || null;
let fallbackCommitScope = core.getInput('fallback-commit-scope') || null;
let commitMsgWithFooter = core.getInput('commit-msg-with-footer') || null;
Expand All @@ -77,6 +78,7 @@ try {
ignoreFiles,
branchFallback,
tagFallback,
tagMatchPattern,
!isEmpty(fallbackCommitType) ? fallbackCommitType : "",
!isEmpty(fallbackCommitScope) ? fallbackCommitScope : "",
!isEmpty(commitMsgWithFooter) ? commitMsgWithFooter.toLowerCase() === 'true' : false,
Expand Down Expand Up @@ -104,6 +106,7 @@ function run(
ignoreFiles: Set<string>,
branchFallback: string,
tagFallback: string,
tagMatchPattern: string,
fallbackCommitType: string,
fallbackCommitScope: string,
commitMsgWithFooter: boolean,
Expand All @@ -118,6 +121,7 @@ function run(
result.set('ignore-files', Array.from(ignoreFiles).join(", ") || null);
result.set('branch-fallback', branchFallback);
result.set('tag-fallback', tagFallback);
result.set('tag-match-pattern', tagMatchPattern);
result.set('fallback-commit-type', fallbackCommitType);
result.set('fallback-commit-scope', fallbackCommitScope);
result.set('commit-msg-with-footer', commitMsgWithFooter);
Expand All @@ -134,7 +138,7 @@ function run(
result.set('branch_default', context?.repository?.default_branch || getDefaultBranch(workDir, branchFallback));
result.set('is_default_branch', result.get('branch') === result.get('branch_default') && result.get('branch') !== null);
result.set('sha_latest', cmd(workDir, 'git rev-parse HEAD'));
result = setLatestTag(workDir, result, tagFallback);
result = setLatestTag(workDir, result, tagFallback, tagMatchPattern);
result.set('has_changes', result.get('sha_latest') !== result.get('sha_latest_tag'));

addChanges(ignoreFiles, workDir, result);
Expand All @@ -146,7 +150,6 @@ function run(
return sortMap(nullToEmpty ? replaceNullWithEmptyMap(result) : result);
}


function addAheadBehind(workDir: PathOrFileDescriptor | number, result: Map<string, ResultType>) {
let aheadBehind = cmd(workDir, 'git rev-list --count --left-right ' + result.get('branch') + '...' + result.get('branch_default'));
let ahead = isEmpty(aheadBehind) ? null : aheadBehind?.split(/\s/)[0].trim();
Expand Down Expand Up @@ -247,8 +250,11 @@ function hasFileEnding(fileNames: Set<string>, fileEndings: string[]): boolean {
});
}

function setLatestTag(workDir: PathOrFileDescriptor, result: Map<string, ResultType>, tagFallback: string): Map<string, ResultType> {
let latestTag = cmd(workDir, 'git describe --tags --abbrev=0');
function setLatestTag(workDir: PathOrFileDescriptor, result: Map<string, ResultType>, tagFallback: string, tagMatchPattern?: string | null): Map<string, ResultType> {
const baseCommand = 'git describe --tags --abbrev=0';
const command = tagMatchPattern ? `${baseCommand} --match ${tagMatchPattern}` : baseCommand;

let latestTag = cmd(workDir, command);
if (!isEmpty(latestTag)) {
result.set('tag_latest', latestTag);
result.set('sha_latest_tag', cmd(workDir, 'git rev-list -n 1 ' + latestTag));
Expand Down
Loading

0 comments on commit c5d854f

Please sign in to comment.