Git Product home page Git Product logo

gh-find-current-pr's Introduction

gh-find-current-pr

semantic-release

This action tries to figure out the current PR.

If the event is a pull_request, it's very easy to get the current PR number from the context via ${{ github.event.number }}, but unfortunately this information does not seem to be readily available for a push event. This action sends a request to GitHub to find the PR associated with the current SHA, and returns its number in the number output. number will be an empty string if there is no PR.

Additionally, title and body outputs are available as well to get the respective title and body of the PR.

By default, gh-find-current-pr will only return open PRs. You can pass in a state parameter to pick "open", "closed", or "all" PRs.

Usage

steps:
  - uses: actions/checkout@v4
  # Find the PR associated with this push, if there is one.
  - uses: jwalton/gh-find-current-pr@master
    id: findPr
    with:
      # Can be "open", "closed", or "all".  Defaults to "open".
      state: open
  # This will echo "Your PR is 7", or be skipped if there is no current PR.
  - run: echo "Your PR is ${PR}"
    if: success() && steps.findPr.outputs.number
    env:
      PR: ${{ steps.findPr.outputs.pr }}

gh-find-current-pr's People

Contributors

8bitjonny avatar alexesprit avatar bdo avatar dependabot[bot] avatar donovanmuller avatar guioconnor avatar justanotherdev avatar jwalton avatar mvarrieur avatar semantic-release-bot avatar tlvenn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

gh-find-current-pr's Issues

Make sure the returned PR is open

The API used (client.repos.listPullRequestsAssociatedWithCommit) will include open and closed pull requests, the action should only return an open pull request.

Return PR labels

Just as done in #1 there should be an output that return the PRs labels.

My use case: I want to check on each push if the associated PR has a certain label attached.

Find pr during workflow triggered by push after rebase merge

This is what I found to work. It's not fool-proof as the same commit message could occur more than once and have been in more than one pr.

- name: Find PR
  id: find_pr
   env:
     GH_TOKEN: ${{ github.token }}
   run: |
     prs=$(gh pr list --search "${{ github.event.head_commit.message }}" --state merged)
     number=$(echo $prs | cut -d ' ' -f 1)
     echo "number=$number" >> $GITHUB_OUTPUT

It would be nice if this action could be made to accommodate this situation. If I get time, I'll take a look at the source to see if there's a sane way to add it.

Fun facts:

  • due to rebase, the commit sha has changed and the new one was never tied to a pr
  • the topic branch may already be deleted (as it is in my case)
  • searching the reflog you'd still end up having to use the message at some point afaict

Doesn't seem to work on force push.

if create a PR for a branch, and then I force push to that branch associated with the PR, the push event rather than the pull_request event occurs as expected, and the PR remains open for the branch as expected, but this action doesn't seem to return any associated PR.

Not working

Using the following workflow:

on:
  pull_request:
    branches:
      - main
    types:
      - closed

name: Tag on merged PR

jobs:
  tag:
    name: Tag merged MR
    if: github.event.pull_request.merged == true
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v1
      - name: Get PR description
        uses: jwalton/gh-find-current-pr@v1
        id: findPr
      - name: Echo PR data
        run: echo "Number = ${number} | Title = ${title} | Body = ${body}"
        env:
          number: ${{ steps.findPr.outputs.pr }}
          title: ${{ steps.findPr.outputs.title }}
          body: ${{ steps.findPr.outputs.body }}

The steps.findPr.outputs.* fields return empty.

If I modify the workflow to use the example given in this repository readme, the step does not run, as if it was not triggered by a merged PR. I'm working on a Private repository (for testing purposes), maybe this is not possible with this Action? I tried using the GITHUB_TOKEN, as I saw on the Action source that it was an optional parameter.

on:
  push:
    branches:
      - main

name: Tag on merged PR

jobs:
  tag:
    name: Tag merged MR
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      # Find the PR associated with this push, if there is one.
      - uses: jwalton/gh-find-current-pr@v1
        id: findPr
        env:  # tried with "env" and "with"
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      # This will echo "Your PR is 7", or be skipped if there is no current PR.
      - run: echo "Your PR is ${PR}"
        if: success() && steps.findPr.outputs.number
        env:
          PR: ${{ steps.findPr.outputs.pr }}

Output the entire pull request payload

Currently, this action outputs only specific values such as title, number, and so on.

