issue-builder #305
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |