包详细信息

code-suggester

googleapis685.8kApache-2.05.0.0

Library to propose code changes

google, google cloud platform, google cloud

自述文件

Google Cloud Platform logo

Code-Suggester: Node.js Client

release level npm version

Description

Code-suggester automates the steps involved in making code changes or code suggestions to your GitHub repository! Code-suggester

  1. can be imported as a library, or
  2. used as a CLI tool, or
  3. configured in a GitHub Action

Core Library

Installation

npm i code-suggester

Example

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',
  )
}

reviewPullRequest(options)

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.

Syntax

reviewPullRequest(octokit, diffContents, config [, logger])

Parameters

octokit

octokit
Required. An authenticated octokit instance.

diffContents

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. |

options

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

Logger
The default logger is Pino. You can plug in any logger that conforms to Pino's interface

returns

returns the review number if a review was created, or null if a review was not made and an exception was not thrown.

Exceptions

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.

createPullRequest(options)

The createPullRequest() method creates a GitHub Pull request with the files given as input.

Syntax

createPullRequest(octokit, changes, config [, logger])

Parameters

octokit

octokit
Required. An authenticated octokit instance.

changes

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. |

options

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

Logger
The default logger is Pino. You can plug in any logger that conforms to Pino's interface

Exceptions

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.

parseTextFiles(options)

The parseTextFiles() method takes a Map<string, string> or Object<string, string> and outputs the changes object for text files only.

Syntax

parseTextFiles(textFiles)

Parameters

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.

CLI

Installation

npm i code-suggester -g

code-suggester pr

code-suggester pr - opens a GitHub Pull Request against the upstream primary branch with the provided set of changes.

Syntax

code-suggester pr [options] --upstream-repo=<string> --upstream-owner=<string>

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.

--description, -d

string
The GitHub Pull Request description. Default value is: 'code suggestions'.

--title, -t

string
The GitHub Pull Request title. Default value is: 'chore: code suggestions'.

--branch, -b

string
The GitHub working branch name. Default value is: 'code-suggestions'.

--primary, -p

string
The primary upstream branch to open a PR against. Default value is: 'main'.

--message, -m

string
The GitHub commit message. Default value is: 'code suggestions'.

--force, -f

boolean
Whether or not to force push a reference with different commit history before the remote reference HEAD. Default value is: false.

--git-dir

string
Required. The path of a git directory

--fork

boolean
Whether or not to attempt forking to a separate repository. Default value is: true.

--labels

array
The list of labels to add to the pull request. Default is none.

Example

code-suggester pr -o foo -r bar -d 'description' -t 'title' -m 'message' --git-dir=.

code-suggester review

code-suggester review - review an open GitHub Pull Request and suggest changes.

Syntax

code-suggester review --upstream-repo=<string> --upstream-owner=<string> --pull-number=<number> --git-dir=<string>

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

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.

Options

upstream_repo

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.

title

string
Required. The GitHub Pull Request title.

branch

string
The GitHub working branch name. Default value is: 'code-suggestions'.

primary

string
The primary upstream branch to open a PR against. Default value is: 'main'.

message

string
Required. The GitHub commit message.

force

boolean
Whether or not to force push a reference with different commit history before the remote reference HEAD. Default value is: false.

maintainers_can_modify

boolean
Whether or not maintainers can modify the pull request. Default value is: true.

git_dir

string
Required. The path of a git directory. Relative to $GITHUB_WORKSPACE.

fork

boolean
Whether or not to attempt forking to a separate repository. Default value is: true.

labels

array
The list of labels to add to the pull request. Default is none.

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.

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

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.

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.

Supported Node.js Versions

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 tags available

  • legacy-8: install client libraries from this dist-tag for versions compatible with Node.js 8.

Versioning

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

Contributing

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.

License

Apache Version 2.0

See LICENSE

更新日志

Changelog

5.0.0 (2025-02-26)

