Skip to content

Commit

Permalink
Merge pull request #198 from 8BitJonny/feat/set-pr-found-output
Browse files Browse the repository at this point in the history
Add pr_found output
  • Loading branch information
8BitJonny authored Dec 8, 2022
2 parents 42acde3 + 780544c commit 2215326
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 11 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ jobs:
sha: "44b060a85b83f5baf49f523d8a58444ccca52ead"
- name: Assert PR properties
run: |
[[ ${{ steps.pr.outputs.number }} == 5 ]] || (echo "PR miss match"; exit 1)
[[ ${{ steps.pr.outputs.pr_found }} == true ]] || (echo "outputs.pr_found != true"; exit 1)
[[ ${{ steps.pr.outputs.number }} == 5 ]] || (echo "outputs.number != 5"; exit 1)
test-if-closed-pr-is-not-found-when-filtering-closed-prs:
runs-on: ubuntu-latest
steps:
Expand All @@ -60,6 +61,9 @@ jobs:
filterOutClosed: true
github-token: ${{ secrets.GITHUB_TOKEN }}
sha: "44b060a85b83f5baf49f523d8a58444ccca52ead"
- name: Exit 1, if PR was falsely found
if: steps.pr.outputs.pr_found != 'false'
run: echo "if 'outputs.pr_found' falsely triggered"; exit 1
- name: Assert PR properties
run: |
[[ '${{ steps.pr.outputs.number }}' == '' ]] || (echo "PR falsely found"; exit 1)
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ This action enables you to get the PR no matter which event type triggered the w

```yml
steps:
- uses: 8BitJonny/gh-get-current-pr@2.1.3
- uses: 8BitJonny/gh-get-current-pr@2.2.0
id: PR

- run: echo "Your PR number is ${{ steps.PR.outputs.number }} and its JSON is ${{ steps.PR.outputs.pr }}"
Expand All @@ -39,7 +39,7 @@ This action enables you to get the PR no matter which event type triggered the w
See [action.yml](action.yml) for more details.
```yml
steps:
- uses: 8BitJonny/gh-get-current-pr@2.1.3
- uses: 8BitJonny/gh-get-current-pr@2.2.0
id: PR
with:
# Authetication token to access GitHub APIs. (Can be omitted by default.)
Expand All @@ -56,12 +56,11 @@ See [action.yml](action.yml) for more details.
See [action.yml](action.yml) for more details.
```yml
steps:
- uses: 8BitJonny/gh-get-current-pr@2.1.3
- uses: 8BitJonny/gh-get-current-pr@2.2.0
id: PR

- run: echo "PR ${prNumber} ${prTitle} at ${prUrl} is ${prJSON}"
# 'steps.PR.outcome' reports if the action errors and 'steps.PR.outputs.pr' reports if a PR has been found
if: steps.PR.outcome == 'success' && steps.PR.outputs.pr
if: steps.PR.outputs.pr_found == 'true'
env:
# JSON object with the full PR object
# toJSON(fromJSON(...pr)) parses it into memory and then format is with pretty-print.
Expand All @@ -82,10 +81,11 @@ Useful when the information you're looking for is not exported as a direct outpu
See [GitHub Documentation](https://docs.github.com/en/rest/commits/commits#list-pull-requests-associated-with-a-commit) for details how the object looks like.
```yml
steps:
- uses: 8BitJonny/gh-get-current-pr@2.1.3
- uses: 8BitJonny/gh-get-current-pr@2.2.0
id: PR
- name: "Pull Request ${{ steps.PR.outputs.number }}"
if: steps.PR.outputs.pr_found == 'true'
run: |
echo "from ${{ fromJSON(steps.PR.outputs.pr).head.ref }}"
echo "to ${{ fromJSON(steps.PR.outputs.pr).base.ref }}"
Expand All @@ -103,7 +103,7 @@ A short form of the article's explanation is, that Github creates an extra merge
To always find and pass the correct commit SHA to this action use this workflow config:
```yml
steps:
- uses: 8BitJonny/gh-get-current-pr@2.1.3
- uses: 8BitJonny/gh-get-current-pr@2.2.0
id: PR
with:
sha: ${{ github.event.pull_request.head.sha }}
Expand Down
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ inputs:
description: True, False, 1 or 0 if only non-draft PRs should be returned. Defaults to false.
required: false
outputs:
pr_found:
description: The outcome if a PR has been found. If so, the other outputs are set.
pr:
description: The whole PR object if one was found.
number:
Expand Down
1 change: 1 addition & 0 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "actiongetcurrentpr",
"version": "2.1.1",
"version": "2.2.0",
"description": "Github Action for getting the current PR if the commit belongs to one",
"main": "lib/main.js",
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion src/io/set-output.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import * as core from '@actions/core'
import {PR} from '../types/pull-request'

function setOutputWithDebug(key: string, value: string | null): void {
function setOutputWithDebug(key: string, value: unknown): void {
core.debug(`Setting output: key: "${key}", value: "${value}"`)
core.setOutput(key, value)
}

export default function setOutput(pr: PR | null): void {
setOutputWithDebug('pr_found', !!pr)
if (pr) {
setOutputWithDebug('number', pr.number.toString())
setOutputWithDebug('pr', JSON.stringify(pr))
Expand Down

0 comments on commit 2215326

Please sign in to comment.