Skip to content

Commit bf9ac8f

Browse files
committed
Make TypeScript test workflow reusable
1 parent 3465b80 commit bf9ac8f

File tree

7 files changed

+421
-140
lines changed

7 files changed

+421
-140
lines changed

.github/workflows/reusable-ci.yml

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
name: Reusable Complete CI Workflow
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
target-branch:
7+
description: 'Branch to checkout and test (defaults to the calling branch)'
8+
required: false
9+
type: string
10+
default: ''
11+
enable-commit-changes:
12+
description: 'Whether to commit and push pre-commit fixes'
13+
required: false
14+
type: boolean
15+
default: true
16+
enable-status-reporting:
17+
description: 'Whether to enable status reporting'
18+
required: false
19+
type: boolean
20+
default: true
21+
status-context:
22+
description: 'Context for status checks'
23+
required: false
24+
type: string
25+
default: 'master/unit'
26+
target-repo:
27+
description: 'Repository to post status to'
28+
required: false
29+
type: string
30+
default: 'datadog-api-spec'
31+
node-version:
32+
description: 'Node.js version to use'
33+
required: false
34+
type: string
35+
default: '16'
36+
37+
secrets:
38+
PIPELINE_GITHUB_APP_ID:
39+
required: false
40+
PIPELINE_GITHUB_APP_PRIVATE_KEY:
41+
required: false
42+
# Integration test secrets
43+
DD_API_KEY:
44+
required: false
45+
DD_CLIENT_API_KEY:
46+
required: false
47+
DD_CLIENT_APP_KEY:
48+
required: false
49+
SLEEP_AFTER_REQUEST:
50+
required: false
51+
52+
jobs:
53+
pre-commit:
54+
uses: ./.github/workflows/reusable-pre-commit.yml
55+
with:
56+
target-branch: ${{ inputs.target-branch }}
57+
enable-commit-changes: ${{ inputs.enable-commit-changes }}
58+
secrets:
59+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
60+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
61+
62+
test:
63+
uses: ./.github/workflows/reusable-typescript-test.yml
64+
with:
65+
target-branch: ${{ inputs.target-branch }}
66+
node-versions: '["16", "18"]'
67+
platforms: '["ubuntu-latest"]'
68+
test-script: './run-tests.sh'
69+
enable-status-reporting: false # We handle reporting in the main report job
70+
status-context: ${{ inputs.status-context }}
71+
secrets:
72+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
73+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
74+
75+
examples:
76+
uses: ./.github/workflows/reusable-examples.yml
77+
with:
78+
target-branch: ${{ inputs.target-branch }}
79+
examples-script: './check-examples.sh'
80+
node-version: ${{ inputs.node-version }}
81+
82+
integration:
83+
uses: ./.github/workflows/reusable-integration-test.yml
84+
with:
85+
target-branch: ${{ inputs.target-branch }}
86+
enable-status-reporting: false # We handle reporting in the main report job
87+
status-context: 'integration'
88+
target-repo: ${{ inputs.target-repo }}
89+
node-version: ${{ inputs.node-version }}
90+
secrets:
91+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
92+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
93+
DD_API_KEY: ${{ secrets.DD_API_KEY }}
94+
DD_CLIENT_API_KEY: ${{ secrets.DD_CLIENT_API_KEY }}
95+
DD_CLIENT_APP_KEY: ${{ secrets.DD_CLIENT_APP_KEY }}
96+
SLEEP_AFTER_REQUEST: ${{ secrets.SLEEP_AFTER_REQUEST }}
97+
98+
report:
99+
uses: ./.github/workflows/reusable-report.yml
100+
needs:
101+
- test
102+
- examples
103+
- integration
104+
if: always()
105+
with:
106+
test-result: ${{ needs.test.result }}
107+
examples-result: ${{ needs.examples.result }}
108+
integration-result: ${{ needs.integration.result }}
109+
context: ${{ inputs.status-context }}
110+
target-repo: ${{ inputs.target-repo }}
111+
enable-status-reporting: ${{ inputs.enable-status-reporting }}
112+
secrets:
113+
PIPELINE_GITHUB_APP_ID: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
114+
PIPELINE_GITHUB_APP_PRIVATE_KEY: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Reusable Examples Workflow
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
target-branch:
7+
description: 'Branch to checkout and test (defaults to the calling branch)'
8+
required: false
9+
type: string
10+
default: ''
11+
examples-script:
12+
description: 'Examples script to execute'
13+
required: false
14+
type: string
15+
default: './check-examples.sh'
16+
node-version:
17+
description: 'Node.js version to use'
18+
required: false
19+
type: string
20+
default: '16'
21+
22+
jobs:
23+
examples:
24+
runs-on: ubuntu-latest
25+
if: >
26+
(github.event.pull_request.draft == false &&
27+
!contains(github.event.pull_request.labels.*.name, 'ci/skip') &&
28+
!contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) ||
29+
github.event_name == 'schedule'
30+
steps:
31+
- uses: actions/checkout@v3
32+
with:
33+
ref: ${{ inputs.target-branch || github.ref }}
34+
- name: Set up Node ${{ inputs.node-version }}
35+
uses: actions/setup-node@v3
36+
with:
37+
node-version: ${{ inputs.node-version }}
38+
cache: 'yarn'
39+
- name: Check examples
40+
run: ${{ inputs.examples-script }}
41+
shell: bash

