本文件說明如何設定 Gemini Code Assist 程式碼自訂功能,方法是將 Gemini Code Assist 連結至私人程式碼存放區。這項功能可讓您接收程式碼建議,這些建議會參考貴機構的內部程式庫、私人 API 和程式設計樣式。
事前準備
- 使用 Enterprise 訂閱設定 Gemini Code Assist。
- 建立或設定使用者帳戶。貴組織中使用 Gemini Code Assist 的每位開發人員,都必須在 Google Cloud 中擁有使用者身分,並具備存取 Google Cloud 專案的權限。詳情請參閱「在 Google Cloud 控制台中授予角色」。確認每位使用者都具備下列角色:
設定 Developer Connect,然後連結至 GitHub.com、GitLab.com 或 Bitbucket.org 存放區:
- 連結至 GitHub。
- 連結至 GitHub Enterprise Cloud。
- 連結至 GitHub Enterprise。
- 連結至 GitLab。
- 連結至 GitLab Enterprise。
- 連結 Bitbucket Cloud。
- 連結 Bitbucket Data Center。
此外,請注意,程式碼自訂功能僅支援下列地區的開發人員連線:
us-central1
europe-west1
asia-southeast1
在 Shell 環境中執行
gcloud components update
指令,確保您已將 gcloud 的所有已安裝元件更新至最新版本。在這個步驟中,您可以安裝並初始化 gcloud,也可以使用 Cloud Shell 編輯器。gcloud components update
選擇要連結的私人存放區
最佳做法是確保連結的存放區符合下列條件:
- 程式碼的風格或結構應與您希望開發人員編寫的程式碼相似。
- 您想從目前的程式碼集呼叫私人程式庫或 API。
(選用) 選擇不建立索引的檔案
根據預設,程式碼自訂功能會為指定存放區中的所有支援的程式碼檔案建立索引。在許多情況下,您可能會有不想索引的特定檔案或子樹狀結構。例如:
- 貴機構內部存取權受限的高度機密資訊
- 舊版或已淘汰的程式碼
- 自動產生或暫時性的程式碼
如要避免公開不想編入索引的程式碼,您可以使用分支模式控制索引的存取權,並使用穩定的分支 (例如 main
)。
或者,您也可以建立 .aiexclude
檔案,從索引中排除檔案。
建立索引
程式碼自訂功能會使用索引分析及剖析存放區,以便更快速地產生程式碼建議和查詢。
如要建立索引,請在 Shell 環境中使用 gemini code-repository-indexes create
指令:
gcloud gemini code-repository-indexes create INDEX_NAME \
--project=PROJECT_ID \
--location=REGION
resource "google_gemini_code_repository_index" "example" {
location = "REGION "
code_repository_index_id = "INDEX_NAME "
}
更改下列內容:
INDEX_NAME
:索引名稱。重要事項:請記下索引名稱。您將在本文件的幾個步驟中使用這項資訊。PROJECT_ID
:您的 Google Cloud 專案 ID。REGION
:您在本頁「事前準備」一節中所述的支援區域,也就是您在 Google Cloud 專案的 Developer Connect 中所設定的區域。
索引建立作業通常需要 30 分鐘才能完成,但也可能需要一小時。
Google 規定每個專案和機構的程式碼存放區索引數量上限為 1。
使用存放區群組控管索引存取權
存放區群組是索引設定的容器,其中包含存放區和分支模式。儲存庫群組專為精細的 IAM 控管而設計,可讓開發人員存取這些群組中的索引資料,前提是他們擁有 cloudaicompanion.repositoryGroups.use
權限。
存放區群組包含來自相同專案和位置的 Developer Connect 存放區或連結。
管理員執行下列動作:
- 建立程式碼存放區索引資源。
- 在相同的專案和位置中,設定新的 Developer Connect 連線。
- 在連線中連結 Git 存放區。
取得連結的資源名稱、選擇要為每個連結建立索引的分支模式,並將其放入一個或多個存放區群組。
如要建立存放區群組,請在 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": "REPOSITORY_RESOURCE_NAME ", "branchPattern": "BRANCH_NAMES "}]'
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」欄下方的連線 ID。如要複製資源名稱,請按一下 more_vert 選單,查看更多選項,然後選取「複製資源路徑」。BRANCH_NAMES
:要建立索引的分支名稱,例如main|dev
。
您也可以使用 JSON (或 YAML) 檔案中定義的存放區建立存放區群組,格式如下:
[
{
"resource": "REPOSITORY_RESOURCE_NAME ", "branchPattern": "main|dev"
},
{
"resource": "REPOSITORY_RESOURCE_NAME ", "branchPattern": "dev"
}
]
- resource: REPOSITORY_RESOURCE_NAME
branchPattern: main|dev
- resource: REPOSITORY_RESOURCE_NAME
branchPattern: dev
如要根據 JSON 或 YAML 檔案建立存放區群組,請在殼層環境中使用 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=FILEPATH .json
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 主體。
如要設定 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_NAME POLICY_FILE \
--project=PROJECT_ID \
--location=REGION \
--code-repository-index=INDEX_NAME
更改下列內容:
GROUP_NAME
:您在先前步驟中建立的存放區群組名稱,用於透過存放區群組控制索引存取權。POLICY_FILE
:IAM 政策。REGION
:如本頁「事前準備」一節所述,您在 Google Cloud 專案的 Developer Connect 中設定的支援區域。INDEX_NAME
:您在先前步驟中建立索引時建立的索引名稱。詳情請參閱
gcloud gemini code-repository-indexes repository-groups set-iam-policy
。
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 查詢語言」。前往 Google Cloud 控制台中的「Logs Explorer」。
使用記錄名稱篩選器查看
indexing
記錄。
如要搜尋索引記錄,請在 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 版 Google Cloud 如何使用您的資料,以及使用時機。