Skip to content

Commit

Permalink
feat: Add state parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwalton committed Aug 12, 2021
1 parent de950fc commit 93f196a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ 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

```yaml
Expand All @@ -19,6 +22,9 @@ Additionally, `title` and `body` outputs are available as well to get the respec
# Find the PR associated with this push, if there is one.
- uses: jwalton/gh-find-current-pr@v1
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
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ inputs:
github-token:
description: The GitHub token used to create an authenticated client. Defaults to github provided token.
default: ${{ github.token }}
state:
description: The state of the PR to return. One of "open", "closed", or "all".
default: 'open'
sha:
description: Sha to get PR for. Defaults to current sha.
default: ${{ github.sha }}
Expand Down
18 changes: 10 additions & 8 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@ const github = require('@actions/github');

async function main() {
const token = core.getInput('github-token', { required: false }) || process.env.GITHUB_TOKEN;
const sha = core.getInput('sha', { required: true});
const state = (core.getInput('state', { required: false }) || 'open').toLowerCase();
const sha = core.getInput('sha', { required: true });

const octokit = github.getOctokit(token)
const octokit = github.getOctokit(token);
const context = github.context;
const result = await octokit.rest.repos.listPullRequestsAssociatedWithCommit({
owner: context.repo.owner,
repo: context.repo.repo,
commit_sha: sha,
});

const pr = result.data.length > 0 && result.data.filter(el => el.state === 'open')[0];
const prs = result.data.filter((el) => state === 'all' || el.state === state);
const pr = prs[0];

core.setOutput('pr', pr && pr.number || '');
core.setOutput('number', pr && pr.number || '');
core.setOutput('title', pr && pr.title || '');
core.setOutput('body', pr && pr.body || '');
core.setOutput('pr', (pr && pr.number) || '');
core.setOutput('number', (pr && pr.number) || '');
core.setOutput('title', (pr && pr.title) || '');
core.setOutput('body', (pr && pr.body) || '');
}

main().catch(err => core.setFailed(err.message));
main().catch((err) => core.setFailed(err.message));

0 comments on commit 93f196a

Please sign in to comment.