⚠ BREAKING CHANGES

  • require node 18+ (#510)

Features

Bug Fixes

4.3.4 (2024-10-25)

Bug Fixes

4.3.3 (2023-07-11)

Bug Fixes

  • deps: Update word-wrap dependency to 1.2.6 (#477) (4946260)

4.3.2 (2023-04-11)

Bug Fixes

  • Configure NullLogger as default logger (#472) (a5dd35e)

4.3.1 (2023-02-24)

Bug Fixes

  • deps: Update dependency parse-diff to ^0.11.0 (#451) (da26e41)

4.3.0 (2023-01-25)

Features

4.2.0 (2023-01-05)

Features

  • Throw new CommitError if an API error occurs during commit process (#434) (5ee7f2a)

Bug Fixes

  • deps: Update dependency parse-diff to ^0.10.0 (#430) (06638c2)

4.1.1 (2022-11-09)

Bug Fixes

4.1.0 (2022-08-24)

Features

4.0.1 (2022-08-16)

Bug Fixes

  • action: run on node16, node14 does not exist (#392) (a6296cc)

4.0.0 (2022-08-09)

⚠ BREAKING CHANGES

  • drop Node 12 support
  • action: upgrade to Node 14
  • update @octokit/rest to v19 (#385)

deps

Build System

3.0.0 (2022-05-17)

⚠ BREAKING CHANGES

  • update library to use Node 12 (#372)

Build System

2.2.0 (2022-02-28)

Features

2.1.4 (2022-02-23)

Bug Fixes

  • cli: don't crash when there are no changes (#334) (e606126), closes #229
  • try to merge the upstream repo branch on the fork (#330) (2d5c3f9), closes #335

2.1.3 (2022-02-22)

Bug Fixes

2.1.2 (2022-01-19)

Bug Fixes

  • deps: update dependency parse-diff to ^0.9.0 (#303) (25983c9)

2.1.1 (2021-09-03)

Bug Fixes

2.1.0 (2021-05-31)

Features

  • add gcf-owl-bot[bot] to ignoreAuthors (#224) (3ed8f15)

2.0.0 (2021-04-26)

⚠ BREAKING CHANGES

  • This does not actually break any interfaces that are intended to be public (exported in index.ts), but would be breaking if you are importing from deeply nested paths which is not intended.
  • move custom logger configuration into options (#212)
  • cleanup custom logging (#206)

Features

Code Refactoring

1.11.0 (2021-04-22)

Features

  • add optional draft flag to openPullRequest() (#207) (ff9516c)

1.10.0 (2021-03-30)

Features

1.9.3 (2021-03-23)

Bug Fixes

  • deps: update to the latest version of octokit (#181) (2c6c89d)

1.9.2 (2021-03-15)

Bug Fixes

  • deps: update dependency parse-diff to ^0.8.0 (#177) (edce69c)

1.9.1 (2021-03-02)

Bug Fixes

1.9.0 (2021-02-25)

Features

Bug Fixes

  • deps: update dependency @types/yargs to v16 (#168) (6cc6657)

1.8.2 (2020-12-08)

Bug Fixes

  • add package-lock.json to fix Docker build (#161) (0d5ecf1)

1.8.1 (2020-11-11)

Bug Fixes

1.8.0 (2020-10-09)

Features

  • review: allow raw diff for reviewPullRequest (#137) (841526d)

Bug Fixes

  • handle addition-only and deletion-only changes (#144) (ab936a2), closes #126 #127
  • the review comment should go on the old file lines, not the new file lines (#140) (662391f)

1.7.0 (2020-10-07)

Features

  • action: action can now review pull requests (#135) (c90a128)
  • cli: add command for reviewing a pr (#130) (ffac451)

Bug Fixes

1.6.0 (2020-09-25)

Features

1.5.0 (2020-09-10)

Features

  • cli,action: enable configuring --fork option (#109) (fd77d4a)

Bug Fixes

  • deps: update dependency yargs to v16 (#111) (8a1ec95)

1.4.0 (2020-09-08)

Features

Bug Fixes

  • listBranches fails if repo has many branches (#102) (eda2336)

1.3.0 (2020-09-04)

Features

  • logging: allow pino to be configured (#97) (d45e474)
  • types: make Changes type public (#98) (8df9310)

1.2.0 (2020-09-02)

Features

1.1.1 (2020-08-13)

Bug Fixes

  • cli,action: when process fails, make exit code 1 (#81) (c1495cb), closes #72
  • framework-core: retry mechanism for when forked git object isn't quite ready (#78) (326145f)

1.1.0 (2020-08-07)

Features

  • action: setup configuration for GitHub actions (#69) (b879d75)

Bug Fixes

  • core-library: do not create pr when a pr already exists for a ref (#66) (4c7b259), closes #17

1.0.1 (2020-08-04)

Bug Fixes

  • command and public package alias (#61) (79b540d)

1.0.0 (2020-08-04)

Features

  • basic pr opening (#33) (2be3e59)
  • commit and push changes onto a SHA to a target remote (#30) (8bf1782), closes #19
  • create a branch from origin owner, repo, branch name, and an optional primary branch (#27) (fecaaba)
  • forking with octokit given repo and owner (#20) (eb9047f)
  • cli: cli interface (#53) (836e0bc)
  • core: create a GitHub Pull Request from a fork (#45) (782bced)

Bug Fixes