|
2 | 2 |
|
3 | 3 | ## Goal |
4 | 4 |
|
5 | | -The goal of this exercise is to learn how to use GitHub Copilot, using an exercise that consist of building a REST API using Spring Boot. |
| 5 | +The goal is to create a REST API using Spring Boot and a corresponding Docker image with the help of GitHub Copilot. |
| 6 | +Follow the instructions below and try to use GitHub Copilot as much as possible. |
| 7 | +Try different things and see what GitHub Copilot can do for you, like generating a Dockerfile or a class, add comments, etc. |
6 | 8 |
|
7 | | -## Exercises |
| 9 | +> Make sure GitHub Copilot is configure and enabled for the current laguage, just check the status bar on the bottom right corner of VS Code. |
8 | 10 |
|
9 | | -We have created a Spring Boot project with some files already created, you can find the project in the folder **exercisefiles/springboot**. |
| 11 | +## Instructions |
10 | 12 |
|
11 | | -Let's start copiloting!!! |
| 13 | +The `copilot-demo` folder contains the Maven project and Maven installer. |
12 | 14 |
|
13 | | -### 1. Create the code to handle a simple GET request |
| 15 | +- `src` contains `main` and `test`. Where `main` is where the source code lives, and `test` contains the unit tests. |
| 16 | +- `Dockerfile` will be used to create a docker image for the project. |
| 17 | +- `mvnw` is the unix CLI that can be invoked to use maven CLI. |
| 18 | +- `mvnw.cmd` is the Windows CLI that can be invoked to use maven CLI. |
14 | 19 |
|
15 | | -Move to the 'DemoController.java' file and start writing the code to handle a simple GET request. In this first exercise, we have provided a comment that describes the code you need to generate. Just press enter and wait a couple of seconds, Copilot will generate the code for you. |
| 20 | +To run Copilot inline on Windows you press `Ctrl + i` (Windows) / `⌘ + i` (Mac) |
16 | 21 |
|
17 | | -There is already a unit test implemented for this exercise, you can run it using the command `mvn test` before and after to validate that the code generated by Copilot is correct. |
| 22 | +To run the tests open a terminal in the `copilot-demo` folder and run: |
18 | 23 |
|
19 | | -Then, create a new unit test for the case when no key is provided in the request. |
| 24 | +```sh |
| 25 | +./mvnw test |
| 26 | +``` |
20 | 27 |
|
21 | | -After every exercise, feel free to package and run your application to test it. |
| 28 | +To run the app open a terminal in the `copilot-demo` folder and run: |
22 | 29 |
|
23 | | -Package: `mvn package` |
| 30 | +```sh |
| 31 | +./mvnw package |
| 32 | +./mvnw spring-boot:run |
| 33 | +``` |
24 | 34 |
|
25 | | -Run: `mvn spring-boot:run` |
| 35 | +### Unix Note |
26 | 36 |
|
27 | | -Test: `curl -v http://localhost:8080/hello?key=world` |
| 37 | +The `mvnw` might not have execute permissions. |
| 38 | +Please run the following to give it execute permissions. |
28 | 39 |
|
29 | | -### 2. Dates comparison |
| 40 | +```sh |
| 41 | +chmod +x ./mvnw |
| 42 | +``` |
30 | 43 |
|
31 | | -New operation under /diffdates that calculates the difference between two dates. The operation should receive two dates as parameter in format dd-MM-yyyy and return the difference in days. |
| 44 | +### Exercise 1: Introduction |
| 45 | + |
| 46 | +For this exercise we will be adding a new endpoint to handle a simple GET request. |
| 47 | + |
| 48 | +- Move to the `src/main/.../DemoController.java` file |
| 49 | +- Start writing the code to handle a simple GET request based on the javadoc comment. Just press enter and wait a couple of seconds, Copilot will generate the code for you. |
| 50 | +- Alternatively, you can test the Copilot inline feature by pressing `ctrl/⌘ + i`. Then write in the text box the desired behaviour. |
| 51 | +- run `./mvnw test` |
| 52 | +- If the test passed you should see something like this: |
| 53 | + |
| 54 | +```sh |
| 55 | +[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.501 s - in com.microsoft.hackathon.copilotdemo.CopilotDemoApplicationTests |
| 56 | +[INFO] |
| 57 | +[INFO] Results: |
| 58 | +[INFO] |
| 59 | +[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 |
| 60 | +[INFO] |
| 61 | +[INFO] ------------------------------------------------------------------------ |
| 62 | +[INFO] BUILD SUCCESS |
| 63 | +[INFO] ------------------------------------------------------------------------ |
| 64 | +[INFO] Total time: 2.713 s |
| 65 | +[INFO] Finished at: 2024-10-03T10:49:49+08:00 |
| 66 | +[INFO] ------------------------------------------------------------------------ |
| 67 | +``` |
| 68 | + |
| 69 | +You can now run the application and then test it with curl. |
| 70 | + |
| 71 | +1. Run the spring app: `./mvnw spring-boot:run` |
| 72 | +2. Test with curl: `curl -v http://localhost:8080/hello?key=world` |
| 73 | + |
| 74 | +Let's now create a new unit test for the case when no key is provided in the request. |
| 75 | + |
| 76 | +- Move to `src/test/.../CopilotDemoApplicationTests.java` |
| 77 | +- Create a comment with `//` and ask it to generate the test case for you. Wait a couple of seconds and it should autocomplete the test for you. |
| 78 | +- You should then see the following output |
| 79 | + |
| 80 | +```sh |
| 81 | +[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.451 s - in com.microsoft.hackathon.copilotdemo.CopilotDemoApplicationTests |
| 82 | +[INFO] |
| 83 | +[INFO] Results: |
| 84 | +[INFO] |
| 85 | +[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0 |
| 86 | +[INFO] |
| 87 | +[INFO] ------------------------------------------------------------------------ |
| 88 | +[INFO] BUILD SUCCESS |
| 89 | +[INFO] ------------------------------------------------------------------------ |
| 90 | +[INFO] Total time: 2.686 s |
| 91 | +[INFO] Finished at: 2024-10-03T11:04:09+08:00 |
| 92 | +[INFO] ------------------------------------------------------------------------ |
| 93 | +``` |
| 94 | + |
| 95 | +### Exercise 2: Building new functionalities |
| 96 | + |
| 97 | +For this exercise, the code can either but in their own controller, or you can reuse the existing `DemoController.java` |
| 98 | + |
| 99 | +Add the following endpoints using the help of Copilot. |
32 | 100 |
|
33 | 101 | Additionally, create a unit test that validates the operation. |
34 | 102 |
|
35 | | -From now on, you will have to create the unit tests for every new operation. Wasn't it easy with Copilot? |
| 103 | +From now on, you will have to create the unit tests for every new operation. Wasn't it easy with Copilot? |
36 | 104 |
|
37 | | -### 3. Validate the format of a spanish phone |
| 105 | +- **/daysbetweendates**: |
38 | 106 |
|
39 | | -Validate the format of a spanish phone number (+34 prefix, then 9 digits, starting with 6, 7 or 9). The operation should receive a phone number as parameter and return true if the format is correct, false otherwise. |
| 107 | + - calculate days between two dates |
| 108 | + - receive by query string two parameters `date1` and `date2`, and calculate the days between those two dates. |
40 | 109 |
|
41 | | -### 4. Validate the format of a spanish DNI |
| 110 | +> **_NOTE:_** Use above information inside the Copilot inline feature. Press enter and wait for Copilot to suggest you the code. |
42 | 111 |
|
43 | | -Validate the format of a spanish DNI (8 digits and 1 letter). The operation should receive a DNI as parameter and return true if the format is correct, false otherwise. |
| 112 | +- **/validatephonenumber**: |
44 | 113 |
|
45 | | -### 5. From color name to hexadecimal code |
| 114 | + - receive by querystring a parameter called phoneNumber |
| 115 | + - validate phoneNumber with Spanish format, for example `+34666777888` |
| 116 | + - if phoneNumber is valid return true |
46 | 117 |
|
47 | | -Based on existing colors.json file under resources, given the name of the color as path parameter, return the hexadecimal code. If the color is not found, return 404 |
| 118 | +> **_NOTE:_** Use above information inside a comment. Press enter and wait for Copilot to suggest you the code. |
48 | 119 |
|
49 | | -Hint: Use TDD. Start by creating the unit test and then implement the code. |
| 120 | +- **/validatespanishdni**: |
50 | 121 |
|
51 | | -### 6. Jokes creator |
| 122 | + - receive by querystring a parameter called dni |
| 123 | + - calculate DNI letter |
| 124 | + - if DNI is valid return "valid" |
| 125 | + - if DNI is not valid return "invalid" |
52 | 126 |
|
53 | | -Create a new operation that call the API https://api.chucknorris.io/jokes/random and return the joke. |
| 127 | +> **_NOTE:_** Use above information inside a comment. In this case, you may want to see multiple solutions from Copilot to pick the one that best fits the way to calculate the letter. In order to see the firs 10 suggestions from Copilot press `ctrl/⌘ + enter`. |
54 | 128 |
|
55 | | -### 7. URL parsing |
| 129 | +- **/returncolorcode**: |
56 | 130 |
|
57 | | -Given a url as query parameter, parse it and return the protocol, host, port, path and query parameters. The response should be in Json format. |
| 131 | + - receive by querystring a parameter called color |
| 132 | + - read colors.json file under resources and return the rgba field |
| 133 | + - get color var from querystring |
| 134 | + - iterate for each color in colors.json to find the color |
| 135 | + - return the hex field |
| 136 | + - return 404 if not found |
58 | 137 |
|
59 | | -### 8. List files and folders |
| 138 | +> **_NOTE:_** Hint: Use TDD. Start by creating the unit test and then implement the code. |
| 139 | +> Lets try Copilot chat now. |
| 140 | +> Paste the above information and make it as detailed as possible in the Copilot chat text box. |
| 141 | +> Copilot will use by default the open file as context in order to generate the suggestion. |
60 | 142 |
|
61 | | -List files and folders under a given path. The path should be a query parameter. The response should be in Json format. |
| 143 | +- **/tellmeajoke**: |
62 | 144 |
|
63 | | -### 9. Word counting |
| 145 | + - Make a call to the joke api and return a random joke - <https://api.chucknorris.io/jokes/random> |
64 | 146 |
|
65 | | -Given the path of a file and count the number of occurrence of a provided word. The path and the word should be query parameters. The response should be in Json format. |
| 147 | +> **_NOTE:_** Here's example where you might need to use you own knowledge and judgement |
| 148 | +> to validate that Copilot follows best practices. Just because Copilot mimics |
| 149 | +> what many developers do, doesn't always mean it's the correct way. You might need |
| 150 | +> to be extra specific in your prompt to let Copilot know what's best practices. |
66 | 151 |
|
67 | | -### 10. Zipping |
| 152 | +- **/moviesbydirector**: |
68 | 153 |
|
69 | | -Create a zip file with the content of a given folder. The path of the folder should be a query parameter. |
| 154 | + - Receive by querystring a parameter called director |
| 155 | + - Make a call to the movie api and return a list of movies of that director |
| 156 | + - Return the full list of movies |
70 | 157 |
|
71 | | -### 11. Containerize the application |
| 158 | +> **_NOTE:_** This will require to browse to <https://www.omdbapi.com/apikey.aspx> and request a FREE API Key |
72 | 159 |
|
73 | | -Use the Dockerfile provided to create a docker image of the application. There are some comments in the Dockerfile that will help you to complete the exercise. |
| 160 | +- **/parseurl**: |
74 | 161 |
|
75 | | -In order to build, run and test the docker image, you can use Copilot as well to generate the commands. |
| 162 | + - Retrieves a parameter from querystring called someurl |
| 163 | + - Parse the url and return the protocol, host, port, path, querystring and hash |
| 164 | + - Return the parsed host |
76 | 165 |
|
77 | | -For instance, create a DOCKER.md file where you can store the commands to build, run and test the docker image. You will notice that Copilot will also help you to document your project and commands. |
| 166 | +> **_NOTE:_** Copilot can help you learn new frameworks. |
78 | 167 |
|
79 | | -Examples of steps to document: Build the container image, Run the container, Test the container. |
| 168 | +- **/listfiles**: |
| 169 | + |
| 170 | + - Get the current directory |
| 171 | + - Get the list of files in the current directory |
| 172 | + - Return the list of files |
80 | 173 |
|
| 174 | +> **_NOTE:_** Copilot can also help with these kind of commands locally. The feature is called Copilot in the CLI. You can learn more information about this feature [here](https://docs.github.com/en/copilot/github-copilot-in-the-cli/about-github-copilot-in-the-cli). |
81 | 175 |
|
| 176 | +- **/calculatememoryconsumption**: |
82 | 177 |
|
| 178 | + - Return the memory consumption of the process in GB, rounded to 2 decimals |
83 | 179 |
|
| 180 | +- **/randomeuropeancountry**: |
| 181 | + |
| 182 | + - Make an array of european countries and its iso codes |
| 183 | + - Return a random country from the array |
| 184 | + - Return the country and its iso code |
| 185 | + |
| 186 | +### Exercise 3: Document the code |
| 187 | + |
| 188 | +Documenting code is always a boring and painful task. However, we can use Copilot to document it for us. In the chat, ask Copilot to add javadoc to all of your files. |
| 189 | + |
| 190 | +### Exercise 4: Verify Tests |
| 191 | + |
| 192 | +Have you been building your Unit Tests along the way? If not this is the perfect time to take a breather and get Copilot to write some unit tests for you! |
| 193 | + |
| 194 | +We will create automated tests to check that the functionality of the previous endpoints is correctly implemented. The tests should be together in the `CopilotDemoApplicationTests.java` file. |
| 195 | + |
| 196 | +You can leverage Copilot to run the tests. There is a `/tests` command that you can directly run from Copilot Chat or by selecting the piece of code you want to create tests for and using the Copilot inline feature. |
| 197 | + |
| 198 | +### Exercise 5: Create a Dockerfile |
| 199 | + |
| 200 | +Use the Dockerfile provided to create a docker image of the application. There are some comments in the Dockerfile that will help you to complete the exercise. |
| 201 | + |
| 202 | +In order to build, run and test the docker image, you can use Copilot as well to generate the commands. |
| 203 | + |
| 204 | +For instance, create a `DOCKER.md` file where you can store the commands to build, run and test the docker image. |
| 205 | + |
| 206 | +Examples of steps to document: Build the container image, Run the container, Test the container. |
84 | 207 |
|
| 208 | +## Summary |
85 | 209 |
|
| 210 | +With the previous exercises you have gone through some common activities that developers usually run: |
86 | 211 |
|
| 212 | +- Create new features in the code |
| 213 | +- Work with external APIs |
| 214 | +- Create documentation |
| 215 | +- Create tests |
87 | 216 |
|
| 217 | +However, there are many other things that Copilot can helkp you with. Feel free to explore other slash command in the Copilot chat like: |
88 | 218 |
|
| 219 | +- `/fix`: to fix the problems in your code |
| 220 | +- `/explain`: for Copilot to explain you what the code does |
0 commit comments