1+ name : Reusable TypeScript Testing Workflow
2+
3+ on :
4+ workflow_call :
5+ inputs :
6+ node-versions :
7+ description : ' JSON array of Node.js versions to test against'
8+ required : false
9+ type : string
10+ default : ' ["16", "18"]'
11+ platforms :
12+ description : ' JSON array of platforms to run tests on'
13+ required : false
14+ type : string
15+ default : ' ["ubuntu-latest"]'
16+ test-script :
17+ description : ' Test script to execute'
18+ required : false
19+ type : string
20+ default : ' ./run-tests.sh'
21+ examples-script :
22+ description : ' Examples script to execute'
23+ required : false
24+ type : string
25+ default : ' ./check-examples.sh'
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 : ' master/unit'
36+ secrets :
37+ PIPELINE_GITHUB_APP_ID :
38+ required : false
39+ PIPELINE_GITHUB_APP_PRIVATE_KEY :
40+ required : false
41+
42+ jobs :
43+ pre-commit :
44+ runs-on : ubuntu-latest
45+ if : >
46+ (github.event.pull_request.draft == false &&
47+ !contains(github.event.pull_request.labels.*.name, 'ci/skip') &&
48+ !contains(github.event.pull_request.head.ref, 'datadog-api-spec/test/')) ||
49+ github.event_name == 'schedule'
50+ steps :
51+ # Run only in this repository
52+ - name : Get GitHub App token
53+ id : get_token
54+ if : github.event.pull_request.head.repo.full_name == github.repository
55+ uses : actions/create-github-app-token@v1
56+ with :
57+ app-id : ${{ secrets.PIPELINE_GITHUB_APP_ID }}
58+ private-key : ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
59+ - uses : actions/checkout@v3
60+ if : github.event.pull_request.head.repo.full_name == github.repository
61+ with :
62+ fetch-depth : 0
63+ ref : ${{ github.event.pull_request.head.sha }}
64+ token : ${{ steps.get_token.outputs.token }}
65+ - uses : actions/setup-python@v4
66+ with :
67+ python-version : ' 3.11'
68+ # Fetch a fork of the repo
69+ - uses : actions/checkout@v3
70+ if : github.event.pull_request.head.repo.full_name != github.repository
71+ with :
72+ fetch-depth : 0
73+ ref : ${{ github.event.pull_request.head.sha }}
74+ - name : Install pre-commit
75+ run : python -m pip install pre-commit
76+ - name : set PY
77+ run : echo "PY=$(python -c 'import hashlib, sys, platform;print(hashlib.sha256(platform.python_version().encode()+sys.executable.encode()).hexdigest())')" >> $GITHUB_ENV
78+ - uses : actions/cache@v3
79+ with :
80+ path : ~/.cache/pre-commit
81+ key : pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
82+ - id : pre_commit
83+ name : Run pre-commit
84+ if : github.event.action != 'closed' && github.event.pull_request.merged != true
85+ run : |
86+ pre-commit run --from-ref "${FROM_REF}" --to-ref "${TO_REF}" --show-diff-on-failure --color=always
87+ env :
88+ FROM_REF : ${{ github.event.pull_request.base.sha }}
89+ TO_REF : ${{ github.event.pull_request.head.sha }}
90+ - name : Commit changes
91+ if : github.event.pull_request.head.repo.full_name == github.repository && failure()
92+ run : |-
93+ git add -A
94+ git config user.name "${GIT_AUTHOR_NAME}"
95+ git config user.email "${GIT_AUTHOR_EMAIL}"
96+ git commit -m "pre-commit fixes"
97+ git push origin "HEAD:${HEAD_REF}"
98+ exit 1
99+ env :
100+ HEAD_REF : ${{ github.event.pull_request.head.ref }}
101+ GIT_AUTHOR_EMAIL : " packages@datadoghq.com"
102+ GIT_AUTHOR_NAME : " ci.datadog-api-spec"
103+ - id : pre_commit_schedule
104+ name : Run pre-commit in schedule
105+ if : github.event_name == 'schedule'
106+ run : |
107+ pre-commit run --all-files --show-diff-on-failure --color=always
108+
109+ test :
110+ strategy :
111+ matrix :
112+ node-version : ${{ fromJSON(inputs.node-versions) }}
113+ platform : ${{ fromJSON(inputs.platforms) }}
114+ runs-on : ${{ matrix.platform }}
115+ 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'
116+ steps :
117+ - uses : actions/checkout@v3
118+ - name : Set up Node ${{ matrix.node-version }}
119+ uses : actions/setup-node@v3
120+ with :
121+ node-version : ${{ matrix.node-version }}
122+ cache : ' yarn'
123+ - name : Test
124+ run : ${{ inputs.test-script }}
125+ shell : bash
126+
127+ examples :
128+ runs-on : ubuntu-latest
129+ 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'
130+ steps :
131+ - uses : actions/checkout@v3
132+ - name : Set up Node 16
133+ uses : actions/setup-node@v3
134+ with :
135+ node-version : 16
136+ cache : ' yarn'
137+ - name : Check examples
138+ run : ${{ inputs.examples-script }}
139+ shell : bash
140+
141+ report :
142+ runs-on : ubuntu-latest
143+ if : always() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/') && inputs.enable-status-reporting
144+ needs :
145+ - test
146+ - examples
147+ steps :
148+ - name : Get GitHub App token
149+ if : github.event_name == 'pull_request'
150+ id : get_token
151+ uses : actions/create-github-app-token@v1
152+ with :
153+ app-id : ${{ secrets.PIPELINE_GITHUB_APP_ID }}
154+ private-key : ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
155+ repositories : datadog-api-spec
156+ - name : Post status check
157+ uses : DataDog/github-actions/post-status-check@v2
158+ with :
159+ github-token : ${{ steps.get_token.outputs.token }}
160+ repo : datadog-api-spec
161+ status : ${{ (needs.test.result == 'cancelled' || needs.examples.result == 'cancelled') && 'pending' || needs.test.result == 'success' && needs.examples.result == 'success' && 'success' || 'failure' }}
162+ context : ${{ inputs.status-context }}
0 commit comments