Skip to content

Commit 580b7d7

Browse files
committed
Update 0502
1 parent 1af5463 commit 580b7d7

File tree

2 files changed

+11
-66
lines changed

2 files changed

+11
-66
lines changed

Media/0502_Branches.png

35.2 KB
Loading

docs/05_fix_performance_issue/0502.md

Lines changed: 11 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ parent: 'Exercise 05: Fix a performance issue using GitHub Copilot'
77

88
# Task 02 - Remediate in code and create a pull request summary (20 minutes)
99

10-
- Ask Copilot a question in the repository: https://docs.github.com/en/enterprise-cloud@latest/copilot/using-github-copilot/asking-github-copilot-questions-in-githubcom
11-
- Create an Issue and a feature branch
12-
- Find the relevant calling code -- might want to ask GitHub Copilot "Is this code optimized for performance?"
13-
- Update the code to make it faster
14-
- Check in, use GitHub Copilot to create a pull request summary, and have another team member review the PR before merging into prod
15-
- Let deployment complete and ensure that the web application is faster now
16-
1710
## Introduction
1811

1912
In the prior task, you identified which application call is performing the worst. In this task, the MP&P team would like you to use GitHub Copilot to resolve this issue, once again following the Git Flow: creating an Issue, creating a feature branch, resolving the issue, committing changes, creating a pull request, and deploying code out to each environment by way of a GitHub Actions workflow.
@@ -46,70 +39,22 @@ In this task, you will improve the performance of the Team Messaging System.
4639

4740
## Solution
4841

49-
TODO:
5042
<details markdown="block">
5143
<summary>Expand this section to view the solution</summary>
5244

