From c9e477ec051a8fb2ef523640a5ecbfe6297514d3 Mon Sep 17 00:00:00 2001 From: Peter Evans Date: Tue, 5 Nov 2019 14:10:23 +0900 Subject: [PATCH] Update examples --- examples.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/examples.md b/examples.md index e53a9cf..51a3a10 100644 --- a/examples.md +++ b/examples.md @@ -4,6 +4,7 @@ - [Update NPM dependencies](#update-npm-dependencies) - [Keep Go up to date](#keep-go-up-to-date) - [Spider and download a website](#spider-and-download-a-website) +- [Use case: Create a pull request to update X by calling the GitHub API](#use-case-create-a-pull-request-to-update-x-by-calling-the-github-api) - [Use case: Create a pull request to modify/fix pull requests](#use-case-create-a-pull-request-to-modifyfix-pull-requests) - [autopep8](#autopep8) - [Misc workflow tips](#misc-workflow-tips) @@ -116,6 +117,33 @@ jobs: BRANCH_SUFFIX: none ``` +## Use case: Create a pull request to update X by calling the GitHub API + +You can use the GitHub API to trigger a webhook event called [`repository_dispatch`](https://help.github.com/en/github/automating-your-workflow-with-github-actions/events-that-trigger-workflows#external-events-repository_dispatch) when you want to trigger a workflow for activity that happens outside of GitHub. +This pattern will work well for updating any kind of static content from an external source. + +You can modify any of the examples in the previous section to work in this fashion. + +1. Set the workflow to execute `on: repository_dispatch`. + ``` + on: + repository_dispatch: + types: [create-pull-request] + ``` + +2. To trigger the workflow call the GitHub API as follows. + - `[username]` is a GitHub username + - `[token]` is a `repo` scoped [Personal Access Token](https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line) + - `[repository]` is the name of the repository the workflow resides in. + + ``` + curl -XPOST -u "[username]:[token]" \ + -H "Accept: application/vnd.github.everest-preview+json" \ + -H "Content-Type: application/json" \ + https://api.github.com/repos/[username]/[repository]/dispatches \ + --data '{"event_type": "create-pull-request"}' + ``` + ## Use case: Create a pull request to modify/fix pull requests This is a pattern that works well for any automated code linting and fixing. A pull request can be created to fix or modify something during an `on: pull_request` workflow. The pull request containing the fix will be raised with the original pull request as the base. This can be then be merged to update the original pull request and pass any required tests.