diff --git a/__tests__/index.test.js b/__tests__/index.test.js index c6f7f94..2a3e2ef 100644 --- a/__tests__/index.test.js +++ b/__tests__/index.test.js @@ -128,6 +128,84 @@ describe('Azure DevOps Commit Validator', () => { // Restore context mockContext.payload.pull_request = originalPR; }); + + it('should fail if both check-commits and check-pull-request are false', async () => { + mockGetInput.mockImplementation(name => { + if (name === 'check-commits') return 'false'; + if (name === 'check-pull-request') return 'false'; + if (name === 'github-token') return 'github-token'; + return 'false'; + }); + + await run(); + + expect(mockSetFailed).toHaveBeenCalledWith( + "At least one of 'check-commits' or 'check-pull-request' must be set to true. Both are currently set to false." + ); + }); + + it('should pass when only check-commits is enabled', async () => { + mockGetInput.mockImplementation(name => { + if (name === 'check-commits') return 'true'; + if (name === 'check-pull-request') return 'false'; + if (name === 'github-token') return 'github-token'; + if (name === 'fail-if-missing-workitem-commit-link') return 'false'; + return 'false'; + }); + + mockOctokit.rest.pulls.listCommits.mockResolvedValue({ + data: [] + }); + + await run(); + + expect(mockSetFailed).not.toHaveBeenCalled(); + }); + + it('should pass when only check-pull-request is enabled', async () => { + mockGetInput.mockImplementation(name => { + if (name === 'check-commits') return 'false'; + if (name === 'check-pull-request') return 'true'; + if (name === 'github-token') return 'github-token'; + return 'false'; + }); + + mockOctokit.rest.pulls.get.mockResolvedValue({ + data: { + title: 'Test PR AB#123', + body: 'Test body' + } + }); + + await run(); + + expect(mockSetFailed).not.toHaveBeenCalled(); + }); + + it('should pass when both checks are enabled', async () => { + mockGetInput.mockImplementation(name => { + if (name === 'check-commits') return 'true'; + if (name === 'check-pull-request') return 'true'; + if (name === 'github-token') return 'github-token'; + if (name === 'fail-if-missing-workitem-commit-link') return 'false'; + return 'false'; + }); + + mockOctokit.rest.pulls.listCommits.mockResolvedValue({ + data: [] + }); + + mockOctokit.rest.pulls.get.mockResolvedValue({ + data: { + title: 'Test PR AB#123', + body: 'Test body' + } + }); + + await run(); + + expect(mockSetFailed).not.toHaveBeenCalled(); + }); }); describe('Commit validation', () => { diff --git a/src/index.js b/src/index.js index fa41214..fd7587a 100644 --- a/src/index.js +++ b/src/index.js @@ -39,6 +39,14 @@ export async function run() { const commentOnFailure = core.getInput('comment-on-failure') === 'true'; const validateWorkItemExistsFlag = core.getInput('validate-work-item-exists') === 'true'; + // Validate that at least one check is enabled + if (!checkPullRequest && !checkCommits) { + core.setFailed( + "At least one of 'check-commits' or 'check-pull-request' must be set to true. Both are currently set to false." + ); + return; + } + // Get context const context = github.context; const pullNumber = context.payload.pull_request?.number;