-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpublish_edition.sh
More file actions
executable file
·270 lines (195 loc) · 6.8 KB
/
publish_edition.sh
File metadata and controls
executable file
·270 lines (195 loc) · 6.8 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/bin/sh
#
# This script should be used from the root directory of the
# git.github.io repository. It will publish an edition using an
# existing draft (this will create a commit) and create a new empty
# draft for the following edition (this will create another commit).
#
# This script can be run like this:
#
# $ ../getreleases/publish_edition.sh [--token <token>] [<next date> [<cur date>]]
#
# where:
# - <token> is an optional GitHub Personal Access Token (PAT) with
# 'public_repo' scope. It is used to authenticate the GitHub CLI
# (gh) to create the issue for the next edition. If not provided,
# 'gh' must be already authenticated (e.g. via 'gh auth login' or
# the GH_TOKEN env var).
# - <next date> is the publication date for the following edition.
# - <cur date> is the publication date for the current edition.
#
# If <cur date> is not provided, the publication date for the current
# edition made from the current draft is today.
#
# If <next date> is not provided to the script, it will compute it by
# adding one month to today's date and then finding the following
# wednesday.
#
# Global environment
export LANG=C
# Repo related constants
repo_name="git/git.github.io"
repo_url="https://github.com/$repo_name.git"
known_good_commit="5bc243932ea7938830757e8370df6bd86df39cab"
src_dir="rev_news/drafts"
dst_dir="_posts"
main_branch="master"
# Useful infos from environments
today=$(date "+%F")
basedir=$(dirname "$0")
# Helper functions
die() {
printf >&2 "FATAL: %s\n" "$@"
exit 1
}
add_order_suffix() {
perl -e '
my $nb = $ARGV[0];
if ($nb =~ m/^(.*[02-9\D])?1$/) {
print $nb . "st\n";
} elsif ($nb =~ m/^(.*[02-9\D])?2$/) {
print $nb . "nd\n";
} elsif ($nb =~ m/^(.*[02-9\D])?3$/) {
print $nb . "rd\n";
} else {
print $nb . "th\n";
}
' "$1"
}
# Process arguments
arg_next=""
arg_cur=""
token=""
while test "$#" -gt 0
do
case "$1" in
--token)
test "$#" -ge 2 || die "--token requires an argument"
token="$2"
shift 2
;;
-*)
die "unknown option '$1'"
;;
*)
if test -z "$arg_next"
then
arg_next="$1"
elif test -z "$arg_cur"
then
arg_cur="$1"
else
die "too many arguments" "Usage: $0 [--token <token>] [<next date> [<cur date>]]"
fi
shift
;;
esac
done
test -n "$token" &&
export GH_TOKEN="$token"
# Compute nextdate, the publication date for the next edition
if test -n "$arg_next"
then
nextdate=$(date "+%F" --date="$arg_next") ||
die "failed to understand '$arg_next' as a date"
else
nextdate=$(date "+%F" --date="$today + 1 month")
day_of_week=$(date "+%u" --date="$nextdate")
test "$day_of_week" -lt 3 &&
nextdate=$(date "+%F" --date="$nextdate + $(( 3 - $day_of_week )) days")
test "$day_of_week" -gt 3 &&
nextdate=$(date "+%F" --date="$nextdate + $(( 10 - $day_of_week )) days")
fi
echo "Next publication date is: $(date "+%A %B %d, %Y" --date="$nextdate")"
# Compute curdate, the publication date for the current edition
if test -n "$arg_cur"
then
curdate=$(date "+%F" --date="$arg_cur") ||
die "failed to understand '$arg_cur' as a date"
else
curdate="$today"
fi
echo "Current publication date is: $(date "+%A %B %d, %Y" --date="$curdate")"
# Basic checks
type git >/dev/null || die "git not found" "we need git"
git show "$known_good_commit" >/dev/null 2>&1 ||
die "$known_good_commit not found" \
"we need to be in a repo cloned from $repo_url"
cur_branch=$(git rev-parse --abbrev-ref HEAD)
test "$cur_branch" = "$main_branch" || die "please switch to the '$main_branch' branch"
test -d "$src_dir" || die "no source '$src_dir' directory"
test -d "$dst_dir" || die "no destination '$dst_dir' directory"
nb_ed=$(ls "$src_dir"/edition-*.md | wc -l)
test "$nb_ed" -le 1 || die "more than one 'edition-*.md' file in '$src_dir' directory"
test "$nb_ed" -ne 0 || die "no 'edition-*.md' file in '$src_dir' directory"
type gh >/dev/null ||
die "gh not found" "we need the GitHub CLI (gh) to create the issue"
gh auth status >/dev/null 2>&1 ||
die "gh is not logged in" \
"please use '--token <token>' or 'export GH_TOKEN=...' or run 'gh auth login'"
# Find edition info we need
edition=$(ls "$src_dir"/edition-*.md)
cur=$(expr "$edition" : "rev_news/drafts/edition-\([0-9]\+\).md")
test -n "$cur" || die "'$edition' should contain a number"
next=$(expr "$cur" + 1)
# Find dates we need
next_month_date=$(date "+%F" --date="$curdate + 15 days")
next_month=$(date "+%B %Y" --date="$next_month_date")
cur_year=$(date "+%Y" --date="$curdate")
next_year=$(date "+%Y" --date="$next_month_date")
# Set cur_month conditionally based on the year comparison
if [ "$cur_year" = "$next_year" ]; then
# If the years are the same, just use the month name
cur_month=$(date "+%B" --date="$curdate")
else
# Otherwise, include the year for clarity
cur_month=$(date "+%B %Y" --date="$curdate")
fi
f_day=$(date "+%-d" --date="$nextdate")
f_month=$(date "+%B" --date="$nextdate")
f_year=$(date "+%Y" --date="$nextdate")
full_date="$f_month $(add_order_suffix $f_day), $f_year"
# Publish current draft
git mv "$src_dir"/edition-$cur.md "$dst_dir"/$curdate-edition-$cur.markdown
git commit -m "Publish rn-$cur in $dst_dir/"
# Create a draft for next edition
next_ed="$src_dir/edition-$next.md"
edition_template="$basedir/templates/edition-XXX.md"
test -f "$edition_template" ||
die "failed to find edition template at '$edition_template'"
cp "$edition_template" "$next_ed" ||
die "failed to 'cp $edition_template $next_ed'"
next_ord=$(add_order_suffix "$next")
perl -pi -e "
s/Edition _ED_NUM_/Edition $next/g;
s/_ED_ORD_ edition/${next_ord} edition/g;
s/_ED_DATE_/$nextdate/g;
s/_ED_FULL_DATE_/$full_date/g;
s/_ED_CUR_MONTH_YEAR_/$cur_month/g;
s/_ED_NEXT_MONTH_YEAR_/$next_month/g;
" "$next_ed"
git add "$next_ed" ||
die "failed to 'git add $next_ed'"
git commit -m "Add draft for rn-$next"
# Sync with upstream and push
echo "Syncing with upstream..."
git pull --rebase origin "$main_branch" ||
die "failed to 'git pull --rebase'"
echo "Pushing changes to origin..."
git push origin "$main_branch" ||
die "failed to 'git push'"
# Create GitHub Issue for the next edition
echo "Creating GitHub issue for edition $next..."
# Construct the issue body
issue_body="A currently mostly empty draft is there:
https://github.com/$repo_name/blob/$main_branch/rev_news/drafts/edition-$next.md
Feel free to comment in this issue, suggest topics, suggest persons to interview, or use the edit button (that looks like a pen) to edit and create a pull request with the changes you would like.
Let's try to publish this edition around the end of $f_month $f_year!
Thanks!
cc @jnareb @mjaix @sivaraam @gitster @stepnem"
# Create the issue
gh issue create \
--repo "$repo_name" \
--title "Any comment about upcoming Git Rev News edition $next" \
--body "$issue_body"
echo "Success! The edition is published, the next draft is created, and the GitHub issue is live."