Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for workflow_dispatch event #51

Merged
merged 29 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
0f45911
feat: support workflow dispatch trigger
jhoffmcd Jul 5, 2021
dfb2c6b
feat: return json instead of text in fetch
jhoffmcd Jul 5, 2021
cfed977
feat: add PR_NUMBER to inputs
jhoffmcd Jul 5, 2021
c755248
chore: add logging for troubleshooting
jhoffmcd Jul 5, 2021
6f88a38
fix: wrong headers on get PR
jhoffmcd Jul 5, 2021
6cb66ee
chore: remove some logs
jhoffmcd Jul 5, 2021
368b327
chore: update logs again
jhoffmcd Jul 5, 2021
845957e
fix: broken url formatting
jhoffmcd Jul 5, 2021
8dfd403
chore: build fix to url
jhoffmcd Jul 5, 2021
5438ef4
chore: remove logs and cleanup
jhoffmcd Jul 5, 2021
ea01dd1
docs: add workflow docs to readme file
jhoffmcd Jul 5, 2021
f123b54
chore: change back to v1 lockfile
jhoffmcd Jul 9, 2021
58fc09a
feat: use octokit instead of node-fetch
jhoffmcd Jul 9, 2021
39de057
chore: retur error if no PR number is present
jhoffmcd Jul 9, 2021
20c39d4
chore: finish unfinished error message
jhoffmcd Jul 9, 2021
76ed400
chore: update dist from build
jhoffmcd Jul 11, 2021
e7b8ba4
chore: add better conditionals and checks for user input
jhoffmcd Jul 11, 2021
0ae082f
chore: consolidate getter for PR
jhoffmcd Jul 24, 2021
e8d4ef5
fix: add missing await keyword
jhoffmcd Jul 24, 2021
1b0e367
chore: reduce logic and update pull request function
jhoffmcd Aug 5, 2021
287b10d
chroe: update pr number check logic
jhoffmcd Aug 5, 2021
a64acf4
chore: test output
jhoffmcd Aug 5, 2021
552a813
chore: log more output
jhoffmcd Aug 5, 2021
f86e518
chore: remove output logging and change PR number checks
jhoffmcd Aug 5, 2021
5bc196f
chore: test PR number input
jhoffmcd Aug 5, 2021
804cf47
chore: log additional data
jhoffmcd Aug 5, 2021
641f3c5
chore: use proper condition for check for PR
jhoffmcd Aug 6, 2021
8938e24
Update README.md
jhoffmcd Aug 9, 2021
bb64d45
docs: update parameter for pr number
jhoffmcd Aug 9, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ _Optional_ A flag to only auto-merge updates based on Semantic Versioning. Defau

