Skip to content

Here's an example of how to use this with GitHub Actions #340

@voxpelli

Description

@voxpelli

I made a GitHub Action that uses this in an internal repository at work and I thought I could just as well copy it into a gist as well as share it here:

name: List Todos

on:
  push:
    branches:
      - main
    tags:
      - '*'
  pull_request:
    branches:
      - main

permissions:
  contents: read
  issues: write

jobs:
  list-todos:
    runs-on: 'ubuntu-latest'
    steps:
      - uses: actions/checkout@v4
        with:
          persist-credentials: false
          show-progress: false

      - name: Use Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 'lts/*'
          registry-url: 'https://registry.npmjs.org'

      - run: npm ci --ignore-scripts --no-fund --no-audit

      - name: Generate job summary
        run: npx --yes leasot index.js 'lib/**/*.js' --exit-nicely --reporter markdown | sed -e 's|][(]|](/${{ github.repository }}/blob/${{ github.sha }}/|' >> $GITHUB_STEP_SUMMARY

      # If on main branch, then create / update the reference issue

      - if: github.ref_name == 'main'
        id: current-issue
        name: Look up existing issue
        run: echo "number=$(gh issue list --json number,title -q 'map(select(.title=="Current TODO comments")) | .[0].number')" >> $GITHUB_OUTPUT
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Edit issue
        if: github.ref_name == 'main' && steps.current-issue.outputs.number
        run: >
          npm run --silent list-todos-md |
          sed -e 's|][(]|](/${{ github.repository }}/blob/${{ github.sha }}/|' |
          gh issue edit ${{ steps.current-issue.outputs.number }} --body-file -
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Create issue
        if: github.ref_name == 'main' && !steps.current-issue.outputs.number
        run: >
          npm run --silent list-todos-md |
          sed -e 's|][(]|](/${{ github.repository }}/blob/${{ github.sha }}/|' |
          gh issue create --title "Current TODO comments" --body-file -
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

The sed part could be avoided if the reporter were to be extended to take repository and sha as inputs – or if there would be a github reporter that would read it automatically from GITHUB_REPOSITORY and GITHUB_SHA env variables

The workflow adds a job summary to all runs and for runs against main it also creates / updates an issue with the title Current TODO comments that contains the very same output.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions