-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.ts
69 lines (58 loc) · 2.67 KB
/
validator.ts
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
/*
* SPDX-FileCopyrightText: 2023 Kevin de Jong <monkaii@hotmail.com>
* SPDX-License-Identifier: MIT
*/
import { Commit, ConventionalCommit } from "@dev-build-deploy/commit-it";
import { DiagnosticsMessage, FixItHint } from "@dev-build-deploy/diagnose-it";
import { Configuration } from "./configuration";
/**
* Validates a single commit message against the Conventional Commit specification.
* @param commit Commit message to validate against the Conventional Commit specification
* @returns Validation result
*/
function validateCommit(commit: Commit): ConventionalCommit {
return ConventionalCommit.fromCommit(commit, Configuration.getInstance());
}
/**
* Validates the pull request against CommitMe requirements.
* @param pullrequest The pull request to validate
* @param commits The commits associated with the pull request
* @returns Validation result
*/
export function validatePullRequest(pullrequest: Commit, commits: ConventionalCommit[]): ConventionalCommit {
const result = validateCommit(pullrequest);
if (!result.isValid) return result;
const orderValue = (commit: ConventionalCommit): number => {
if (commit.breaking) return 3;
if (commit.type?.toLowerCase() === "feat") return 2;
if (commit.type?.toLowerCase() === "fix") return 1;
return 0;
};
const pullRequestValue = orderValue(result);
const validConventionalCommits = commits.filter(commit => commit.isValid);
// No valid commits found, return the pull request validation result
if (validConventionalCommits.length === 0) return result;
// Sort the commits by order of precedence (SemVer) and validate against the Pull Request (SemVer)
const commitsValue = orderValue(validConventionalCommits.sort((a, b) => (orderValue(a) < orderValue(b) ? 1 : -1))[0]);
if (pullRequestValue < commitsValue) {
result.errors.push(
DiagnosticsMessage.createError(result.hash, {
text: `A Pull Request MUST correlate with a Semantic Versioning identifier (\`MAJOR\`, \`MINOR\`, or \`PATCH\`) with the same or higher precedence than its associated commits`,
linenumber: 1,
column: 1,
})
.setContext(1, result.subject)
.addFixitHint(FixItHint.create({ index: 1, length: result.type?.length ?? 1 }))
);
}
return result;
}
/**
* Validates the given set of commit messages against the conventional commit specification.
* @param commits The commits to validate
* @returns A list of validation results
* @see https://www.conventionalcommits.org/en/v1.0.0/
*/
export function validateCommits(commits: Commit[]): ConventionalCommit[] {
return commits.filter(commit => !commit.isFixupCommit && !commit.isMergeCommit).map(commit => validateCommit(commit));
}