.github/workflows/test_integration.yml renamed to .github/workflows/reusable-integration-test.yml

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,68 @@
1-
name: Run Integration Tests
1+
name: Reusable Integration Test Workflow
22

33
permissions:
44
contents: read
55

66
on:
77
pull_request:
8+
branches:
9+
- master
810
types:
911
- opened
1012
- reopened
1113
- ready_for_review
1214
- synchronize
1315
- labeled
1416
- unlabeled
15-
branches:
16-
- master
1717
schedule:
1818
- cron: "0 4 * * *"
19+
workflow_call:
20+
inputs:
21+
target-branch:
22+
description: 'Branch to checkout and test (defaults to the calling branch)'
23+
required: false
24+
type: string
25+
default: ''
26+
enable-status-reporting:
27+
description: 'Whether to post status checks to datadog-api-spec repo'
28+
required: false
29+
type: boolean
30+
default: false
31+
status-context:
32+
description: 'Context for status checks'
33+
required: false
34+
type: string
35+
default: 'integration'
36+
target-repo:
37+
description: 'Repository to post status to'
38+
required: false
39+
type: string
40+
default: 'datadog-api-spec'
41+
node-version:
42+
description: 'Node.js version to use'
43+
required: false
44+
type: string
45+
default: '16'
46+
secrets:
47+
PIPELINE_GITHUB_APP_ID:
48+
required: false
49+
PIPELINE_GITHUB_APP_PRIVATE_KEY:
50+
required: false
51+
DD_API_KEY:
52+
required: true
53+
DD_CLIENT_API_KEY:
54+
required: true
55+
DD_CLIENT_APP_KEY:
56+
required: true
57+
SLEEP_AFTER_REQUEST:
58+
required: false
1959

2060
concurrency:
2161
group: integration-${{ github.head_ref }}
2262
cancel-in-progress: true
2363

