From 3b2dc20ddda3bed67e5076429e3a45a8b2565c50 Mon Sep 17 00:00:00 2001 From: Ulysse Mavrocordatos Date: Mon, 22 Sep 2025 16:59:43 +0200 Subject: [PATCH] Make typescript v2 workflow reusable --- .github/workflows/reusable-ci.yml | 71 +++++++++++ .github/workflows/reusable-examples.yml | 42 +++++++ .github/workflows/reusable-pre-commit.yml | 95 +++++++++++++++ .../workflows/reusable-typescript-test.yml | 56 +++++++++ .github/workflows/test.yml | 114 +++++------------- 5 files changed, 293 insertions(+), 85 deletions(-) create mode 100644 .github/workflows/reusable-ci.yml create mode 100644 .github/workflows/reusable-examples.yml create mode 100644 .github/workflows/reusable-pre-commit.yml create mode 100644 .github/workflows/reusable-typescript-test.yml diff --git a/.github/workflows/reusable-ci.yml b/.github/workflows/reusable-ci.yml new file mode 100644 index 000000000000..19e1ac62bb7d --- /dev/null +++ b/.github/workflows/reusable-ci.yml @@ -0,0 +1,71 @@ +name: Reusable Complete CI Workflow + +on: + workflow_call: + inputs: + target-branch: + description: 'Branch to checkout and test (defaults to the calling branch)' + required: false + type: string + default: '' + node-versions: + description: 'JSON array of Node.js versions to test against' + required: false + type: string + default: '["18", "20"]' + platforms: + description: 'JSON array of platforms to run tests on' + required: false + type: string + default: '["ubuntu-latest"]' + test-script: + description: 'Test script to execute' + required: false + type: string + default: './run-tests.sh' + examples-script: + description: 'Examples script to execute' + required: false + type: string + default: './check-examples.sh' + secrets: + PIPELINE_GITHUB_APP_ID: + required: false + PIPELINE_GITHUB_APP_PRIVATE_KEY: + required: false + # Integration test secrets + DD_API_KEY: + required: false + DD_CLIENT_API_KEY: + required: false + DD_CLIENT_APP_KEY: + required: false + SLEEP_AFTER_REQUEST: + required: false + +jobs: + pre-commit: + uses: ./.github/workflows/reusable-pre-commit.yml + with: + target-branch: ${{ inputs.target-branch }} + enable-commit-changes: false # Don't auto-commit in external CI + secrets: + PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }} + PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }} + + test: + uses: ./.github/workflows/reusable-typescript-test.yml + with: + target-branch: ${{ inputs.target-branch }} + node-versions: ${{ inputs.node-versions }} + platforms: ${{ inputs.platforms }} + test-script: ${{ inputs.test-script }} + secrets: + PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }} + PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }} + + # examples: + # uses: ./.github/workflows/reusable-examples.yml + # with: + # target-branch: ${{ inputs.target-branch }} + # examples-script: ${{ inputs.examples-script }} \ No newline at end of file diff --git a/.github/workflows/reusable-examples.yml b/.github/workflows/reusable-examples.yml new file mode 100644 index 000000000000..37aab269f517 --- /dev/null +++ b/.github/workflows/reusable-examples.yml @@ -0,0 +1,42 @@ +name: Reusable Examples Workflow + +on: + workflow_call: + inputs: + target-branch: + description: 'Branch to checkout and test (defaults to the calling branch)' + required: false + type: string + default: '' + examples-script: + description: 'Examples script to execute' + required: false + type: string + default: './check-examples.sh' + node-version: + description: 'Node.js version to use for examples' + required: false + type: string + default: '16' + +jobs: + examples: + runs-on: ubuntu-latest + if: (github.event.pull_request.draft == false && !contains(github.event.pull_request.labels.*.name, 'ci/skip') && !contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) || github.event_name == 'schedule' + steps: + - uses: actions/checkout@v3 + with: + repository: DataDog/datadog-api-client-typescript + ref: ${{ inputs.target-branch || github.ref }} + - name: Enable Corepack + run: corepack enable + env: + COREPACK_ENABLE_DOWNLOAD_PROMPT: 0 + - name: Set up Node ${{ inputs.node-version }} + uses: actions/setup-node@v3 + with: + node-version: ${{ inputs.node-version }} + cache: 'yarn' + - name: Check examples + run: ${{ inputs.examples-script }} + shell: bash \ No newline at end of file diff --git a/.github/workflows/reusable-pre-commit.yml b/.github/workflows/reusable-pre-commit.yml new file mode 100644 index 000000000000..b3175f9fbc26 --- /dev/null +++ b/.github/workflows/reusable-pre-commit.yml @@ -0,0 +1,95 @@ +name: Reusable Pre-commit Workflow + +on: + workflow_call: + inputs: + target-branch: + description: 'Branch to checkout and test (defaults to the calling branch)' + required: false + type: string + default: '' + enable-commit-changes: + description: 'Whether to commit and push pre-commit fixes' + required: false + type: boolean + default: true + secrets: + PIPELINE_GITHUB_APP_ID: + required: false + PIPELINE_GITHUB_APP_PRIVATE_KEY: + required: false + +env: + GIT_AUTHOR_EMAIL: "packages@datadoghq.com" + GIT_AUTHOR_NAME: "ci.datadog-api-spec" + +jobs: + pre-commit: + runs-on: ubuntu-latest + if: > + (github.event.pull_request.draft == false && + !contains(github.event.pull_request.labels.*.name, 'ci/skip') && + !contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) || + github.event_name == 'schedule' + steps: + - name: Get GitHub App token + id: get_token + if: inputs.enable-commit-changes && github.event.pull_request.head.repo.full_name == github.repository + uses: actions/create-github-app-token@v1 + with: + app-id: ${{ secrets.PIPELINE_GITHUB_APP_ID }} + private-key: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }} + - uses: actions/checkout@v3 + if: github.event.pull_request.head.repo.full_name == github.repository + with: + repository: DataDog/datadog-api-client-typescript + ref: ${{ inputs.target-branch || github.event.pull_request.head.sha || github.ref }} + fetch-depth: 50 + token: ${{ inputs.enable-commit-changes && steps.get_token.outputs.token || github.token }} + - uses: actions/checkout@v3 + if: github.event.pull_request.head.repo.full_name != github.repository + with: + repository: DataDog/datadog-api-client-typescript + ref: ${{ inputs.target-branch || github.event.pull_request.head.sha || github.ref }} + fetch-depth: 50 + - uses: actions/setup-python@v4 + with: + python-version: '3.11' + - name: Install pre-commit + run: python -m pip install pre-commit + - name: set PY + run: echo "PY=$(python -c 'import hashlib, sys, platform;print(hashlib.sha256(platform.python_version().encode()+sys.executable.encode()).hexdigest())')" >> $GITHUB_ENV + - uses: actions/cache@v3 + with: + path: ~/.cache/pre-commit + key: pre-commit|split-package|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }} + - name: Fetch v2 branch + run: | + git fetch origin v2 --depth=50 + - name: Determine pre-commit range + id: commit_range + run: | + FROM_REF=$(git merge-base HEAD origin/v2) + echo "from_ref=$FROM_REF" >> $GITHUB_OUTPUT + echo "to_ref=HEAD" >> $GITHUB_OUTPUT + echo "Pre-commit will check from $FROM_REF to HEAD" + - id: pre_commit + name: Run generate to fix lint and format + if: github.event.action != 'closed' && github.event.pull_request.merged != true + run: bash -c "./generate.sh" + - name: Commit changes + if: failure() && inputs.enable-commit-changes && github.event.pull_request.head.repo.full_name == github.repository + run: |- + git add -A + git config user.name "${GIT_AUTHOR_NAME}" + git config user.email "${GIT_AUTHOR_EMAIL}" + git commit -m "pre-commit fixes" + git push origin "HEAD:${HEAD_REF}" + exit 1 + env: + HEAD_REF: ${{ github.event.pull_request.head.ref }} + - id: pre_commit_schedule + name: Run pre-commit in schedule + if: github.event_name == 'schedule' + run: | + pre-commit run --all-files --show-diff-on-failure --color=always \ No newline at end of file diff --git a/.github/workflows/reusable-typescript-test.yml b/.github/workflows/reusable-typescript-test.yml new file mode 100644 index 000000000000..53b6e48cc7bd --- /dev/null +++ b/.github/workflows/reusable-typescript-test.yml @@ -0,0 +1,56 @@ +name: Reusable TypeScript Testing Workflow + +on: + workflow_call: + inputs: + target-branch: + description: 'Branch to checkout and test (defaults to the calling branch)' + required: false + type: string + default: '' + node-versions: + description: 'JSON array of Node.js versions to test against' + required: false + type: string + default: '["18", "20"]' + platforms: + description: 'JSON array of platforms to run tests on' + required: false + type: string + default: '["ubuntu-latest"]' + test-script: + description: 'Test script to execute' + required: false + type: string + default: './run-tests.sh' + secrets: + PIPELINE_GITHUB_APP_ID: + required: false + PIPELINE_GITHUB_APP_PRIVATE_KEY: + required: false + +jobs: + test: + strategy: + matrix: + node-version: ${{ fromJSON(inputs.node-versions) }} + platform: ${{ fromJSON(inputs.platforms) }} + runs-on: ${{ matrix.platform }} + if: (github.event.pull_request.draft == false && !contains(github.event.pull_request.labels.*.name, 'ci/skip') && !contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) || github.event_name == 'schedule' + steps: + - uses: actions/checkout@v3 + with: + repository: DataDog/datadog-api-client-typescript + ref: ${{ inputs.target-branch || github.ref }} + - name: Enable Corepack + run: corepack enable + env: + COREPACK_ENABLE_DOWNLOAD_PROMPT: 0 + - name: Set up Node ${{ matrix.node-version }} + uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node-version }} + cache: 'yarn' + - name: Test + run: ${{ inputs.test-script }} + shell: bash \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 534505aa0013..cee61ff71a6b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -20,99 +20,43 @@ concurrency: jobs: pre-commit: - runs-on: ubuntu-latest if: > (github.event.pull_request.draft == false && !contains(github.event.pull_request.labels.*.name, 'ci/skip') && !contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) || github.event_name == 'schedule' - steps: - # Run only in this repository - - name: Get GitHub App token - id: get_token - if: github.event.pull_request.head.repo.full_name == github.repository - uses: actions/create-github-app-token@v1 - with: - app-id: ${{ secrets.PIPELINE_GITHUB_APP_ID }} - private-key: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }} - - uses: actions/checkout@v3 - if: github.event.pull_request.head.repo.full_name == github.repository - with: - ref: ${{ github.event.pull_request.head.sha }} - token: ${{ steps.get_token.outputs.token }} - - uses: actions/setup-python@v4 - with: - python-version: '3.11' - # Fetch a fork of the repo - - uses: actions/checkout@v3 - if: github.event.pull_request.head.repo.full_name != github.repository - with: - fetch-depth: 0 - ref: ${{ github.event.pull_request.head.sha }} - - name: Install pre-commit - run: python -m pip install pre-commit - - name: set PY - run: echo "PY=$(python -c 'import hashlib, sys, platform;print(hashlib.sha256(platform.python_version().encode()+sys.executable.encode()).hexdigest())')" >> $GITHUB_ENV - - uses: actions/cache@v3 - with: - path: ~/.cache/pre-commit - key: pre-commit|split-package|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }} - - id: pre_commit - name: Run generate to fix lint and format - if: github.event.action != 'closed' && github.event.pull_request.merged != true - run: bash -c "./generate.sh" - - name: Commit changes - if: github.event.pull_request.head.repo.full_name == github.repository && failure() - run: |- - git add -A - git config user.name "${GIT_AUTHOR_NAME}" - git config user.email "${GIT_AUTHOR_EMAIL}" - git commit -m "pre-commit fixes" - git push origin "HEAD:${HEAD_REF}" - exit 1 - env: - HEAD_REF: ${{ github.event.pull_request.head.ref }} - - id: pre_commit_schedule - name: Run pre-commit in schedule - if: github.event_name == 'schedule' - run: | - pre-commit run --all-files --show-diff-on-failure --color=always + uses: ./.github/workflows/reusable-pre-commit.yml + with: + enable-commit-changes: true + secrets: + PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }} + PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }} test: - strategy: - matrix: - node-version: ["18", "20"] - platform: [ubuntu-latest] - runs-on: ${{ matrix.platform }} - if: (github.event.pull_request.draft == false && !contains(github.event.pull_request.labels.*.name, 'ci/skip') && !contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) || github.event_name == 'schedule' - steps: - - uses: actions/checkout@v3 - - name: Enable Corepack - run: corepack enable - env: - COREPACK_ENABLE_DOWNLOAD_PROMPT: 0 - - name: Set up Node ${{ matrix.node-version }} - uses: actions/setup-node@v3 - with: - node-version: ${{ matrix.node-version }} - cache: 'yarn' - - name: Test - run: ./run-tests.sh - shell: bash + if: > + (github.event.pull_request.draft == false && + !contains(github.event.pull_request.labels.*.name, 'ci/skip') && + !contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) || + github.event_name == 'schedule' + uses: ./.github/workflows/reusable-typescript-test.yml + with: + node-versions: '["18", "20"]' + platforms: '["ubuntu-latest"]' + test-script: './run-tests.sh' + secrets: + PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }} + PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }} # examples: - # runs-on: ubuntu-latest - # if: (github.event.pull_request.draft == false && !contains(github.event.pull_request.labels.*.name, 'ci/skip') && !contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) || github.event_name == 'schedule' - # steps: - # - uses: actions/checkout@v3 - # - name: Set up Node 16 - # uses: actions/setup-node@v3 - # with: - # node-version: 16 - # cache: 'yarn' - # - name: Check examples - # run: ./check-examples.sh - # shell: bash + # if: > + # (github.event.pull_request.draft == false && + # !contains(github.event.pull_request.labels.*.name, 'ci/skip') && + # !contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) || + # github.event_name == 'schedule' + # uses: ./.github/workflows/reusable-examples.yml + # with: + # examples-script: './check-examples.sh' + # node-version: '16' report: runs-on: ubuntu-latest @@ -139,7 +83,7 @@ jobs: with: github-token: ${{ steps.get_token.outputs.token }} repo: datadog-api-spec - # status: ${{ (needs.test.result == 'cancelled' || needs.examples.result == 'cancelled') && 'pending' || needs.test.result == 'success' && needs.examples.result == 'success' && 'success' || 'failure' }} + # status: ${{ (needs.test.result == 'cancelled' || needs.examples.result == 'cancelled') && 'pending' || (needs.test.result == 'success' && needs.examples.result == 'success') && 'success' || 'failure' }} status: ${{ needs.test.result == 'cancelled' && 'pending' || needs.test.result == 'success' && 'success' || 'failure' }} context: v2/unit pull-request: ${{ steps.get_pr_number.outputs.spec_pr }}