What Is GitHub Copilot?
Developed in collaboration with OpenAI and Microsoft, GitHub Copilot uses an AI model trained on millions of public GitHub repositories to offer real-time code suggestions as you type. It also generates code based on natural language prompts in a chat window or directly in the code editor.
In addition to helping you create new code, when used in VS Code, Copilot can explain and fix existing code, as well as summarize your changes and write commit messages for you.
Copilot is like an automated pair programmer that synthesizes the collective knowledge of the GitHub community and brings it to every individual developer.
At the same time, Copilot takes your existing code into account by analyzing the surrounding context in all opened code editor tabs.
Basic Copilot use cases include writing boilerplate code, scaffolding new projects, and generating unit tests.
Copilot can help with any languages that appear in public repositories, but it works best with languages that are most popular on GitHub, such as JavaScript, TypeScript, Python, Java, C#, C/C++, PHP, Shell, and Ruby.
There are several ways to launch Copilot inside your code editor (IDE):
- Simply start writing code and wait for code-completion suggestions to appear.
- Write a code comment describing what you want to do in a natural language.
- Enter a prompt in a chat window or a dedicated pop-up provided by the IDE extension that you’re using.
Why Do You Need to Ensure the Quality of AI-Generated Code?
Can generative AI ever be wrong? Aren’t GitHub Copilot and other AI tools supposed to write perfect code for you?
Unfortunately, no.
While generative AI tools can suggest how to write code to solve a certain task, they don’t make sure that it’s free of errors and vulnerabilities.
After all, they’re trained on existing code written by humans, and humans tend to make mistakes.
Ultimately, it’s your responsibility to double-check any code that AI generates for you.
When dealing with AI-generated code, you should apply the same critical review process that you would when you copy and paste code snippets from StackOverflow, review your own code, or cross-review your teammates.
AI-generated code may introduce bugs and vulnerabilities that were only recently discovered, contain semantic errors, use outdated language syntax, or follow formatting conventions that aren’t a good fit for your codebase.
Another factor at play with AI tools like GitHub Copilot is that they’re probabilistic, not deterministic.
They’re going to be right most of the time, but not all the time.
If you use the same prompt several times, you’re also going to get different results.
To ensure the best possible experience with generative AI tools like GitHub Copilot, you need thorough testing and code quality and vulnerability scanning tools.
Installing GitHub Copilot in VS Code
Let’s see what using GitHub Copilot is like in action.
This article shows GitHub Copilot in Visual Studio Code (VS Code), but you can use it with other IDEs, including most JetBrains IDEs and Microsoft Visual Studio.
If you already have a GitHub Copilot account and the Copilot extension installed in VS Code, feel free to skip this section.
Otherwise, first and foremost, make sure you have VS Code installed.
Once you’re inside VS Code, open the Extensions pane, search for the GitHub Copilot extension, and install it:
![](https://assets-eu-01.kc-usercontent.com:443/02cb980b-5a8a-016f-80ee-dce623e76463/9cd35d7d-e697-4a37-9d08-51e6607507dd/github-copilot-extension.png?w=1248&h=829&auto=format&fit=crop)
After installing the extension, you’ll be prompted to log in to GitHub from VS Code.
Click Sign in to GitHub, then allow the Copilot extension to sign in. Once GitHub opens in the browser, click Authorize Visual-Studio-Code.
![](https://assets-eu-01.kc-usercontent.com:443/02cb980b-5a8a-016f-80ee-dce623e76463/f85bef5b-8c49-4ba9-8198-fc1dcc6b0ea5/authorize-github-vs-code.png?w=731&h=689&auto=format&fit=crop)
GitHub Copilot isn’t available to GitHub users by default—you need to explicitly sign up for it.
Unless you’re an existing Copilot user, when you return to VS Code, you’ll see a pop-up notification that says No access to GitHub Copilot found:
![](https://assets-eu-01.kc-usercontent.com:443/02cb980b-5a8a-016f-80ee-dce623e76463/d2db4dbd-acaf-46de-849e-41adbccf8443/sign-up-for-github-copilot.png?w=535&h=202&auto=format&fit=crop)
Click Signup for GitHub Copilot.
In the GitHub Copilot registration page that opens in the browser, click Get access to GitHub Pilot to start a thirty-day Copilot trial.
![](https://assets-eu-01.kc-usercontent.com:443/02cb980b-5a8a-016f-80ee-dce623e76463/7f1d9955-a4c1-4456-bbbb-90ed2181481a/github-copilot-30-day-free-trial.png?w=602&h=877&auto=format&fit=crop)
You’ll be asked to enter or confirm your payment details on GitHub.
When you’re done with this part, GitHub will let you set your basic preferences.
Set Suggestions matching public code to Allowed, then click Save and complete setup.
![](https://assets-eu-01.kc-usercontent.com:443/02cb980b-5a8a-016f-80ee-dce623e76463/9ec71916-706f-4aa5-872f-3c846774b8c5/github-copilot-individual.png?w=835&h=708&auto=format&fit=crop)
Finally, restart VS Code, and you should be all set to use GitHub Copilot.
Analyzing AI-Generated Code Quality in Real Time with SonarLint
To monitor the quality of your code as you type or as Copilot generates it, you can install SonarLint for VS Code.
SonarLint is a free advanced linter extension that helps maintain clean code, enabling you to prevent bugs and code smells even before you commit your changes.
To install SonarLint, open Visual Studio Code’s Extensions pane, search for SonarLint, and install the SonarLint extension by SonarSource when it shows up in search results.
![](https://assets-eu-01.kc-usercontent.com:443/02cb980b-5a8a-016f-80ee-dce623e76463/8ff607b3-578e-4055-b94f-cdc15fd3f998/sonarlint-for-vs-code-extension.png?w=1415&h=793&auto=format&fit=crop)
Let’s see how SonarLint can help when you generate code using Copilot. If you want to follow along, here is a repo with the starter code.
Let’s say you’re editing an ASP.NET MVC web application written in C# that is a restaurant directory. Initially, you have a controller, HomeController.cs, that looks like this:
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OdeToFood.Models;
using OdeToFood.Services;
using OdeToFood.ViewModels;
namespace OdeToFood.Controllers
{
[Authorize]
public class HomeController : Controller
{
IRestaurantData _restaurantData;
public HomeController(IRestaurantData restaurantData)
{
_restaurantData = restaurantData;
}
[AllowAnonymous]
public IActionResult Index()
{
var model = new HomeIndexViewModel();
model.Restaurants = _restaurantData.GetAll();
return View(model);
}
[AllowAnonymous]
public IActionResult Details(int id)
{
var model = _restaurantData.Get(id);
if (model == null)
{
return RedirectToAction(nameof(Index));
}
return View(model);
}
[HttpGet]
public IActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(RestaurantEditModel model)
{
if (ModelState.IsValid)
{
var newRestaurant = new Restaurant();
newRestaurant.Name = model.Name;
newRestaurant.Cuisine = model.Cuisine;
newRestaurant = _restaurantData.Add(newRestaurant);
return RedirectToAction("Details", new {id = newRestaurant.Id});
}
return View();
}
}
}
The controller has existing actions for listing all restaurants (Index
) and showing detailed information about an existing restaurant (Details
), as well as a pair of actions used to create a restaurant (Create
).
Let’s use Copilot to generate code to edit an existing restaurant.
One way to trigger Copilot to suggest code is to write a comment describing your prompt, press Enter to start a new line, and wait for Copilot to start suggesting.
If you give Copilot a prompt that says “Create an action to edit a restaurant”, here’s what it may come up with:
![](https://assets-eu-01.kc-usercontent.com:443/02cb980b-5a8a-016f-80ee-dce623e76463/88f8d767-e13d-45da-81d7-06ae4f120d86/github-copilot-restaurant-code-example.png?w=1242&h=987&auto=format&fit=crop)
Copilot has generated two controller actions: Edit(int id)
to display the restaurant edit view and Edit(int id, RestaurantEditModel model)
to process updated restaurant data.
The generated code looks fine, but SonarLint has detected a potential problem related to the first method.
It’s indicated by a squiggle in the code editor as well as by a new item in the Problems view in the lower part of the VS Code workspace.
If you press Ctrl+. with the caret on the squiggle, you’ll see a pop-up menu that lets you see details about the problem, automated actions to fix it (if any), and any other contextually applicable actions provided by SonarLint, VS Code, or other VS Code plugins.
![](https://assets-eu-01.kc-usercontent.com:443/02cb980b-5a8a-016f-80ee-dce623e76463/420c3b7d-0b75-4153-8fe8-5e8c841b806b/sonarlint-detect-issue-in-github-copilot-ai-generated-code.png?w=1312&h=916&auto=format&fit=crop)
In this case, SonarLint has detected that the implementation of the new action is identical to an existing action, Details(int id)
.
If you select SonarLint: Open description of rule csharpsquid:S4144, you can read the description of the applicable rule in SonarLint’s Rule Description pane.
SonarLint classifies this problem as a possible maintainability issue.
If duplication is intended, a refactoring that involves extracting reused code may be necessary.
Let’s now ask Copilot to generate an Entity Framework
model class defining a restaurant review.
Here’s a possible prompt that could work for this purpose and the code that Copilot might generate in response:
![](https://assets-eu-01.kc-usercontent.com:443/02cb980b-5a8a-016f-80ee-dce623e76463/2dd42ff3-2e4e-4b3f-a4b2-ace5d71deb02/github-copilot-generate-entity-framework-code.png?w=716&h=774&auto=format&fit=crop)
That’s some nice usage of Entity Framework
model attributes by Copilot!
However, the generated code has a few problems.
Some of them are spotted by Microsoft’s C# language plugin, others by SonarLint.
For example, SonarLint notices that the generated class is not inside a namespace, which introduces the potential for name collision later on.
![](https://assets-eu-01.kc-usercontent.com:443/02cb980b-5a8a-016f-80ee-dce623e76463/ac619923-6d66-470a-be15-c840a3caff3e/sonarlint-github-copilot-entity-framework-generated-code-issue.png?w=1275&h=695&auto=format&fit=crop)
This problem can be solved easily by adding a namespace OdeToFood.Models;
declaration to the Review.cs file.
Applying Team-Wide Code Quality Rules with SonarLint’s Connected Mode
Using SonarLint’s code inspections inside VS Code is nice in itself, but the code you write doesn’t usually exist in isolation. More often than not, you’re writing code that builds upon an existing codebase.
This codebase is probably maintained by a team that you’re part of, and the team might be using SonarQube or SonarCloud to analyze all commits using a custom rule set.
This is where you can take advantage of SonarLint’s Connected Mode.
It pairs SonarLint with your SonarQube or SonarCloud instance, ensuring that both your local edits and the commits you push to the repository are inspected for code quality using the same analysis settings across your team.
To enable Connected Mode, open the SonarLint pane in VS Code and click Add SonarQube Connection or Add SonarCloud Connection, depending on which of these tools you’re using for server-side code quality analysis:
![](https://assets-eu-01.kc-usercontent.com:443/02cb980b-5a8a-016f-80ee-dce623e76463/d7dbf82e-4f31-4014-96a0-584e2214766a/sonarlint-connected-mode-code-quality-rules-team-wide.png?w=505&h=301&auto=format&fit=crop)
In the SonarQube Connection editor tab that opens, enter your SonarQube server URL, and click Generate Token.
This will open your SonarQube instance, which will prompt you to allow connecting to your SonarLint plugin in VS Code. Click Allow connection:
![](https://assets-eu-01.kc-usercontent.com:443/02cb980b-5a8a-016f-80ee-dce623e76463/eba1859b-43ec-4459-93c6-e94d2a943150/sonarlint-allow-sonarqube-connection-generated-token.png?w=746&h=407&auto=format&fit=crop)
When you return to VS Code, the SonarQube Connection pane should already include a new connection token that SonarQube has just generated.
Click Save Connection.
Finally, SonarLint will display a notification suggesting that you bind your editor workspace to SonarQube’s server project for your code repository. Click Configure Binding to complete the Connected Mode setup.
![](https://assets-eu-01.kc-usercontent.com:443/02cb980b-5a8a-016f-80ee-dce623e76463/dd84fd53-6e62-4cc7-97ab-ee0d376463f2/configure-binding-sonarlint-connected-mode.png?w=445&h=144&auto=format&fit=crop)
When Connected Mode is on and your SonarQube project uses an extended quality profile (aka rule set), you will see SonarLint display problems accordingly.
For example, if your SonarQube quality profile for C# requires adding an empty line at the end of every file, SonarLint will remind you to add one in the Review.cs file with the review model class that Copilot has generated:
![](https://assets-eu-01.kc-usercontent.com:443/02cb980b-5a8a-016f-80ee-dce623e76463/9f4e2e5e-55d9-420b-8365-d7d0ce52f6dc/sonarlint-rule-description-review-github-copilot-code.png?w=1161&h=645&auto=format&fit=crop)
Connected Mode also enables synchronization of customized rule descriptions and issue status (Accept/False Positive) between SonarQube (or SonarCloud) and the SonarLint plugin in your IDE.
Summary
GitHub Copilot can help developers spend less time writing boilerplate code and focus on higher-order matters like clean architecture, scalability, and maintainability.
However, the code that Copilot generates is not guaranteed to be free of errors and vulnerabilities.
As you integrate Copilot and other generative AI services into your toolset, remember to keep your code quality gates working.
To help you with that, Sonar offers tools for continuous code quality analysis—including SonarLint, SonarQube, and SonarCloud—that keep your code clean in the code editor and your CI/CD pipelines.
These tools help you detect and fix bugs and vulnerabilities in AI-generated code, as well as make sure that it adheres to your team’s code style and code quality standards.
This article was written by Jura Gorohovsky as part of the process of using GitHub Copilot Ensuring Quality AI-Generated Code.
- June 11, 2024