This repository has been archived by the owner on Nov 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
collect-questions.sh
executable file
·115 lines (100 loc) · 3.04 KB
/
collect-questions.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
#!/usr/bin/env bash
set -euo pipefail
tmp=$(mktemp -d)
trap 'echo rm -rf "$tmp"' exit
SCRIPT_DIR=$(dirname "$0")
for file in "$SCRIPT_DIR"/../candidates/*; do
basename=$(basename "$file")
echo -e "\n\n## Q&A\n" >> "$file"
done
gh api \
--method GET \
--paginate \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/NixOS/SC-election-2024/issues \
-f labels=question \
-f sort=comments |
jq -c '.[]' > "$tmp/questions.json"
while read -r questionJson; do
number=$(jq -r .number <<< "$questionJson")
title=$(jq -r .title <<< "$questionJson")
questionUrl=$(jq -r .html_url <<< "$questionJson")
question=$(jq -r .body <<< "$questionJson" |
awk '
{
if ($0 ~ "### Question.*") {
q=1
} else if ($0 ~ "### Candidates.*") {
q=0
} else {
if (q == 1) {
print $0
}
}
}')
echo "Processing question: $title"
gh api \
--paginate \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/NixOS/SC-election-2024/issues/$number/comments" \
--jq 'map({ key: .user.login, value: .}) | from_entries' > "$tmp/answers-$number.json"
for file in "$SCRIPT_DIR"/../candidates/*; do
basename=$(basename "$file")
# Capitalisation might be off
login=$(gh api --cache=1000h /users/"${basename%%.md}" --jq .login)
if answerJson=$(jq -e --arg login "$login" '."\($login)"' "$tmp/answers-$number.json"); then
answer=$(jq -r '.body' <<< "$answerJson")
answerUrl=$(jq -r '.html_url' <<< "$answerJson")
{
echo "### $title ([link]($questionUrl))"
echo ""
echo "$question"
echo ""
echo "<details>"
echo "<summary>Answer (<a href=\"$answerUrl\">link</a>)</summary>"
echo ""
echo "$answer"
echo "</details>"
echo ""
} >> "$file"
fi
done
done < "$tmp/questions.json"
for file in "$SCRIPT_DIR"/../candidates/*; do
echo "## Unanswered questions" >> "$file"
done
# Again for the unanswered questions
while read -r questionJson; do
number=$(jq -r .number <<< "$questionJson")
title=$(jq -r .title <<< "$questionJson")
questionUrl=$(jq -r .html_url <<< "$questionJson")
question=$(jq -r .body <<< "$questionJson" |
awk '
{
if ($0 ~ "### Question.*") {
q=1
} else if ($0 ~ "### Candidates.*") {
q=0
} else {
if (q == 1) {
print $0
}
}
}')
echo "Processing question: $title"
for file in "$SCRIPT_DIR"/../candidates/*; do
basename=$(basename "$file")
# Capitalisation might be off
login=$(gh api --cache=1000h /users/"${basename%%.md}" --jq .login)
if ! jq -e --arg login "$login" '."\($login)"' "$tmp/answers-$number.json" >/dev/null; then
{
echo "<details>"
echo "<summary>$title (<a href=\"$questionUrl\">link</a>)</summary>"
echo "$question"
echo "</details>"
} >> "$file"
fi
done
done < "$tmp/questions.json"