53-
1. Select **Settings** in your repo, then **Code security and analysis**. Select **Enable** on "Dependabot alerts" and "Dependabot security updates."
54-
55-
![Enabled Dependabot alerts and security updates](../../Media/EnableDependabot.png)
56-
57-
**Note** This will also automatically enable "Dependency graph."
58-
2. Navigate to [https://github.com/electron/electron/blob/main/SECURITY.md](https://github.com/electron/electron/blob/main/SECURITY.md) for information about security policies. This is an example of a sample security policy that you could use for this exercise.
59-
3. In your GitHub repo, select **Security**, **Policy**, and **Start setup**
60-
61-
![Start the security policy setup](../../Media/StartSecurityPolicySetup.png)
62-
63-
4. Paste the security policy into the Markdown file (you can overwrite what is there now) and update it for the Munson's Pickles and Preserves Team Messaging System and the GitHub repo your code is in. Then, commit the changes to the main branch.
64-
65-
![Commit the updated security policy](../../Media/CommitSecurityPolicy.png)
66-
67-
5. Next, we need to enable CodeQL. Select **Settings** and then **Code security and analysis**.
68-
6. Scroll down if needed and select **Set up** in "Code scanning" for "CodeQL analysis."
69-
70-
![Setup CodeQL analysis](../../Media/CodeQLAnalysisSetup.png)
71-
72-
7. If you select "Default", the code scan will immediately be run. For this exercise, select **Advanced**.
73-
74-
![Select Default](../../Media/CodeQLAdvanced.png)
75-
76-
8. By choosing the advanced option, you can see the YAML for the pipeline that actually performs the code check. We don't need to make any changes here, but it's something you should be familiar with. An easy change to make in this file would be if you want to adjust the schedule of when the scan runs.
77-
78-
![Commit the CodeQL YAML](../../Media/CodeQLYAMLCommit.png)
79-
80-
After you've reviewed the YAML, commit the change to main.
81-
82-
![Commit the change](../../Media/CodeQLCommitChange.png)
83-
84-
9. After you've committed the change, select **Actions** and you should see your CodeQL Scan workflow running.
85-
86-
![CodeQL scan running](../../Media/CodeQLScanRunning.png)
87-
88-
10. After about 5 minutes, you should see the workflow has completed.
89-
90-
![Workflow complete](../../Media/CodeQLWorkflowComplete.png)
91-
92-
11. After it's complete, go back to **Settings** and **Code security and analysis**. Then, select the ellipsis **...** next to the "Set up" menu. From the ellipsis dropdown, explore each of the first two options: "View last scan log" and "View Code Scanning alerts." You will find one High-risk vulnerability around arbitrary file access during archive extraction.
93-
94-
{: .note }
95-
> This page will still show "Set up" because we chose the Advanced option instead of Basic.
96-
97-
![View code scanning results](../../Media/CodeQLViewResults.png)
45+
1. In order to [ask an exploratory question about the repository](https://docs.github.com/en/enterprise-cloud@latest/copilot/using-github-copilot/asking-github-copilot-questions-in-githubcom#asking-exploratory-questions-about-a-repository), select the GitHub copilot icon at the top-right of a page. Then, ask a question such as "Where can I find the AnalyzeMessages code?"
9846

99-
12. Select the alert and then choose "Show more" to view details on the security issue. It turns out that an attacker could traverse to an arbitrary directory based on the way the MP&P staff wrote this function. In order to correct the function and prevent a directory traversal attack, replace `WriteToDirectory()` with the following code:
47+
Alternatively, you could perform a code search within Visual Studio Code, looking for this text within the `/src/` directory.
48+
2. Once you have located the relevant code, navigate to your GitHub repository and create a new issue. You can do so either by selecting the **Issues** menu and then selecting the **New issue** button, or by navigating to the **Projects** menu, selecting your Kanban board, and then selecting the **Add Item** option for the **Backlog** card. Enter the text for your issue. If you add via the board, make sure to associate your issue with the repository. Keep track of your issue number for a later commit.
49+
3. Create a feature branch by selecting the **Code** menu for your repository, then selecting the **Branches** link next to the branch drop-down.
10050

101-
```csharp
102-
public static void WriteToDirectory(ZipArchiveEntry entry, string destDirectory)
103-
{
104-
string destFileName = Path.GetFullPath(Path.Combine(destDirectory, entry.FullName));
105-
string fullDestDirPath = Path.GetFullPath(destDirectory + Path.DirectorySeparatorChar);
106-
if (!destFileName.StartsWith(fullDestDirPath)) {
107-
throw new System.InvalidOperationException("Entry is outside the target dir: " + destFileName);
108-
}
109-
entry.ExtractToFile(destFileName);
110-
}
111-
```
51+
![Select the branches link](../../Media/0502_Branches.png)
11252

113-
Commit the code and create a pull request to the main branch. You should then see a CodeQL scan for the pull request, and after it succeeds, complete the pull request. Then, return to the code scanning results view and confirm that no issues remain on the list.
53+
After that, select **New branch** and enter a name, such as `performance-improvement`. Select **Create new branch** to complete the process in GitHub. Then, fetch your changes locally and check out the new branch.
54+
4. Return to Visual Studio Code. In the file `src/Application/src/RazorPagesTestSample/Pages/Index.cshtml.cs`, highlight the `OnPostAnalyzeMessagesAsync()` method and ask GitHub Copilot a question such as "Is this code optimized for performance?" The response should indicate several reasons why this is sub-optimal code and offer a modified version that will perform better. Replace the existing code with a faster-running version.
55+
5. Check in your changes, using GitHub Copilot to generate a commit message for you. Be sure to include "Resolves #X" in the commit message, where X is the issue number you created. Push the changes into the remote feature branch.
56+
6. Create a pull request and assign it to another member of your team. Use GitHub Copilot Enterprise to generate a reasonable pull request summary.
57+
7. After your team member completes the code review, complete the merge. Then, navigate to the **Actions** tab and ensure that the **.NET CI** workflow runs successfully.
58+
8. Once deployment completes successfully, navigate to your production website, `{your_random_code}-prod.azurewebsites.net`. Perform the same testing you did in the prior task: add several messages, delete several messages, select the **Analyze** button multiple times, and refresh the page. Try a variety of these activities in different orders so you can ensure functionality remains as it was before, but the performance of the **Analyze** button is significantly faster.
11459

11560
</details>

0 commit comments

Comments
 (0)