Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(ci): keep SDK test code away from the publish credentials
Yudhi Armyndharisbcfc4fafce80
diff --git a/.github/workflows/java-sdk-release.yml b/.github/workflows/java-sdk-release.ymlindex 275493c96..5bfab1c9a 100644--- a/.github/workflows/java-sdk-release.yml+++ b/.github/workflows/java-sdk-release.yml@@ -126,11 +126,13 @@ jobs:gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }}gpg-passphrase: GPG_PASSPHRASE- # -DskipTests: the verify job already ran every test on this commit, without credentials; running- # them again here would execute test code beside the token and the signing key.+ # -Dmaven.test.skip=true, not -DskipTests: the verify job already ran every test on this commit,+ # without credentials. -DskipTests still compiles the test sources against every test dependency,+ # and javac runs the annotation processors it finds there, in this step, beside the token and the+ # signing key.- name: Deploy to Maven Centralenv:MAVEN_CENTRAL_USERNAME: ${{ secrets.MAVEN_CENTRAL_USERNAME }}MAVEN_CENTRAL_PASSWORD: ${{ secrets.MAVEN_CENTRAL_PASSWORD }}GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}- run: mvn -B -Prelease deploy -DskipTests+ run: mvn -B -Prelease deploy -Dmaven.test.skip=truediff --git a/.github/workflows/php-sdk-release.yml b/.github/workflows/php-sdk-release.ymlindex 8f6996bf7..229d796c5 100644--- a/.github/workflows/php-sdk-release.yml+++ b/.github/workflows/php-sdk-release.yml@@ -24,14 +24,16 @@ permissions:contents: readjobs:- release:+ # The SDK's dev dependencies (phpunit and what it pulls in) execute on this job's runner. It holds no+ # secret, and the job that does (release, below) takes a checkout of its own and installs nothing, so+ # nothing those packages write reaches the push that carries the mirror token.+ test:runs-on: ubuntu-latesttimeout-minutes: 15steps:- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7with:- fetch-depth: 0 # full history is required for `git subtree split`- persist-credentials: false # stop the GITHUB_TOKEN extraheader from shadowing the PAT in the push URL+ persist-credentials: false# Guards are textual and secret-free, so a misconfigured release fails BEFORE anything is# pushed. Mirrors java/js/python-sdk-release.yml.@@ -80,6 +82,29 @@ jobs:composer install --no-interaction --no-progress./vendor/bin/phpunit+ release:+ needs: test+ runs-on: ubuntu-latest+ timeout-minutes: 15+ steps:+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7+ with:+ fetch-depth: 0 # full history is required for `git subtree split`+ persist-credentials: false # stop the GITHUB_TOKEN extraheader from shadowing the PAT in the push URL++ # The job that holds the token refuses any other ref itself, whatever ran before it.+ - name: Guard — ref must be a php-sdk-v* release tag+ env:+ REF: ${{ github.ref }}+ run: |+ case "$REF" in+ refs/tags/php-sdk-v*) ;;+ *)+ echo "::error::This workflow publishes to Packagist and must run from a php-sdk-v* tag (got '$REF')."+ exit 1+ ;;+ esac+- name: Tag the mirror repoenv:SPLIT_TOKEN: ${{ secrets.PHP_SDK_SPLIT_TOKEN }}@@ -112,7 +137,7 @@ jobs:echo "Split SHA: $SPLIT_SHA"# Tag without the `v` prefix, matching the existing 0.1.0 tag on the mirror. Composer accepts# both, but mixing the two in one repository makes the version list read as two series.- # Hooks off: the SDK tests ran earlier in this job, with their dev dependencies, and a hook one of- # them left under .git/hooks would otherwise run here with the token in its environment.+ # Hooks off: nothing in this job runs project code, so none are expected, and none may run with+ # the token in the environment of this push.git -c core.hooksPath=/dev/null push "$MIRROR" "${SPLIT_SHA}:refs/tags/${VERSION}"echo "Tagged rmyndharis/openwa-php ${VERSION} -> ${SPLIT_SHA}"diff --git a/src/common/ci-privilege-gate.spec.ts b/src/common/ci-privilege-gate.spec.tsindex 658835c89..f641549dd 100644--- a/src/common/ci-privilege-gate.spec.ts+++ b/src/common/ci-privilege-gate.spec.ts@@ -405,6 +405,22 @@ describe('a job that can mint a publish credential pins global npm installs and},);+ // -DskipTests only skips running the tests: the test sources are still compiled against every test+ // dependency, and javac runs the annotation processors it finds on that classpath beside the credentials.+ it('java-sdk-release.yml: the deploy compiles no test sources', () => {+ const workflow = workflowOf('java-sdk-release.yml');+ const runs = Object.values(workflow.jobs ?? {})+ .flatMap(job => job.steps ?? [])+ .map(step => executableLines(step.run ?? ''))+ .join('\n');+ const deploy = runs.split('\n').filter(line => /\bmvn\b.*\bdeploy\b/.test(line));+ expect(deploy.length).toBeGreaterThan(0);+ for (const line of deploy) {+ expect(line).toContain('-Dmaven.test.skip=true');+ expect(line).not.toContain('-DskipTests');+ }+ });+it('finds a cache restored beside a publish credential', () => {const workflow: OidcWorkflow = {jobs: {@@ -620,11 +636,37 @@ describe('the dashboard unit tests are bounded', () => {});/**- * The PHP release runs the SDK tests and then pushes to the mirror with a personal access token in the- * environment, in one job. A git hook that a dev dependency of those tests left behind would run in that- * push with the token, so the push runs with hooks switched off.+ * The PHP release pushes to the mirror with a personal access token in the environment. The SDK's tests,+ * and the dev dependencies they run, execute in a job of their own that holds no secret, and the job that+ * pushes takes a fresh checkout, installs nothing and runs only git, with hooks switched off.*/-describe('the PHP SDK release pushes to the mirror with hooks disabled', () => {+describe('the PHP SDK release keeps the mirror token away from the SDK test run', () => {+ type PhpJob = { needs?: string | string[]; steps?: Step[] };+ const jobs = (): Record<string, PhpJob> =>+ (workflowOf('php-sdk-release.yml') as unknown as { jobs: Record<string, PhpJob> }).jobs;+ const runsOf = (job: PhpJob): string => (job.steps ?? []).map(step => executableLines(step.run ?? '')).join('\n');+ const usesToken = (job: PhpJob): boolean =>+ (job.steps ?? []).some(step =>+ JSON.stringify((step as { env?: unknown }).env ?? {}).includes('PHP_SDK_SPLIT_TOKEN'),+ );++ it('runs the tests in a job that holds no token, and the push in a job that needs it', () => {+ const entries = Object.entries(jobs());+ const testers = entries.filter(([, job]) => /\bphpunit\b/.test(runsOf(job)));+ const pushers = entries.filter(([, job]) => usesToken(job));+ expect(testers.map(([name]) => name)).toEqual(['test']);+ expect(pushers.map(([name]) => name)).toEqual(['release']);+ expect(usesToken(jobs().test)).toBe(false);+ expect([jobs().release.needs].flat()).toContain('test');+ });++ it('runs no PHP, Composer or project script in the job that holds the token', () => {+ const run = runsOf(jobs().release);+ const commands = run.split('\n').map(line => line.trim().split(/\s+/)[0]);+ expect(commands.filter(command => ['composer', 'php', 'phpunit'].includes(command))).toEqual([]);+ expect(run).not.toMatch(/vendor\/bin|\.\/scripts\//);+ });+it('names core.hooksPath on the push that carries the token', () => {const pushes = runCommandsOf('php-sdk-release.yml').flatMap(run => run.split('\n'))