1+ name : Reusable Rust Testing Workflow
2+
3+ on :
4+ workflow_call :
5+ inputs :
6+ rust-versions :
7+ description : ' JSON array of Rust versions to test against'
8+ required : false
9+ type : string
10+ default : ' ["stable"]'
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-command :
22+ description : ' Examples command to execute'
23+ required : false
24+ type : string
25+ default : ' cargo check --examples'
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+ - name : Get GitHub App token
52+ if : github.event_name == 'pull_request'
53+ id : get_token
54+ uses : actions/create-github-app-token@v1
55+ with :
56+ app-id : ${{ secrets.PIPELINE_GITHUB_APP_ID }}
57+ private-key : ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
58+ repositories : datadog-api-spec
59+ - uses : actions/checkout@v3
60+ with :
61+ fetch-depth : 0
62+ ref : ${{ github.event.pull_request.head.sha }}
63+ token : ${{ steps.get_token.outputs.token }}
64+ - name : Install pre-commit
65+ run : python -m pip install pre-commit
66+ - name : set PY
67+ run : echo "PY=$(python -c 'import platform;print(platform.python_version())')" >> $GITHUB_ENV
68+ - uses : actions/cache@v3
69+ with :
70+ path : ~/.cache/pre-commit
71+ key : pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
72+ - id : pre_commit
73+ name : Run pre-commit
74+ if : github.event.action != 'closed' && github.event.pull_request.merged != true
75+ run : |
76+ pre-commit run --from-ref "${FROM_REF}" --to-ref "${TO_REF}" --show-diff-on-failure --color=always
77+ env :
78+ FROM_REF : ${{ github.event.pull_request.base.sha }}
79+ TO_REF : ${{ github.event.pull_request.head.sha }}
80+ - name : Commit changes
81+ if : ${{ failure() }}
82+ run : |-
83+ git add -A
84+ git config user.name "${GIT_AUTHOR_NAME}"
85+ git config user.email "${GIT_AUTHOR_EMAIL}"
86+ git commit -m "pre-commit fixes"
87+ git push origin "HEAD:${HEAD_REF}"
88+ exit 1
89+ env :
90+ HEAD_REF : ${{ github.event.pull_request.head.ref }}
91+ GIT_AUTHOR_EMAIL : " packages@datadoghq.com"
92+ GIT_AUTHOR_NAME : " ci.datadog-api-spec"
93+ - id : pre_commit_schedule
94+ name : Run pre-commit in schedule
95+ if : github.event_name == 'schedule'
96+ run : |
97+ pre-commit run --all-files --show-diff-on-failure --color=always
98+
99+ test :
100+ strategy :
101+ matrix :
102+ rust-version : ${{ fromJSON(inputs.rust-versions) }}
103+ platform : ${{ fromJSON(inputs.platforms) }}
104+ runs-on : ${{ matrix.platform }}
105+ 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'
106+ steps :
107+ - name : Checkout code
108+ uses : actions/checkout@v3
109+ - name : Install Rust
110+ uses : dtolnay/rust-toolchain@v1
111+ with :
112+ toolchain : ${{ matrix.rust-version }}
113+ - uses : Swatinem/rust-cache@v2
114+ - name : Test
115+ run : ${{ inputs.test-script }}
116+
117+ examples :
118+ runs-on : ubuntu-latest
119+ 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'
120+ steps :
121+ - uses : actions/checkout@v3
122+ - name : Install Rust
123+ uses : dtolnay/rust-toolchain@master
124+ with :
125+ toolchain : stable
126+ - name : Check examples
127+ run : ${{ inputs.examples-command }}
128+ shell : bash
129+
130+ report :
131+ runs-on : ubuntu-latest
132+ if : always() && github.event_name == 'pull_request' && contains(github.event.pull_request.head.ref, 'datadog-api-spec/generated/') && inputs.enable-status-reporting
133+ needs :
134+ - test
135+ - examples
136+ steps :
137+ - name : Get GitHub App token
138+ if : github.event_name == 'pull_request'
139+ id : get_token
140+ uses : actions/create-github-app-token@v1
141+ with :
142+ app-id : ${{ secrets.PIPELINE_GITHUB_APP_ID }}
143+ private-key : ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
144+ repositories : datadog-api-spec
145+ - name : Post status check
146+ uses : DataDog/github-actions/post-status-check@v2
147+ with :
148+ github-token : ${{ steps.get_token.outputs.token }}
149+ repo : datadog-api-spec
150+ status : ${{ (needs.test.result == 'cancelled' || needs.examples.result == 'cancelled') && 'pending' || needs.test.result == 'success' && needs.examples.result == 'success' && 'success' || 'failure' }}
151+ context : ${{ inputs.status-context }}
0 commit comments