forked from Domain-Connect/Templates
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GITHUB: added linter action for pr review (Domain-Connect#526)
- Loading branch information
Showing
1 changed file
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
name: Lint Domain Connect Templates | ||
|
||
on: | ||
pull_request_target: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
lint: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Check out code | ||
uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 2 | ||
ref: ${{ github.event.pull_request.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: ${{ github.event.pull_request.base.sha }}" | ||
echo "Head: ${{ github.event.pull_request.head.sha }}" | ||
for file in ${ALL_MODIFIED_FILES}; do | ||
echo "$file was modified" | ||
echo "File diff $file:" | ||
git diff -U0 ${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }} -- $file | ||
echo "Check version field diff:" | ||
if ! git diff -U0 ${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.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 | ||
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 | ||
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: | | ||
git status | ||
ls -al | ||
echo "linter_error=0" >> "$GITHUB_OUTPUT" | ||
echo "linter_output<<EOF" >> $GITHUB_ENV | ||
echo "Linter result for $file" >> $GITHUB_ENV | ||
for file in ${ALL_CHANGED_FILES}; do | ||
`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 | ||
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 | ||
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 |