From fdbf7804eb012faa3f50360eed1c04d665feb39b Mon Sep 17 00:00:00 2001 From: Phuwathid Summaviwat <28344318+phwt@users.noreply.github.com> Date: Tue, 8 Oct 2024 00:19:39 +0700 Subject: [PATCH] Add `pull-request` Input Support for Retrieving Result from the Pull Request (#30) * build: update patch versions * build: update minor versions * feat: add support for `pullRequest` input * docs: add `pullRequest` input to README * feat/refactor: define actions input and use `kebab-case` for input name * ci: update PR workflow to verify action run with `pull-request` input * ci/fix: add `sonar.pullrequest.branch` parameter * build: bump version to `1.5.0` * ci/refactor: update step name and remove unused input * ci: try to specify both `branch` and `pull-request` input * feat: add mutually exclusive error on `branch` and `pull-request` input * ci/refactor: remove test to fail step * refactor: update inputs description --- .github/workflows/pr.yml | 51 +++++- README.md | 9 +- action.yml | 5 +- package-lock.json | 172 +++++++++++--------- package.json | 10 +- src/index.ts | 4 +- src/modules/__tests__/sonarqube-api.test.ts | 45 ++++- src/modules/models.ts | 1 + src/modules/sonarqube-api.ts | 16 +- 9 files changed, 216 insertions(+), 97 deletions(-) diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index a924fa1..ddb2dff 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -9,7 +9,7 @@ env: SONAR_PROJECT_KEY: sonarqube-quality-gate-action jobs: - sonar-scan: + sonar-scan-branch: name: Check code quality on pull request branch runs-on: ubuntu-latest steps: @@ -57,6 +57,55 @@ jobs: echo "${{ steps.quality-gate-check.outputs.project-status }}" echo "${{ steps.quality-gate-check.outputs.quality-gate-result }}" + sonar-scan-pr: + name: Check code quality on the pull request + if: github.event_name == 'pull_request' + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - uses: actions/setup-node@v4 + with: + cache: npm + + - name: Install dependencies and run test + run: | + npm ci + npm test + + - name: SonarCloud Scan + uses: SonarSource/sonarcloud-github-action@v2 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + with: + args: > + -Dsonar.organization=phwt + -Dsonar.projectKey=sonarqube-quality-gate-action + -Dsonar.javascript.lcov.reportPaths=./coverage/lcov.info + -Dsonar.pullrequest.key=${{ github.event.number }} + -Dsonar.pullrequest.branch=${{ github.event.pull_request.head.ref }} + -Dsonar.cpd.exclusions=**/__tests__/*.ts + + - name: Wait for the quality gate result + run: sleep 5 + + - name: Retrieve quality gate result + uses: ./ + id: quality-gate-check + with: + sonar-host-url: ${{ env.SONAR_HOST_URL }} + sonar-project-key: ${{ env.SONAR_PROJECT_KEY }} + sonar-token: ${{ secrets.SONAR_TOKEN }} + pull-request: ${{ github.event.number }} + disable-pr-comment: true + + - run: | + echo "${{ steps.quality-gate-check.outputs.project-status }}" + echo "${{ steps.quality-gate-check.outputs.quality-gate-result }}" + sonar-scan-main: name: Retrieve quality gate result of the main branch runs-on: ubuntu-latest diff --git a/README.md b/README.md index df8a094..23be093 100644 --- a/README.md +++ b/README.md @@ -20,7 +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` | | +| 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` | | @@ -54,7 +55,8 @@ jobs: sonar-host-url: ${{ secrets.SONAR_HOST_URL }} sonar-token: ${{ secrets.SONAR_TOKEN }} github-token: ${{ secrets.GITHUB_TOKEN }} - branch: main # Optional input + branch: main # Optional input, mutually exclusive with `pull-request` + pull-request: 8 # Optional input, mutually exclusive with `branch` - name: Output result run: | @@ -89,7 +91,8 @@ jobs: sonar-host-url: ${{ secrets.SONAR_HOST_URL }} sonar-token: ${{ secrets.SONAR_TOKEN }} github-token: ${{ secrets.GITHUB_TOKEN }} - branch: main # 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/action.yml b/action.yml index fd8b87c..31c0b9e 100644 --- a/action.yml +++ b/action.yml @@ -22,7 +22,10 @@ inputs: required: false default: "false" branch: - description: "Branch name to retrieve the quality gate result" + description: "Branch name to retrieve the quality gate result, mutually exclusive with `pull-request` input" + required: false + pull-request: + description: "Pull request id to retrieve the quality gate result, mutually exclusive with `branch` input" required: false outputs: project-status: diff --git a/package-lock.json b/package-lock.json index b1f0b7f..4c90b35 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,34 +1,44 @@ { "name": "sonarqube-quality-gate-action", - "version": "1.3.2", + "version": "1.5.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "sonarqube-quality-gate-action", - "version": "1.3.2", + "version": "1.5.0", "license": "MIT", "dependencies": { - "@actions/core": "^1.10.1", + "@actions/core": "^1.11.1", "@actions/github": "^5.1.1", - "axios": "^1.7.4" + "axios": "^1.7.7" }, "devDependencies": { - "@types/jest": "^29.5.12", + "@types/jest": "^29.5.13", "jest": "^29.7.0", "timezone-mock": "^1.3.6", - "ts-jest": "^29.2.4", + "ts-jest": "^29.2.5", "ts-node": "^10.9.2", "typescript": "^4.9.5" } }, "node_modules/@actions/core": { - "version": "1.10.1", - "resolved": "https://registry.npmjs.org/@actions/core/-/core-1.10.1.tgz", - "integrity": "sha512-3lBR9EDAY+iYIpTnTIXmWcNbX3T2kCkAEQGIQx4NVQ0575nk2k3GRZDTPQG+vVtS2izSLmINlxXf0uLtnrTP+g==", + "version": "1.11.1", + "resolved": "https://registry.npmjs.org/@actions/core/-/core-1.11.1.tgz", + "integrity": "sha512-hXJCSrkwfA46Vd9Z3q4cpEpHB1rL5NG04+/rbqW9d3+CSvtB1tYe8UTpAlixa1vj0m/ULglfEK2UKxMGxCxv5A==", + "license": "MIT", "dependencies": { - "@actions/http-client": "^2.0.1", - "uuid": "^8.3.2" + "@actions/exec": "^1.1.1", + "@actions/http-client": "^2.0.1" + } + }, + "node_modules/@actions/exec": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@actions/exec/-/exec-1.1.1.tgz", + "integrity": "sha512-+sCcHHbVdk93a0XT19ECtO/gIXoxvdsgQLzb2fE2/5sIZmWQuluYyjPQtrtTHdU1YzTZ7bAPN4sITq2xi1679w==", + "license": "MIT", + "dependencies": { + "@actions/io": "^1.0.1" } }, "node_modules/@actions/github": { @@ -50,6 +60,12 @@ "tunnel": "^0.0.6" } }, + "node_modules/@actions/io": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/@actions/io/-/io-1.1.3.tgz", + "integrity": "sha512-wi9JjgKLYS7U/z8PPbco+PvTb/nRWjeoFlJ1Qer83k/3C5PHQi28hiVdeE2kHXmIL99mQFawx8qt/JPjZilJ8Q==", + "license": "MIT" + }, "node_modules/@ampproject/remapping": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/@ampproject/remapping/-/remapping-2.2.0.tgz", @@ -1334,9 +1350,9 @@ } }, "node_modules/@types/jest": { - "version": "29.5.12", - "resolved": "https://registry.npmjs.org/@types/jest/-/jest-29.5.12.tgz", - "integrity": "sha512-eDC8bTvT/QhYdxJAulQikueigY5AsdBRH2yDKW3yveW7svY3+DzN84/2NUgkw10RTiJbWqZrTtoGVdYlvFJdLw==", + "version": "29.5.13", + "resolved": "https://registry.npmjs.org/@types/jest/-/jest-29.5.13.tgz", + "integrity": "sha512-wd+MVEZCHt23V0/L642O5APvspWply/rGY5BcW4SUETo2UzPU3Z26qr8jC2qxpimI2jjx9h7+2cj2FwIr01bXg==", "dev": true, "license": "MIT", "dependencies": { @@ -1472,9 +1488,9 @@ "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" }, "node_modules/axios": { - "version": "1.7.4", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.4.tgz", - "integrity": "sha512-DukmaFRnY6AzAALSH4J2M3k6PkaC+MfaAGdEERRWcC9q3/TWQwLpHR8ZRLKTdQ3aBDL64EdluRDjJqKw+BPZEw==", + "version": "1.7.7", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.7.tgz", + "integrity": "sha512-S4kL7XrjgBmvdGut0sN3yJxqYzrDOnivkBiN0OFs6hLiUam3UPvswUo0kqGyhqUZGEOytHyumEdXsAkgCOUf3Q==", "license": "MIT", "dependencies": { "follow-redirects": "^1.15.6", @@ -3224,12 +3240,13 @@ "dev": true }, "node_modules/micromatch": { - "version": "4.0.5", - "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.5.tgz", - "integrity": "sha512-DMy+ERcEW2q8Z2Po+WNXuw3c5YaUSFjAO5GsJqfEl7UjvtIuFKO6ZrKvcItdy98dwFI2N1tg3zNIdKaQT+aNdA==", + "version": "4.0.8", + "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.8.tgz", + "integrity": "sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==", "dev": true, + "license": "MIT", "dependencies": { - "braces": "^3.0.2", + "braces": "^3.0.3", "picomatch": "^2.3.1" }, "engines": { @@ -3861,21 +3878,21 @@ "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" }, "node_modules/ts-jest": { - "version": "29.2.4", - "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-29.2.4.tgz", - "integrity": "sha512-3d6tgDyhCI29HlpwIq87sNuI+3Q6GLTTCeYRHCs7vDz+/3GCMwEtV9jezLyl4ZtnBgx00I7hm8PCP8cTksMGrw==", + "version": "29.2.5", + "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-29.2.5.tgz", + "integrity": "sha512-KD8zB2aAZrcKIdGk4OwpJggeLcH1FgrICqDSROWqlnJXGCXK4Mn6FcdK2B6670Xr73lHMG1kHw8R87A0ecZ+vA==", "dev": true, "license": "MIT", "dependencies": { - "bs-logger": "0.x", + "bs-logger": "^0.2.6", "ejs": "^3.1.10", - "fast-json-stable-stringify": "2.x", + "fast-json-stable-stringify": "^2.1.0", "jest-util": "^29.0.0", "json5": "^2.2.3", - "lodash.memoize": "4.x", - "make-error": "1.x", - "semver": "^7.5.3", - "yargs-parser": "^21.0.1" + "lodash.memoize": "^4.1.2", + "make-error": "^1.3.6", + "semver": "^7.6.3", + "yargs-parser": "^21.1.1" }, "bin": { "ts-jest": "cli.js" @@ -3910,13 +3927,11 @@ } }, "node_modules/ts-jest/node_modules/semver": { - "version": "7.5.4", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz", - "integrity": "sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==", + "version": "7.6.3", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.6.3.tgz", + "integrity": "sha512-oVekP1cKtI+CTDvHWYFUcMtsK/00wmAEfyqKfNdARm8u1wNVhSgaX7A8d4UuIlUI5e84iEwOhs7ZPYRmzU9U6A==", "dev": true, - "dependencies": { - "lru-cache": "^6.0.0" - }, + "license": "ISC", "bin": { "semver": "bin/semver.js" }, @@ -4040,14 +4055,6 @@ "browserslist": ">= 4.21.0" } }, - "node_modules/uuid": { - "version": "8.3.2", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", - "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", - "bin": { - "uuid": "dist/bin/uuid" - } - }, "node_modules/v8-compile-cache-lib": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz", @@ -4213,12 +4220,20 @@ }, "dependencies": { "@actions/core": { - "version": "1.10.1", - "resolved": "https://registry.npmjs.org/@actions/core/-/core-1.10.1.tgz", - "integrity": "sha512-3lBR9EDAY+iYIpTnTIXmWcNbX3T2kCkAEQGIQx4NVQ0575nk2k3GRZDTPQG+vVtS2izSLmINlxXf0uLtnrTP+g==", + "version": "1.11.1", + "resolved": "https://registry.npmjs.org/@actions/core/-/core-1.11.1.tgz", + "integrity": "sha512-hXJCSrkwfA46Vd9Z3q4cpEpHB1rL5NG04+/rbqW9d3+CSvtB1tYe8UTpAlixa1vj0m/ULglfEK2UKxMGxCxv5A==", "requires": { - "@actions/http-client": "^2.0.1", - "uuid": "^8.3.2" + "@actions/exec": "^1.1.1", + "@actions/http-client": "^2.0.1" + } + }, + "@actions/exec": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@actions/exec/-/exec-1.1.1.tgz", + "integrity": "sha512-+sCcHHbVdk93a0XT19ECtO/gIXoxvdsgQLzb2fE2/5sIZmWQuluYyjPQtrtTHdU1YzTZ7bAPN4sITq2xi1679w==", + "requires": { + "@actions/io": "^1.0.1" } }, "@actions/github": { @@ -4240,6 +4255,11 @@ "tunnel": "^0.0.6" } }, + "@actions/io": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/@actions/io/-/io-1.1.3.tgz", + "integrity": "sha512-wi9JjgKLYS7U/z8PPbco+PvTb/nRWjeoFlJ1Qer83k/3C5PHQi28hiVdeE2kHXmIL99mQFawx8qt/JPjZilJ8Q==" + }, "@ampproject/remapping": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/@ampproject/remapping/-/remapping-2.2.0.tgz", @@ -5282,9 +5302,9 @@ } }, "@types/jest": { - "version": "29.5.12", - "resolved": "https://registry.npmjs.org/@types/jest/-/jest-29.5.12.tgz", - "integrity": "sha512-eDC8bTvT/QhYdxJAulQikueigY5AsdBRH2yDKW3yveW7svY3+DzN84/2NUgkw10RTiJbWqZrTtoGVdYlvFJdLw==", + "version": "29.5.13", + "resolved": "https://registry.npmjs.org/@types/jest/-/jest-29.5.13.tgz", + "integrity": "sha512-wd+MVEZCHt23V0/L642O5APvspWply/rGY5BcW4SUETo2UzPU3Z26qr8jC2qxpimI2jjx9h7+2cj2FwIr01bXg==", "dev": true, "requires": { "expect": "^29.0.0", @@ -5391,9 +5411,9 @@ "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" }, "axios": { - "version": "1.7.4", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.4.tgz", - "integrity": "sha512-DukmaFRnY6AzAALSH4J2M3k6PkaC+MfaAGdEERRWcC9q3/TWQwLpHR8ZRLKTdQ3aBDL64EdluRDjJqKw+BPZEw==", + "version": "1.7.7", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.7.tgz", + "integrity": "sha512-S4kL7XrjgBmvdGut0sN3yJxqYzrDOnivkBiN0OFs6hLiUam3UPvswUo0kqGyhqUZGEOytHyumEdXsAkgCOUf3Q==", "requires": { "follow-redirects": "^1.15.6", "form-data": "^4.0.0", @@ -6686,12 +6706,12 @@ "dev": true }, "micromatch": { - "version": "4.0.5", - "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.5.tgz", - "integrity": "sha512-DMy+ERcEW2q8Z2Po+WNXuw3c5YaUSFjAO5GsJqfEl7UjvtIuFKO6ZrKvcItdy98dwFI2N1tg3zNIdKaQT+aNdA==", + "version": "4.0.8", + "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.8.tgz", + "integrity": "sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==", "dev": true, "requires": { - "braces": "^3.0.2", + "braces": "^3.0.3", "picomatch": "^2.3.1" } }, @@ -7146,30 +7166,27 @@ "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" }, "ts-jest": { - "version": "29.2.4", - "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-29.2.4.tgz", - "integrity": "sha512-3d6tgDyhCI29HlpwIq87sNuI+3Q6GLTTCeYRHCs7vDz+/3GCMwEtV9jezLyl4ZtnBgx00I7hm8PCP8cTksMGrw==", + "version": "29.2.5", + "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-29.2.5.tgz", + "integrity": "sha512-KD8zB2aAZrcKIdGk4OwpJggeLcH1FgrICqDSROWqlnJXGCXK4Mn6FcdK2B6670Xr73lHMG1kHw8R87A0ecZ+vA==", "dev": true, "requires": { - "bs-logger": "0.x", + "bs-logger": "^0.2.6", "ejs": "^3.1.10", - "fast-json-stable-stringify": "2.x", + "fast-json-stable-stringify": "^2.1.0", "jest-util": "^29.0.0", "json5": "^2.2.3", - "lodash.memoize": "4.x", - "make-error": "1.x", - "semver": "^7.5.3", - "yargs-parser": "^21.0.1" + "lodash.memoize": "^4.1.2", + "make-error": "^1.3.6", + "semver": "^7.6.3", + "yargs-parser": "^21.1.1" }, "dependencies": { "semver": { - "version": "7.5.4", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz", - "integrity": "sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==", - "dev": true, - "requires": { - "lru-cache": "^6.0.0" - } + "version": "7.6.3", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.6.3.tgz", + "integrity": "sha512-oVekP1cKtI+CTDvHWYFUcMtsK/00wmAEfyqKfNdARm8u1wNVhSgaX7A8d4UuIlUI5e84iEwOhs7ZPYRmzU9U6A==", + "dev": true } } }, @@ -7232,11 +7249,6 @@ "picocolors": "^1.0.0" } }, - "uuid": { - "version": "8.3.2", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", - "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==" - }, "v8-compile-cache-lib": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz", diff --git a/package.json b/package.json index 87babfc..400bcb5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sonarqube-quality-gate-action", - "version": "1.4.5", + "version": "1.5.0", "description": "", "main": "index.js", "scripts": { @@ -17,15 +17,15 @@ }, "homepage": "https://github.com/phwt/sonarqube-quality-gate-action#readme", "dependencies": { - "@actions/core": "^1.10.1", + "@actions/core": "^1.11.1", "@actions/github": "^5.1.1", - "axios": "^1.7.4" + "axios": "^1.7.7" }, "devDependencies": { - "@types/jest": "^29.5.12", + "@types/jest": "^29.5.13", "jest": "^29.7.0", "timezone-mock": "^1.3.6", - "ts-jest": "^29.2.4", + "ts-jest": "^29.2.5", "ts-node": "^10.9.2", "typescript": "^4.9.5" } diff --git a/src/index.ts b/src/index.ts index e7e9132..a6c348a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,6 +16,7 @@ import { findComment } from "./modules/find-comment/main"; failOnQualityGateError: core.getInput("fail-on-quality-gate-error") === "true", branch: core.getInput("branch"), + pullRequest: core.getInput("pull-request"), githubToken: core.getInput("github-token"), }; @@ -23,7 +24,8 @@ import { findComment } from "./modules/find-comment/main"; inputs.hostURL, inputs.projectKey, inputs.token, - inputs.branch + inputs.branch, + inputs.pullRequest ); core.setOutput("project-status", result.projectStatus.status); diff --git a/src/modules/__tests__/sonarqube-api.test.ts b/src/modules/__tests__/sonarqube-api.test.ts index de7de6f..63bae7b 100644 --- a/src/modules/__tests__/sonarqube-api.test.ts +++ b/src/modules/__tests__/sonarqube-api.test.ts @@ -4,7 +4,21 @@ import { fetchQualityGate } from "../sonarqube-api"; jest.mock("axios"); describe("fetchQualityGate", () => { - it("should make a GET request to the correct URL with all parameters when branch is defined", async () => { + it("should make a GET request to the correct URL with only `projectKey` parameter when only `projectKey` is defined", async () => { + (axios.get as jest.Mock).mockResolvedValue({}); + + await fetchQualityGate("https://example.com", "key", "token"); + + expect(axios.get).toHaveBeenCalledWith( + `https://example.com/api/qualitygates/project_status`, + { + params: { projectKey: "key" }, + auth: { username: "token", password: "" }, + } + ); + }); + + it("should make a GET request to the correct URL with `projectKey` and `branch` parameter when `branch` is defined", async () => { (axios.get as jest.Mock).mockResolvedValue({}); await fetchQualityGate("https://example.com", "key", "token", "branch"); @@ -18,17 +32,40 @@ describe("fetchQualityGate", () => { ); }); - it("should make a GET request to the correct URL with all parameters except branch when branch is not defined", async () => { + it("should make a GET request to the correct URL with `projectKey` and `pullRequest` parameter when `pull-request` is defined", async () => { (axios.get as jest.Mock).mockResolvedValue({}); - await fetchQualityGate("https://example.com", "key", "token"); + await fetchQualityGate( + "https://example.com", + "key", + "token", + undefined, + "pull-request" + ); expect(axios.get).toHaveBeenCalledWith( `https://example.com/api/qualitygates/project_status`, { - params: { projectKey: "key" }, + params: { projectKey: "key", pullRequest: "pull-request" }, auth: { username: "token", password: "" }, } ); }); + + 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/models.ts b/src/modules/models.ts index e37686e..f2db81a 100644 --- a/src/modules/models.ts +++ b/src/modules/models.ts @@ -32,5 +32,6 @@ export interface ActionInputs { commentDisabled?: boolean; failOnQualityGateError?: boolean; branch?: string; + pullRequest?: string; githubToken?: string; } diff --git a/src/modules/sonarqube-api.ts b/src/modules/sonarqube-api.ts index 14a4fe7..65ca016 100644 --- a/src/modules/sonarqube-api.ts +++ b/src/modules/sonarqube-api.ts @@ -5,9 +5,21 @@ export const fetchQualityGate = async ( url: string, projectKey: string, token: string, - branch?: string + branch?: string, + pullRequest?: string ): Promise => { - const params = branch ? { projectKey, branch } : { projectKey }; + if (branch && pullRequest) { + throw new Error( + "The `branch` and `pull-request` inputs are mutually exclusive and cannot be used together" + ); + } + + // Only include `branch` or `pullRequest` in the params object if they are defined + const params = { + projectKey, + ...(branch && { branch }), + ...(pullRequest && { pullRequest }), + }; const response = await axios.get( `${url}/api/qualitygates/project_status`,