TOC Generator

March 19, 2021 · View on GitHub

CI Status codecov CodeFactor License: MIT

Read this in other languages: English, 日本語.

This is a GitHub Actions to generate TOC (Table of Contents),
which executes DocToc and commits if changed.

Installation | Screenshot | Options | Action event details | Addition | Configuration Examples | Author

Installation

  1. Specify location of TOC (option)
    e.g. README.md
    <!-- START doctoc -->
    <!-- END doctoc -->
    
    detail
  2. Setup workflow
    e.g. .github/workflows/toc.yml
    on: push
    name: TOC Generator
    jobs:
      generateTOC:
        name: TOC Generator
        runs-on: ubuntu-latest
        steps:
          - uses: technote-space/toc-generator@v3
    

Screenshot

behavior

Options

namedescriptiondefaultrequirede.g.
TARGET_PATHSTarget file path. (Comma separated, Detail)README*.mdtrueREADME*.md,CHANGELOG.md, .
TOC_TITLETOC Title**Table of Contents**''
MAX_HEADER_LEVELMaximum heading level. (Detail)3
CUSTOM_MODEWhether it is custom mode(Generated Example)falsetrue
CUSTOM_TEMPLATECustom template for custom mode<p align="center">${ITEMS}</p>
ITEM_TEMPLATEItem template for custom mode<a href="${LINK}">${TEXT}</a>
SEPARATORSeparator for custom mode<span>|</span>
FOLDINGWhether to make TOC foldable(Generated Example)falsetrue
COMMIT_MESSAGECommit messagechore(docs): update TOCtruedocs: update TOC
COMMIT_NAMEGit commit name${github.actor}
COMMIT_EMAILGit commit email${github.actor}@users.noreply.github.com
CREATE_PRWhether to check only default branchfalsetrue
CHECK_ONLY_DEFAULT_BRANCHWhether to create PullRequestfalsetrue
PR_BRANCH_PREFIXPullRequest branch prefixtoc-generator/true
PR_BRANCH_NAMEPullRequest branch name
Context variables
update-toc-${PR_ID}truetoc-${PR_NUMBER}
PR_TITLEPullRequest title
Context variables
chore(docs): update TOC (${PR_MERGE_REF})truedocs: update TOC
PR_BODYPullRequest body
Context PR variables
action.ymltrue
PR_COMMENT_BODYPullRequest body for comment
Context PR variables
action.yml
PR_CLOSE_MESSAGEMessage body when closing PullRequestThis PR has been closed because it is no longer needed.
TARGET_BRANCH_PREFIXFilter by branch namerelease/
INCLUDE_LABELSLabels used to check if the PullRequest has itLabel1, Label2
OPENING_COMMENTOpening comment (for other than DocToc)<!-- toc
CLOSING_COMMENTClosing comment (for other than DocToc)<!-- tocstop
GITHUB_TOKENAccess token${{github.token}}true${{secrets.ACCESS_TOKEN}}

Action event details

Target event

eventName: actioncondition
push: *condition1
pull_request: [opened, synchronize, reopened, labeled, unlabeled]condition2
pull_request: [closed]
schedule, repository_dispatch, workflow_dispatch
  • The following activity types must be explicitly specified (detail)
    • labeled, unlabeled, closed

Conditions

condition1

condition2

Addition

GITHUB_TOKEN

The GITHUB_TOKEN that is provided as a part of GitHub Actions doesn't have authorization to create any successive events.
So it won't spawn actions which triggered by push.

This can be a problem if you have branch protection configured.

If you want to trigger actions, use a personal access token instead.

  1. Generate a personal access token with the public_repo or repo scope.
    (repo is required for private repositories).
  2. Save as ACCESS_TOKEN
  3. Add input to use ACCESS_TOKEN instead of GITHUB_TOKEN.
    e.g. .github/workflows/toc.yml
    on: push
    name: TOC Generator
    jobs:
      generateTOC:
        name: TOC Generator
        runs-on: ubuntu-latest
        steps:
          - uses: technote-space/toc-generator@v3
            with:
              GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
    

Create PullRequest

If CREATE_PR is set to true, a PullRequest is created.

on: pull_request
name: TOC Generator
jobs:
  generateTOC:
    name: TOC Generator
    runs-on: ubuntu-latest
    steps:
      - uses: technote-space/toc-generator@v3
        with:
          CREATE_PR: true

create pr

If the closed activity type is set, this action closes the PR when it is no longer needed.

on:
  pull_request:
    types: [opened, synchronize, reopened, closed]
name: TOC Generator
jobs:
  generateTOC:
    name: TOC Generator
    runs-on: ubuntu-latest
    steps:
      - uses: technote-space/toc-generator@v3

Context variables

namedescription
PR_NUMBERpull_request.number (e.g. 11)
PR_NUMBER_REF#${pull_request.number} (e.g. #11)
PR_IDpull_request.id (e.g. 21031067)
PR_HEAD_REFpull_request.head.ref (e.g. change)
PR_BASE_REFpull_request.base.ref (e.g. master)
PR_MERGE_REFpull_request.base.ref (e.g. change -> master)
PR_TITLEpull_request.title (e.g. update the README with new information.)

Payload example

Context PR variables

namedescription
PR_LINKLink to PR
COMMANDS_OUTPUTResult of TOC command
FILES_SUMMARYe.g. Changed 2 files
FILESChanged file list

Configuration Examples

Example 1

Execute actions at push without limiting the branch and commit directly

on: push
name: TOC Generator
jobs:
  generateTOC:
    name: TOC Generator
    runs-on: ubuntu-latest
    steps:
      - uses: technote-space/toc-generator@v3

Example 2

Create or update a Pull Request by executing actions on a Pull Request update only for branches starting with release/.

on:
  pull_request:
    types: [opened, synchronize, reopened, closed]
name: TOC Generator
jobs:
  generateTOC:
    name: TOC Generator
    runs-on: ubuntu-latest
    steps:
      - uses: technote-space/toc-generator@v3
        with:
          CREATE_PR: true
          TARGET_BRANCH_PREFIX: release/

Example 3

Execute actions in the schedule for the default branch only and commit directly.
(Using the Token created for the launch of other workflows)

on:
  schedule:
    - cron: "0 23 * * *"
name: TOC Generator
jobs:
  generateTOC:
    name: TOC Generator
    runs-on: ubuntu-latest
    steps:
      - uses: technote-space/toc-generator@v3
        with:
          GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
          CHECK_ONLY_DEFAULT_BRANCH: true

Author

GitHub (Technote)
Blog