-
Notifications
You must be signed in to change notification settings - Fork 75
116 lines (108 loc) · 4.66 KB
/
lint-codeowners.yaml
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
name: Lint CODEOWNERS checks
# Any change in triggers needs to be reflected in the concurrency group.
on:
pull_request:
branches:
- main
- release-*
permissions: read-all
env:
# Hard-code the path instead of using ${{ github.repository }}
REPOSITY_NAME: github.com/spidernet-io/spiderpool
# for each pr, queue all workflows
# concurrency:
# group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
# cancel-in-progress: true
jobs:
# find all changed files from PR
check_changes:
name: Deduce required tests from code changes
runs-on: ubuntu-latest
outputs:
added-files: ${{ steps.changes.outputs.added-files }}
deleted-files: ${{ steps.changes.outputs.deleted-files }}
codeowners-changed: ${{ steps.changes.outputs.codeowners-changed }}
steps:
- name: Check code changes
uses: dorny/paths-filter@v2.11.1
id: changes
with:
filters: |
added-files:
- added: '**'
deleted-files:
- deleted: '**'
codeowners-changed:
- 'CODEOWNERS'
# check whether there is owner relationship entry on CODEOWNERS for the changed file
codeowners:
needs: check_changes
if: ${{ needs.check_changes.outputs.added-files == 'true' || needs.check_changes.outputs.deleted-files == 'true' }}
name: Check CODEOWNERS consistency
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
# Hard-code the path instead of using ${{ github.repository }}
# to make sure it works for forked repo as well.
path: src/${{ env.REPOSITY_NAME }}
# for adding files on the PR , check whether there is relationship entry on the CODEOWNERS
- name: Check if all files have attributed code owners
if: ${{ needs.check_changes.outputs.added-files == 'true' }}
run: |
# CODEOWNERS patterns follows nearly the same syntax as a .gitignore.
# To check if all files are covered by patterns other than the
# catch-all '*', we turn the file into a .gitignore and list
# unmatched files.
cd src/${{ env.REPOSITY_NAME }}
# Copy all patterns from CODEOWNERS, but skipping the comments
# ('^[^#]') and the catch-all '*' rule (the only one with a single
# character, we skip it with '^.[^ ]').
awk '/^[^#][^ ]/ {print $1}' CODEOWNERS > .gitignore
# Reinitialize the repo and list all files NOT covered by .gitignore.
rm -rf .git
git init -q
if [[ -n "$(git ls-files --others -X .gitignore)" ]]; then
echo '::error title=missing_code_owners::Following files have no owners in CODEOWNERS:'
git ls-files --others -X .gitignore
exit 1
fi
# when some file deleted in the PR , check whether some path recorded in the CODEOWNERS does not exist ,
# if so, should update CODEOWNERS
- name: Check if CODEOWNERS has stale entries
if: ${{ needs.check_changes.outputs.deleted-files == 'true' }}
run: |
cd src/${{ env.REPOSITY_NAME }}
EXIT_STATUS=0
# We go through the patterns in CODEOWNERS, and for each of them we
# search for corresponding files in the repo.
# loop all path recorded in the CODEOWNER
while read l; do
case "${l}" in
/*)
# The pattern should match from the root of the repo,
# we'll use 'ls'. For now, just append pattern to $LIST.
LIST+=" ${l#/}"
;;
*)
# No leading slash: may not be at the root of the repo,
# search with 'find'. Print pattern if no file found.
if [[ -z $(find . -path "*${l}*" -print -quit) ]]; then
echo "${l}"
EXIT_STATUS=1
fi
;;
esac
done <<< $(awk '/^[^#][^ ]/ {print $1}' CODEOWNERS)
# Just one final call to 'ls' with all /* patterns found. Catch
# patterns with no corresponding files/directories from stderr.
STALE_PATTERNS="$(ls -- ${LIST} 2>&1 >/dev/null | sed "s|.*'\(.*\)':.*|/\1|")"
if [[ -n "${STALE_PATTERNS}" ]]; then
echo "${STALE_PATTERNS}" | sed 's/ /\n/g'
EXIT_STATUS=1
fi
if [[ ${EXIT_STATUS} -ne 0 ]]; then
echo '::error title=stale_patterns::The patterns above should be removed from CODEOWNERS.'
exit ${EXIT_STATUS}
fi