Skip to content
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
85 changes: 82 additions & 3 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,81 @@ describe('Azure DevOps Commit Validator', () => {

expect(mockSetFailed).not.toHaveBeenCalled();
});

it('should fail when link-commits-to-pull-request is true but azure-devops-organization is missing', async () => {
mockGetInput.mockImplementation(name => {
const defaults = {
'check-pull-request': 'false',
'check-commits': 'true',
'fail-if-missing-workitem-commit-link': 'true',
'link-commits-to-pull-request': 'true',
'azure-devops-token': 'test-token',
'azure-devops-organization': '', // Missing org
'github-token': 'github-token',
'comment-on-failure': 'true',
'validate-work-item-exists': 'false'
};
return defaults[name] || '';
});

await run();

expect(mockSetFailed).toHaveBeenCalledWith(
'Azure DevOps organization is required when link-commits-to-pull-request is true'
);
});

it('should fail when link-commits-to-pull-request is true but azure-devops-token is missing', async () => {
mockGetInput.mockImplementation(name => {
const defaults = {
'check-pull-request': 'false',
'check-commits': 'true',
'fail-if-missing-workitem-commit-link': 'true',
'link-commits-to-pull-request': 'true',
'azure-devops-token': '', // Missing token
'azure-devops-organization': 'test-org',
'github-token': 'github-token',
'comment-on-failure': 'true',
'validate-work-item-exists': 'false'
};
return defaults[name] || '';
});

await run();

expect(mockSetFailed).toHaveBeenCalledWith(
'Azure DevOps token is required when link-commits-to-pull-request is true'
);
});

it('should pass when link-commits-to-pull-request is false even if azure-devops config is missing', async () => {
mockGetInput.mockImplementation(name => {
const defaults = {
'check-pull-request': 'false',
'check-commits': 'true',
'fail-if-missing-workitem-commit-link': 'true',
'link-commits-to-pull-request': 'false', // Linking disabled
'azure-devops-token': '',
'azure-devops-organization': '',
'github-token': 'github-token',
'comment-on-failure': 'true',
'validate-work-item-exists': 'false'
};
return defaults[name] || '';
});

mockOctokit.paginate.mockResolvedValueOnce([
{
sha: '1234567890abcdef',
commit: { message: 'Fix: AB#123' }
}
]);

await run();

// Should not call setFailed for missing Azure DevOps config since linking is disabled
expect(mockSetFailed).not.toHaveBeenCalled();
});
});

describe('Commit validation', () => {
Expand Down Expand Up @@ -1175,7 +1250,7 @@ describe('Azure DevOps Commit Validator', () => {
});

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

await run();

// Should still call linkWorkItem even with empty credentials
expect(mockLinkWorkItem).toHaveBeenCalled();
// Should fail due to missing Azure DevOps organization
expect(mockSetFailed).toHaveBeenCalledWith(
'Azure DevOps organization is required when link-commits-to-pull-request is true'
);
// Should not call linkWorkItem since validation failed
expect(mockLinkWorkItem).not.toHaveBeenCalled();
});
});

Expand Down
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "azure-devops-work-item-link-enforcer-and-linker",
"version": "3.0.6",
"version": "3.0.7",
"private": true,
"type": "module",
"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 ",
Expand Down
12 changes: 12 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ export async function run() {
return;
}

// Validate Azure DevOps configuration if linking is enabled
if (linkCommitsToPullRequest) {
if (!azureDevopsOrganization) {
core.setFailed('Azure DevOps organization is required when link-commits-to-pull-request is true');
return;
}
if (!azureDevopsToken) {
core.setFailed('Azure DevOps token is required when link-commits-to-pull-request is true');
return;
}
}
Comment thread
joshjohanning marked this conversation as resolved.
Comment thread
joshjohanning marked this conversation as resolved.

const octokit = github.getOctokit(githubToken);

// Store work item to commit mapping and validation results
Expand Down