Skip to content

Instantly share code, notes, and snippets.

@JordanShurmer
Last active February 16, 2023 15:13
Show Gist options
  • Save JordanShurmer/34cd186711cc7953ba11f63d5ea8761e to your computer and use it in GitHub Desktop.
Save JordanShurmer/34cd186711cc7953ba11f63d5ea8761e to your computer and use it in GitHub Desktop.
action from chatGPT

Someone tweeted about a niche Github Action idea:

https://twitter.com/tmcw/status/1626233552735211527

I pasted that tweet into chatGPT and it gave me an almost right answer.

The response created a new PR rather than updating the existing. So I said that it should update the existing and it gave m a new answer

It also included this explanation:

Here's what this workflow does:

  1. It is triggered by pull requests that are opened, reopened, or synchronized.
  2. It checks out the repository code using the actions/checkout action.
  3. It uses the grep command to find all TODO and FIXME comments in the codebase, excluding the .git, .github, and .vscode directories.
  4. If there are no TODO or FIXME comments, it sets the output variable to an empty string.
  5. If there are TODO or FIXME comments, it creates a pull request using the peter-evans/create-pull-request action.
  6. The pull request includes a to-do list with the found TODO and FIXME comments in the pull request description.

Note that you will need to have the peter-evans/create-pull-request action installed in your repository for this workflow to work. You can find more information about this action and how to use it in the GitHub Marketplace: https://github.com/marketplace/actions/create-pull-request.

The follow-up included this information:

Here's what's different in this updated workflow:

  1. The Update pull request step now uses the run key instead of the uses key.
  2. The run command uses the curl command to send a PATCH request to the GitHub API, updating the pull request with the new to-do list.
  3. The curl command uses the GITHUB_REPOSITORY and GITHUB_REF environment variables to construct the URL for the API request, and the secrets.GITHUB_TOKEN secret to authenticate the request.
  4. The sed command extracts the pull request number from the GITHUB_REF environment variable.
  5. The to-do list is URL-encoded using percent encoding so that it can be included in the API request.

With this updated workflow, the existing pull request will be updated with the to-do list each time the workflow is triggered by a pull request event.

name: TODO/FIXME to-do list
on:
pull_request:
types: [opened, reopened, synchronize]
jobs:
extract-todos:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Find TODOs and FIXMEs
run: |
todos=$(grep -rEI --exclude-dir={.git,.github,.vscode} "(TODO|FIXME)" . || true)
if [ -z "$todos" ]; then
echo "::set-output name=todos::"
else
echo "::set-output name=todos::$todos"
fi
- name: Update pull request
if: ${{ steps.extract-todos.outputs.todos != '' }}
uses: peter-evans/create-pull-request@v3
with:
commit-message: Add TODO/FIXME to-do list to pull request description
title: Add TODO/FIXME to-do list to pull request description
body: |
## To-do list
${{ steps.extract-todos.outputs.todos }}
name: Update pull request with TODO/FIXME to-do list
on:
pull_request:
types: [opened, reopened, synchronize]
jobs:
extract-todos:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Find TODOs and FIXMEs
run: |
todos=$(grep -rEI --exclude-dir={.git,.github,.vscode} "(TODO|FIXME)" . || true)
if [ -z "$todos" ]; then
echo "::set-output name=todos::"
else
echo "::set-output name=todos::$todos"
fi
- name: Update pull request
if: ${{ steps.extract-todos.outputs.todos != '' }}
run: |
todo_list="## To-do list\n\n${{ steps.extract-todos.outputs.todos }}"
todo_list="${todo_list//'%'/'%25'}"
todo_list="${todo_list//$'\n'/'%0A'}"
todo_list="${todo_list//$'\r'/'%0D'}"
number=$(echo "$GITHUB_REF" | sed -n 's/refs\/pull\/\([0-9]*\)\/merge/\1/p')
curl --request PATCH \
--url "https://api.github.com/repos/$GITHUB_REPOSITORY/pulls/$number" \
--header "authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
--header 'content-type: application/json' \
--data "{\"body\": \"$todo_list\"}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment