Code-suggester automates the steps involved in making code changes or code suggestions to your GitHub repository! Code-suggester
- can be imported as a library, or
- used as a CLI tool, or
- configured in a GitHub Action
npm i code-suggester
const suggester = require("code-suggester");
async function main() {
const octokit = new Octokit({ auth: process.env.ACCESS_TOKEN });
const changes =
{
'baz.txt':
{
mode: '100644',
content: 'hello world!'
}
};
await suggester.createPullRequest(
changes,
octokit,
'Foo-Repo',
'Bar-Owner',
)
}
The reviewPullRequest()
method creates a code suggestion review on a GitHub Pull request with the files given as input.
From these files, calculate the hunk diff and make all the multiline code suggestion review comments on a given pull request with these hunks given
that they are in scope of the pull request. Outof scope suggestions are not made.
In-scope suggestions are specifically: suggestions whose file is in-scope of the pull request, and suggestions whose diff hunk is a subset of the pull request's files hunks.
If a file is too large to load in the review, it is skipped in the suggestion phase.
If the program terminates without exception, a timeline comment will be made with all errors or suggestions that could not be made.
reviewPullRequest(octokit, diffContents, config [, logger])
octokit
Required. An authenticated octokit instance.
Map<string, FileDiffContent> | null | undefined
Required. A set of files with their respective original text file content and the new file content.
If the map is null, the empty map, or undefined, a review is not made.
A review is also not made when forall FileDiffContent objects, f,
f.oldContent == f.newContent
FileDiffContent Object
field | type | description |
---|---|---|
oldContent | string |
Required. The older version of a file. |
newContent | string |
Required. The newer version of a file. |
Review Pull Request Options Object
Required.
Review Pull Request Options Object
field | type | description |
---|---|---|
repo | string |
Required. The repository containing the pull request. |
owner | string |
Required. The owner of the repository. |
pullNumber | number |
Required. The GitHub Pull Request number. |
pageSize | number |
Required. The number of files to return in the pull request list files query. Used when getting data on the remote PR's files. |
Logger
The default logger is Pino. You can plug in any logger that conforms to Pino's interface
returns the review number if a review was created, or null if a review was not made and an exception was not thrown.
The core-library will throw an exception if the GitHub V3 API returns an error response, or if the response data format did not come back as expected.
The createPullRequest()
method creates a GitHub Pull request with the files given as input.
createPullRequest(octokit, changes, config [, logger])
octokit
Required. An authenticated octokit instance.
Map<string, FileData> | null | undefined
Required. A set of files with their respective file contents conforming where the key is the file path, and the value is the a FileData object. If it is null, the empty map, or undefined, no changes will be made.
FileData Object
field | type | description |
---|---|---|
mode | '100644' | '100755' | '040000' | '160000' | '120000' |
The file type as specified in the GitHub API. Default is '100644' . From the docs: "The file mode; one of 100644 for file (blob), 100755 for executable (blob), 040000 for subdirectory (tree), 160000 for submodule (commit), or 120000 for a blob that specifies the path of a symlink." |
content | string | null |
Required. The entire file contents. |
Pull Request Options Object
Required. Descriptive values or enforced rules for pull requests, branching, and commits.
Pull Request Options Object
field | type | description |
---|---|---|
upstreamRepo | string |
Required. The repository to suggest changes to. |
upstreamOwner | string |
Required. The owner of the upstream repository. |
description | string |
The GitHub Pull Request description. Default is 'code suggestions' . |
title | string |
The GitHub Pull Request title. Default is 'chore: code suggestions' . |
branch | string |
The branch containing the changes. Default is 'code-suggestions' . |
primary | string |
The primary upstream branch to open a PR against. Default is 'main' . |
message | string |
The commit message for the changes. Default is 'code suggestions' . We recommend following conventional commits. |
force | boolean |
Whether or not to force push the reference even if the ancestor commits differs. Default is false . |
fork | boolean |
Whether or not code suggestion should be made from a fork, defaults to true (Note: forking does not work when using secrets.GITHUB_TOKEN in an action). |
labels | string[] |
The list of labels to add to the pull request. Default is none. |
Logger
The default logger is Pino. You can plug in any logger that conforms to Pino's interface
The core-library will throw an exception if the GitHub V3 API returns an error response, or if the response data format did not come back as expected.
The parseTextFiles()
method takes a Map<string, string>
or Object<string, string>
and outputs the changes object for text files only.
parseTextFiles(textFiles)
Object<string, string> | Map<string, string>
Required. The key should be the relative file path in the source code, and the value should be the entire file content.
npm i code-suggester -g
code-suggester pr
- opens a GitHub Pull Request against the upstream primary branch with the provided set of changes.
code-suggester pr [options] --upstream-repo=<string> --upstream-owner=<string>
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.
string
Required. The repository to create the fork off of.
string
Required. The owner of the upstream repository.
string
The GitHub Pull Request description. Default value is: 'code suggestions'
.
string
The GitHub Pull Request title. Default value is: 'chore: code suggestions'
.
string
The GitHub working branch name. Default value is: 'code-suggestions'
.
string
The primary upstream branch to open a PR against. Default value is: 'main'
.
string
The GitHub commit message. Default value is: 'code suggestions'
.
boolean
Whether or not to force push a reference with different commit history before the remote reference HEAD. Default value is: false
.
string
Required. The path of a git directory
boolean
Whether or not to attempt forking to a separate repository. Default value is: true
.
array
The list of labels to add to the pull request. Default is none.
code-suggester pr -o foo -r bar -d 'description' -t 'title' -m 'message' --git-dir=.
code-suggester review
- review an open GitHub Pull Request and suggest changes.
code-suggester review --upstream-repo=<string> --upstream-owner=<string> --pull-number=<number> --git-dir=<string>
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.
string
Required. The repository to create the fork off of.
string
Required. The owner of the upstream repository.
number
Required. The pull request number.
string
Required. The path of a git directory
code-suggester pr -o foo -r bar -p 1234 --git-dir=.
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.
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. We recommend storing it as a secret in your GitHub repository.
string
Required. The repository to create the fork off of.
string
Required. The owner of the upstream repository.
string
Required. The GitHub Pull Request description.
string
Required. The GitHub Pull Request title.
string
The GitHub working branch name. Default value is: 'code-suggestions'
.
string
The primary upstream branch to open a PR against. Default value is: 'main'
.
string
Required. The GitHub commit message.
boolean
Whether or not to force push a reference with different commit history before the remote reference HEAD. Default value is: false
.
boolean
Whether or not maintainers can modify the pull request. Default value is: true
.
string
Required. The path of a git directory. Relative to $GITHUB_WORKSPACE
.
boolean
Whether or not to attempt forking to a separate repository. Default value is: true
.
array
The list of labels to add to the pull request. Default is none.
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.
on:
push:
branches:
- main
name: ci
jobs:
add-license:
runs-on: ubuntu-latest
env:
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
steps:
- uses: actions/checkout@v2
- name: <YOUR CHANGES> # the physical changes you want to make to your repository
run: (curl http://www.apache.org/licenses/LICENSE-2.0.txt) > LICENSE # For example adding LICENSE file
- uses: googleapis/code-suggester@v2 # takes the changes from git directory
with:
command: pr
upstream_owner: Octocat
upstream_repo: HelloWorld
description: 'This pull request is adding a LICENSE file'
title: 'chore(license): add license file'
message: 'chore(license): add license file'
branch: my-branch
git_dir: '.'
labels: |
bug
priority: p1
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.
string
Required. The GitHub access token for the user making the suggested changes. We recommend storing it as a secret in your GitHub repository.
string
Required. The repository to create the fork off of.
string
Required. The owner of the upstream repository.
number
Required. The GitHub Pull Request number.
string
Required. The path of a git directory. Relative to $GITHUB_WORKSPACE
.
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.
on:
pull_request_target:
types: [opened, synchronize]
branches:
- main
name: ci
jobs:
review-pr:
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@v2 # 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.
Our client libraries follow the Node.js release schedule. Libraries are compatible with all current active and maintenance versions of Node.js.
Client libraries targeting some end-of-life versions of Node.js are available, and
can be installed via npm dist-tags.
The dist-tags follow the naming convention legacy-(version)
.
Legacy Node.js versions are supported as a best effort:
- Legacy versions will not be tested in continuous integration.
- Some security patches may not be able to be backported.
- Dependencies will not be kept up-to-date, and features will not be backported.
legacy-8
: install client libraries from this dist-tag for versions compatible with Node.js 8.
This library follows Semantic Versioning.
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
Contributions welcome! See the Contributing Guide.
Please note that this README.md
, the samples/README.md
,
and a variety of configuration files in this repository (including .nycrc
and tsconfig.json
)
are generated from a central template. To edit one of these files, make an edit
to its template in this
directory.
Apache Version 2.0
See LICENSE