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 19 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions README.md
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:
jhoffmcd marked this conversation as resolved.
Show resolved Hide resolved
required: true

jobs:
automerge:
runs-on: ubuntu-latest
steps:
- uses: fastify/github-action-merge-dependabot@v2.2.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
pr: ${{ github.event.inputs.pr }}
jhoffmcd marked this conversation as resolved.
Show resolved Hide resolved
```

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
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
61 changes: 56 additions & 5 deletions dist/index.js
Expand Up @@ -6676,6 +6676,53 @@ const checkTargetMatchToPR = (prTitle, target) => {
module.exports = checkTargetMatchToPR


/***/ }),

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

"use strict";


const github = __nccwpck_require__(5438)

const { getInputs } = __nccwpck_require__(6254)
const { logError } = __nccwpck_require__(653)

const { GITHUB_TOKEN, PR_NUMBER } = getInputs()

const getPullRequest = async () => {
const payload = github.context.payload

// Checks for "workflow" context to set the pull request, otherwise defaults to checking "pull request" context
if (payload.workflow) {
if (!PR_NUMBER || (PR_NUMBER && isNaN(PR_NUMBER))) {
return logError(
'Missing or invalid pull request number. Please make sure you are using a valid pull request number'
)
}

const octokit = github.getOctokit(GITHUB_TOKEN)

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: PR_NUMBER,
})

return pullRequest
} else {
return payload.pull_request
}
}

module.exports = getPullRequest


/***/ }),

/***/ 5013:
Expand Down Expand Up @@ -6772,6 +6819,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 +6984,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 @@ -6953,15 +7002,17 @@ 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, workflow } = github.context.payload

const isSupportedContext = pull_request || workflow

if (!pr) {
if (!isSupportedContext) {
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 a Workflow Dispatch event'
)
}

const pullRequestNumber = pr.number
const pr = await getPullRequest()

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

Expand Down Expand Up @@ -6989,7 +7040,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
39 changes: 39 additions & 0 deletions src/getPullRequest.js
@@ -0,0 +1,39 @@
'use strict'

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

const { getInputs } = require('./util')
const { logError } = require('./log')

const { GITHUB_TOKEN, PR_NUMBER } = getInputs()

const getPullRequest = async () => {
const payload = github.context.payload

// Checks for "workflow" context to set the pull request, otherwise defaults to checking "pull request" context
if (payload.workflow) {
if (!PR_NUMBER || (PR_NUMBER && isNaN(PR_NUMBER))) {
return logError(
'Missing or invalid pull request number. Please make sure you are using a valid pull request number'
)
}

const octokit = github.getOctokit(GITHUB_TOKEN)

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: PR_NUMBER,
})

return pullRequest
} else {
return payload.pull_request
}
}

module.exports = getPullRequest
13 changes: 8 additions & 5 deletions src/index.js
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 @@ -22,15 +23,17 @@ 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, workflow } = github.context.payload

if (!pr) {
const isSupportedContext = pull_request || workflow

if (!isSupportedContext) {
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 a Workflow Dispatch event'
)
}

const pullRequestNumber = pr.number
const pr = await getPullRequest()

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

Expand Down Expand Up @@ -58,7 +61,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
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'),
})