Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Want to automate creation of a PR on behalf of a user #1971

Closed
exosyphon-rxrevu opened this issue May 19, 2023 · 9 comments
Closed

Want to automate creation of a PR on behalf of a user #1971

exosyphon-rxrevu opened this issue May 19, 2023 · 9 comments

Comments

@exosyphon-rxrevu
Copy link

exosyphon-rxrevu commented May 19, 2023

This is more of a request on using the API after struggling for several hours.

Is it possible to have someone push to a branch and then open a PR on behalf of that user?

Here is what I have so far and it doesn't reflect the changes on the original commit. Any guidance would be very appreciated, thank you!

name: Create PR

on:
  push: 
    branches-ignore: 
      - main

jobs:
  create-pr:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Make changes to pull request
        run: date +%s > report.txt
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v5
        with:
            title: ${{github.ref_name}}
            token: ${{secrets.TOKEN}}
            body: |  
              Auto-generated by [create-pull-request][1]
  
              [1]: https://github.com/peter-evans/create-pull-request
            base: main
            branch: ${{github.ref_name}}
@peter-evans
Copy link
Owner

Hi @exosyphon-rxrevu

You can change the committer and author for the commit using inputs.

Additionally, if you want the PR to be created by a specific user then the PAT must be created on that user and passed to the token input.

Let me know if you need further help.

@fregante
Copy link

Judging by cli/cli#6575, the automatic GITHUB_TOKEN generated by GitHub can be used to create pull requests. This means it will likely create a PR "authored by GitHub Actions", which is what I'd like to see for such automated PRs.

This is what I'd need this action to accept:

  • custom PAT to push commits, this will trigger the push event in the workflows
  • automatic GITHUB_TOKEN to create PRs, this won't trigger the pull_request event but that's usually duplicate anyway.

@peter-evans
Copy link
Owner

peter-evans commented Sep 25, 2023

Hi @fregante

I've made your suggestion possible in the next major version of the action. You can test it like this:

      - uses: peter-evans/create-pull-request@v6-rc
        with:
          git-token: ${{ secrets.PAT }}

Just leave the token input unset and it will use the default GITHUB_TOKEN for creating the PR.

I'll be releasing v6 at some point in the near future.

@fregante
Copy link

That's fantastic! But are you sure it can actually take the token automatically? Sounds unsafe, I think only action/checkout can do that

@peter-evans
Copy link
Owner

That's fantastic! But are you sure it can actually take the token automatically? Sounds unsafe, I think only action/checkout can do that

Perhaps you're referring to how the the token is stored for use with git. It uses the git extraheader config, which I originally implemented based on how the official checkout action works. You can see it here: https://github.com/peter-evans/create-pull-request/blob/main/src/git-auth-helper.ts

The action has been working this way for a long time. The git-token input was a very small change just to separate the token for git ops and the GitHub API. See 32179d9

@kubukoz
Copy link

kubukoz commented Feb 14, 2024

sorry to dig up a closed issue, but do I understand correctly that it's not possible to set a custom PR author if using an app token? We aren't able to use PATs at this time.

@peter-evans
Copy link
Owner

@kubukoz There's no such thing as a custom PR author. The author of the PR is always the user account associated with the token. There is no way to change that as far as I know.

I'm not very familiar with GitHub apps, but my guess is that the tokens generated by apps are associated with the owner of the app. If you want the PR author to be a different user, then you would probably need that user to be the app owner.

@exosyphon-rxrevu
Copy link
Author

@kubukoz I ended up creating a custom workflow. Hope this helps you solve your issue but it does use PATs.

name: Make Pull Request

on:
  workflow_call:
    secrets:
      GH_PACKAGES_PAT:
        required: true

jobs:
  test:
    name: Make Pull Request
    runs-on: ubuntu-latest
    steps:
      -
        uses: actions/checkout@v4
        with:
          ref: ${{ github.head_ref }}
          fetch-depth: 0
      -
        run: |
          main_branch=`git remote show origin | grep 'HEAD branch' | cut -d' ' -f5`
          new_git_body=`mktemp`
          git log --pretty=format:"## %s%n%n%b%n---%n" origin/$main_branch..HEAD >> $new_git_body
          echo >> $new_git_body
          echo "---" >> $new_git_body
          echo >> $new_git_body
          gh api -H "Accept: application/vnd.github.raw" /repos/COMPANY/.github/contents/.github/PULL_REQUEST_TEMPLATE/pull_request_template.md\?ref\=main >> $new_git_body
          gh pr create --draft --assignee ${{github.actor}} --body-file $new_git_body --title "$(git log --pretty=format:%s -1)"
        continue-on-error: true
        name: Create draft pull request
        env:
          GH_TOKEN: ${{secrets.GH_PACKAGES_PAT}}

@peter-evans
Copy link
Owner

Just for your information. This is advanced notice that the git-token input will change it's name to branch-token in the upcoming v7 release. There should be no change in its current behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants