From b4734d9e320f45209d6c8e18477734c5b6634c91 Mon Sep 17 00:00:00 2001 From: phwt <28344318+phwt@users.noreply.github.com> Date: Tue, 8 Oct 2024 00:10:14 +0700 Subject: [PATCH] feat: add mutually exclusive error on `branch` and `pull-request` input --- README.md | 12 ++++++------ src/modules/__tests__/sonarqube-api.test.ts | 17 +++++++++++++++++ src/modules/sonarqube-api.ts | 6 ++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 86ba1b4..23be093 100644 --- a/README.md +++ b/README.md @@ -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` | | @@ -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: | @@ -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: | diff --git a/src/modules/__tests__/sonarqube-api.test.ts b/src/modules/__tests__/sonarqube-api.test.ts index fcc473e..63bae7b 100644 --- a/src/modules/__tests__/sonarqube-api.test.ts +++ b/src/modules/__tests__/sonarqube-api.test.ts @@ -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(); + }); }); diff --git a/src/modules/sonarqube-api.ts b/src/modules/sonarqube-api.ts index f6c015a..290855a 100644 --- a/src/modules/sonarqube-api.ts +++ b/src/modules/sonarqube-api.ts @@ -8,6 +8,12 @@ export const fetchQualityGate = async ( branch?: string, pullRequest?: string ): Promise => { + 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,