Making pull requests to a GitHub repository automa

2020-02-29 07:58发布

问题:

I have a file in a GitHub repository that needs updating occasionally by running a command.

As part of a GitHub Workflows, I want to have a bot running a command, and seeing if it creates a diff on the repo, and if so, make a pull request to the repository automatically.

I have a suspicion that the GitHub Workflows can help me do that as GitHub now lets people run arbitrary containers ("Actions") that do stuff like builds in a repository. I see some official automation workflows that let you "label" and "comment" issues etc here: https://github.com/actions/starter-workflows/tree/master/automation

If I wanted to run an arbitrary command and make a PR to the repository, which GitHub Actions should I be looking at instead of reinventing my own Actions? Any pointers are appreciated.

回答1:

I made a GitHub Action that I think will help you with this use case. https://github.com/peter-evans/create-pull-request

create-pull-request action needs to be run in conjunction with other actions or steps that modify or add files to your repository. The changes will be automatically committed to a new branch and a pull request created.

Here is an example that sets most of the main inputs.

on:
  repository_dispatch:
    types: [create-pull-request]
name: Create Pull Request
jobs:
  createPullRequest:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Create report file
        run: date +%s > report.txt
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v2
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          commit-message: Add report file
          committer: Peter Evans <peter-evans@users.noreply.github.com>
          body: |
            New report
            - Contains *today's* date
            - Auto-generated by [create-pull-request][1]

            [1]: https://github.com/peter-evans/create-pull-request
          title: '[Example] Add report file'
          labels: report, automated pr
          assignees: peter-evans
          reviewers: peter-evans
          milestone: 1
          branch: example-patches

To make it bot-like you can trigger the workflow periodically.

on:
 schedule:
   - cron: '*/5 * * * *'

Alternatively, you can set the workflow to trigger via webhook, as in the example above.

on:
  repository_dispatch:
    types: [create-pull-request]

To trigger the workflow call the following. [username] is a GitHub username. [token] is a repo scoped token. [repository] is the name of the repository the workflow resides in.

curl -XPOST -u "[username]:[token]" -H "Accept: application/vnd.github.everest-preview+json" -H "Content-Type: application/json" https://api.github.com/repos/[username]/[repository]/dispatches --data '{"event_type": "create-pull-request"}'

For further examples check out the documentation here.