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: commisery-action support runs triggered by merge_group event #287

Merged
merged 1 commit into from
May 31, 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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ The `commisery-action` supports a configuration file, allowing you to:
Please refer to the [documentation](docs/configuration.md) for more details

### Conventional Commit message validation
The following example workflow will trigger on pull request creation/modification and verify
all associated commit messages.
The following example workflow will trigger on pull request creation/modification,
and on the merge queue run, and verify all associated commit messages.

```yaml
name: Commisery
on:
pull_request:
merge_group:

jobs:
commit-message:
Expand Down
9 changes: 8 additions & 1 deletion dist/bump/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions dist/validate/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions src/actions/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import { getVersionBumpType } from "../bump";
import { ConventionalCommitMessage } from "../commit";

import { Configuration } from "../config";
import { getConfig, isPullRequestEvent, updateLabels } from "../github";
import {
getConfig,
isMergeGroupEvent,
isPullRequestEvent,
updateLabels
} from "../github";
import * as Label from "../label";
import { SemVerType } from "../semver";
import {
Expand Down Expand Up @@ -59,9 +64,9 @@ async function determineLabels(
*/
export async function run(): Promise<void> {
try {
if (!isPullRequestEvent()) {
if (!isPullRequestEvent() && !isMergeGroupEvent()) {
core.warning(
"Conventional Commit Message validation requires a workflow using the `pull_request` trigger!"
"Conventional Commit Message validation requires a workflow using the `pull_request` or `merge_group` trigger!"
);
return;
}
Expand Down
7 changes: 7 additions & 0 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ function githubCommitsAsICommits(
});
}

/**
* Returns whether we are running in context of a Merge Group event
*/
export function isMergeGroupEvent(): boolean {
return github.context.eventName === "merge_group";
}

/**
* Returns whether we are running in context of a Pull Request event
*/
Expand Down
61 changes: 61 additions & 0 deletions test/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,67 @@ describe("Warning cases", () => {
);
});

describe("Event type cases", () => {
const eventTypeCases = [
{
testDescription:
"should issue a warning if not `pull_request` nor `merge_group` trigger",
messages: [OK_1],
prTitle: "ci: proper title",
warningMessages: [
"Conventional Commit Message validation requires a workflow using the `pull_request` or `merge_group` trigger!",
],
isPullRequest: false,
isMergeGroup: false,
},
{
testDescription: "should not issue a warning if `pull_request` trigger",
messages: [OK_1],
prTitle: "ci: proper title",
warningMessages: [],
isPullRequest: true,
isMergeGroup: false,
},
{
testDescription: "should not issue a warning if `merge_group` trigger",
messages: [OK_1],
prTitle: "ci: proper title",
warningMessages: [],
isPullRequest: false,
isMergeGroup: true,
},
];
test.each(eventTypeCases)(
"$testDescription",
({
testDescription,
messages,
prTitle,
warningMessages,
isPullRequest,
isMergeGroup,
}) => {
jest.spyOn(github, "getCommitsInPR").mockResolvedValue(messages);
jest.spyOn(github, "getPullRequestTitle").mockResolvedValue(prTitle);
jest.spyOn(github, "isPullRequestEvent").mockReturnValue(isPullRequest);
jest.spyOn(github, "isMergeGroupEvent").mockReturnValue(isMergeGroup);

validate.run().then(() => {
if (!warningMessages.length) {
expect(core.warning).not.toHaveBeenCalled();
} else {
expect(core.warning).toHaveBeenCalled();
for (const msg of warningMessages) {
expect(core.warning).toHaveBeenCalledWith(
expect.stringContaining(msg)
);
}
}
});
}
);
});

describe("Error cases", () => {
const INVALID_CONVENTIONAL_COMMIT_MSG = "not valid Conventional Commits";
const PR_TITLE_NOT_COMPLIANT_MSG = "pull request title is not compliant";
Expand Down
Loading