GitHub Copilot is an innovative coding assistant powered by machine learning that helps you write code faster and with less effort. This README provides an overview of GitHub Copilot's features and guides you through the process of getting started with it using Visual Studio Code.
GitHub Copilot is designed to enhance your coding experience with the following key features:
- Code Autocompletion: Copilot suggests whole lines or blocks of code as you type, saving you time and effort.
- Natural Language Understanding: Write code in plain English comments, and Copilot intelligently generates corresponding code snippets.
- Extensive Language Support: Supports multiple programming languages, making it versatile for various development tasks.
Before diving into GitHub Copilot, ensure you have the following prerequisites:
- Visual Studio Code: GitHub Copilot is tightly integrated with VS Code, so make sure you have it installed.
- GitHub Account: Sign in to VS Code using your GitHub account for a seamless experience.
To start using GitHub Copilot in Visual Studio Code, follow these steps:
-
Install the GitHub Copilot extension from the VS Code marketplace.
-
Sign in to your GitHub account within VS Code.
-
Open a code file, and start coding! Copilot will provide suggestions as you type.
-
Accessing Azure Key Vault and Azure Cosmos DB required to successfully run the final program. Refer to:
for addtional information and final code sample.
- Option 1: GitHub CoPilot Chat extension, avaialabe on the VS Code activity bar
- Option 2: Right-click within a file, or code, select the menu CoPilot->Start inline chat item, or, Ctrl+I
- Option 3: Start writing a comment // how do I create a comment in dotnet, enter or tab to see or accept recommendations.
CoPilot Chat Extension | CoPilot Inline Chat | CoPilot Comment Style |
---|---|---|
Start typing a comment in code, tab or enter |
Using the GitHub Copilot "Chat" prompt extension, here are a few suggested dialog prompts to launch your discovery:
What azure service is recommended to create a REST endpoint for connecting to Cosmos Db
What vs code extentions are required to create an azure function and run with local storage for testing
What .net packages are required to run a function in azure, access keyvault and connect to cosmos db
What .net using statements are required for an http post, access keyvault and connect to cosmos db
How do I create an azure http request .net function in visual studio code
What dotnet commands are required to create an azure http function
Here's an example using the VS Code GitHub CoPilot chat interface
Learn how to create an Azure Function using GitHub Copilot and Visual Studio Code with the following steps:
- Install the required Azure extensions in VS Code. Ask CoPilot how
- Create a new file or project for your Azure Function.
- Recommended: use the VS Code builtin Azure Function create dialog.
- Optionally: use VS Code Terminal & create the function with dotnet func commands
#
# Sample func init command in VS Code Terminal to create
# a new Azure function in CWD, using Azure Function Core V4
$PS1> func init MyHttpFunction --dotnet --AzureFunctionsVersion v4
$PS1 cd MyHttpFunction
$PS1> func new --name MyHttpTrigger --template "HTTP trigger" --authlevel "anonymous"
Now, let's get started using Copilot to speed up your discovery!
Copilot seamlessly integrates with Azure services. Follow suggested prompt & flow to build something fun.
- Open the desired file or project.
- Use Copilot to generate code snippets for steps.
- Customize the generated code based on your specific configurations.
- Prompts & instructions are intended as a guide, not a step-by-step tutorial. The intent is to discover & explore features and familarity with GitHub CoPilot.
NOTE: CoPilot interacts with the code using current context and style, therefore, consistency in prompt dialog helps CoPilot achieve the desired patterns.
Ultimately, the developer is pilot-in-command, PIC, therefore applying logic and thought to the desired outcome is REQUIRED before accepting code suggestions.
- Open the MyHttpFunction project in VS Code, and start with a clean pallete, in other words: DELETE the generated function code EXCEPT initial method header and using statements. Optionally, run the empty function and test locally to verify a starting point. Ask Copilot
- Start off by verifying parameters & properties in the POST, thence, run a local test
// verify that the B2BCustomerId is not null and return a 400 if it is null
// insert a 200 response and include the B2BCustomerId in the response body
- Moving back after the verification of B2BCustomerId stanza, let's add some checks for other data. tab or accept as CoPilot prompts
// read request header and verify the B2BCustomerIdKey is not null, if null return unauthorized
// deserialize the request body to a dynamic object
// verify the data object includes an OrderId, OrderDateTime, CustomerId, and OrderId and if not return BadRequestBody
At this point, you should be getting the flow and interaction with CoPilot:
- adding a comment, view recommendations
- inspecting the recommendations
- accepting or trying again
- we'll see what the code looks like momentarily for this first section.
Generate a unique id for later, therefore, let's use CoPilot to generate and log what we have so far
// create a uuid and add to data.id
// send requestBody the log in the format of RequestBody: {requestBody}
// send data object ot the log in the format of Data: {data}
- Use CoPilot to assist!
- If extensions are missing use the CoPilot Chat dialog to help resolve any missing extensions, or references
- Install extensions, or add missing using statements, stop and restart VS Code
NOTE: Inspect the recommendations as generated by CoPilot. The prompt/response model requires some insight here, may need to add using statements, or add dotnet packages
// create a connection to Azure Key Vault with DefaultAzureCredential and return server error 500 if unable to connect
Using GetSecretAsync in the comment to help Copilot come to the correct recommendation
// verify a secretValue with GetSecretAsync and return server error 500 if unable to get secret
// assign the secretValue to a variable
CAUTION: Do NOT send secrets to the log, let's truncate, or better yet, let CoPilot help us mask the string
// send secretValue to the log in the format of SecretValue: {secretValue}, truncate the secretValue to 5 characters
// send secretValue to the log in the format of SecretValue: {secretValue}, mask after the string "AccountKey="
Enable authentication and authorization for the Function instance to
1: Access Azure Key Vault, and
2: read a secret
- Azure Function instance must run using a credential defined in Azure (Entra) Active Directory (user principal, user assigned, or system assigned identity)
- Configure Azure Key Vault RBAC controls to allow Key Vault Secrets User for intended secret (Ask CoPilot how to do this)
Refer to the following article for creating, configuring and connecting to Cosmos DB.
NOTE: Inspect the recommendations as generated by CoPilot. The prompt/response model requires some insight here, may need to add using statements, or add dotnet packages
By now, you should be getting the hang of interacting with CoPilot, try a few more hints.
// Create CosmosDB client connection using the secretValue as the connection string if unable to connect return server error 500
// Connect to Cosmos DB and create a database if unable to connect return server error 500
// Connect to Cosmos DB container and if unable to connect return server error 500
// try to send data object to DemoOrderItemsContainer and if it fails return a 500 error
// insert a 200 return statement and return the B2BCustomerId. the OrderId and DataId
NOTE: If VS Code is not able to build the program.cs due to errors, navigate to the PROBLEMS tab in VS Code, right-click on the error and Explain using CoPilot
Save our progress, build and run the program with VS Code. If Azure Key Vault Secret and Azure Cosmos DB services are configured, use your favorite REST Client to test.
Sample REST POST request syntax:
http://localhost:7071/api/MyHttpTrigger1?B2BCustomerId=27330
Sample REST POST request header:
B2BCustomerIdKey: 27330Key
Sample REST POST request JSON body:
{
"OrderId": 123456789,
"OrderDateTime": "2023-11-30T13:07:01.001Z",
"CustomerId": "Az0123456789",
"ProductId": "Prod0123456789"
}
- Ask CoPilot to find out how to move the string variables in the program to an appsettings.json file. This will result in better style and formatting
- Ask CoPilot how to move the Azure Key Vault method to connect using the default credential to a static function. This will improve initialization and performance.
- Ask Copilot how to create a method and move the AzureKeyVaultConnect and AzureCosmosDbConnect sections into seperate files.
GitHub Copilot revolutionizes the way we write code, making development more efficient and enjoyable. Experiment with Copilot in various scenarios, explore its capabilities, and enhance your coding productivity.
Feel free to share your experiences, report issues, or contribute to the GitHub Copilot community.
Happy coding!