GPT 操作库:GitHub

简介

本文档为开发者提供了将 GPT 操作连接到 GitHub 的说明。在继续之前,请熟悉以下资源:

此 GPT 操作可帮助开发者评估 GitHub 拉取请求(Pull Request)差异(diff)的质量和安全性。它为每个领域提供反馈和建议,允许开发者在将反馈自动提交到拉取请求作为评论之前进行修改或接受。

价值与示例业务用例

价值

用户可以利用 ChatGPT 的自然语言能力来协助进行 GitHub 拉取请求审查。

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

示例用例

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

演示视频:

观看视频

应用程序信息

关键链接

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

先决条件

确保您有一个包含已打开拉取请求的存储库。

应用程序设置

选择一个拉取请求

  1. 导航到一个存储库,例如 示例 PR。 - 注意所有者(例如,“microsoft”)、存储库名称(例如,“vscode”)和 PR 编号(例如,“229241”)。 - 如果存储库所有者是 SSO 组织,您的令牌可能需要 批准
  2. 阅读 如何执行高质量的代码审查

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

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

ChatGPT 步骤

自定义 GPT 说明

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

# **背景:** 您通过提供 GitHub 托管的存储库中拉取请求差异内容(diff content)的详细信息来支持软件开发人员。您通过提供有关代码更改的简洁反馈(基于已知最佳实践),帮助他们理解拉取请求的质量、安全性和完整性影响。开发人员可以选择将反馈(可能包含他们的修改)发布回拉取请求。假设开发人员熟悉软件开发。

# **说明:**

## 场景
### - 当用户询问特定拉取请求的信息时,请遵循以下 5 个步骤:

1. 如果您还没有,请要求用户指定他们想要协助的拉取请求的所有者、存储库和拉取请求编号,以及关注的具体领域(例如,代码性能、安全漏洞和最佳实践)。
2. 使用 getPullRequestDiff API 调用、提供的所有者、存储库和拉取请求编号从 GitHub 检索拉取请求信息。
3. 提供拉取请求差异的摘要(四句话或更少),然后针对关注的特定领域(例如,代码性能、安全漏洞和最佳实践)提出改进建议。
4. 询问用户是否希望将反馈作为评论发布,或在发布前进行修改。如果用户修改了反馈,请合并该反馈并重复此步骤。
5. 如果用户确认希望将反馈作为评论发布回拉取请求,请使用 postPullRequestComment API 将反馈作为评论发布到拉取请求。

OpenAPI 架构

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

以下是连接到 GitHub 以获取拉取请求差异(GET Pull Request Diff)和发布反馈到拉取请求(POST the Feedback to the 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 中,点击“身份验证”并选择 “Bearer”。在此处输入信息。确保您的令牌具有“应用程序设置”中所述的权限。

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

测试 GPT

您现在可以开始测试 GPT 了。您可以输入一个简单的提示,例如“你能审查我的拉取请求吗?所有者:,存储库:,拉取请求编号:”,并期望看到以下内容:

landing_page.png

  1. 对所引用拉取请求(PR)的更改摘要。

First Interaction

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

First Feedback

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

First Interaction

您希望我们优先集成哪些功能?我们的集成是否存在错误?在我们的 GitHub 中提交 PR 或 issue,我们将进行查看。