|
| 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 | + --label "$LABELS" \ |
| 130 | + --body "$BODY"$'\n'"$COMMIT_LIST") |
| 131 | + if [[ $PINNED == true ]]; then |
| 132 | + gh issue pin "$new_issue_url" |
| 133 | + fi |
| 134 | + else |
| 135 | + # Exists => edit existing issue |
| 136 | + # Check if issue is closed in order to open it |
| 137 | + for issue_number in "${matching_issues[@]}"; do |
| 138 | + ISSUE_STATE=$(gh api repos/${DOWNSTREAM}/issues/"$issue_number" --jq '.state') |
| 139 | + if [[ "$ISSUE_STATE" == "closed" ]]; then |
| 140 | + echo "Issue closed. Reopening..." |
| 141 | + gh issue reopen "$issue_number" |
| 142 | + fi |
| 143 | + done |
| 144 | + |
| 145 | + echo "Issue already exists; editting and commenting on issue" |
| 146 | + for issue_number in "${matching_issues[@]}"; do |
| 147 | + # Extract commit body for comparison |
| 148 | + ISSUE_BODY=$(gh api repos/${DOWNSTREAM}/issues/"$issue_number" --jq '.body') |
| 149 | + # Compare old list with new list |
| 150 | + # Check for changes between new list and old list |
| 151 | + # Debug echoes for body comparison |
| 152 | + echo "Old issue body: $ISSUE_BODY" |
| 153 | + echo "New issue body: $BODY"$'\n'"$COMMIT_LIST" |
| 154 | + if [[ "$ISSUE_BODY"$'\n' != "$BODY"$'\n'"$COMMIT_LIST" ]]; then |
| 155 | + echo "Bodies different => edit comment" |
| 156 | + gh issue comment "$issue_number" \ |
| 157 | + --edit-last --create-if-none\ |
| 158 | + --body "Commits list updated." |
| 159 | + else |
| 160 | + echo "Bodies same => no comment" |
| 161 | + fi |
| 162 | + gh issue edit "$issue_number" \ |
| 163 | + --body "$BODY"$'\n'"$COMMIT_LIST" |
| 164 | + done |
| 165 | + fi |
| 166 | +
|
| 167 | + exit 0 |
| 168 | + fi |
| 169 | + env: |
| 170 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 171 | + GH_REPO: ${{ github.repository }} |
| 172 | + UPSTREAM: "tock/tock" |
| 173 | + DOWNSTREAM: "WyliodrinEmbeddedIoT/tockloader-rs" |
| 174 | + TITLE: Bring updates from 'tock-tbf' to 'tbf-parser' |
| 175 | + LABELS: D3-TBF PARSER,P1-CRITICAL,EXCLUSIVE LABEL |
| 176 | + BODY: | |
| 177 | + 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: |
| 178 | + PINNED: false |
| 179 | + CLOSE_PREVIOUS: true |
| 180 | + |
| 181 | + |
| 182 | + |
0 commit comments