本文說明如何將 Gemini Code Assist 連線至私人程式碼存放區,設定 Gemini Code Assist 程式碼自訂功能。這項功能會參考貴機構的內部程式庫、私人 API 和程式設計樣式,提供程式碼建議。
事前準備
- 透過企業版訂閱方案設定 Gemini Code Assist。
- 建立或設定使用者帳戶。貴機構中每位使用 Gemini Code Assist 的開發人員,都必須在Google Cloud 具有存取專案權限的 Google Cloud 帳戶中擁有使用者身分。詳情請參閱「在 Google Cloud 控制台中授予角色」。確認每位使用者都具備下列角色:
設定 Developer Connect,然後連結至 GitHub.com、GitLab.com 或 Bitbucket.org 存放區:
GitHub
GitLab
Bitbucket
此外,請注意,程式碼自訂功能僅支援下列位置 (區域) 的 Developer Connect 連線:
us-central1
europe-west1
asia-southeast1
在 Shell 環境中執行
gcloud components update
指令,確保已將 gcloud CLI 的所有已安裝元件更新至最新版本。在這個步驟中,您可以安裝及初始化 gcloud CLI,也可以使用 Cloud Shell 編輯器。gcloud components update
選擇要連結的私人存放區
最佳做法是確保連結的存放區符合下列條件:
- 程式碼的樣式或結構與您希望開發人員編寫的程式碼類似。
- 擁有要從目前程式碼集呼叫的私有程式庫或 API。
(選用) 選擇不要建立索引的檔案
根據預設,程式碼自訂功能會為指定存放區中的所有支援程式碼的檔案建立索引。
如要避免不想編入索引的程式碼曝光,可以使用分支模式控管索引存取權,並使用穩定分支 (例如 main
)。
或者,您也可以建立 .aiexclude
檔案,將檔案從索引中排除。
建立索引
為了更快生成程式碼建議及執行查詢,這項功能須使用索引來分析及剖析存放區。
gcloud CLI
如要建立索引,請在 Shell 環境中使用 gemini code-repository-indexes create
指令:
gcloud gemini code-repository-indexes create INDEX_NAME \
--project=PROJECT_ID \
--location=REGION
Terraform
resource "google_gemini_code_repository_index" "example" {
location = "REGION"
code_repository_index_id = "INDEX_NAME"
}
更改下列內容:
INDEX_NAME
:索引名稱。重要事項:請記下索引名稱。您會在本文的幾個步驟中用到這項資訊。PROJECT_ID
:您的 Google Cloud 專案 ID。REGION
:支援的區域,如本頁「開始前」一節所述,您已在專案的 Developer Connect 中設定該區域。 Google Cloud
建立索引通常需要 30 分鐘,但最多可能需要一小時。
Google 限制每個專案和機構只能有一個程式碼存放區索引。
使用存放區群組控管索引存取權
存放區群組是索引設定的容器,包括存放區及其分支模式。存放區群組專為精細的 IAM 控制而設計,可讓開發人員存取這些群組的索引資料,前提是他們擁有 cloudaicompanion.repositoryGroups.use
權限。
存放區群組包含來自相同專案和位置的 Developer Connect 存放區或連結。
管理員會執行下列動作:
- 建立程式碼存放區索引資源。
- 在相同專案和位置中,設定新的 Developer Connect 連線。
- 連結連線中的 Git 存放區。
取得連結的資源名稱,為每個連結挑選要建立索引的分支模式,然後將其放入一或多個存放區群組。
gcloud CLI
如要建立存放區群組,請在殼層環境中使用 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": "REPOSITORY_RESOURCE_NAME", "branchPattern": "BRANCH_NAMES"}]'
Terraform
resource "google_gemini_repository_group" "example" {
location = "REGION"
code_repository_index = "INDEX_NAME"
repository_group_id = "REPOSITORY_GROUP"
repositories {
resource = "REPOSITORY_RESOURCE_NAME"
branch_pattern = "BRANCH_NAMES"
}
}
更改下列內容:
REPOSITORY_GROUP
:存放區群組名稱,例如default
。PROJECT_ID
:您的 Google Cloud 專案 ID。REGION
:支援的區域,如本頁「開始前」一節所述,您已在 Google Cloud專案的 Developer Connect 中設定。INDEX_NAME
:您在先前步驟中建立的索引名稱,用於建立索引。REPOSITORY_RESOURCE_NAME
:Developer Connect 連線中的存放區名稱。如要找出存放區名稱,請前往 Google Cloud 控制台的「Developer Connect」頁面,然後在「Repositories」分頁中,查看表格「Connection」欄下方的「Connection ID」。如要複製資源名稱,請按一下「更多選項」選單 more_vert,然後選取「複製資源路徑」。BRANCH_NAMES
:要建立索引的分支名稱,例如main|dev
。
您也可以使用 JSON (或 YAML) 檔案中定義的存放區建立存放區群組,格式如下:
JSON
[
{
"resource": "REPOSITORY_RESOURCE_NAME", "branchPattern": "main|dev"
},
{
"resource": "REPOSITORY_RESOURCE_NAME", "branchPattern": "dev"
}
]
YAML
- resource: REPOSITORY_RESOURCE_NAME
branchPattern: main|dev
- resource: REPOSITORY_RESOURCE_NAME
branchPattern: dev
如要根據 JSON 或 YAML 檔案建立存放區群組,請在 Shell 環境中使用 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
如要加密及控管資料,您也可以透過 Cloud Key Management Service,使用客戶管理的加密金鑰 (CMEK)。如要進一步瞭解如何使用 CMEK,請參閱「使用客戶自行管理的加密金鑰加密資料」。
在專案中將 IAM 角色授予存放區群組
您只會收到索引中存放區的建議。每個存放區都屬於一或多個存放區群組。如要存取建議,請透過下列任一方式,將「Cloud AI Companion 存放區群組使用者」IAM 角色 (roles/cloudaicompanion.repositoryGroupsUser
) 授予存放區群組,該角色包含必要的 cloudaicompanion.repositoryGroups.user
IAM 權限:
- 授予主體存取整個索引的權限。
- 授予主體存取索引子集的權限。
授予主體存取整個索引的權限
如要繫結專案的 IAM 政策,請在 Shell 環境中使用
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
。
系統提示指定條件時,請輸入
None
。
授予主體索引子集的存取權
您可以建立多個存放區群組,並將 IAM 角色指派給不同的 IAM 主體。
gcloud CLI
如要設定 IAM 政策,請準備 IAM 政策 JSON 或 YAML 檔案,其中包含 IAM 群組和指派角色的清單。例如:
bindings:
- members:
- group:my-group@example.com
- user:test-user@example.com
role: roles/cloudaicompanion.repositoryGroupsUser
如需更多詳細資料和語法,請參閱「瞭解允許政策」。
如要設定 IAM 政策,請在 Shell 環境中使用 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
:身分與存取權管理政策。REGION
:支援的區域,如本頁「開始前」一節所述,您已在Google Cloud 專案的 Developer Connect 中設定該區域。INDEX_NAME
:您在上一個步驟中建立的索引名稱,用於建立索引。詳情請參閱
gcloud gemini code-repository-indexes repository-groups set-iam-policy
。
Terraform
data "google_iam_policy" "foo" {
binding {
role = "roles/cloudaicompanion.repositoryGroupsUser"
members = ["test-user@example.com"]
}
}
resource "google_gemini_repository_group_iam_policy" "foo" {
project = "PROJECT_ID"
location = "REGION"
code_repository_index_id = "INDEX_NAME"
repository_group_id = "GROUP_NAME"
policy_data = data.google_iam_policy.foo.policy_data
}
data "google_gemini_repository_group_iam_policy" "foo" {
project = "PROJECT_ID"
location = "REGION"
code_repository_index_id = "INDEX_NAME"
repository_group_id = "GROUP_NAME"
depends_on = [
google_gemini_repository_group_iam_policy.foo
]
}
您也可以建立繫結:
resource "google_gemini_repository_group_iam_binding" "foo" {
project = "PROJECT_ID"
location = "REGION"
code_repository_index_id = "INDEX_NAME"
repository_group_id = "GROUP_NAME"
role = "roles/cloudaicompanion.repositoryGroupsUser"
members = ["test-user@example.com"]
}
更改下列內容:
GROUP_NAME
:您在先前步驟中建立的存放區群組名稱,用於透過存放區群組控管索引存取權。REGION
:支援的區域,如本頁「開始前」一節所述,您已在Google Cloud 專案的 Developer Connect 中設定該區域。INDEX_NAME
:您在上一個步驟中建立的索引名稱,用於建立索引。詳情請參閱
gcloud gemini code-repository-indexes repository-groups set-iam-policy
。
檢查索引狀態
視要建立索引的存放區數量和大小而定,建立內容索引最多可能需要 24 小時。如果存放區很大,建立索引的時間可能會比較長。系統每 24 小時會建立一次索引,擷取存放區中的所有變更。
搜尋
indexing
記錄。詳情請參閱「Logging 查詢語言」。主控台
gcloud CLI
如要搜尋索引記錄,請在 Shell 環境中使用
logging read
指令: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"
查看相關的索引狀態,例如:
- 存放區索引編製作業的開始時間,例如
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
是您要查看的存放區。- 存放區索引編製作業的開始時間,例如
查看相關的索引錯誤,例如:
- 無法擷取存放區。
- 無法列出存放區檔案。
- 無法從索引擷取存放區資訊。
- 無法從索引擷取檔案。
- 內部錯誤。
使用程式碼自訂功能
設定程式碼自訂功能後,您會開始看到程式碼自動完成和程式碼生成建議,這些建議可能以您編列索引的私人程式碼為依據,以及來自完整程式碼集感知功能的結果。
如要進一步瞭解如何使用程式碼自訂功能和最佳做法,請參閱「使用程式碼自訂功能」。
關閉代碼自訂功能
如要在 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
更改下列內容:
如要從目前的索引刪除存放區群組,請使用
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,直到從索引中刪除所有存放區群組為止。
選用:如要刪除索引,請在 Shell 環境中使用
gemini code-repository-indexes delete
指令:gcloud gemini code-repository-indexes delete INDEX_NAME \ --location=REGION \ --project=PROJECT_ID
後續步驟
- 開始使用 Gemini Code Assist:
- VS Code、IntelliJ 和其他支援的 JetBrains IDE:使用 Gemini Code Assist 輔助功能編寫程式碼
- Cloud Shell 編輯器:透過 Gemini Code Assist 編寫程式碼
- Cloud Workstations:透過 Gemini Code Assist 編寫程式碼
- 瞭解如何自訂程式碼和最佳做法。
- 瞭解如何使用客戶自行管理的加密金鑰 (CMEK) 加密資料。
- 進一步瞭解 Developer Connect。
- 瞭解 Gemini for Google Cloud 如何使用您的資料。