Skip to content

Commit 88a7760

Browse files
committed
feat: add CI workflows to check for updates to 'tock-tbf'
1 parent 76b9397 commit 88a7760

3 files changed

Lines changed: 236 additions & 0 deletions

File tree

.github/.lastcommsha

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4586a000ce72d970be7cfaef0ae40371c1e80177
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
name: issue-builder
2+
3+
on:
4+
schedule:
5+
- cron: '00 11 * * *'
6+
push:
7+
paths:
8+
- .github/.lastcommsha
9+
10+
workflow_dispatch:
11+
12+
jobs:
13+
check:
14+
runs-on: ubuntu-latest
15+
permissions:
16+
issues: write
17+
contents: read
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v2
21+
- name: Check for 'tock-tbf' updates
22+
run: |
23+
TARGET_FILE="libraries/tock-tbf"
24+
25+
RESPONSE=$(curl -s "https://api.github.com/repos/${UPSTREAM}/commits?path=$TARGET_FILE&sha=master")
26+
27+
# Find sha of last commit
28+
COMMIT_SHA=$(echo "$RESPONSE" | jq -r ".[0].sha")
29+
echo "Commit sha: $COMMIT_SHA"
30+
31+
# Find sha stored in file
32+
SHA_RESPONSE=$(curl -s -L \
33+
-H "Accept: application/vnd.github+json" \
34+
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
35+
-H "X-GitHub-Api-Version: 2022-11-28" \
36+
https://api.github.com/repos/${DOWNSTREAM}/contents/.github/.lastcommsha)
37+
38+
STORED_SHA=$(echo "$SHA_RESPONSE" | jq -r '.content' | base64 -d | tr -d '\n')
39+
echo "Last stored SHA: $STORED_SHA"
40+
41+
# Check if provided sha is valid
42+
NUM_COMMITS=$(echo "$RESPONSE" | jq length)
43+
FOUND=0
44+
45+
for (( i=0; i<NUM_COMMITS; i++ )); do
46+
CURR_COMM_SHA=$(echo "$RESPONSE" | jq -r ".[$i].sha")
47+
if [ "$CURR_COMM_SHA" == "$STORED_SHA" ]; then
48+
FOUND=1
49+
break
50+
fi
51+
done
52+
53+
if [ "$FOUND" -eq 0 ]; then
54+
echo "Invalid SHA. Please double-check it."
55+
exit 1
56+
fi
57+
58+
# Check if the two shas match to check if we're up to date
59+
if [ "$COMMIT_SHA" == "$STORED_SHA" ]; then
60+
echo "Up to date."
61+
# Since we're up to date => close existing issue if open
62+
63+
# Get existing issue
64+
mapfile -t matching_open_issues < <(gh issue list \
65+
--label "$LABELS" \
66+
--state open \
67+
--json number,title \
68+
--jq ".[] | select(.title == \"$TITLE\") | .number")
69+
70+
# If open issue is found => close
71+
if (( ${#matching_open_issues[@]} != 0 )); then
72+
for issue_number in "${matching_open_issues[@]}"; do
73+
echo "Closing issue..."
74+
gh issue close "$issue_number"
75+
done
76+
fi
77+
78+
exit 0
79+
else
80+
echo "Different - must update"
81+
82+
i=0
83+
COMM_SHAS=()
84+
COMM_MSGS=()
85+
86+
# Build array of shas that need to be listed in the issue
87+
for (( i=0; i<NUM_COMMITS; i++ )); do
88+
CURR_COMM_SHA=$(echo "$RESPONSE" | jq -r ".[$i].sha")
89+
CURR_COMM_MSG=$(echo "$RESPONSE" | jq -r ".[$i].commit.message")
90+
91+
if [ "$CURR_COMM_SHA" == "$STORED_SHA" ]; then
92+
break
93+
fi
94+
95+
COMM_SHAS+=("$CURR_COMM_SHA")
96+
COMM_MSGS+=("$CURR_COMM_MSG")
97+
done
98+
99+
# Debug echo for array
100+
for idx in "${!COMM_SHAS[@]}"; do
101+
echo "sha $idx is ${COMM_SHAS[$idx]}"
102+
done
103+
for idx in "${!COMM_MSGS[@]}"; do
104+
echo "sha $idx is ${COMM_MSGS[$idx]}"
105+
done
106+
107+
# Make commit markdown list
108+
COMMIT_LIST=""
109+
for idx in "${!COMM_SHAS[@]}"; do
110+
short_sha=$(git rev-parse --short "${COMM_SHAS[$idx]}")
111+
url="https://github.com/${UPSTREAM}/commit/${COMM_SHAS[$idx]}"
112+
message="$(echo "${COMM_MSGS[$idx]}" | head -n1)"
113+
COMMIT_LIST+="- [\`$short_sha\`]($url): $message"$'\n'
114+
done
115+
116+
# Get matching issues
117+
mapfile -t matching_issues < <(gh issue list \
118+
--label "$LABELS" \
119+
--state all \
120+
--json number,title \
121+
--jq ".[] | select(.title == \"$TITLE\") | .number")
122+
123+
# Check if issue already exists
124+
if (( ${#matching_issues[@]} == 0 )); then
125+
# Doesn't exist => new issue creation
126+
echo "New issue creation"
127+
new_issue_url=$(gh issue create \
128+
--title "$TITLE" \
129+
--assignee "$ASSIGNEES" \
130+
--label "$LABELS" \
131+
--body "$BODY"$'\n'"$COMMIT_LIST")
132+
if [[ $PINNED == true ]]; then
133+
gh issue pin "$new_issue_url"
134+
fi
135+
else
136+
# Exists => edit existing issue
137+
# Check if issue is closed in order to open it
138+
for issue_number in "${matching_issues[@]}"; do
139+
ISSUE_STATE=$(gh api repos/${DOWNSTREAM}/issues/"$issue_number" --jq '.state')
140+
if [[ "$ISSUE_STATE" == "closed" ]]; then
141+
echo "Issue closed. Reopening..."
142+
gh issue reopen "$issue_number"
143+
fi
144+
done
145+
146+
echo "Issue already exists; editting and commenting on issue"
147+
for issue_number in "${matching_issues[@]}"; do
148+
# Extract commit body for comparison
149+
ISSUE_BODY=$(gh api repos/${DOWNSTREAM}/issues/"$issue_number" --jq '.body')
150+
# Compare old list with new list
151+
# Check for changes between new list and old list
152+
# Debug echoes for body comparison
153+
echo "Old issue body: $ISSUE_BODY"
154+
echo "New issue body: $BODY"$'\n'"$COMMIT_LIST"
155+
if [[ "$ISSUE_BODY"$'\n' != "$BODY"$'\n'"$COMMIT_LIST" ]]; then
156+
echo "Bodies different => edit comment"
157+
gh issue comment "$issue_number" \
158+
--edit-last --create-if-none\
159+
--body "Commits list updated."
160+
else
161+
echo "Bodies same => no comment"
162+
fi
163+
gh issue edit "$issue_number" \
164+
--body "$BODY"$'\n'"$COMMIT_LIST"
165+
done
166+
fi
167+
168+
exit 0
169+
fi
170+
env:
171+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
172+
GH_REPO: ${{ github.repository }}
173+
UPSTREAM: "tock/tock"
174+
DOWNSTREAM: "WyliodrinEmbeddedIoT/tockloader-rs"
175+
TITLE: Bring updates from 'tock-tbf' to 'tbf-parser'
176+
ASSIGNEES: unassigned
177+
LABELS: D3-TBF PARSER,P1-CRITICAL,EXCLUSIVE LABEL
178+
BODY: |
179+
Please review the commits in order of oldest to newest (top to bottom). 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:
180+
PINNED: false
181+
CLOSE_PREVIOUS: true
182+
183+
184+

.github/workflows/sha-checker.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: sha-checker
2+
3+
on:
4+
pull_request:
5+
types: [labeled, unlabeled, opened, reopened, synchronize]
6+
workflow_dispatch:
7+
jobs:
8+
check:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
contents: read
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v2
15+
- name: Check if files included in appropriately labeled PRs
16+
run: |
17+
# Get PR number
18+
PR_NO=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")
19+
echo "PR number: $PR_NO"
20+
21+
# Get PR labels
22+
labels="$(gh api repos/${DOWNSTREAM}/pulls/$PR_NO --jq '.labels.[].name')"
23+
echo "$labels"
24+
25+
# Check for exclusive label to continue operations
26+
if [[ $labels =~ (EXCLUSIVE LABEL) ]]; then
27+
echo "Label found. Continuing..."
28+
else
29+
echo "Label not found. No sha-checker needed."
30+
exit 0
31+
fi
32+
33+
# Get changed files
34+
files="$(gh pr view "$PR_NO" --json files -q '.files[].path')"
35+
echo "$files"
36+
37+
# Check if '.lastcommsha' among changed files
38+
if [[ $files =~ (.lastcommsha) ]]; then
39+
echo "File found."
40+
exit 0
41+
else
42+
echo "File not found. Please make sure you update the sha within '.lastcommsha'."
43+
exit 1
44+
fi
45+
46+
env:
47+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
48+
UPSTREAM: "tock/tock"
49+
DOWNSTREAM: "WyliodrinEmbeddedIoT/tockloader-rs"
50+
51+

0 commit comments

Comments
 (0)