GPT Actions 库 - GitHub

2024 年 10 月 23 日
在 Github 中打开

简介

本页面为开发者提供将 GPT Action 连接到 GitHub 的说明。在继续之前,请先熟悉以下资源

此 GPT Action 帮助开发者评估 GitHub Pull Request diff 的质量和安全性。它为每个领域提供反馈和建议,允许开发者在自动将其作为评论提交到 Pull Request 之前修改或接受反馈。

价值 & 示例业务用例

价值:

用户可以利用 ChatGPT 的自然语言能力来辅助 GitHub Pull Request 审查。

  • 对于开发者:分析代码更改并执行高质量的审查,即时获得关于建议修改的反馈。
  • 对于组织:确保 diff 符合最佳实践和编码标准,或自动提出重构的替代方案(可能需要额外的 API 请求来定义最佳实践)。
  • 总体而言:通过这款 AI 驱动的代码审查助手,提高生产力并确保更高质量、更安全的代码。

示例用例:

  • 审查者寻求关于提议的代码更改的质量和安全性的反馈。
  • 组织鼓励在代码审查期间自动遵守最佳实践和标准。

演示视频

Watch the video

应用信息

开始之前,请浏览这些资源

先决条件

确保你有一个包含开放 pull request 的仓库。

应用设置

选择一个 Pull Request

  1. 导航到一个仓库,例如,示例 PR
    • 记下所有者(例如,“microsoft”)、仓库名称(例如,“vscode”)和 PR 编号(例如,“229241”)。
    • 如果仓库所有者是 SSO 组织,你的令牌可能需要批准
  2. 查看如何执行高质量的代码审查

生成“精细粒度” GitHub 个人访问令牌

  1. 登录到 GitHub 并转到设置
  2. 导航到开发者设置 > 精细粒度个人访问令牌
  3. 点击生成新令牌,命名它,设置过期日期,并选择必要的范围(例如,read:contentread&write:pull_requests)。
  4. 复制并安全地存储令牌。

ChatGPT 步骤

自定义 GPT 指令

创建自定义 GPT 后,将以下内容复制到“指令”面板中

# **Context:** You support software developers by providing detailed information about their pull request diff content from repositories hosted on GitHub. You help them understand the quality, security and completeness implications of the pull request by providing concise feedback about the code changes based on known best practices. The developer may elect to post the feedback (possibly with their modifications) back to the Pull Request. Assume the developer is familiar with software development.

# **Instructions:**

## Scenarios
### - When the user asks for information about a specific pull request, follow this 5 step process:
1. If you don't already have it, ask the user to specify the pull request owner, repository and pull request number they want assistance with and the particular area of focus (e.g., code performance, security vulnerabilities, and best practices).
2. Retrieve the Pull Request information from GitHub using the getPullRequestDiff API call, owner, repository and the pull request number provided. 
3. Provide a summary of the pull request diff in four sentences or less then make improvement suggestions where applicable for the particular areas of focus (e.g., code performance, security vulnerabilities, and best practices).
4. Ask the user if they would like to post the feedback as a comment or modify it before posting. If the user modifies the feedback, incorporate that feedback and repeat this step. 
5. If the user confirms they would like the feedback posted as a comment back to the Pull request, use the postPullRequestComment API to comment the feedback on the pull request.

OpenAPI 模式

创建自定义 GPT 后,将以下文本复制到“Actions”面板中。有问题?请查看入门示例,了解此步骤的详细工作原理。

下面是连接到 GitHub 以 GET Pull Request Diff 并 POST Feedback 到 Pull Request 的示例。

openapi: 3.1.0
info:
  title: GitHub Pull Request API
  description: Retrieve the diff of a pull request and post comments back to it.
  version: 1.0.0
servers:
  - url: https://api.github.com
    description: GitHub API
paths:
  /repos/{owner}/{repo}/pulls/{pull_number}:
    get:
      operationId: getPullRequestDiff
      summary: Get the diff of a pull request.
      parameters:
        - name: owner
          in: path
          required: true
          schema:
            type: string
          description: Owner of the repository.
        - name: repo
          in: path
          required: true
          schema:
            type: string
          description: Name of the repository.
        - name: pull_number
          in: path
          required: true
          schema:
            type: integer
          description: The number of the pull request.
        - name: Accept
          in: header
          required: true
          schema:
            type: string
            enum:
              - application/vnd.github.v3.diff
          description: Media type for the diff format.
      responses:
        "200":
          description: Successfully retrieved the pull request diff.
          content:
            text/plain:
              schema:
                type: string
        "404":
          description: Pull request not found.
  /repos/{owner}/{repo}/issues/{issue_number}/comments:
    post:
      operationId: postPullRequestComment
      summary: Post a comment to the pull request.
      parameters:
        - name: owner
          in: path
          required: true
          schema:
            type: string
          description: Owner of the repository.
        - name: repo
          in: path
          required: true
          schema:
            type: string
          description: Name of the repository.
        - name: issue_number
          in: path
          required: true
          schema:
            type: integer
          description: The issue or pull request number.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                body:
                  type: string
                  description: The content of the comment.
      responses:
        "201":
          description: Successfully created a comment.
          content:
            application/json:
              schema:
                type: object
                properties:
                  id:
                    type: integer
                  body:
                    type: string
                  user:
                    type: object
                    properties:
                      login:
                        type: string
                      id:
                        type: integer
        "404":
          description: Pull request not found.

身份验证说明

以下是关于设置与此第三方应用程序进行身份验证的说明。有问题?请查看入门示例,了解此步骤的详细工作原理。

在 ChatGPT 中(参考入门示例中的步骤 2)

在 ChatGPT 中,点击“Authentication”并选择 “Bearer”。输入以下信息。确保你的令牌具有上面“应用设置”中描述的权限。

  • 身份验证类型:API 密钥
  • 身份验证类型:Bearer
  • API 密钥 <personal_access_token>

测试 GPT

你现在可以测试 GPT 了。你可以输入一个简单的提示,例如“你能否审查我的 pull request?owner: <org_name>, repo: <repo_name>, pull request number: <PR_Number>”,并期望看到以下内容

landing_page.png

  1. 引用的 pull request (PR) 中的更改摘要。

First Interaction

  1. 质量和安全反馈以及纳入 PR 下一次迭代的建议。

First Feedback

  1. 一个迭代反馈或接受反馈的选项,并让 GPT 将其作为你的评论直接发布到 PR。

First Interaction

是否有您希望我们优先考虑的集成?我们的集成中是否存在错误?在我们的 github 中提交 PR 或 issue,我们将查看。