Skip to content

Commit

Permalink
feat: add mutually exclusive error on branch and pull-request input
Browse files Browse the repository at this point in the history
  • Loading branch information
phwt committed Oct 7, 2024
1 parent 071d2b4 commit b4734d9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ Check quality gate result from latest analysis and report result in the pull req
| github-token | GitHub Token for commenting on the pull request - not required if `disable-pr-comment` is set to `true` | `false` | |
| disable-pr-comment | Disable commenting result on the pull request | `false` | false |
| fail-on-quality-gate-error | Set the action status to failed when quality gate status is `ERROR` | `false` | false |
| branch | Branch name to retrieve the quality gate result | `false` | |
| pull-request | Pull request id to retrieve the quality gate result | `false` | |
| branch | Branch name to retrieve the quality gate result, mutually exclusive with `pull-request` input | `false` | |
| pull-request | Pull request id to retrieve the quality gate result, mutually exclusive with `branch` input | `false` | |

<!-- action-docs-inputs -->

Expand Down Expand Up @@ -55,8 +55,8 @@ jobs:
sonar-host-url: ${{ secrets.SONAR_HOST_URL }}
sonar-token: ${{ secrets.SONAR_TOKEN }}
github-token: ${{ secrets.GITHUB_TOKEN }}
branch: main # Optional input
pull-request: 8 # Optional input
branch: main # Optional input, mutually exclusive with `pull-request`
pull-request: 8 # Optional input, mutually exclusive with `branch`

- name: Output result
run: |
Expand Down Expand Up @@ -91,8 +91,8 @@ jobs:
sonar-host-url: ${{ secrets.SONAR_HOST_URL }}
sonar-token: ${{ secrets.SONAR_TOKEN }}
github-token: ${{ secrets.GITHUB_TOKEN }}
branch: main # Optional input
pull-request: 8 # Optional input
branch: main # Optional input, mutually exclusive with `pull-request`
pull-request: 8 # Optional input, mutually exclusive with `branch`

- name: Output result
run: |
Expand Down
17 changes: 17 additions & 0 deletions src/modules/__tests__/sonarqube-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,21 @@ describe("fetchQualityGate", () => {
}
);
});

it("should thrown an error when both `branch` and `pull-request` are defined", async () => {
(axios.get as jest.Mock).mockResolvedValue({});

const fetchQualityGateFunction = async () => {
await fetchQualityGate(
"https://example.com",
"key",
"token",
"branch",
"pull-request"
);
};

await expect(fetchQualityGateFunction).rejects.toThrow();
expect(axios.get).not.toHaveBeenCalled();
});
});
6 changes: 6 additions & 0 deletions src/modules/sonarqube-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ export const fetchQualityGate = async (
branch?: string,
pullRequest?: string
): Promise<QualityGate> => {
if (branch && pullRequest) {
throw new Error(
"The `branch` and `pull-request` input are mutually exclusive and cannot be used at the same time"
);
}

// Only include `branch` or `pullRequest` in the params object if they are defined
const params = {
projectKey,
Expand Down

0 comments on commit b4734d9

Please sign in to comment.