forked from tock/tockloader-rs
-
Notifications
You must be signed in to change notification settings - Fork 15
181 lines (154 loc) · 6.78 KB
/
Copy pathissue-builder.yml
File metadata and controls
181 lines (154 loc) · 6.78 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
name: issue-builder
on:
schedule:
- cron: '00 11 * * *'
push:
paths:
- .github/.lastcommsha
workflow_dispatch:
jobs:
check:
runs-on: ubuntu-latest
permissions:
issues: write
contents: read
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Check for 'tock-tbf' updates
run: |
TARGET_FILE="libraries/tock-tbf"
RESPONSE=$(curl -s "https://api.github.com/repos/${UPSTREAM}/commits?path=$TARGET_FILE&sha=master")
# Find sha of last commit
COMMIT_SHA=$(echo "$RESPONSE" | jq -r ".[0].sha")
echo "Commit sha: $COMMIT_SHA"
# Find sha stored in file
SHA_RESPONSE=$(curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${DOWNSTREAM}/contents/.github/.lastcommsha)
STORED_SHA=$(echo "$SHA_RESPONSE" | jq -r '.content' | base64 -d | tr -d '\n')
echo "Last stored SHA: $STORED_SHA"
# Check if provided sha is valid
NUM_COMMITS=$(echo "$RESPONSE" | jq length)
FOUND=0
for (( i=0; i<NUM_COMMITS; i++ )); do
CURR_COMM_SHA=$(echo "$RESPONSE" | jq -r ".[$i].sha")
if [ "$CURR_COMM_SHA" == "$STORED_SHA" ]; then
FOUND=1
break
fi
done
if [ "$FOUND" -eq 0 ]; then
echo "Invalid SHA. Please double-check it."
exit 1
fi
# Check if the two shas match to check if we're up to date
if [ "$COMMIT_SHA" == "$STORED_SHA" ]; then
echo "Up to date."
# Since we're up to date => close existing issue if open
# Get existing issue
mapfile -t matching_open_issues < <(gh issue list \
--label "$LABELS" \
--state open \
--json number,title \
--jq ".[] | select(.title == \"$TITLE\") | .number")
# If open issue is found => close
if (( ${#matching_open_issues[@]} != 0 )); then
for issue_number in "${matching_open_issues[@]}"; do
echo "Closing issue..."
gh issue close "$issue_number"
done
fi
exit 0
else
echo "Different - must update"
i=0
COMM_SHAS=()
COMM_MSGS=()
# Build array of shas that need to be listed in the issue
for (( i=0; i<NUM_COMMITS; i++ )); do
CURR_COMM_SHA=$(echo "$RESPONSE" | jq -r ".[$i].sha")
CURR_COMM_MSG=$(echo "$RESPONSE" | jq -r ".[$i].commit.message")
if [ "$CURR_COMM_SHA" == "$STORED_SHA" ]; then
break
fi
COMM_SHAS+=("$CURR_COMM_SHA")
COMM_MSGS+=("$CURR_COMM_MSG")
done
# Debug echo for array
for idx in "${!COMM_SHAS[@]}"; do
echo "sha $idx is ${COMM_SHAS[$idx]}"
done
for idx in "${!COMM_MSGS[@]}"; do
echo "sha $idx is ${COMM_MSGS[$idx]}"
done
# Make commit markdown list
COMMIT_LIST=""
for idx in "${!COMM_SHAS[@]}"; do
short_sha=$(git rev-parse --short "${COMM_SHAS[$idx]}")
url="https://github.com/${UPSTREAM}/commit/${COMM_SHAS[$idx]}"
message="$(echo "${COMM_MSGS[$idx]}" | head -n1)"
COMMIT_LIST+="- [\`$short_sha\`]($url): $message"$'\n'
done
# Get matching issues
mapfile -t matching_issues < <(gh issue list \
--label "$LABELS" \
--state all \
--json number,title \
--jq ".[] | select(.title == \"$TITLE\") | .number")
# Check if issue already exists
if (( ${#matching_issues[@]} == 0 )); then
# Doesn't exist => new issue creation
echo "New issue creation"
new_issue_url=$(gh issue create \
--title "$TITLE" \
--label "$LABELS" \
--body "$BODY"$'\n'"$COMMIT_LIST")
if [[ $PINNED == true ]]; then
gh issue pin "$new_issue_url"
fi
else
# Exists => edit existing issue
# Check if issue is closed in order to open it
for issue_number in "${matching_issues[@]}"; do
ISSUE_STATE=$(gh api repos/${DOWNSTREAM}/issues/"$issue_number" --jq '.state')
if [[ "$ISSUE_STATE" == "closed" ]]; then
echo "Issue closed. Reopening..."
gh issue reopen "$issue_number"
fi
done
echo "Issue already exists; editting and commenting on issue"
for issue_number in "${matching_issues[@]}"; do
# Extract commit body for comparison
ISSUE_BODY=$(gh api repos/${DOWNSTREAM}/issues/"$issue_number" --jq '.body')
# Compare old list with new list
# Check for changes between new list and old list
# Debug echoes for body comparison
echo "Old issue body: $ISSUE_BODY"
echo "New issue body: $BODY"$'\n'"$COMMIT_LIST"
if [[ "$ISSUE_BODY"$'\n' != "$BODY"$'\n'"$COMMIT_LIST" ]]; then
echo "Bodies different => edit comment"
gh issue comment "$issue_number" \
--edit-last --create-if-none\
--body "Commits list updated."
else
echo "Bodies same => no comment"
fi
gh issue edit "$issue_number" \
--body "$BODY"$'\n'"$COMMIT_LIST"
done
fi
exit 0
fi
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
UPSTREAM: "tock/tock"
DOWNSTREAM: ${{ github.repository }}
TITLE: Bring updates from `tock-tbf` to `tbf-parser`
LABELS: D3-TBF PARSER,P1-CRITICAL,EXCLUSIVE LABEL
BODY: |
Please review the commits in order of oldest to newest (bottom to top). Once you have implemented the corresponding updates, copy-paste the sha of the commit you last covered in [.lastcommsha](https://github.com/WyliodrinEmbeddedIoT/tockloader-rs/tree/main/.github/.lastcommsha). Make sure to label any PRs addressing the commits in this list with `EXCLUSIVE LABEL`. The following commits need to be accounted for:
PINNED: false
CLOSE_PREVIOUS: true