2464
jobs:
25-
test_integration:
65+
integration_tests:
2666
runs-on: ubuntu-latest
2767
if: >
2868
(github.event_name == 'pull_request' &&
@@ -48,21 +88,23 @@ jobs:
4888
with:
4989
app-id: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
5090
private-key: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
51-
repositories: datadog-api-spec
91+
repositories: ${{ inputs.target-repo || 'datadog-api-spec' }}
5292
- name: Checkout code
5393
uses: actions/checkout@v3
94+
with:
95+
ref: ${{ inputs.target-branch || github.ref }}
5496
- name: Post pending status check
55-
if: github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/')
97+
if: github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/') && (inputs.enable-status-reporting || github.event_name != 'workflow_call')
5698
uses: DataDog/github-actions/post-status-check@v2
5799
with:
58100
github-token: ${{ steps.get_token.outputs.token }}
59-
repo: datadog-api-spec
101+
repo: ${{ inputs.target-repo || 'datadog-api-spec' }}
60102
status: pending
61-
context: integration
62-
- name: Set up Node 16
103+
context: ${{ inputs.status-context || 'integration' }}
104+
- name: Set up Node ${{ inputs.node-version }}
63105
uses: actions/setup-node@v3
64106
with:
65-
node-version: 16
107+
node-version: ${{ inputs.node-version }}
66108
cache: 'yarn'
67109
- name: Run integration tests
68110
run: ./run-tests.sh
@@ -77,20 +119,20 @@ jobs:
77119
DD_TEST_CLIENT_API_KEY: ${{ secrets.DD_CLIENT_API_KEY }}
78120
DD_TEST_CLIENT_APP_KEY: ${{ secrets.DD_CLIENT_APP_KEY }}
79121
RECORD: "none"
80-
SLEEP_AFTER_REQUEST: "${{ vars.SLEEP_AFTER_REQUEST }}"
122+
SLEEP_AFTER_REQUEST: ${{ secrets.SLEEP_AFTER_REQUEST || vars.SLEEP_AFTER_REQUEST }}
81123
- name: Post failure status check
82-
if: failure() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/')
124+
if: failure() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/') && (inputs.enable-status-reporting || github.event_name != 'workflow_call')
83125
uses: DataDog/github-actions/post-status-check@v2
84126
with:
85127
github-token: ${{ steps.get_token.outputs.token }}
86-
repo: datadog-api-spec
128+
repo: ${{ inputs.target-repo || 'datadog-api-spec' }}
87129
status: failure
88-
context: integration
130+
context: ${{ inputs.status-context || 'integration' }}
89131
- name: Post success status check
90-
if: "!failure() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/')"
132+
if: "!failure() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/') && (inputs.enable-status-reporting || github.event_name != 'workflow_call')"
91133
uses: DataDog/github-actions/post-status-check@v2
92134
with:
93135
github-token: ${{ steps.get_token.outputs.token }}
94-
repo: datadog-api-spec
136+
repo: ${{ inputs.target-repo || 'datadog-api-spec' }}
95137
status: success
96-
context: integration
138+
context: ${{ inputs.status-context || 'integration' }}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
name: Reusable Pre-commit Workflow
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
target-branch:
7+
description: 'Branch to checkout and test (defaults to the calling branch)'
8+
required: false
9+
type: string
10+
default: ''
11+
enable-commit-changes:
12+
description: 'Whether to commit and push pre-commit fixes'
13+
required: false
14+
type: boolean
15+
default: true
16+
secrets:
17+
PIPELINE_GITHUB_APP_ID:
18+
required: false
19+
PIPELINE_GITHUB_APP_PRIVATE_KEY:
20+
required: false
21+
22+
env:
23+
GIT_AUTHOR_EMAIL: "[email protected]"
24+
GIT_AUTHOR_NAME: "ci.datadog-api-spec"
25+
26+
jobs:
27+
pre-commit:
28+
runs-on: ubuntu-latest
29+
if: >
30+
(github.event.pull_request.draft == false &&
31+
!contains(github.event.pull_request.labels.*.name, 'ci/skip') &&
32+
!contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) ||
33+
github.event_name == 'schedule'
34+
steps:
35+
# Run only in this repository
36+
- name: Get GitHub App token
37+
id: get_token
38+
if: inputs.enable-commit-changes && github.event.pull_request.head.repo.full_name == github.repository
39+
uses: actions/create-github-app-token@v1
40+
with:
41+
app-id: ${{ secrets.PIPELINE_GITHUB_APP_ID }}
42+
private-key: ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
43+
- uses: actions/checkout@v3
44+
if: github.event.pull_request.head.repo.full_name == github.repository
45+
with:
46+
fetch-depth: 0
47+
ref: ${{ inputs.target-branch || github.event.pull_request.head.sha || github.ref }}
48+
token: ${{ inputs.enable-commit-changes && steps.get_token.outputs.token || github.token }}
49+
- uses: actions/setup-python@v4
50+
with:
51+
python-version: '3.11'
52+
# Fetch a fork of the repo
53+
- uses: actions/checkout@v3
54+
if: github.event.pull_request.head.repo.full_name != github.repository
55+
with:
56+
fetch-depth: 0
57+
ref: ${{ inputs.target-branch || github.event.pull_request.head.sha || github.ref }}
58+
- name: Install pre-commit
59+
run: python -m pip install pre-commit
60+
- name: set PY
61+
run: echo "PY=$(python -c 'import hashlib, sys, platform;print(hashlib.sha256(platform.python_version().encode()+sys.executable.encode()).hexdigest())')" >> $GITHUB_ENV
62+
- uses: actions/cache@v3
63+
with:
64+
path: ~/.cache/pre-commit
65+
key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
66+
- id: pre_commit
67+
name: Run pre-commit
68+
if: github.event.action != 'closed' && github.event.pull_request.merged != true
69+
run: |
70+
pre-commit run --from-ref "${FROM_REF}" --to-ref "${TO_REF}" --show-diff-on-failure --color=always
71+
env:
72+
FROM_REF: ${{ github.event.pull_request.base.sha }}
73+
TO_REF: ${{ github.event.pull_request.head.sha }}
74+
- name: Commit changes
75+
if: github.event.pull_request.head.repo.full_name == github.repository && failure() && inputs.enable-commit-changes
76+
run: |-
77+
git add -A
78+
git config user.name "${GIT_AUTHOR_NAME}"
79+
git config user.email "${GIT_AUTHOR_EMAIL}"
80+
git commit -m "pre-commit fixes"
81+
git push origin "HEAD:${HEAD_REF}"
82+
exit 1
83+
env:
84+
HEAD_REF: ${{ github.event.pull_request.head.ref }}
85+
- id: pre_commit_schedule
86+
name: Run pre-commit in schedule
87+
if: github.event_name == 'schedule'
88+
run: |
89+
pre-commit run --all-files --show-diff-on-failure --color=always

0 commit comments

Comments
 (0)