For more details on how semantic version difference calculated please see [semver](https://www.npmjs.com/package/semver) package

### `pr-number`

_Optional_ A pull request number, only required if triggered from a workflow_dispatch event. Typically this would be triggered by a script running in a seperate CI provider. See [Trigger action from workflow_dispatch event](#trigger-action-from-workflow_dispatch-event)

## Example usage

Expand Down Expand Up @@ -85,6 +88,42 @@ steps:
approve-only: true
```

### Trigger action from workflow_dispatch event

If you need to trigger this action manually, you can use the [workflow_dispatch](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#workflow_dispatch) event. A use case might be that your CI runs on a seperate provider, so you would like to run this action as a result of a successful CI run.

When using the `workflow_dispatch` approach, you will need to send the PR number as part of the input for this action:

```yml
name: automerge

on:
workflow_dispatch:
inputs:
pr-number:
required: true

jobs:
automerge:
runs-on: ubuntu-latest
steps:
- uses: fastify/[email protected]
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
pr-number: ${{ github.event.inputs.pr-number }}
```

You can initiate a call to trigger this event via [API](https://docs.github.com/en/rest/reference/actions/#create-a-workflow-dispatch-event):

```bash
# Note: replace dynamic values with your relevant data
curl -X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token {token}" \
https://api.github.com/repos/{owner}/{reponame}/actions/workflows/{workflow}/dispatches \
-d '{"ref":"{ref}", "inputs":{ "pr-number": "{number}"}}'
```

## Notes

- A GitHub token is automatically provided by Github Actions, which can be accessed using `secrets.GITHUB_TOKEN` and supplied to the action as an input `github-token`.
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ inputs:
description: 'Auto-merge on major, minor, patch updates based on Semantic Versioning'
required: false
default: 'major'
pr-number:
description: 'A pull request number, only required if triggered from a workflow_dispatch event'
required: false
runs:
using: 'node12'
main: 'dist/index.js'
Expand Down
48 changes: 43 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6676,6 +6676,36 @@ const checkTargetMatchToPR = (prTitle, target) => {
module.exports = checkTargetMatchToPR


/***/ }),

/***/ 6754:
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {

"use strict";


const github = __nccwpck_require__(5438)

const getPullRequest = async ({ pullRequestNumber, githubToken }) => {
const payload = github.context.payload
const octokit = github.getOctokit(githubToken)

const repo = payload.repository
const owner = repo.owner.login
const repoName = repo.name

const { data: pullRequest } = await octokit.rest.pulls.get({
owner,
repo: repoName,
pull_number: pullRequestNumber,
})

return pullRequest
}

module.exports = getPullRequest


/***/ }),

/***/ 5013:
Expand Down Expand Up @@ -6772,6 +6802,7 @@ exports.getInputs = () => ({
APPROVE_ONLY: /true/i.test(core.getInput('approve-only')),
API_URL: core.getInput('api-url'),
TARGET: getTargetInput(core.getInput('target')),
PR_NUMBER: core.getInput('pr-number'),
})


Expand Down Expand Up @@ -6936,6 +6967,7 @@ const github = __nccwpck_require__(5438)
const fetch = __nccwpck_require__(467)

const checkTargetMatchToPR = __nccwpck_require__(7186)
const getPullRequest = __nccwpck_require__(6754)
const { logInfo, logWarning, logError } = __nccwpck_require__(653)
const { getInputs } = __nccwpck_require__(6254)

Expand All @@ -6947,21 +6979,27 @@ const {
APPROVE_ONLY,
API_URL,
TARGET,
PR_NUMBER,
} = getInputs()

const GITHUB_APP_URL = 'https://github.com/apps/dependabot-merge-action'

async function run() {
try {
const { pull_request: pr } = github.context.payload
const { pull_request } = github.context.payload

if (!pr) {
if (!pull_request && !PR_NUMBER) {
return logError(
'This action must be used in the context of a Pull Request'
'This action must be used in the context of a Pull Request or with a Pull Request number'
)
}

const pullRequestNumber = pr.number
const pr =
pull_request ||
(await getPullRequest({
pullRequestNumber: PR_NUMBER,
githubToken: GITHUB_TOKEN,
}))

const isDependabotPR = pr.user.login === 'dependabot[bot]'

Expand Down Expand Up @@ -6989,7 +7027,7 @@ async function run() {
'content-type': 'application/json',
},
body: JSON.stringify({
pullRequestNumber,
pullRequestNumber: pr.number,
approveOnly: APPROVE_ONLY,
excludePackages: EXCLUDE_PKGS,
approveComment: MERGE_COMMENT,
Expand Down
22 changes: 22 additions & 0 deletions src/getPullRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

const github = require('@actions/github')

const getPullRequest = async ({ pullRequestNumber, githubToken }) => {
const payload = github.context.payload
const octokit = github.getOctokit(githubToken)

const repo = payload.repository
const owner = repo.owner.login
const repoName = repo.name

const { data: pullRequest } = await octokit.rest.pulls.get({
owner,
repo: repoName,
pull_number: pullRequestNumber,
})

return pullRequest
}

module.exports = getPullRequest
17 changes: 12 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const github = require('@actions/github')
const fetch = require('node-fetch')

const checkTargetMatchToPR = require('./checkTargetMatchToPR')
const getPullRequest = require('./getPullRequest')
const { logInfo, logWarning, logError } = require('./log')
const { getInputs } = require('./util')

Expand All @@ -16,21 +17,27 @@ const {
APPROVE_ONLY,
API_URL,
TARGET,
PR_NUMBER,
} = getInputs()

const GITHUB_APP_URL = 'https://github.com/apps/dependabot-merge-action'

async function run() {
try {
const { pull_request: pr } = github.context.payload
const { pull_request } = github.context.payload

if (!pr) {
if (!pull_request && !PR_NUMBER) {
return logError(
'This action must be used in the context of a Pull Request'
'This action must be used in the context of a Pull Request or with a Pull Request number'
)
}

const pullRequestNumber = pr.number
const pr =
pull_request ||
(await getPullRequest({
pullRequestNumber: PR_NUMBER,
githubToken: GITHUB_TOKEN,
}))

const isDependabotPR = pr.user.login === 'dependabot[bot]'

Expand Down Expand Up @@ -58,7 +65,7 @@ async function run() {
'content-type': 'application/json',
},
body: JSON.stringify({
pullRequestNumber,
pullRequestNumber: pr.number,
approveOnly: APPROVE_ONLY,
excludePackages: EXCLUDE_PKGS,
approveComment: MERGE_COMMENT,
Expand Down
1 change: 1 addition & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ exports.getInputs = () => ({
APPROVE_ONLY: /true/i.test(core.getInput('approve-only')),
API_URL: core.getInput('api-url'),
TARGET: getTargetInput(core.getInput('target')),
PR_NUMBER: core.getInput('pr-number'),
})