forked from check-spelling/check-spelling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-pull-requests.sh
executable file
·180 lines (171 loc) · 5.26 KB
/
check-pull-requests.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/bin/bash
. "$spellchecker/common.sh"
if [ "$(uname)" = "Linux" ]; then
date_to_epoch() {
date -u -d "$1" +'%s'
}
else
date_to_epoch() {
date -u -j -f '%Y-%m-%dT%H:%M:%SZ' "$1" +'%s'
}
fi
timeframe=${timeframe:-60}
time_limit="$(( $timeframe * 60 ))"
strip_quotes() {
tr '"' ' '
}
(
echo '# :warning: check-spelling `on: schedule` support is deprecated'
echo 'See https://github.com/check-spelling/check-spelling/wiki/Breaking-change:-Dropping-support-for-on:-schedule'
) >> "$GITHUB_STEP_SUMMARY"
echo '::warning title=Deprecated Feature: `on: schedule`::See https://github.com/check-spelling/check-spelling/wiki/Breaking-change:-Dropping-support-for-on:-schedule'
begin_group 'Retrieving open pull requests'
pulls="$temp/pulls.json"
escaped="$temp/escaped.b64"
pull="$temp/pull.json"
fake_event="$temp/fake_event.json"
if [ -n "$INPUT_BUCKET" ] && [ -n "$INPUT_PROJECT" ]; then
tree_config="$INPUT_BUCKET/$INPUT_PROJECT/"
else
tree_config="$INPUT_CONFIG/"
fi
stored_config="$temp/config/"
Q='"'
owner=${GITHUB_REPOSITORY%/*}
repo=${GITHUB_REPOSITORY#*/}
length=20
get_open_pulls() {
echo "query {
repository(owner:${Q}$owner${Q} name:${Q}$repo${Q}) {
pullRequests(first: $length states: OPEN $continue) {
totalCount
pageInfo {
hasNextPage
endCursor
}
nodes {
baseRefName
baseRefOid
baseRepository {
nameWithOwner
}
headRefName
headRefOid
headRepository {
nameWithOwner
url
}
potentialMergeCommit {
oid
}
createdAt
url
commits(last: 1) {
nodes {
commit {
pushedDate
}
}
}
}
}
}
}"
}
if [ -e "$pulls.nodes" ]; then
echo "using cached $pulls.nodes"
else
rm -f "$pulls.nodes"
touch "$pulls.nodes"
while :
do
gh api graphql -f query="$(get_open_pulls)" > "$pulls"
jq .data.repository.pullRequests "$pulls" > "$pulls.pull_requests"
jq -c 'try .nodes[]' "$pulls.pull_requests" >> "$pulls.nodes"
jq .pageInfo "$pulls.pull_requests" > "$pulls.page_info"
if [ "$(jq -c -r .hasNextPage "$pulls.page_info")" != "true" ]; then
continue=''
break
fi
continue="after: ${Q}$(jq -c -r .endCursor "$pulls.page_info")${Q}"
done
fi
jq -c -r '{
head_repo: .headRepository.nameWithOwner,
base_repo: .baseRepository.nameWithOwner,
head_ref: .headRefName,
head_sha: .headRefOid,
base_sha: .baseRefOid,
clone_url: .headRepository.url,
merge_commit_sha: .potentialMergeCommit.oid,
created_at: .createdAt,
updated_at: .commits.nodes[0].commit.pushedDate,
issue_url: .url | sub("github.com"; "api.github.com/repos") | sub("/pull/(?<id>[0-9]+)$"; "/issues/\(.id)"),
commits_url: .url | sub("github.com"; "api.github.com/repos") | sub("/pull/(?<id>[0-9]+)$"; "/pulls/\(.id)/commits"),
comments_url: .url | sub("github.com"; "api.github.com/repos") | sub("/pull/(?<id>[0-9]+)$"; "/issues/\(.id)/comments"),
} | @base64' "$pulls.nodes" > "$escaped"
if [ -s "$escaped" ]; then
if [ -d "$tree_config" ]; then
mkdir -p "$stored_config"
rsync -a "$tree_config" "$stored_config"
fi
fi
end_group
for a in $(cat "$escaped"); do
echo "$a" | base64 --decode | jq -r . > "$pull"
url="$(jq -r .commits_url "$pull" | perl -pe 's{://api.github.com/repos/(.*/pull)s}{://github.com/$1}')"
begin_group "Considering $url"
created_at="$(jq -r '.updated_at // .created_at' "$pull")"
created_at="$(date_to_epoch "$created_at")"
age="$(( $start / 1000000000 - $created_at ))"
if [ "$age" -gt "$time_limit" ]; then
end_group
continue
fi
head_repo="$(jq -r .head_repo "$pull")"
base_repo="$(jq -r .base_repo "$pull")"
if [ "$head_repo" = "$base_repo" ]; then
end_group
continue
fi
head_sha="$(jq -r .head_sha "$pull")"
base_sha="$(jq -r .base_sha "$pull")"
merge_commit_sha="$(jq -r '.merge_commit_sha // empty' "$pull")"
comments_url="$(jq -r .comments_url "$pull")"
commits_url="$(jq -r .commits_url "$pull")"
clone_url="$(jq -r .clone_url "$pull")"
clone_url="${clone_url//https:/http:}"
head_ref="$(jq -r .head_ref "$pull")"
echo "do work for $head_repo -> $base_repo: $head_sha as ${merge_commit_sha:-(merge commit not available)}"
export GITHUB_SHA="$head_sha"
export GITHUB_EVENT_NAME=pull_request
echo '{}' | jq \
--arg head_sha "$head_sha" \
--arg base_sha "$base_sha" \
--arg comments_url "$comments_url" \
--arg commits_url "$commits_url" \
-r '{pull_request: {base: {sha: $base_sha}, head: {sha: $head_sha}, comments_url: $comments_url, commits_url: $commits_url }}' \
> "$fake_event"
export GITHUB_EVENT_PATH="$fake_event"
git remote rm pr 2>/dev/null || true
git remote add pr "$clone_url"
cat .git/config
git fetch pr "$head_ref"
git checkout "$head_sha"
git remote rm pr 2>/dev/null || true
end_group
(
temp="$(mktemp -d)"
if [ -d "$stored_config" ] && [ ! -d "$tree_config" ]; then
mkdir -p "$tree_config"
rsync -a "$stored_config" "$tree_config"
cleanup_tree_config=1
fi
export INPUT_REPORT_TITLE_SUFFIX="$head_repo: $head_ref into -> $base_repo: $base_sha"
"$spellchecker/unknown-words.sh" || true
rm -rf "$temp"
if [ -n "$cleanup_tree_config" ]; then
rm -rf "$tree_config"
fi
)
done