Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making pull requests to a GitHub repository automatically with GitHub Actions

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.

like image 647
ahmet alp balkan Avatar asked Aug 15 '19 00:08

ahmet alp balkan


People also ask

How do I automate a pull request on GitHub?

People with write permissions to a repository can enable auto-merge for a pull request. On GitHub.com, navigate to the main page of the repository. Under your repository name, click Pull requests. In the "Pull Requests" list, click the pull request you'd like to auto-merge.

Can I make a pull request to my own repository?

Once you've committed changes to your local copy of the repository, click the Create Pull Request icon. Check that the local branch and repository you're merging from, and the remote branch and repository you're merging into, are correct. Then give the pull request a title and a description. Click Create.

Can you schedule GitHub actions?

You can configure your workflows to run when specific activity on GitHub happens, at a scheduled time, or when an event outside of GitHub occurs.

What is GitHub pull request synchronize?

synchronize: Triggered when a pull request's head branch is updated. For example, when the head branch is updated from the base branch, when new commits are pushed to the head branch, or when the base branch is changed. Beta Was this translation helpful?

How do I pull a pull request from a GitHub repository?

On GitHub.com, navigate to the main page of the repository. In the "Branch" menu, choose the branch that contains your commits. Above the list of files, click Pull request .

What is action-pull-request in GitHub?

GitHub Action that will create a pull request from the current branch. Useful in combination with my other action devops-infra/action-commit-push. Dockerized as devopsinfra/action-pull-request. Creates pull request if triggered from a current branch or any specified by source_branch to a target_branch.

Who can create a pull request?

Anyone with read access to a repository can create a pull request. If you want to create a new branch for your pull request and do not have write permissions to the repository, you can fork the repository first. For more information, see "Creating a pull request from a fork" and " About forks ."

What happens when I create a pull request from local changes?

The local changes will be automatically committed to a new branch and a pull request created. Create Pull Request action will: Check for repository changes in the Actions workspace. This includes:


1 Answers

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@v3
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          commit-message: Add report file
          committer: Peter Evans <[email protected]>
          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.

like image 200
peterevans Avatar answered Sep 28 '22 15:09

peterevans