-
Notifications
You must be signed in to change notification settings - Fork 2
/
entrypoint.sh
93 lines (83 loc) · 2.28 KB
/
entrypoint.sh
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
#!/bin/sh
export DISTRIBUTION="25"
export TARGET=".github/CODEOWNERS"
if [ -n "$INPUT_DISTRIBUTION" ]; then
DISTRIBUTION="$INPUT_DISTRIBUTION"
echo "Action defined distribution to $DISTRIBUTION%"
fi
if [ -n "$INPUT_PATH" ]; then
TARGET="$INPUT_PATH"
echo "Action defined target path to be $TARGET"
fi
if [ -n "$INPUT_GRANULAR" ]; then
echo "Action enabled granular file processing"
fi
identification() {
read -r email
if [ -n "$INPUT_USERNAME" ] && [ -n "$email" ]; then
if ! username=$( \
curl "https://api.github.com/search/users?q=$email+in:email" \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer $INPUT_TOKEN" \
| jq -r -e '.items[0].login' \
);
then
if ! commit_username_json=$( \
curl "https://api.github.com/search/commits?q=author-email:$email&sort=author-date&per_page=1" \
-H "Accept: application/vnd.github.cloak-preview" \
-H "Authorization: Bearer $INPUT_TOKEN" \
);
then
echo "$email"
else
if ! commit_username=$(echo "$commit_username_json" | jq -r -e '.items[0].author.login');
then
>&2 echo "::warning:: $commit_username_json"
echo "$email"
else
echo "@$commit_username"
fi
fi
else
echo "@$username"
fi
else
echo "$email"
fi
}
owners() {
files=""
if [ -n "$INPUT_GRANULAR" ]; then
files=$(git ls-files)
else
dirs=$(git ls-tree -d -r --name-only HEAD)
files=$(git ls-tree --name-only HEAD)
files=$(printf "%s\n%s" "$dirs" "$files" | sort | uniq)
fi
for file in $files; do
if ! users=$( \
git fame -eswMC --incl "$file/?[^/]*\.?[^/]*$" \
| tr '/' '|' \
| awk -v "dist=$DISTRIBUTION" -F '|' '(NR>6 && $6>=dist) {gsub(/ /, "", $2); print $2}' \
| identification
);
then
echo "::error:: git fame command did not succeed"
exit 1
else
if [ -n "$users" ]; then
if [ -n "$INPUT_GRANULAR" ]; then
echo "/$file $users"
else
if echo "$dirs" | grep -w "$file" > /dev/null; then
echo "/$file/* $users"
else
echo "/$file $users"
fi
fi
fi
fi
done
}
echo "Update code owners..."
owners | tee "$TARGET"