Skip to content

Commit 3d675be

Browse files
fix: enhance Azure DevOps configuration validation for linking and work item checks (#124)
* fix: enhance Azure DevOps configuration validation for linking and work item checks * fix: update coverage badge to reflect new coverage percentage of 81.38% * fix: update error messages to clarify required inputs for Azure DevOps validation features
1 parent d4d1aa3 commit 3d675be

3 files changed

Lines changed: 67 additions & 16 deletions

File tree

__tests__/index.test.js

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ describe('Azure DevOps Commit Validator', () => {
226226
await run();
227227

228228
expect(mockSetFailed).toHaveBeenCalledWith(
229-
'Azure DevOps organization is required when link-commits-to-pull-request is true'
229+
'The following input is required when link-commits-to-pull-request is enabled: azure-devops-organization'
230230
);
231231
});
232232

@@ -249,11 +249,11 @@ describe('Azure DevOps Commit Validator', () => {
249249
await run();
250250

251251
expect(mockSetFailed).toHaveBeenCalledWith(
252-
'Azure DevOps token is required when link-commits-to-pull-request is true'
252+
'The following input is required when link-commits-to-pull-request is enabled: azure-devops-token'
253253
);
254254
});
255255

256-
it('should pass when link-commits-to-pull-request is false even if azure-devops config is missing', async () => {
256+
it('should pass when link-commits-to-pull-request is false and validate-work-item-exists is false even if azure-devops config is missing', async () => {
257257
mockGetInput.mockImplementation(name => {
258258
const defaults = {
259259
'check-pull-request': 'false',
@@ -264,7 +264,7 @@ describe('Azure DevOps Commit Validator', () => {
264264
'azure-devops-organization': '',
265265
'github-token': 'github-token',
266266
'comment-on-failure': 'true',
267-
'validate-work-item-exists': 'false'
267+
'validate-work-item-exists': 'false' // Validation disabled
268268
};
269269
return defaults[name] || '';
270270
});
@@ -278,9 +278,55 @@ describe('Azure DevOps Commit Validator', () => {
278278

279279
await run();
280280

281-
// Should not call setFailed for missing Azure DevOps config since linking is disabled
281+
// Should not call setFailed for missing Azure DevOps config since both features are disabled
282282
expect(mockSetFailed).not.toHaveBeenCalled();
283283
});
284+
285+
it('should fail when validate-work-item-exists is true but azure-devops credentials are missing', async () => {
286+
mockGetInput.mockImplementation(name => {
287+
const defaults = {
288+
'check-pull-request': 'false',
289+
'check-commits': 'true',
290+
'fail-if-missing-workitem-commit-link': 'true',
291+
'link-commits-to-pull-request': 'false', // Linking disabled
292+
'azure-devops-token': '',
293+
'azure-devops-organization': '',
294+
'github-token': 'github-token',
295+
'comment-on-failure': 'true',
296+
'validate-work-item-exists': 'true' // Validation enabled but no credentials
297+
};
298+
return defaults[name] || '';
299+
});
300+
301+
await run();
302+
303+
expect(mockSetFailed).toHaveBeenCalledWith(
304+
'The following inputs are required when validate-work-item-exists is enabled: azure-devops-organization, azure-devops-token'
305+
);
306+
});
307+
308+
it('should fail when both link-commits-to-pull-request and validate-work-item-exists are true but credentials are missing', async () => {
309+
mockGetInput.mockImplementation(name => {
310+
const defaults = {
311+
'check-pull-request': 'false',
312+
'check-commits': 'true',
313+
'fail-if-missing-workitem-commit-link': 'true',
314+
'link-commits-to-pull-request': 'true', // Linking enabled
315+
'azure-devops-token': '',
316+
'azure-devops-organization': '',
317+
'github-token': 'github-token',
318+
'comment-on-failure': 'true',
319+
'validate-work-item-exists': 'true' // Validation also enabled
320+
};
321+
return defaults[name] || '';
322+
});
323+
324+
await run();
325+
326+
expect(mockSetFailed).toHaveBeenCalledWith(
327+
'The following inputs are required when link-commits-to-pull-request or validate-work-item-exists are enabled: azure-devops-organization, azure-devops-token'
328+
);
329+
});
284330
});
285331

286332
describe('Commit validation', () => {
@@ -1277,9 +1323,9 @@ describe('Azure DevOps Commit Validator', () => {
12771323

12781324
await run();
12791325

1280-
// Should fail due to missing Azure DevOps organization
1326+
// Should fail due to missing Azure DevOps organization and token
12811327
expect(mockSetFailed).toHaveBeenCalledWith(
1282-
'Azure DevOps organization is required when link-commits-to-pull-request is true'
1328+
'The following inputs are required when link-commits-to-pull-request is enabled: azure-devops-organization, azure-devops-token'
12831329
);
12841330
// Should not call linkWorkItem since validation failed
12851331
expect(mockLinkWorkItem).not.toHaveBeenCalled();

badges/coverage.svg

Lines changed: 1 addition & 1 deletion
Loading

src/index.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,19 @@ export async function run() {
5656
return;
5757
}
5858

59-
// Validate Azure DevOps configuration if linking is enabled
60-
if (linkCommitsToPullRequest) {
61-
if (!azureDevopsOrganization) {
62-
core.setFailed('Azure DevOps organization is required when link-commits-to-pull-request is true');
63-
return;
64-
}
65-
if (!azureDevopsToken) {
66-
core.setFailed('Azure DevOps token is required when link-commits-to-pull-request is true');
59+
// Validate Azure DevOps configuration if linking or work item validation is enabled
60+
if (linkCommitsToPullRequest || validateWorkItemExistsFlag) {
61+
const missingConfig = [];
62+
if (!azureDevopsOrganization) missingConfig.push('azure-devops-organization');
63+
if (!azureDevopsToken) missingConfig.push('azure-devops-token');
64+
65+
if (missingConfig.length > 0) {
66+
const features = [];
67+
if (linkCommitsToPullRequest) features.push('link-commits-to-pull-request');
68+
if (validateWorkItemExistsFlag) features.push('validate-work-item-exists');
69+
core.setFailed(
70+
`The following input${missingConfig.length === 1 ? ' is' : 's are'} required when ${features.join(' or ')} ${features.length === 1 ? 'is' : 'are'} enabled: ${missingConfig.join(', ')}`
71+
);
6772
return;
6873
}
6974
}

0 commit comments

Comments
 (0)