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
+ - name : Get GitHub App token
36
+ id : get_token
37
+ if : inputs.enable-commit-changes && github.event.pull_request.head.repo.full_name == github.repository
38
+ uses : actions/create-github-app-token@v1
39
+ with :
40
+ app-id : ${{ secrets.PIPELINE_GITHUB_APP_ID }}
41
+ private-key : ${{ secrets.PIPELINE_GITHUB_APP_PRIVATE_KEY }}
42
+ - uses : actions/checkout@v3
43
+ if : github.event.pull_request.head.repo.full_name == github.repository
44
+ with :
45
+ repository : DataDog/datadog-api-client-typescript
46
+ ref : ${{ inputs.target-branch || github.event.pull_request.head.sha || github.ref }}
47
+ fetch-depth : 50
48
+ token : ${{ inputs.enable-commit-changes && steps.get_token.outputs.token || github.token }}
49
+ - uses : actions/checkout@v3
50
+ if : github.event.pull_request.head.repo.full_name != github.repository
51
+ with :
52
+ repository : DataDog/datadog-api-client-typescript
53
+ ref : ${{ inputs.target-branch || github.event.pull_request.head.sha || github.ref }}
54
+ fetch-depth : 50
55
+ - uses : actions/setup-python@v4
56
+ with :
57
+ python-version : ' 3.11'
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|split-package|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
66
+ - name : Fetch v2 branch
67
+ run : |
68
+ git fetch origin v2 --depth=50
69
+ - name : Determine pre-commit range
70
+ id : commit_range
71
+ run : |
72
+ FROM_REF=$(git merge-base HEAD origin/v2)
73
+ echo "from_ref=$FROM_REF" >> $GITHUB_OUTPUT
74
+ echo "to_ref=HEAD" >> $GITHUB_OUTPUT
75
+ echo "Pre-commit will check from $FROM_REF to HEAD"
76
+ - id : pre_commit
77
+ name : Run generate to fix lint and format
78
+ if : github.event.action != 'closed' && github.event.pull_request.merged != true
79
+ run : bash -c "./generate.sh"
80
+ - name : Commit changes
81
+ if : failure() && inputs.enable-commit-changes && github.event.pull_request.head.repo.full_name == github.repository
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
+ - id : pre_commit_schedule
92
+ name : Run pre-commit in schedule
93
+ if : github.event_name == 'schedule'
94
+ run : |
95
+ pre-commit run --all-files --show-diff-on-failure --color=always
0 commit comments