本文档介绍了如何通过将 Gemini Code Assist 连接到您的私有代码库来设置 Gemini Code Assist 代码自定义。借助此功能,您可以接收代码建议,这些建议会参考贵组织的内部库、专用 API 和编码样式。
准备工作
- 请与您的销售代表联系,以获取 Gemini Code Assist 代码自定义功能的访问权限。
- 请确保您拥有 Google Cloud 管理员账号 对项目进行更改
- 设置 Gemini Code Assist。
- 创建或配置最终用户账号。贵组织中的每位开发者 使用 Gemini Code Assist 的 有权访问您的 Google Cloud 项目的 Google Cloud。 如需了解详情,请参阅 在 Google Cloud 控制台中授予角色。 确保每个用户都具有以下角色:
配置 Developer Connect,然后连接到您的 GitHub.com 或 GitLab.com 代码库:
此外,请注意,代码自定义仅支持
us-central1
和europe-west1
位置(区域)中的 Developer Connect 关联。在 shell 环境中,运行
gcloud components update
命令,确保您已将 Google Cloud CLI 的所有已安装组件更新到最新版本。
选择要关联的私有代码库
最佳实践是,确保您关联的代码库:
- 让代码在样式或结构上与您期望的 让开发者轻松编写应用
- 拥有要从现有服务调用的私有库或 API 代码库。
(可选)选择不编入索引的文件
默认情况下,代码自定义会将所有 支持的代码文件 存储在指定的代码库中在许多情况下,您可能有特定的文件或子树不希望编入索引。例如:
- 在贵组织内仅限特定人员访问的高度敏感信息
- 旧代码或已废弃的代码
- 自动生成的代码或临时代码
为了防止公开您不想编入索引的代码,您可以使用分支模式来控制对索引的访问权限,并使用稳定的分支(例如 main
)。
或者,您也可以通过创建 .aiexclude
文件将文件从索引中排除。
创建索引
代码自定义功能依赖于索引来分析和解析您的代码库,以便更快地生成代码建议和进行查询。
-
In the Google Cloud console, activate Cloud Shell.
如需创建索引,请使用
gemini code-repository-indexes create
命令:gcloud gemini code-repository-indexes create INDEX_NAME \ --project=PROJECT_ID \ --location=REGION
替换以下内容:
INDEX_NAME
:您的索引名称。PROJECT_ID
:您的 Google Cloud 项目 ID。REGION
:您在 Google Cloud 项目的 Developer Connect 中配置的受支持区域(us-central1
或europe-west1
)。
索引创建通常需要 30 分钟才能完成,但最长可能需要 1 小时。
Google 限制每个项目和组织的代码库索引数量为 1。如果您需要更多代码库索引,可以 申请更高配额。
使用代码库组控制对索引的访问权限
-
In the Google Cloud console, activate Cloud Shell.
如需创建代码库组,请使用
gemini code-repository-indexes repository-groups create
命令:gcloud gemini code-repository-indexes repository-groups create REPOSITORY_GROUP \ --project=PROJECT_ID \ --location=REGION \ --code-repository-index=INDEX_NAME \ --repositories='[{"resource": "projects/PROJECT_ID/locations/REGION/connections/INDEX_CONNECTION/gitRepositoryLinks/REPOSITORY", "branch_pattern": "BRANCH_NAMES"}]'
替换以下内容:
REPOSITORY_GROUP
:代码库组的名称,例如default
。PROJECT_ID
:您的 Google Cloud 项目 ID。INDEX_NAME
:您在上一步中创建索引时创建的索引的名称。REGION
:您在 Google Cloud 项目的 Developer Connect 中配置的受支持区域(us-central1
或europe-west1
)。INDEX_CONNECTION
:将您在上一步中创建的索引连接到创建索引。BRANCH_NAMES
:您要编入索引的分支的名称,例如main|dev
。REPOSITORY
:您要编入索引的代码库。您 必须至少指定一个代码库,并且可以指定多个 代码库。
您还可以使用 JSON(或 YAML)文件中定义的代码库创建代码库组,格式如下所示:
JSON
[
{
"resource": "projects/PROJECT_ID/locations/REGION/connections/INDEX_CONNECTION/gitRepositoryLinks/REPOSITORY", "branchPattern": "main|dev"
},
{
"resource": "projects/PROJECT_ID/locations/REGION/connections/INDEX_CONNECTION/gitRepositoryLinks/REPOSITORY", "branchPattern": "dev"
}
]
YAML
- resource: projects/PROJECT_ID/locations/REGION/connections/INDEX_CONNECTION/gitRepositoryLinks/REPOSITORY
branchPattern: main|dev
- resource: projects/PROJECT_ID/locations/REGION/connections/INDEX_CONNECTION/gitRepositoryLinks/REPOSITORY
branchPattern: dev
如需基于 JSON 或 YAML 文件创建代码库组,请使用
gemini code-repository-indexes repository-groups create
命令:
JSON
gcloud gemini code-repository-indexes repository-groups create REPOSITORY_GROUP \
--project=PROJECT_ID \
--location=REGION \
--code-repository-index=INDEX_NAME \
--repositories=FILEPATH.json
YAML
gcloud gemini code-repository-indexes repository-groups create REPOSITORY_GROUP \
--project=PROJECT_ID \
--location=REGION \
--code-repository-index=INDEX_NAME \
--repositories=FILEPATH.yaml
向代码库组授予项目的 IAM 角色
您只会收到由索引中的代码库提供的建议。每个仓库
属于一个或多个代码库组。要查看建议,您必须
授予 Cloud AI Companion Repository Groups User IAM 角色
(roles/cloudaicompanion.repositoryGroupsUser
) - 其中包含所需的
cloudaicompanion.repositoryGroups.user
IAM 权限 -
按以下任一方式对代码库进行分组:
- 向主账号授予访问整个索引的权限。
- 向主账号授予对索引子集的访问权限。
向主账号授予访问整个索引的权限
-
In the Google Cloud console, activate Cloud Shell.
如需为项目绑定 IAM 政策,请使用
projects add-iam-policy-binding
命令:gcloud projects add-iam-policy-binding PROJECT_ID \ --member='PRINCIPAL' \ --role='roles/cloudaicompanion.repositoryGroupsUser'
替换以下内容:
PROJECT_ID
:代码库所在项目的 ID 。PRINCIPAL
:需要访问权限的主账号的电子邮件地址,例如个人的电子邮件地址为user:test-user@gmail.com
,群组的电子邮件地址为group:admins@example.com
。
如需了解详情,请参阅
gcloud projects set-iam-policy
。当系统提示您指定条件时,输入
None
。
向主账号授予对索引子集的访问权限
您可以创建多个代码库组并分配 IAM 角色 不同的 IAM 主账号。
如需设置 IAM 政策,您必须准备 IAM 政策 JSON 或 YAML 文件,其中将包含 IAM 组和分配的角色的列表。例如:
bindings:
- members:
- group:my-group@example.com
- user:test-user@example.com
role: roles/cloudaicompanion.repositoryGroupsUser
如需了解更多详情和语法,请参阅 了解允许政策。
如需设置 IAM 政策,请使用
gemini code-repository-indexes repository-groups set-iam-policy
命令:
gcloud gemini code-repository-indexes repository-groups set-iam-policy GROUP_NAMEPOLICY_FILE \
--project=PROJECT_ID \
--location=REGION \
--code-repository-index=INDEX_NAME
替换以下内容:
GROUP_NAME
:您在上一步中创建的代码库组名称,用于使用代码库组控制对索引的访问权限。POLICY_FILE
:IAM 政策。REGION
:您在 Google Cloud 项目的 Developer Connect 中配置的受支持区域(us-central1
或europe-west1
)。INDEX_NAME
:您在 创建索引之前的步骤。如需了解详情,请参阅
gcloud gemini code-repository-indexes repository-groups set-iam-policy
。
查看索引编制状态
索引编制内容最长可能需要 24 小时,具体取决于您要编制的代码库数量及其大小。对于大型代码库,编制索引可能需要更长时间。系统每 24 小时编入索引一次,记录所有更改 创建 Deployment 清单
在 Google Cloud 控制台中,前往日志浏览器。
您可以使用日志名称过滤条件查看
indexing
日志,也可以使用以下命令搜索索引编制日志:gcloud logging read "logName="projects/PROJECT_ID/logs/indexing""
将
PROJECT_ID
替换为代码库组所在的项目 ID。例如,如需查看
indexing
日志中的错误,请运行以下命令:gcloud logging read "logName="projects/PROJECT_ID/logs/indexing" AND severity>=ERROR"
如需了解详情,请参阅 Logging 查询语言。
查看关联的索引编入状态,例如:
- 代码库索引的开头 - 例如
Indexing repository REPOSITORY_NAME. Total number of repositories: 10, succeeded: 6, failed: 0.
- 结束单个代码库索引编制 - 例如:
- 成功:
Successfully finished indexing repository REPOSITORY_NAME. Total number of repositories: 10, succeeded: 7, failed: 0.
- 失败:
Failed to index repository REPOSITORY_NAME. Error: [<error message>]. Total number of repositories: 10, succeeded: 7, failed: 1.
- 成功:
- 代码库索引编制结束,例如:
- 成功:
Finished indexing process. Repositories attempted: 10. Repositories successfully indexed: 9. Repositories unsuccessfully fetched: 0.
- 失败:
Finished indexing process. Repositories attempted: 10. Repositories successfully indexed: 9. Repositories unsuccessfully fetched: 1. Repositories that were not successfully fetched will be retried in the next run.
- 成功:
在索引状态中,
REPOSITORY_NAME
是代码库 您想要查看的内容。- 代码库索引的开头 - 例如
查看相关的索引编制错误,例如:
- 未能提取代码库。
- 未能列出代码库文件。
- 无法从索引检索到代码库信息。
- 未能从索引检索到文件。
- 内部错误。
使用代码自定义
设置代码自定义后,用户将开始看到代码补全和代码生成建议,这些建议可能基于您已编入索引的私有代码,以及来自完整代码库感知的结果。
关闭代码自定义
-
In the Google Cloud console, activate Cloud Shell.
如需列出当前索引的所有代码库组,请使用
gemini code-repository-indexes repository-groups list
命令:gcloud gemini code-repository-indexes repository-groups list --location=REGION \ --project=PROJECT_ID \ --code-repository-index=INDEX_NAME --uri
替换以下内容:
REGION
:支持的区域us-central1
或europe-west1
在 Google Cloud 项目的 Developer Connect 中配置PROJECT_ID
:您的 Google Cloud 项目 ID。INDEX_NAME
:您在 创建索引之前的步骤。
如需从当前索引中删除代码库组,请使用
gemini code-repository-indexes repository-groups delete
命令:gcloud gemini code-repository-indexes repository-groups delete REPOSITORY_GROUP \ --location=REGION \ --project=PROJECT_ID \ --code-repository-index=INDEX_NAME
对每个代码库组重复第 3 步,直到从索引中删除所有代码库组。
可选:如需删除该索引,请使用
gemini code-repository-indexes delete
命令:gcloud gemini code-repository-indexes delete INDEX_NAME \ --location=REGION \ --project=PROJECT_ID
后续步骤
- 开始使用 Gemini Code Assist:
- VS Code:使用 Gemini Code Assist 编写代码
- IntelliJ:使用 Gemini Code Assist 编写代码
- Cloud Shell Editor:使用 Gemini Code Assist 编写代码
- Cloud Workstations:使用 Gemini Code Assist 编写代码
- 详细了解 Developer Connect。
- 了解 Gemini for Google Cloud 如何以及何时使用您的数据。