core.info(`Setting output: draft: ${(pr && pr.draft) || ''}`);
core.setOutput('draft', (pr && pr.draft) || '');
core.info(`Setting output: pr: ${(pr && pr.number) || ''}`);
core.setOutput('pr', (pr && pr.number) || '');
core.info(`Setting output: number: ${(pr && pr.number) || ''}`);
core.setOutput('number', (pr && pr.number) || '');
core.info(`Setting output: title: ${(pr && pr.title) || ''}`);
core.setOutput('title', (pr && pr.title) || '');
core.setOutput('body', (pr && pr.body) || '');

If this action outputs the entire pull request payload, we can use all pull request attributes.
We can output the entire pull request payload as a JSON string, then we can get each attribute by fromJson.

Does not seem to work with issue_comment event

When the action is used and the event is issue_comment, the PR output is empty.

This is the output of an execution upon the issuing of a comment in an open PR

Run jwalton/gh-find-current-pr@v1
  with:
    state: open
    github-token: ***
    sha: jkdjd04434k4303jsnx20ewm
Setting output: draft: 
Setting output: pr: 
Setting output: number: 
Setting output: title: 
0s

Can I provide branch-name to find the PR number?

Right now We are not able to get the details of PR of a particular branch.
It only gets the details of base branch.
Can you please implement, where we can provide branch name to find the pr details? like below:

    - name: Identify Pull request number
      uses: jwalton/gh-find-current-pr@v1
      id: findPr
      with:
        state: open
        branch: my-branch-name
       

action fails unless actions have write permissions

Summary

Our org recently upgraded to Github enterprise and found that workflows using this action would fail with a message "Resource not accessible by integration"

Version

1.3.0

Description

The gh-find-current-pr action fails when granted read-only access at the Enterprise > policies > actions level. It succeeds when we grant read/write access to Github actions. I cannot think of why this action would need write access.

Deprecation warning

The action outputs the following on each build:

Warning: The `set-output` command is deprecated and will be disabled soon. Please upgrade to using Environment Files. For more information see: https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/

The code should be changed to resolve this issue and prevent the tool being impacted by the deprecation.

Richer outputs

Hi, thanks for this handy action, it's been very useful for us.

However, for us we need to know whether the PR is a draft or not.

Of course we, can get the PR number and make a further request for the PR metadata, but since the information is already on the first request, it's wasteful.

I forked your repo and and made the following change.

https://github.com/guioconnor/gh-find-current-pr/blob/master/main.js#L21

I wonder if you'd take a pull request so this is available for everyone and avoids me hosting it separately.

Also, perhaps there are other properties that's worth including instead of doing piecemeal work.

allow to set state

It would be nice if actions could allow to set state:

uses: jwalton/gh-find-current-pr@v1
with:
  state: 'closed'

That is helpful to find merged PR.

Create license for this action?

This solves an issue I am dealing with at work, but because there is no license I cannot use it.

Would it be possible to add a permissive license that would allow me to use this action?

Returns unexpected PR number when multiple PRs including same commit

Thanks for your work of this library!

This issue says, for instance, when there are three PRs like this:

  • PR #1: commit a
  • PR #2: commit a -> commit b
  • PR #3: commit a -> commit b -> commit c

I pass each last commit's sha to gh-find-current-pr, but each all actions return number #3.

method octokit.rest.repos.listPullRequestsAssociatedWithCommit() returns [3,2,1] but code prs[0] in main.js always returns 3.
Actually I'm not sure the order of the array returned though, but in my case, always returns last PR's number.

(official doc: https://octokit.github.io/rest.js/v18#repos-list-pull-requests-associated-with-commit)

Action returns empty string, but PR exists

My workflow runs on push and open pr. When I do the very first push - steps.findPr.outputs.pr return an empty string - which is expected. When I open a PR from that branch - steps.findPr.outputs.pr returns PR number, which is also great. But when I push additional commits to the branch - steps.findPr.outputs.pr returns an empty string, even though PR has already been open from that branch. Any idea what could be wrong?

not triggered by `pull_request_target`

When I specify event type pull_request everything works fine; however, on pull_request_target the step is not recognised by the CI (as if it was not in the YAML); that sucks, since PRs from other forks wont get comments on the first event type...

Return the PR object instead of simply its number

Hi,

I believe this action would be a lot more useful if it would return the entire PR that is fetched instead of simply returning its number.

For some context, I found this action while I was searching for one to get the title of the current PR.

It would also align better with the action name ;)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.