|
| 1 | +# NodeJS Webapp |
| 2 | + |
| 3 | +This is a simple NodeJS webapp that uses ExpressJS to serve a simple HTML page. This will add a user to your repository. |
| 4 | + |
| 5 | +## Running the app |
| 6 | + |
| 7 | +To run the app, simply run the following command: |
| 8 | + |
| 9 | +```bash |
| 10 | +node app.js |
| 11 | +``` |
| 12 | + |
| 13 | +This will start the app on port 3000. You can access the app by navigating to `http://localhost:3000` in your browser. |
| 14 | + |
| 15 | + |
| 16 | +# Steps for Copilot Chat Demo |
| 17 | + |
| 18 | +## Step 1: Start the application |
| 19 | + |
| 20 | +```bash |
| 21 | +# Ask copilot chat "How can I start the application?" |
| 22 | +# use the terminal commands it gives to run the code |
| 23 | +# Test the app in the browser |
| 24 | +``` |
| 25 | + |
| 26 | +## Step 2: Add Comments to the App.js file |
| 27 | + |
| 28 | +```bash |
| 29 | +# Add comments to the app.js file explaining what each section of the code does |
| 30 | +``` |
| 31 | + |
| 32 | +## Step 3: Explain the Code |
| 33 | + |
| 34 | +```bash |
| 35 | +# Highlight the code in the app.js file that you need explained |
| 36 | +# Use the inline copilot chat feature to explain the code you like to understand |
| 37 | +# Highlight another section of the code and ask for an explanation in chat UI |
| 38 | +``` |
| 39 | + |
| 40 | +## Step 4: Add more functionality: Permission Input |
| 41 | + |
| 42 | +```bash |
| 43 | +# Copilot Chat: "How can I have the user input a permission field?" |
| 44 | +``` |
| 45 | +<details> |
| 46 | +<summary>Copilot Suggestion</summary> |
| 47 | + |
| 48 | +### Add a permission field to the form |
| 49 | + |
| 50 | +```html |
| 51 | +<form action="/add-collaborator" method="POST"> |
| 52 | + <label for="owner">Owner:</label><br> |
| 53 | + <input type="text" id="owner" name="owner"><br> |
| 54 | + <label for="repo">Repo:</label><br> |
| 55 | + <input type="text" id="repo" name="repo"><br> |
| 56 | + <label for="username">Username:</label><br> |
| 57 | + <input type="text" id="username" name="username"><br> |
| 58 | + <label for="permission">Permission:</label><br> |
| 59 | + <select id="permission" name="permission"> |
| 60 | + <option value="pull">Pull</option> |
| 61 | + <option value="push">Push</option> |
| 62 | + <option value="admin">Admin</option> |
| 63 | + <option value="maintain">Maintain</option> |
| 64 | + </select> |
| 65 | + <input type="submit" value="Add Collaborator"> |
| 66 | +</form> |
| 67 | +``` |
| 68 | + |
| 69 | +### Update app.js to handle the new permission field |
| 70 | + |
| 71 | +```javascript |
| 72 | + |
| 73 | +app.post('/add-collaborator', async (req, res) => { |
| 74 | + const { owner, repo, username, permission } = req.body; |
| 75 | + await addCollaborator(owner, repo, username, permission); |
| 76 | + res.redirect('/'); |
| 77 | +}); |
| 78 | + |
| 79 | + |
| 80 | +async function addCollaborator(owner, repo, username, permission) { |
| 81 | + // ... |
| 82 | + const response = await octokit.rest.repos.addCollaborator({ |
| 83 | + owner, |
| 84 | + repo, |
| 85 | + username, |
| 86 | + permission |
| 87 | + }); |
| 88 | + // ... |
| 89 | +} |
| 90 | + |
| 91 | +``` |
| 92 | + |
| 93 | +</details> |
| 94 | + |
| 95 | +## Step 5: Add UI commponents to this application |
| 96 | + |
| 97 | +```bash |
| 98 | +# Copilot Chat: "how can I make this express and nodejs application look more modern?" |
| 99 | +``` |
| 100 | + |
| 101 | +<details> |
| 102 | +<summary>Copilot Suggestion</summary> |
| 103 | + |
| 104 | +### Add Bootstrap to the HTML |
| 105 | + |
| 106 | +```html |
| 107 | +<!DOCTYPE html> |
| 108 | +<html> |
| 109 | +<head> |
| 110 | + <title>Add Collaborator</title> |
| 111 | + <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"> |
| 112 | +</head> |
| 113 | +<body class="bg-light"> |
| 114 | + <div class="container"> |
| 115 | + <div class="py-5 text-center"> |
| 116 | + <h2>Add Collaborator</h2> |
| 117 | + </div> |
| 118 | + |
| 119 | + <div class="row"> |
| 120 | + <div class="col-md-12"> |
| 121 | + <form class="needs-validation" action="/add-collaborator" method="POST"> |
| 122 | + <div class="row"> |
| 123 | + <div class="col-md-6 mb-3"> |
| 124 | + <label for="owner">Owner</label> |
| 125 | + <input type="text" class="form-control" id="owner" name="owner" required> |
| 126 | + </div> |
| 127 | + <div class="col-md-6 mb-3"> |
| 128 | + <label for="repo">Repo</label> |
| 129 | + <input type="text" class="form-control" id="repo" name="repo" required> |
| 130 | + </div> |
| 131 | + </div> |
| 132 | + |
| 133 | + <div class="mb-3"> |
| 134 | + <label for="username">Username</label> |
| 135 | + <input type="text" class="form-control" id="username" name="username" required> |
| 136 | + </div> |
| 137 | + |
| 138 | + <div class="mb-3"> |
| 139 | + <label for="permission">Permission</label> |
| 140 | + <select class="custom-select d-block w-100" id="permission" name="permission" required> |
| 141 | + <option value="pull">Read</option> |
| 142 | + <option value="push">Write</option> |
| 143 | + <option value="push">Triage</option> |
| 144 | + <option value="admin">Admin</option> |
| 145 | + <option value="maintain">Maintain</option> |
| 146 | + </select> |
| 147 | + </div> |
| 148 | + |
| 149 | + <button class="btn btn-primary btn-lg btn-block" type="submit">Add Collaborator</button> |
| 150 | + </form> |
| 151 | + </div> |
| 152 | + </div> |
| 153 | + </div> |
| 154 | +</body> |
| 155 | +</html> |
| 156 | +``` |
| 157 | +</details> |
| 158 | + |
| 159 | + |
| 160 | +## Step 6: Error Handling |
| 161 | + |
| 162 | +Go to the UI and test a non existing user. |
| 163 | + |
| 164 | +```bash |
| 165 | +# how can I add more error handling to this app? Specifically, I'd like to throw an error in the UI when a user does not exist |
| 166 | +``` |
| 167 | + |
| 168 | +<details> |
| 169 | +<summary>Copilot Suggestion</summary> |
| 170 | + |
| 171 | +### Add Error Handling to the App |
| 172 | + |
| 173 | +```javascript |
| 174 | +// ## Add error handling to the app.js file |
| 175 | +async function addCollaborator(owner, repo, username, permission) { |
| 176 | + try { |
| 177 | + // Call the GitHub API to add a collaborator |
| 178 | + const response = await octokit.rest.repos.addCollaborator({ |
| 179 | + owner, |
| 180 | + repo, |
| 181 | + username, |
| 182 | + permission |
| 183 | + }); |
| 184 | + |
| 185 | + // Return the response |
| 186 | + return response; |
| 187 | + } catch (error) { |
| 188 | + // If there was an error calling the GitHub API, throw the error |
| 189 | + throw error; |
| 190 | + } |
| 191 | +} |
| 192 | +// Add error handling to the app.js file in the add-collaborator route |
| 193 | +app.post('/add-collaborator', async (req, res) => { |
| 194 | + try { |
| 195 | + const { owner, repo, username, permission } = req.body; |
| 196 | + const response = await addCollaborator(owner, repo, username, permission); |
| 197 | + |
| 198 | + // If the response status is 201, the collaborator was added successfully |
| 199 | + if (response.status === 201) { |
| 200 | + res.send('Collaborator added successfully'); |
| 201 | + } |
| 202 | + // If the response status is 204, the user is already a collaborator |
| 203 | + else if (response.status === 204) { |
| 204 | + res.send('User is a collaborator'); |
| 205 | + } |
| 206 | + // If the response status is anything else, there was an error |
| 207 | + else { |
| 208 | + res.send('There was an error adding the collaborator'); |
| 209 | + } |
| 210 | + } catch (error) { |
| 211 | + // If the user does not exist, send an error message to the client |
| 212 | + if (error.status === 404) { |
| 213 | + res.send('User does not exist'); |
| 214 | + } else { |
| 215 | + res.send('There was an error adding the collaborator'); |
| 216 | + } |
| 217 | + } |
| 218 | +}); |
| 219 | +``` |
| 220 | + |
| 221 | +</details> |
0 commit comments