kyontan / update-generated-files-action

Action to update generated files. It pushes change to head branch of pull request

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

update-generated-files-action ts

This is an action for auto-fix of generated files. It pushes the current change to the pull request, i.e., git commit && git push origin.

Here are the example use-cases.

  • Format code such as Prettier, dprint or gofmt
  • Update a lock file such as package-lock.json, yarn.lock or go.sum
  • ESLint with --fix
  • OpenAPI Generator
  • GraphQL Code Generator

Getting Started

Here is an example workflow.

on:
  pull_request:
  push:
    branches:
      - main

jobs:
  generate:
    runs-on: ubuntu-latest
    permissions:
      # required to push a commit
      contents: write
      # required to create a pull request
      pull-requests: write
    steps:
      - uses: actions/checkout@v3

      # something to generate files
      - run: yarn graphql-codegen

      # push the change if exists
      - uses: int128/update-generated-files-action@v2

On pull_request event

When the workflow is run on pull_request event, this action adds the current change into the head branch. If there is no change in the current directory, this action does nothing.

For example, if yarn graphql-codegen updated the code, this action adds a commit of the change.

image

Because the workflow should pass on the new commit, this action exits with the following failure:

image

By default, actions/checkout checks out the merge branch. This action works on both merge branch or head branch.

If the last 5 commits are added by this action, it exits with an error to prevent the infinite loop.

You can customize the commit as follows:

jobs:
  generate:
    steps:
      - uses: int128/update-generated-files-action@v2
        with:
          # set a custom message to the new commit (optional)
          commit-message: "Fix: yarn graphql-codegen"

On push or other events

When the workflow is run on other events such as push or schedule, this action tries to apply the current change by the following order:

  1. Push the current change into the branch by fast-forward
  2. Create a pull request for the branch

If there is no change, this action does nothing.

For example, if yarn graphql-codegen updated the generated code in the workflow, this action pushes a commit to main branch.

image

If push was failed due to the branch protection rule, the action creates a pull request.

image

You can customize the pull request as follows:

jobs:
  generate:
    steps:
      - uses: int128/update-generated-files-action@v2
        with:
          # set a custom title or body to the pull request (optional)
          title: Regenerate graphql code
          body: Updated by `yarn graphql-codegen`
          # request reviewers for the pull request (optional)
          reviewers: |
            username
            org/team
          # set a custom message to the new commit (optional)
          commit-message: "Fix: yarn graphql-codegen"

Best practices

Triggering GitHub Actions on the new commit

This action uses the default token by default, but it does not trigger a workflow on the new commit. You need to reopen a pull request to trigger a workflow.

To trigger a workflow on the new commit, you need to set a personal access token or GitHub App token.

jobs:
  generate:
    steps:
      - uses: int128/update-generated-files-action@v2
        with:
          token: ${{ secrets.YOUR_TOKEN }}

Maintain code by team

It is recommended to set CODEOWNERS to receive a review request when this action creates a pull request. Alternatively, you can set the reviewers, for example,

jobs:
  generate:
    steps:
      - uses: int128/update-generated-files-action@v2
        with:
          reviewers: |
            your-organization/frontend-devs

Working with Renovate

You can update both dependencies and generated files as follows:

  1. Renovate creates a pull request to update a dependency
  2. GitHub Actions triggers a workflow
  3. This action pushes a change if it exists
  4. GitHub Actions triggers a workflow against the new commit

If the generated files are inconsistent, automerge will be stopped due to the failure of this action.

Specification

Inputs

Name Default Description
commit-message action.yaml Commit messgae
commit-message-footer action.yaml Footer of commit message
title action.yaml Title of the pull request
body action.yaml Body of the pull request
reviewers (optional) Request reviewers for the pull request
token github.token GitHub token

Outputs

Name Description
pr-number Pull Request Number (only available when not triggered by pull_request event)

About

Action to update generated files. It pushes change to head branch of pull request

License:Apache License 2.0


Languages

Language:TypeScript 97.4%Language:JavaScript 2.6%