From c90a128bb4e808ed3a1a8212eb4aac2e1179792d Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Wed, 7 Oct 2020 09:57:36 -0700 Subject: [PATCH] feat(action): action can now review pull requests (#135) * feat(action): action can now review pull requests * fix: input name * fix: fix default upstream repo/owner * docs: update actions instructions --- README.md | 113 ++++++++++++++++++++++++++++++++++++++++++++------ action.yaml | 16 ++++--- entrypoint.sh | 39 ++++++++++------- 3 files changed, 132 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 35122a01..39cbd194 100644 --- a/README.md +++ b/README.md @@ -180,7 +180,6 @@ npm i code-suggester -g #### Syntax - `code-suggester pr [options] --upstream-repo= --upstream-owner=` #### Environment Variables @@ -237,14 +236,46 @@ Whether or not to attempt forking to a separate repository. Default value is: `t code-suggester pr -o foo -r bar -d 'description' -t 'title' -m 'message' --git-dir=. ``` -## Action - -### create a pull request -Opens a GitHub Pull Request against the upstream primary branch with the provided git directory. By default the git directory is the same as the `$GITHUB_WORKSPACE` directory. +### code-suggester review +`code-suggester review` - review an open GitHub Pull Request and suggest changes. #### Syntax -`pr --upstream-repo= --upstream-owner= --title= --description= --message= [options] ` +`code-suggester review --upstream-repo= --upstream-owner= --pull-number= --git-dir=` + +#### Environment Variables +#### `ACCESS_TOKEN` +*string*
+**Required.** The GitHub access token which has permissions to fork, write to its forked repo and its branches, as well as create Pull Requests on the upstream repository. + +#### Options + +#### `--upstream-repo, -r` +*string*
+**Required.** The repository to create the fork off of. + + +#### `--upstream-owner, -o` +*string*
+**Required.** The owner of the upstream repository. + +#### `--pull-number, -p` +*number*
+**Required.** The pull request number. + +#### `--git-dir` +*string*
+**Required.** The path of a git directory + +### Example +``` +code-suggester pr -o foo -r bar -p 1234 --git-dir=. +``` + +## Action + +### Create a Pull Request +Opens a GitHub Pull Request against the upstream primary branch with the provided git directory. By default the git directory is the same as the `$GITHUB_WORKSPACE` directory. #### Environment Variables #### `ACCESS_TOKEN` @@ -257,12 +288,10 @@ Opens a GitHub Pull Request against the upstream primary branch with the provide *string*
**Required.** The repository to create the fork off of. - #### `upstream_owner` *string*
**Required.** The owner of the upstream repository. - #### `description` *string*
**Required.** The GitHub Pull Request description. @@ -299,7 +328,8 @@ Whether or not maintainers can modify the pull request. Default value is: `true` *boolean*
Whether or not to attempt forking to a separate repository. Default value is: `true`. -### Example +#### Example + The following example is a `.github/workflows/main.yaml` file in repo `Octocat/HelloWorld`. This would add a LICENSE folder to the root `HelloWorld` repo on every pull request if it is not already there. ```yaml on: @@ -328,6 +358,67 @@ jobs: git_dir: '.' ``` +### Review a Pull Request + +Suggests changes against an open GitHub Pull Request with changes in the provided git directory. By default the git directory is the same as the `$GITHUB_WORKSPACE` directory. + +#### Environment Variables +#### `ACCESS_TOKEN` +*string*
+**Required.** The GitHub access token for the user making the suggested changes. We recommend storing it as a secret in your GitHub repository. + +#### Options + +#### `upstream_repo` +*string*
+**Required.** The repository to create the fork off of. + +#### `upstream_owner` +*string*
+**Required.** The owner of the upstream repository. + +#### `pull_number` +*number*
+**Required.** The GitHub Pull Request number. + +#### `git_dir` +*string*
+**Required.** The path of a git directory. Relative to `$GITHUB_WORKSPACE`. + +#### Example + +The following example is a `.github/workflows/main.yaml` file in repo `Octocat/HelloWorld`. This would run the go formatter on the code and then create a pull request review suggesting `go fmt` fixes. + +```yaml +on: + pull_request_target: + types: [opened, synchronize] + branches: + - master +name: ci +jobs: + add-license: + runs-on: ubuntu-latest + env: + ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} + steps: + - uses: actions/checkout@v2 + with: + ref: ${{github.event.pull_request.head.ref}} + repository: ${{github.event.pull_request.head.repo.full_name}} + - name: format + run: go fmt + - uses: googleapis/code-suggester@v1 # takes the changes from git directory + with: + command: review + pull_number: ${{ github.event.pull_request.number }} + git_dir: '.' +``` + +**Important**: When using `pull_request_target` and `actions/checkout` with the pull +request code, make sure that an external developer cannot override the commands run +from the pull request. + ## Supported Node.js Versions Our client libraries follow the [Node.js release schedule](https://nodejs.org/en/about/releases/). @@ -355,14 +446,10 @@ _Legacy Node.js versions are supported as a best effort:_ This library follows [Semantic Versioning](http://semver.org/). - - This library is considered to be in **alpha**. This means it is still a work-in-progress and under active development. Any release is subject to backwards-incompatible changes at any time. - - More Information: [Google Cloud Platform Launch Stages][launch_stages] [launch_stages]: https://cloud.google.com/terms/launch-stages diff --git a/action.yaml b/action.yaml index 7be49fab..58386d9a 100644 --- a/action.yaml +++ b/action.yaml @@ -17,24 +17,20 @@ author: googleapis description: An action-wrapper for the npm cli code-suggester. It suggests code to your repository inputs: command: - description: 'The command for code-suggester to run' + description: 'The command for code-suggester to run. Can be "pr" or "review"' required: true upstream_repo: description: 'The repository to create the fork off of.' upstream_owner: description: 'The owner of the upstream repository.' description: - description: 'The GitHub Pull Request description.' - required: true + description: 'The GitHub Pull Request description. Required for "pr" command.' title: - description: 'The GitHub Pull Request title.' - required: true + description: 'The GitHub Pull Request title. Required for "pr" command.' message: - description: 'The GitHub commit message.' - required: true + description: 'The GitHub commit message. Required for "pr" command.' branch: - description: 'The GitHub working branch name.' - required: true + description: 'The GitHub working branch name. Required for "pr" command.' primary: description: 'The primary upstream branch to open a PR against.' default: master @@ -53,6 +49,8 @@ inputs: description: >- Whether or not to attempt forking to a separate repository. Default is true. default: true + pull_number: + description: Pull Request number to review. Required for "review" command. runs: using: docker image: Dockerfile diff --git a/entrypoint.sh b/entrypoint.sh index b5a9030a..ff0cb9d9 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -18,23 +18,34 @@ set -e if [[ -z "${INPUT_UPSTREAM_REPO}" ]] then - INPUT_UPSTREAM_REPO=$(cat ${GITHUB_REPOSITORY} | cut -d/ -f2) + INPUT_UPSTREAM_REPO=$(echo ${GITHUB_REPOSITORY} | cut -d/ -f2) fi if [[ -z "${INPUT_UPSTREAM_OWNER}" ]] then - INPUT_UPSTREAM_OWNER=$(cat ${GITHUB_REPOSITORY} | cut -d/ -f1) + INPUT_UPSTREAM_OWNER=$(echo ${GITHUB_REPOSITORY} | cut -d/ -f1) fi -code-suggester ${INPUT_COMMAND} \ - --upstream-repo="${INPUT_UPSTREAM_REPO}" \ - --upstream-owner="${INPUT_UPSTREAM_OWNER}" \ - --description="${INPUT_DESCRIPTION}" \ - --title="${INPUT_TITLE}" \ - --branch="${INPUT_BRANCH}" \ - --primary="${INPUT_PRIMARY}" \ - --message="${INPUT_MESSAGE}" \ - --force="${INPUT_FORCE}" \ - --maintainers-can-modify="${INPUT_MAINTAINERS_CAN_MODIFY}" \ - --git-dir="${INPUT_GIT_DIR}" \ - --fork="${INPUT_FORK}" +case "${INPUT_COMMAND}" in + pr) + code-suggester pr \ + --upstream-repo="${INPUT_UPSTREAM_REPO}" \ + --upstream-owner="${INPUT_UPSTREAM_OWNER}" \ + --description="${INPUT_DESCRIPTION}" \ + --title="${INPUT_TITLE}" \ + --branch="${INPUT_BRANCH}" \ + --primary="${INPUT_PRIMARY}" \ + --message="${INPUT_MESSAGE}" \ + --force="${INPUT_FORCE}" \ + --maintainers-can-modify="${INPUT_MAINTAINERS_CAN_MODIFY}" \ + --git-dir="${INPUT_GIT_DIR}" \ + --fork="${INPUT_FORK}" + ;; + review) + code-suggester review \ + --upstream-repo="${INPUT_UPSTREAM_REPO}" \ + --upstream-owner="${INPUT_UPSTREAM_OWNER}" \ + --pull-number="${INPUT_PULL_NUMBER}" \ + --git-dir="${INPUT_GIT_DIR}" + ;; +esac