Skip to content

Commit d4d1aa3

Browse files
fix: require azure devops credentials for work item linking (#92)
* chore: empty commit to sync branch * feat: validate Azure DevOps org and PAT when link-commits-to-pull-request is true Co-authored-by: joshjohanning <19912012+joshjohanning@users.noreply.github.com> * refactor: lint * chore: bump version to 3.0.7 --------- Co-authored-by: Josh Johanning <joshjohanning@github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: joshjohanning <19912012+joshjohanning@users.noreply.github.com>
1 parent c90924c commit d4d1aa3

5 files changed

Lines changed: 98 additions & 7 deletions

File tree

__tests__/index.test.js

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,81 @@ describe('Azure DevOps Commit Validator', () => {
206206

207207
expect(mockSetFailed).not.toHaveBeenCalled();
208208
});
209+
210+
it('should fail when link-commits-to-pull-request is true but azure-devops-organization is missing', async () => {
211+
mockGetInput.mockImplementation(name => {
212+
const defaults = {
213+
'check-pull-request': 'false',
214+
'check-commits': 'true',
215+
'fail-if-missing-workitem-commit-link': 'true',
216+
'link-commits-to-pull-request': 'true',
217+
'azure-devops-token': 'test-token',
218+
'azure-devops-organization': '', // Missing org
219+
'github-token': 'github-token',
220+
'comment-on-failure': 'true',
221+
'validate-work-item-exists': 'false'
222+
};
223+
return defaults[name] || '';
224+
});
225+
226+
await run();
227+
228+
expect(mockSetFailed).toHaveBeenCalledWith(
229+
'Azure DevOps organization is required when link-commits-to-pull-request is true'
230+
);
231+
});
232+
233+
it('should fail when link-commits-to-pull-request is true but azure-devops-token is missing', async () => {
234+
mockGetInput.mockImplementation(name => {
235+
const defaults = {
236+
'check-pull-request': 'false',
237+
'check-commits': 'true',
238+
'fail-if-missing-workitem-commit-link': 'true',
239+
'link-commits-to-pull-request': 'true',
240+
'azure-devops-token': '', // Missing token
241+
'azure-devops-organization': 'test-org',
242+
'github-token': 'github-token',
243+
'comment-on-failure': 'true',
244+
'validate-work-item-exists': 'false'
245+
};
246+
return defaults[name] || '';
247+
});
248+
249+
await run();
250+
251+
expect(mockSetFailed).toHaveBeenCalledWith(
252+
'Azure DevOps token is required when link-commits-to-pull-request is true'
253+
);
254+
});
255+
256+
it('should pass when link-commits-to-pull-request is false even if azure-devops config is missing', async () => {
257+
mockGetInput.mockImplementation(name => {
258+
const defaults = {
259+
'check-pull-request': 'false',
260+
'check-commits': 'true',
261+
'fail-if-missing-workitem-commit-link': 'true',
262+
'link-commits-to-pull-request': 'false', // Linking disabled
263+
'azure-devops-token': '',
264+
'azure-devops-organization': '',
265+
'github-token': 'github-token',
266+
'comment-on-failure': 'true',
267+
'validate-work-item-exists': 'false'
268+
};
269+
return defaults[name] || '';
270+
});
271+
272+
mockOctokit.paginate.mockResolvedValueOnce([
273+
{
274+
sha: '1234567890abcdef',
275+
commit: { message: 'Fix: AB#123' }
276+
}
277+
]);
278+
279+
await run();
280+
281+
// Should not call setFailed for missing Azure DevOps config since linking is disabled
282+
expect(mockSetFailed).not.toHaveBeenCalled();
283+
});
209284
});
210285

211286
describe('Commit validation', () => {
@@ -1175,7 +1250,7 @@ describe('Azure DevOps Commit Validator', () => {
11751250
});
11761251

11771252
describe('Edge cases - Work item linking with missing credentials', () => {
1178-
it('should attempt linking without failing when credentials are present', async () => {
1253+
it('should fail when linking is enabled but credentials are missing', async () => {
11791254
mockGetInput.mockImplementation(name => {
11801255
if (name === 'check-commits') return 'true';
11811256
if (name === 'check-pull-request') return 'false';
@@ -1202,8 +1277,12 @@ describe('Azure DevOps Commit Validator', () => {
12021277

12031278
await run();
12041279

1205-
// Should still call linkWorkItem even with empty credentials
1206-
expect(mockLinkWorkItem).toHaveBeenCalled();
1280+
// Should fail due to missing Azure DevOps organization
1281+
expect(mockSetFailed).toHaveBeenCalledWith(
1282+
'Azure DevOps organization is required when link-commits-to-pull-request is true'
1283+
);
1284+
// Should not call linkWorkItem since validation failed
1285+
expect(mockLinkWorkItem).not.toHaveBeenCalled();
12071286
});
12081287
});
12091288

badges/coverage.svg

Lines changed: 1 addition & 1 deletion
Loading

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "azure-devops-work-item-link-enforcer-and-linker",
3-
"version": "3.0.6",
3+
"version": "3.0.7",
44
"private": true,
55
"type": "module",
66
"description": "GitHub Action to enforce that each commit in a pull request be linked to an Azure DevOps work item and automatically link the pull request to each work item ",

src/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ 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');
67+
return;
68+
}
69+
}
70+
5971
const octokit = github.getOctokit(githubToken);
6072

6173
// Store work item to commit mapping and validation results

0 commit comments

Comments
 (0)