-
Notifications
You must be signed in to change notification settings - Fork 32
88 lines (85 loc) · 3.29 KB
/
reusable-pre-commit.yml
File metadata and controls
88 lines (85 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
env:
GIT_AUTHOR_EMAIL: "packages@datadoghq.com"
GIT_AUTHOR_NAME: "ci.datadog-api-spec"
jobs:
pre-commit:
runs-on: ubuntu-latest
permissions:
id-token: write # Required for dd-octo-sts OIDC token
steps:
- name: Get GitHub token via dd-octo-sts
id: get_token
if: inputs.enable-commit-changes
uses: DataDog/dd-octo-sts-action@acaa02eee7e3bb0839e4272dacb37b8f3b58ba80 # v1.0.3
with:
scope: DataDog/datadog-api-client-java
policy: self.github.pre-commit.pull-requests
- uses: actions/checkout@v3
with:
fetch-depth: 0
repository: DataDog/datadog-api-client-java
ref: ${{ inputs.target-branch || github.event.pull_request.head.sha || github.ref }}
token: ${{ inputs.enable-commit-changes && steps.get_token.outputs.token || github.token }}
- 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 platform;print(platform.python_version())')" >> $GITHUB_ENV
- uses: actions/cache@v3
with:
path: ~/.cache/pre-commit
key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
- name: Install Java
uses: actions/setup-java@v3
with:
java-version: "16"
distribution: "temurin"
cache: "maven"
- name: Determine pre-commit range
id: commit_range
run: |
FROM_REF=$(git merge-base HEAD origin/master)
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 pre-commit
if: github.event.action != 'closed' && github.event.pull_request.merged != true
run: |
wget https://github.com/google/google-java-format/releases/download/v1.16.0/google-java-format-1.16.0-all-deps.jar -O google-java-format.jar
pre-commit run --verbose --from-ref "${FROM_REF}" --to-ref "${TO_REF}" --show-diff-on-failure --color=always
env:
FROM_REF: ${{ steps.commit_range.outputs.from_ref }}
TO_REF: ${{ steps.commit_range.outputs.to_ref }}
- name: Commit changes
if: failure() && inputs.enable-commit-changes
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