Skip to content

Commit

Permalink
feat: update-pull-request-title-and-body option (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m authored Dec 21, 2021
1 parent c2ea469 commit 220e0d8
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 14 deletions.
76 changes: 65 additions & 11 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ on:
push:
branches:
- master
pull_request_target:
pull_request:
types:
- opened
- synchronize
branches:
- master
workflow_dispatch: {}
name: Test
jobs:
Expand Down Expand Up @@ -53,12 +51,12 @@ jobs:
with:
title: Test pull request
body: This pull request is part of the CI - please ignore.
branch: test-create-new-pull-request
branch: test-create-new-pull-request-${{ github.run_number }}
commit-message: "Just testing [skip ci]"
author: "J. Doe <j@example.com>"
labels: test1, test2
assignees: gr2m
- run: "git push https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git :test-create-new-pull-request"
- run: "git push https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git :test-create-new-pull-request-${{ github.run_number }}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- if: ${{ steps.run.outputs.result != 'created' }}
Expand All @@ -84,7 +82,7 @@ jobs:
with:
title: Test pull request with multiple commits
body: This pull request is part of the CI - please ignore.
branch: test-create-new-pull-request-with-multiple-commits
branch: test-create-new-pull-request-with-multiple-commits-${{ github.run_number }}
author: "J. Doe <j@example.com>"
path: file1.txt
commit-message: "file 1 created [skip ci]"
Expand All @@ -95,7 +93,7 @@ jobs:
with:
title: Test pull request
body: This pull request is part of the CI - please ignore.
branch: test-create-new-pull-request-with-multiple-commits
branch: test-create-new-pull-request-with-multiple-commits-${{ github.run_number }}
author: "J. Doe <j@example.com>"
path: file2.txt
commit-message: "file 2 created [skip ci]"
Expand All @@ -106,11 +104,11 @@ jobs:
with:
title: Test pull request
body: This pull request is part of the CI - please ignore.
branch: test-create-new-pull-request-with-multiple-commits
branch: test-create-new-pull-request-with-multiple-commits-${{ github.run_number }}
author: "J. Doe <j@example.com>"
path: does_not_exist.txt
commit-message: "this should fail silently, not commit should be created"
- run: "git push https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git :test-create-new-pull-request-with-multiple-commits"
- run: "git push https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git :test-create-new-pull-request-with-multiple-commits-${{ github.run_number }}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Expand All @@ -132,8 +130,8 @@ jobs:
title: Test pull request for `auto-merge` option
commit-message: "Testing auto-merge"
auto-merge: squash
branch: test-auto-merge
- run: "git push https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git :test-auto-merge"
branch: test-auto-merge-${{ github.run_number }}
- run: "git push https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git :test-auto-merge-${{ github.run_number }}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Expand All @@ -155,3 +153,59 @@ jobs:
ACTIONS_STEP_DEBUG: true
with:
commit-message: "Just testing [skip ci]"

updatePRTitleAndBody:
name: "[TEST] Update PR title and body"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
persist-credentials: false
- run: "date > test.txt"
- run: "npm ci"
- run: "npm run build"
- uses: ./
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ACTIONS_STEP_DEBUG: true
with:
title: Test pull request
body: This pull request is part of the CI - please ignore.
branch: test-update-pr-title-and-body-${{ github.run_number }}
commit-message: "Just testing [skip ci]"
- run: "echo updated >> test.txt"
- uses: ./
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ACTIONS_STEP_DEBUG: true
with:
title: Skipped Updated test pull request
body: This Skipped updated pull request is part of the CI - please ignore.
branch: test-update-pr-title-and-body-${{ github.run_number }}
commit-message: "Just testing [skip ci]"
- run: "echo updated >> test.txt"
- uses: ./
id: run
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ACTIONS_STEP_DEBUG: true
with:
title: Updated test pull request
body: This updated pull request is part of the CI - please ignore.
branch: test-update-pr-title-and-body-${{ github.run_number }}
commit-message: "Just testing [skip ci]"
update-pull-request-title-and-body: true
- uses: octokit/request-action@v2.x
id: get-pull-request
with:
route: GET /repos/{owner}/{repo}/pulls/{pull_number}
owner: gr2m
repo: create-or-update-pull-request-action
pull_number: ${{ steps.run.outputs.pull-request-number }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- run: "git push https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git :test-update-pr-title-and-body-${{ github.run_number }}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- if: ${{ fromJson(steps.get-pull-request.outputs.data).title != 'Updated test pull request' }}
run: 'echo "Pull request title is \"${{ fromJson(steps.get-pull-request.outputs.data).title }}\" but expected \"Updated test pull request\"" && exit 1'
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ with:
labels: label1, label2
assignees: user1, user2
auto-merge: squash
update-pull-request-title-and-body: false
```

**Note:** `auto-merge` is optional. It can be set to `merge`, `squash`, or `rebase`. If [auto-merging](https://docs.github.com/en/github/collaborating-with-pull-requests/incorporating-changes-from-a-pull-request/automatically-merging-a-pull-request) is disabled in the repository, a warning will be logged, but the action will not fail.
Expand Down Expand Up @@ -94,8 +95,8 @@ If there are changes, it does the following

The actions outputs following properties:

- `pull-request-number` - number of created/updated PR. Not set if result is `unchanged`.
- `result` - `created`, `updated` or `unchanged` based if the PR was created, updated or if there were no local changes.
- `pull-request-number` - number of created/updated PR. Not set if result is `unchanged`.
- `result` - `created`, `updated` or `unchanged` based if the PR was created, updated or if there were no local changes.

The action is written in JavaScript. [Learn how to create your own](https://help.github.com/en/articles/creating-a-javascript-action).

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ inputs:
auto-merge:
description: "Enable auto merge for pull request. Requires auto merging to be enabled in repository settings"
required: false
update-pull-request-title-and-body:
description: "Set to true if you want the title and body to be updated for an existing pull request"
default: "false"
required: false

outputs:
pull-request-number:
Expand Down
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ async function main() {
labels: core.getInput("labels"),
assignees: core.getInput("assignees"),
autoMerge: core.getInput("auto-merge"),
updatePRTitleAndBody: core.getInput("update-pull-request-title-and-body"),
};

core.debug(`Inputs: ${inspect(inputs)}`);
Expand Down Expand Up @@ -85,7 +86,7 @@ async function main() {
core.info("No local changes");
}

core.setOutput("result", "unchanged")
core.setOutput("result", "unchanged");
process.exit(0); // there is currently no neutral exit code
}

Expand Down Expand Up @@ -159,6 +160,15 @@ async function main() {
core.info(
`Existing pull request for branch "${inputs.branch}" updated: ${prInfo.html_url}`
);
if (inputs.updatePRTitleAndBody === "false") return;
await octokit.request(`POST /repos/{owner}/{repo}/pulls/{number}`, {
owner,
repo,
number: prInfo.number,
title: inputs.title,
body: inputs.body,
});
core.info(`PR title and body are updated`);
return;
}
}
Expand Down

0 comments on commit 220e0d8

Please sign in to comment.