Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(conventional-commit-lint): add second check for mismatched PR title #5622

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,6 @@ exports['ConventionalCommitLint PR With Multiple Commits has a valid title, inva
"head_sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e"
}

exports['ConventionalCommitLint adds a comment when the commit message and the PR title differ 1'] = {
"head_sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
"conclusion": "failure",
"name": "conventionalcommits.org",
"output": {
"title": "Commit message did not follow Conventional Commits",
"summary": "Some of your commit messages failed linting.\n\nVisit [conventionalcommits.org](https://conventionalcommits.org) to learn our conventions.\n\nRun `git commit --amend` and edit your message to match Conventional Commit guidelines.",
"text": ":x: The following linting errors found:\n* subject may not be empty\n* type may not be empty\nfor the following input:\n\"*fix all the bugs*\"\n\n"
}
}

exports['ConventionalCommitLint sets a "success" context on PR with very long lines 1'] = {
"name": "conventionalcommits.org",
"conclusion": "success",
Expand All @@ -72,3 +61,24 @@ exports['ConventionalCommitLint PR With Multiple Commits has a valid title, inva
"conclusion": "success",
"head_sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e"
}

exports['ConventionalCommitLint adds an additional check when the commit message and the PR title differ 1'] = {
"head_sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
"conclusion": "failure",
"output": {
"title": "PR title does not match commit message",
"summary": "🤖 I detect that the PR title and the commit message differ and there's only one commit. To use the PR title for the commit history, you can use Github's automerge feature with squashing, or use `automerge` label. Good luck human!\n\n -- conventional-commit-lint bot\nhttps://conventionalcommits.org/",
"text": "🤖 I detect that the PR title and the commit message differ and there's only one commit. To use the PR title for the commit history, you can use Github's automerge feature with squashing, or use `automerge` label. Good luck human!\n\n -- conventional-commit-lint bot\nhttps://conventionalcommits.org/"
}
}

exports['ConventionalCommitLint adds an additional check when the commit message and the PR title differ 2'] = {
"head_sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
"conclusion": "failure",
"name": "conventionalcommits.org",
"output": {
"title": "Commit message did not follow Conventional Commits",
"summary": "Some of your commit messages failed linting.\n\nVisit [conventionalcommits.org](https://conventionalcommits.org) to learn our conventions.\n\nRun `git commit --amend` and edit your message to match Conventional Commit guidelines.",
"text": ":x: The following linting errors found:\n* subject may not be empty\n* type may not be empty\nfor the following input:\n\"*fix all the bugs*\"\n\n"
}
}
36 changes: 12 additions & 24 deletions packages/conventional-commit-lint/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ import {Octokit} from '@octokit/rest';

//import {ILint} from '@commitlint/lint';
import lint from '@commitlint/lint';

import {GCFLogger} from 'gcf-utils';
import {addOrUpdateIssueComment} from '@google-automations/issue-utils';

import {rules} from '@commitlint/config-conventional';

type PullsListCommitsResponseData = components['schemas']['commit'][];
Expand Down Expand Up @@ -159,33 +156,24 @@ export async function scanPullRequest(
`commit message: ${commits[0].commit.message.split('\n')[0]}`
);
logger.info(`PR title: ${pull_request.title}`);
const owner = pull_request.base.repo.owner?.login;
const repo = context.payload.repository.name;
const prNumber = pull_request.number;
const installationId = context.payload.installation?.id;
const message =
const summary =
'🤖 I detect that the PR title and the commit message' +
" differ and there's only one commit. To use the PR title for the" +
" commit history, you can use Github's automerge feature with" +
' squashing, or use `automerge` label. Good luck human!\n\n' +
' -- conventional-commit-lint bot\n' +
'https://conventionalcommits.org/';
try {
await addOrUpdateIssueComment(
octokit,
owner as string,
repo,
prNumber,
installationId as number,
message,
{
onlyUpdate: false,
}
);
} catch (err) {
// This is a solely convenience feature, so we ignore errors.
logger.warn(`Failed to add a comment: ${err}`);
}
await octokit.checks.create({
owner: pull_request.base.repo.owner?.login,
repo: context.payload.repository.name,
head_sha: commits[commits.length - 1].sha as string,
conclusion: 'failure',
output: {
title: 'PR title does not match commit message',
summary,
text: summary,
},
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
/* eslint-disable @typescript-eslint/no-var-requires */

import * as configUtilsModule from '@google-automations/bot-config-utils';
// cannot sinon mock re-exported function in a module
import * as issueUtilsModule from '@google-automations/issue-utils/build/src/issue-comments';
import {ConfigChecker} from '@google-automations/bot-config-utils';
import {logger} from 'gcf-utils';
import {readFileSync} from 'fs';
Expand Down Expand Up @@ -47,7 +45,6 @@ function loadConfig(configFile: string) {
describe('ConventionalCommitLint', () => {
let probot: Probot;
const sandbox = sinon.createSandbox();
let addOrUpdateIssueCommentStub: sinon.SinonStub;
let getAuthenticatedOctokitStub: sinon.SinonStub;
const pr11 = require(resolve(fixturesPath, './pr11'));

Expand Down Expand Up @@ -75,10 +72,6 @@ describe('ConventionalCommitLint', () => {
}),
});
probot.load(myProbotApp);
addOrUpdateIssueCommentStub = sandbox.stub(
issueUtilsModule,
'addOrUpdateIssueComment'
);
getAuthenticatedOctokitStub = sandbox.stub(
gcfUtilsModule,
'getAuthenticatedOctokit'
Expand All @@ -93,7 +86,6 @@ describe('ConventionalCommitLint', () => {

it('sets a "failure" context on PR, if commits fail linting', async () => {
stubConfig();
addOrUpdateIssueCommentStub.resolves(null);
const pr11WithBadMessage = require(resolve(
fixturesPath,
'./pr11WithBadMessage'
Expand All @@ -117,12 +109,10 @@ describe('ConventionalCommitLint', () => {
.reply(200);
await probot.receive({name: 'pull_request', payload, id: 'abc123'});
requests.done();
sinon.assert.notCalled(addOrUpdateIssueCommentStub);
});

it('adds a comment when the commit message and the PR title differ', async () => {
it('adds an additional check when the commit message and the PR title differ', async () => {
stubConfig();
addOrUpdateIssueCommentStub.resolves(null);
const pr11WithCorrectMessage = require(resolve(
fixturesPath,
'./pr11WithCorrectMessage'
Expand All @@ -143,15 +133,14 @@ describe('ConventionalCommitLint', () => {
snapshot(body);
return true;
})
.twice()
.reply(200);
await probot.receive({name: 'pull_request', payload, id: 'abc123'});
requests.done();
sinon.assert.calledOnce(addOrUpdateIssueCommentStub);
});

it('should post "success" context on PR with always_check_pr_title set to true', async () => {
stubConfig('always_check_pr_title.yaml');
addOrUpdateIssueCommentStub.resolves(null);
const pr11WithCorrectMessage = require(resolve(
fixturesPath,
'./pr11WithCorrectMessage'
Expand Down
Loading