-
Notifications
You must be signed in to change notification settings - Fork 205
145 lines (130 loc) · 5.39 KB
/
dc-template-lint.yml
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
name: Lint Domain Connect Templates
#version: 2024-05-10 v1
on:
pull_request_target:
branches:
- master
merge_group:
branches:
- master
jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Setup refs
id: setup-refs
run: |
echo "base_sha=${{ github.event.pull_request.base.sha }}${{ github.event.merge_group.base_sha }}" >> "$GITHUB_OUTPUT"
echo "head_sha=${{ github.event.pull_request.head.sha }}${{ github.event.merge_group.head_sha }}" >> "$GITHUB_OUTPUT"
- name: Check out code
uses: actions/checkout@v3
with:
fetch-depth: 2
ref: ${{ steps.setup-refs.outputs.head_sha }}
- name: Dump GitHub context
env:
GITHUB_CONTEXT: ${{ toJson(github) }}
run: echo "$GITHUB_CONTEXT"
- name: Get all changed json files
id: changed-json-files
uses: tj-actions/changed-files@v42
with:
files: |
**.json
- name: Print all changed json files
if: steps.changed-json-files.outputs.any_changed == 'true'
env:
ALL_CHANGED_FILES: ${{ steps.changed-json-files.outputs.all_changed_files }}
run: |
for file in ${ALL_CHANGED_FILES}; do
echo "$file was changed"
done
- name: Check all modified json files for version update
id: check-version-bump-needed
if: steps.changed-json-files.outputs.modified_files_count > 0
env:
ALL_MODIFIED_FILES: ${{ steps.changed-json-files.outputs.modified_files }}
run: |
echo "version_bump_check<<EOF" >> $GITHUB_ENV
echo "version_bump_check=0" >> "$GITHUB_OUTPUT"
echo "Base: ${{ steps.setup-refs.outputs.base_sha }}"
echo "Head: ${{ steps.setup-refs.outputs.head_sha }}"
for file in ${ALL_MODIFIED_FILES}; do
echo "$file was modified"
echo "File diff $file:"
git diff -U0 ${{ steps.setup-refs.outputs.base_sha }}..${{ steps.setup-refs.outputs.head_sha }} -- $file
echo "Check version field diff:"
if ! git diff -U0 ${{ steps.setup-refs.outputs.base_sha }}..${{ steps.setup-refs.outputs.head_sha }} -- $file | grep "version"; then
echo "No version update detected!"
echo "$file" >> $GITHUB_ENV
echo "version_bump_check=1" >> "$GITHUB_OUTPUT"
fi
done
echo "EOF" >> $GITHUB_ENV
- name: Comment PR with version check result
if: steps.check-version-bump-needed.outputs.version_bump_check == 1 && github.event_name == 'pull_request_target'
uses: thollander/actions-comment-pull-request@v2
with:
message: |
Version bump needed for the files:
```
${{ env.version_bump_check }}
```
comment_tag: version_bump
- name: Remove comment PR with version check result if not needed
if: steps.check-version-bump-needed.outputs.version_bump_check != 1 && github.event_name == 'pull_request_target'
uses: thollander/actions-comment-pull-request@v2
with:
message: "Version bump not needed"
mode: delete
create_if_not_exists: false
comment_tag: version_bump
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '^1.16'
- name: Install dc-template-linter
run: |
go install github.com/Domain-Connect/dc-template-linter@latest
echo `go env GOPATH`/bin
- name: Lint changed Templates
if: steps.changed-json-files.outputs.any_changed == 'true'
id: linter-output
env:
ALL_CHANGED_FILES: ${{ steps.changed-json-files.outputs.all_changed_files }}
run: |
echo "linter_error=0" >> "$GITHUB_OUTPUT"
echo "linter_output<<EOF" >> $GITHUB_ENV
for file in ${ALL_CHANGED_FILES}; do
echo "Linter result for $file" >> $GITHUB_ENV
`go env GOPATH`/bin/dc-template-linter -loglevel info $file 2>> $GITHUB_ENV || true
if ! `go env GOPATH`/bin/dc-template-linter -tolerate warn $file; then
echo "linter_error=1" >> "$GITHUB_OUTPUT"
echo "Linter error for $file"
`go env GOPATH`/bin/dc-template-linter -loglevel error $file || true
fi
done
echo "EOF" >> $GITHUB_ENV
- name: Comment PR with linter error result
if: steps.linter-output.outputs.linter_error == 1 && github.event_name == 'pull_request_target'
uses: thollander/actions-comment-pull-request@v2
with:
message: |
Linter error:
```
${{ env.linter_output }}
```
comment_tag: linter
- name: Comment PR with linter OK result
if: steps.linter-output.outputs.linter_error != 1 && github.event_name == 'pull_request_target'
uses: thollander/actions-comment-pull-request@v2
with:
message: |
Linter OK:
```
${{ env.linter_output }}
```
comment_tag: linter
- name: Fail job if version check or linter fails
if: steps.linter-output.outputs.linter_error == 1 || steps.check-version-bump-needed.outputs.version_bump_check == 1
run: exit 1