Skip to content

Commit 8b84ede

Browse files
committed
Split commits and improve race handling
1 parent 4e67842 commit 8b84ede

File tree

1 file changed

+137
-46
lines changed

1 file changed

+137
-46
lines changed

action.yml

Lines changed: 137 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -44,54 +44,145 @@ runs:
4444
user_email: ${{ inputs.author-email }}
4545
run: |
4646
: publish coverage
47-
export sha=$(git rev-parse HEAD)
48-
cd coverage_repository
49-
if ! grep -q "$GITHUB_EVENT_NAME" index.html; then
50-
perl -pi -e 's#^(\s+)(</ul>)#$1 <li><a href="$ENV{GITHUB_EVENT_NAME}/">$ENV{GITHUB_EVENT_NAME}</a></li>\n$1$2#' index.html
51-
git add index.html
52-
fi
53-
mkdir -p "$GITHUB_EVENT_NAME"
54-
if [ ! -e "$GITHUB_EVENT_NAME/index.html" ]; then
55-
printf "<html><head><title>check-spelling coverage for $GITHUB_EVENT_NAME</title></head>\n<body>\n <h1>check-spelling coverage for $GITHUB_EVENT_NAME</h1>\n <ul>\n </ul>\n</body></html>\n" > "$GITHUB_EVENT_NAME/index.html"
56-
git add "$GITHUB_EVENT_NAME/index.html"
57-
fi
58-
cd "$GITHUB_EVENT_NAME"
59-
if ! grep -q "$sha" index.html; then
60-
date="$(date)" \
61-
perl -pi -e 's#^(\s*)(<ul>)#$1$2\n$1 <li><a href="$ENV{sha}/">$ENV{date} ($ENV{sha})</a></li>#' index.html
62-
git add index.html
63-
fi
64-
mkdir -p "$sha"
65-
cd "$sha"
6647
67-
if [[ "$coverage_source" == /* ]]; then
68-
source_directory="$coverage_source"
69-
else
70-
source_directory="$GITHUB_WORKSPACE/$coverage_source"
71-
fi
72-
cp -r "$source_directory"/* .
73-
if [ ! -e index.html ] && [ -e coverage.html ]; then
74-
mv coverage.html index.html
75-
fi
76-
git add .
77-
if [ -z "$user_name" ]; then
78-
user_name=$(curl -s "$GITHUB_API_URL/users/$GITHUB_ACTOR" | jq -r '.name // empty')
48+
configure_environment() {
49+
date="$(date)"
50+
export sha=$(git rev-parse HEAD)
51+
7952
if [ -z "$user_name" ]; then
80-
user_name="$GITHUB_ACTOR"
53+
user_name=$(curl -s "$GITHUB_API_URL/users/$GITHUB_ACTOR" | jq -r '.name // empty')
54+
if [ -z "$user_name" ]; then
55+
user_name="$GITHUB_ACTOR"
56+
fi
8157
fi
82-
fi
83-
if [ -z "$user_email" ]; then
84-
user_email="$GITHUB_ACTOR_ID+$GITHUB_ACTOR@users.noreply.github.com"
85-
fi
86-
git \
87-
-c user.email="$user_email" \
88-
-c user.name="$user_name" \
89-
commit -m "Publish coverage for $GITHUB_REPOSITORY@$sha ($GITHUB_EVENT_NAME)"
90-
if ! git push origin HEAD; then
91-
git pull --rebase origin &&
58+
if [ -z "$user_email" ]; then
59+
user_email="$GITHUB_ACTOR_ID+$GITHUB_ACTOR@users.noreply.github.com"
60+
fi
61+
cd coverage_repository
62+
}
63+
64+
add_event() {
65+
(
66+
if ! grep -q "$GITHUB_EVENT_NAME" index.html; then
67+
perl -pi -e 's#^(\s+)(</ul>)#$1 <li><a href="$ENV{GITHUB_EVENT_NAME}/">$ENV{GITHUB_EVENT_NAME}</a></li>\n$1$2#' index.html
68+
git add index.html
69+
git \
70+
-c user.email="$user_email" \
71+
-c user.name="$user_name" \
72+
commit -m "Add $GITHUB_EVENT_NAME listing"
73+
fi
74+
)
75+
}
76+
77+
add_event_list() {
78+
(
79+
if [ ! -e "$GITHUB_EVENT_NAME/index.html" ]; then
80+
printf "<html><head><title>check-spelling coverage for $GITHUB_EVENT_NAME</title></head>\n<body>\n <h1>check-spelling coverage for $GITHUB_EVENT_NAME</h1>\n <ul>\n </ul>\n</body></html>\n" > "$GITHUB_EVENT_NAME/index.html"
81+
git add "$GITHUB_EVENT_NAME/index.html"
82+
git \
83+
-c user.email="$user_email" \
84+
-c user.name="$user_name" \
85+
commit -m "Add $GITHUB_EVENT_NAME list"
86+
fi
87+
)
88+
}
89+
90+
add_event_listing() {
91+
add_event_list
92+
(
93+
cd "$GITHUB_EVENT_NAME"
94+
if ! grep -q "$sha" index.html; then
95+
date="$date" \
96+
perl -pi -e 's#^(\s*)(<ul>)#$1$2\n$1 <li><a href="$ENV{sha}/">$ENV{date} ($ENV{sha})</a></li>#' index.html
97+
git add index.html
98+
git \
99+
-c user.email="$user_email" \
100+
-c user.name="$user_name" \
101+
commit -m "Add $GITHUB_EVENT_NAME listing for $ENV{sha}"
102+
fi
103+
)
104+
}
105+
106+
add_coverage() {
107+
(
108+
mkdir -p "$GITHUB_EVENT_NAME/$sha"
109+
cd "$GITHUB_EVENT_NAME/$sha"
110+
111+
if [[ "$coverage_source" == /* ]]; then
112+
source_directory="$coverage_source"
113+
else
114+
source_directory="$GITHUB_WORKSPACE/$coverage_source"
115+
fi
116+
cp -r "$source_directory"/* .
117+
if [ ! -e index.html ] && [ -e coverage.html ]; then
118+
mv coverage.html index.html
119+
fi
120+
git add .
121+
git \
122+
-c user.email="$user_email" \
123+
-c user.name="$user_name" \
124+
commit -m "Publish coverage for $GITHUB_REPOSITORY@$sha ($GITHUB_EVENT_NAME)"
125+
)
126+
}
127+
128+
add_everything() {
129+
add_coverage
130+
add_event_listing
131+
add_event
132+
}
133+
134+
try_push() {
92135
git push origin HEAD
136+
}
137+
138+
try_publish_or_rebase() {
139+
add_everything
140+
if try_push; then
141+
return
142+
fi
143+
if git pull --rebase origin; then
144+
if try_push; then
145+
return
146+
fi
147+
fi
148+
while [ -e .git/rebase-merge/git-rebase-todo ]; do
149+
git clean
150+
git rebase --skip
151+
fi
152+
false
153+
}
154+
155+
try_publish_with_retries() {
156+
for attempt in $(seq 3); do
157+
if try_publish_or_rebase; then
158+
return
159+
fi
160+
false
161+
done
162+
}
163+
164+
report_coverage_published_pending() {
165+
if [ -z "$github_pages_base" ]; then
166+
github_pages_base="https://${coverage_repository%%/*}.github.io/${coverage_repository##*/}"
167+
fi
168+
(
169+
echo "## Publishing [Coverage Report]($github_pages_base/$GITHUB_EVENT_NAME/$sha/)"
170+
echo
171+
echo "If the link isn't ready, check [$coverage_repository actions for status]($GITHUB_SERVER_URL/$coverage_repository/actions/workflows/pages.yml)."
172+
) >> "$GITHUB_STEP_SUMMARY"
173+
}
174+
175+
report_coverage_published_failed() {
176+
(
177+
echo '## Failed to publish Coverage Report'
178+
echo
179+
echo 'Check the logs for more information'
180+
) >> "$GITHUB_STEP_SUMMARY"
181+
}
182+
183+
configure_environment
184+
if try_publish_with_retries; then
185+
report_coverage_published_pending
186+
else
187+
report_coverage_published_failed
93188
fi
94-
if [ -z "$github_pages_base" ]; then
95-
github_pages_base="https://${coverage_repository%%/*}.github.io/${coverage_repository##*/}"
96-
fi
97-
echo "Publishing [Coverage Report]($github_pages_base/$GITHUB_EVENT_NAME/$sha/) (if the link isn't ready, check [$coverage_repository actions for status]($GITHUB_SERVER_URL/$coverage_repository/actions/workflows/pages.yml))" >> "$GITHUB_STEP_SUMMARY"

0 commit comments

Comments
 (0)