Skip to content

Commit 0bd77d5

Browse files
authored
feat: action outputs (#352)
1 parent b56dfed commit 0bd77d5

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

.github/workflows/test.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ jobs:
2222
- run: "npm ci"
2323
- run: "npm run build"
2424
- uses: ./
25+
id: run
2526
env:
2627
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2728
ACTIONS_STEP_DEBUG: true
2829
with:
2930
commit-message: "Just testing [skip ci]"
31+
- if: ${{ steps.run.outputs.result != 'updated' || steps.run.outputs.pull-request-number > 0 }}
32+
run: 'echo "Output of action is not as expected" && exit 1'
3033

3134
createNewPullRequest:
3235
name: "[TEST] Create new pull request"
@@ -39,6 +42,7 @@ jobs:
3942
- run: "npm ci"
4043
- run: "npm run build"
4144
- uses: ./
45+
id: run
4246
env:
4347
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4448
ACTIONS_STEP_DEBUG: true
@@ -53,6 +57,10 @@ jobs:
5357
- run: "git push https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git :test-create-new-pull-request"
5458
env:
5559
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
60+
- if: ${{ steps.run.outputs.result != 'created' }}
61+
run: 'echo "result output is \"${{ steps.run.outputs.result }}\" but expected \"created\"" && exit 1'
62+
- if: ${{ steps.run.outputs.pull-request-number > 0 }}
63+
run: 'echo "pull-request-number output is \"${{ steps.run.outputs.pull-request-number }}\" but expected a number" && exit 1'
5664

5765
multipleCommits:
5866
name: "[TEST] Create multiple commits"

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ If there are changes, it does the following
9090
4. Pushes the local changes to remote using the branch configured in the `branch` input.
9191
5. Creates a pull request using the `title` and `body` inputs. If a pull request exists for the branch, it's checked out locally, rebased with `-XTheirs` and pushed with `--force` to update the pull request with the new changes.
9292

93+
The actions outputs following properties:
94+
95+
- `pull-request-number` - number of created/updated PR. Not set if result is `unchanged`.
96+
- `result` - `created`, `updated` or `unchanged` based if the PR was created, updated or if there were no local changes.
97+
9398
The action is written in JavaScript. [Learn how to create your own](https://help.github.com/en/articles/creating-a-javascript-action).
9499

95100
## Who is using it

action.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ inputs:
1212
description: "Pull Request body"
1313
required: true
1414
default: |
15-
This pull request has been created by the ["Create or Update Pull Request" action](https://github.com/gr2m/create-or-update-pull-request-action#readme).
15+
This pull request has been created by the ["Create or Update Pull Request" action](https://github.com/gr2m/create-or-update-pull-request-action#readme).
1616
1717
You can set a custom pull request title, body, branch and commit messages, see [Usage](https://github.com/gr2m/create-or-update-pull-request-action#usage).
1818
branch:
@@ -40,6 +40,12 @@ inputs:
4040
description: "Enable auto merge for pull request. Requires auto merging to be enabled in repository settings"
4141
required: false
4242

43+
outputs:
44+
pull-request-number:
45+
description: "Number of the created/updated pull request"
46+
result:
47+
description: "'updated', 'created', 'unchanged' based if the PR was created, updated or nothing happened"
48+
4349
runs:
4450
using: "node12"
4551
main: "dist/index.js"

index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ async function main() {
8484
} else {
8585
core.info("No local changes");
8686
}
87+
88+
core.setOutput("result", "unchanged")
8789
process.exit(0); // there is currently no neutral exit code
8890
}
8991

@@ -150,8 +152,12 @@ async function main() {
150152
});
151153

152154
if (data.total_count > 0) {
155+
const prInfo = data.items[0]; // Assuming there is only one PR for given branch
156+
157+
core.setOutput(`pull-request-number`, prInfo.number);
158+
core.setOutput(`result`, `updated`);
153159
core.info(
154-
`Existing pull request for branch "${inputs.branch}" updated: ${data.items.html_url}`
160+
`Existing pull request for branch "${inputs.branch}" updated: ${prInfo.html_url}`
155161
);
156162
return;
157163
}
@@ -171,6 +177,9 @@ async function main() {
171177

172178
core.info(`Pull request created: ${html_url} (#${number})`);
173179

180+
core.setOutput(`pull-request-number`, number);
181+
core.setOutput(`result`, `created`);
182+
174183
if (inputs.labels) {
175184
core.debug(`Adding labels: ${inputs.labels}`);
176185
const labels = inputs.labels.trim().split(/\s*,\s*/);

0 commit comments

Comments
 (0)