Code Customization with Gemini Code Assist Enterprise

1. Before you begin

This codelab shows how to how to prepare Gemini Code Assist to provide code customization suggestions that are tailored to your private repositories. This can make the results from Code Assist significantly more useful, especially for teams that have specific conventions for similar work that's often repeated. Rest assured that allowing Gemini to index your private codebases for this feature does not enable Gemini to broadly train on your private code.

We will also discuss how to exclude sensitive or irrelevant files from consideration for code customization with an .aiexclude file.

Prerequisites

  • A basic understanding of Gemini Code Assist, and access to a project where it is enabled
  • Familiarity with a supported coding language for code customization
  • Ability to create resources in us-central1 or europe-west1, as code customization requires Developer Connect connections in those location.
  • An up to date, authenticated Google Cloud CLI

What you learn

  • How to make use of code customization in Gemini Code Assist Enterprise
  • One of many use cases where code customization can save your teams time

What you need

  • A Google Cloud project Gemini Code Assist enabled
  • A private repository to index for customization requests
  • Time to index the code for customization requests. This can take up to 24 hours
  • An IDE with Gemini Code Assist installed

2. Context

To try out code customization two things are necessary:

  1. Access to a Google Cloud project with Gemini enabled
  2. and A private repository to inform Gemini's responses.

Enabling Gemini

The best candidate repositories for Gemini to index would contain code that is often re-used throughout your organization. The sample repository provided for this codelab contains a standard Spring Boot web service, with a folder of Data Transfer Objects that meet this criteria, since a similar class would created to describe each entity in a database as it's being transported to the presentation layer.

3. (Optional) Repository setup

If you would rather use an example repository than one of your own private ones, the following steps can be followed on the terminal editor of your choice or the Cloud Shell. Start by creating a private repository to help us see code customization in action:

mkdir customization-starter
cd customization-starter
curl https://start.spring.io/starter.zip -d dependencies=web,lombok \
           -d javaVersion=21 \
           -d type=maven-project \
           -d bootVersion=3.3.4 -o cc-starter.zip
unzip cc-starter.zip
rm cc-starter.zip
pushd src/main/java/com/example/demo
mkdir dtos
touch dtos/LedgerDTO.java

Add the following into the LedgerDTO file:

package com.example.demo.dtos;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

@Getter
@Setter
@Accessors(chain = true)
public class LedgerEntryDto {
    private Long id;
    private String accountName;
    private double amount;
    private String transactionType; 
    private String description;
    private java.util.Date transactionDate;
}

This will be sufficient to demonstrate that the accessors other contributors to this codebase would expect to use will be generated accurately with code customization enabled.

Create a new repository, taking care to leave it private. Follow the steps below to push the starter to your new repository:

popd
gh auth login
git init
git add .
git commit -m "code customization starter"
git remote add origin git@github.com:<YOUR_GITHUB_ID>/customization-starter.git
git branch -M main
git push -u origin main

4. Excluding files

Before granting Gemini access to your private repositories, it's important to understand how to exclude irrelevant or sensitive files that your team does not want to be indexed. An .aiexclude file is used to achieve this, which is similar to a .gitignore with some key differences:

  • An empty .aiexclude file blocks all files in its directory and all sub-directories.
    • This is the same as a file that contains **/*.
  • .aiexclude files don't support negation (prefixing patterns with !).

With this in mind, try to consider file types or directories your team would like to exclude from consideration, and list them each on separate lines:

#Block all files with .key extensions
*.key

#Block all files under sensitive/dir
my/sensitive/dir/

#Block all .key files under sensitive/dir
my/sensitive/dir/ /.key

5. Configure Developer Connect

Developer Connect is the service that facilitates Connections and Links to your private code repositories in GitHub or GitLab. This is the mechanism that allows Gemini Code Assist to securely connect to your private repositories, to create the index that will be used to improve responses.

These two concepts are helpful in understanding how Developer Connect securely provides access to your code:

Connection

  • Represents the bridge between Google and the third-party source code management platform.

Link

  • Represents the association with an individual source code repository that you select within a connected source code management platform.

With those concepts in mind, start by navigating to the Developer Connect page to enable the API for your project.

Enabling Developer Connect

Next, follow the appropriate steps to create a Connection and Link for GitHub, or GitLab. Both resources will be created during the wizard process.

Creating Connection and link

Repeat these steps to create a Link for each repository that you want Gemini's code customization feature to consider in its responses. You can reuse the existing connection if multiple repositories come from the same platform.

6. Create and connect to index

In order to parse and analyze your repositories quickly, code customization relies on an Index.

To create an index, run the following command:

gcloud gemini code-repository-indexes create <INDEX_NAME> \
    --project=<YOUR_PROJECT_ID> \
    --location=<REGION>

If you encounter any Invalid choice: ... errors, ensure that your Google Cloud CLI is up to date by running the following command:

gcloud components update

Depending on the number of repositories you want to index and their size, indexing content can take up to 24 hours. Indexing occurs once every 24 hours, picking up any changes that were made in the repository. You can refer to these steps to check on the status of your index generation:

Searching indexing status

Next, allow access to your index by creating a repository group:

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"}]'

Replacing the following values:

  • REPOSITORY_GROUP: name of the repository group, such as default.
  • PROJECT_ID: your Google Cloud project ID.
  • INDEX_NAME: name of the index you created in a preceding step to create an index.
  • REGION: a supported region, either us-central1 or europe-west1, that you have configured in Developer Connect in your Google Cloud project.
  • INDEX_CONNECTION: connection of the index you created in a preceding step to create an index.
  • BRANCH_NAMES: name of the branches you want to index, such as main|dev.
  • REPOSITORY: the repository you want to index. You must specify at least one repository, and you can specify multiple repositories as needed.

And finally, grant the desired principal access to the group:

gcloud projects add-iam-policy-binding PROJECT_ID \
    --member='PRINCIPAL' \
    --role='roles/cloudaicompanion.repositoryGroupsUser'

7. Generate customized code

Now that Gemini has access to the code in your private repository, we can expect to see relevant snippets appear in our code completion requests. In our example repository, we can navigate to our DTO folder, and as we create a new class to represent a new object, you can see the expected annotations appearing as we type:

Generated sample

8. Conclusion

Congratulations on completing this Codelab! You have learned how to use the Code Customization feature of Gemini Code Assist. Now that your responses can be tailored to your teams specific, private codebases, each prompt and code completion will be more valuable to developers on your teams.

Feel free to review these other documents and materials for further reading, and support for things like setting up IAM roles: