chore: add linting for commit messages

This commit is contained in:
Kai Armstrong 2022-10-21 15:02:28 -05:00
parent 2443a0a9fa
commit 58b766ca2a
4 changed files with 1117 additions and 7 deletions

View File

@ -5,7 +5,6 @@ workflow:
- if: $CI_COMMIT_TAG
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
default:
image: golang:1.18
@ -42,6 +41,18 @@ check_docs_markdown:
# Lint Markdown
- markdownlint --config .markdownlint.yml 'docs/**/*.md' README.md
lint_commit:
stage: test
image: node:16-slim
script:
- apt-get update && apt-get install -y git
- git fetch origin $CI_MERGE_REQUEST_TARGET_BRANCH_NAME && git checkout $CI_MERGE_REQUEST_TARGET_BRANCH_NAME && git checkout $CI_COMMIT_SHA
- cd scripts/commit-lint && npm ci
- node lint.js
rules:
- if: '$CI_MERGE_REQUEST_IID && $CI_PROJECT_VISIBILITY == "public"' # lint.js script makes an API call without authentication
when: always
code_navigation:
stage: test
image: golang:latest
@ -64,17 +75,15 @@ run_tests:
- chmod 600 ~/.ssh/known_hosts
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
script:
# `goreleaser` also uses GITLAB_TOKEN and so we need to distinguish between
# the GITLAB_TOKEN_TEST with less privilege used for testing and the GITLAB_TOKEN_RELEASE token
- GITLAB_TOKEN=$GITLAB_TOKEN_TEST make test
coverage: '/coverage: \d+.\d+% of statements/'
artifacts:
reports:
junit: coverage.xml
reports:
junit: coverage.xml
secret_detection:
rules:
- if: $SECRET_DETECTION_DISABLED
@ -113,7 +122,6 @@ release:
DOCKER_REGISTRY: $CI_REGISTRY
DOCKER_USERNAME: $CI_REGISTRY_USER
DOCKER_PASSWORD: $CI_REGISTRY_PASSWORD
script: |
docker run --rm --privileged \
-v $PWD:/go/src/gitlab.com/gitlab-org/cli \

View File

@ -0,0 +1,82 @@
const axios = require('axios').default;
const read = require('@commitlint/read').default;
const lint = require('@commitlint/lint').default;
const format = require('@commitlint/format').default;
const config = require('@commitlint/config-conventional');
const maximumLineLength = 72;
// You can test the script by setting these environment variables
const {
CI_MERGE_REQUEST_PROJECT_ID, // 5261717
CI_MERGE_REQUEST_IID,
CI_COMMIT_SHA,
CI_MERGE_REQUEST_TARGET_BRANCH_NAME, // usually main
} = process.env;
const urlSemanticRelease =
'https://gitlab.com/gitlab-org/cli/-/blob/main/CONTRIBUTING.md#commit-messages';
const customRules = {
'header-max-length': [2, 'always', maximumLineLength],
'body-leading-blank': [2, 'always'],
'footer-leading-blank': [2, 'always'],
'subject-case': [0],
};
async function getMr() {
const result = await axios.get(
`https://gitlab.com/api/v4/projects/${CI_MERGE_REQUEST_PROJECT_ID}/merge_requests/${CI_MERGE_REQUEST_IID}`,
);
const { title, squash } = result.data;
return {
title,
squash,
};
}
async function getCommitsInMr() {
const targetBranch = CI_MERGE_REQUEST_TARGET_BRANCH_NAME;
const sourceCommit = CI_COMMIT_SHA;
const messages = await read({ from: targetBranch, to: sourceCommit });
return messages;
}
async function isConventional(message) {
return lint(message, { ...config.rules, ...customRules }, { defaultIgnores: false });
}
async function lintMr() {
const mr = await getMr();
const commits = await getCommitsInMr();
if (!mr.squash || commits.length === 1) {
console.log(
"INFO: Either the merge request isn't set to squash commits, or contains only one commit. Every commit message must use conventional commits.\n" +
'INFO: For help, read https://gitlab.com/gitlab-org/gitlab-vscode-extension/-/blob/main/docs/developer/commits.md',
);
return Promise.all(commits.map(isConventional));
}
console.log(
'INFO: The merge request is set to both squash commits and use the merge request title for the squash commit.\n' +
'INFO: If the merge request title is incorrect, fix the title and rerun this CI/CD job.\n',
);
return isConventional(mr.title).then(Array.of);
}
async function run() {
const results = await lintMr();
console.error(format({ results }, { helpUrl: urlSemanticRelease }));
const numOfErrors = results.reduce((acc, result) => acc + result.errors.length, 0);
if (numOfErrors !== 0) {
process.exit(1);
}
}
run().catch(err => {
console.error(err);
process.exit(1);
});

1006
scripts/commit-lint/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,14 @@
{
"name": "commit-lint",
"version": "0.0.1",
"main": "lint.js",
"author": "",
"license": "MIT",
"dependencies": {
"@commitlint/config-conventional": "^9.1.1",
"@commitlint/format": "^9.1.1",
"@commitlint/lint": "^9.1.1",
"@commitlint/read": "^9.1.1",
"axios": "^0.19.2"
}
}