devops-infra / action-pull-request

GitHub Action that will create a pull request from the current branch
https://christophshyper.github.io/
MIT License
74 stars 21 forks source link
automation christophshyper ci-cd cicd devops devops-workflow docker github-action github-actions github-actions-docker iaac pull-request

GitHub Action for creating Pull Requests

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.

Features:

Badge swag

Master branch Other branches
GitHub repo GitHub code size in bytes GitHub last commit GitHub license
DockerHub Docker version Image size Docker Pulls

Reference

    - name: Run the Action
      uses: devops-infra/action-pull-request@v0.5.5
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
        source_branch: development
        target_branch: master
        title: My pull request
        template: .github/PULL_REQUEST_TEMPLATE.md
        body: "**Automated pull request**"
        reviewer: octocat
        assignee: octocat
        label: enhancement
        milestone: My milestone
        draft: true
        old_string: "<!-- Add your description here -->"
        new_string: "** Automatic pull request**"
        get_diff: true
        ignore_users: "dependabot"
        allow_no_diff: false
Input Variable Required Default Description
github_token Yes "" GitHub token ${{ secrets.GITHUB_TOKEN }}
allow_no_diff No false Allows to continue on merge commits with no diffs.
assignee No "" Assignee's usernames.
body No list of commits Pull request body.
draft No false Whether to mark it as a draft.
get_diff No false Whether to replace predefined comments with differences between branches - see details below.
ignore_users No "dependabot" List of users to ignore, coma separated.
label No "" Labels to apply, coma separated.
milestone No "" Milestone.
new_string No "" New string for the replacement in the template. If not specified, but old_string was, it will gather commits subjects.
old_string No "" Old string for the replacement in the template.
reviewer No "" Reviewer's username.
source_branch No current branch Name of the source branch.
target_branch No master Name of the target branch. Change it if you use main.
template No "" Template file location.
title No subject of the first commit Pull request title.
Outputs Description
url Pull request URL
pr_number Number of GitHub pull request

How get_diff works

In previous versions occurrences of following strings in a template result with replacing them with list of commits and list of modified files (<!-- Diff commits --> and <!-- Diff files -->).

Now this action will expect to have three types of comment blocks. Meaning anything between START and END comment will get replaced. This is especially important when updating pull request with new commits.

If your template uses old comment strings it will try to adjust them in the pull request body to a new standard when pull request is created. It will not modify the template.

CAUTION Remember to not use default fetch-depth for actions/checkout action. Rather set it to 0 - see example below.

Examples

Red ares show fields that can be dynamically expanded based on commits to the current branch. Blue areas show fields that can be set in action configuration. Example screenshot

Create pull request for non-master branches

name: Run the Action on each commit
on:
  push:
    branches-ignore: master
jobs:
  action-pull-request:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
      - name: Create pull request
        uses: devops-infra/action-pull-request@v0.5.5
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          title: Automatic pull request

Use first commit as a title and part of body, add a label based on a branch name, add git differences in the template

name: Run the Action on each commit
on:
  push:
    branches-ignore: master
jobs:
  action-pull-request:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Run the Action
        if: startsWith(github.ref, 'refs/heads/feature')
        uses: devops-infra/action-pull-request@v0.5.5
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          title: ${{ github.event.commits[0].message }}
          assignee: ${{ github.actor }}
          label: automatic,feature
          template: .github/PULL_REQUEST_TEMPLATE/FEATURE.md
          old_string: "**Write you description here**"
          new_string: ${{ github.event.commits[0].message }}
          get_diff: true