Skip to content

Commit c6cce53

Browse files
authored
Merge branch 'main' into add-console-app-demo-nocopilotchat
2 parents adb0f64 + 63e8844 commit c6cce53

File tree

8 files changed

+330
-5
lines changed

8 files changed

+330
-5
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @ps-copilot-sandbox/copilot-service-offerings-v-team

CODEOWNERS

Lines changed: 0 additions & 1 deletion
This file was deleted.

CONTRIBUTING.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## Contributing to GitHub Copilot repository
2+
3+
Hi there! We're thrilled that you'd like to contribute to this project. Your help is essential for keeping it great.
4+
5+
Make sure that you go through this checklist before making a new contribution. Your contribution can be either one of two categories:
6+
7+
- Suggest a change to an existing documentation or a resource, or
8+
- Suggest a creation of a new documentation or a resource
9+
10+
### Prerequisites to suggest a change to an existing documentation or a resource
11+
12+
- [ ] Did you make sure that you understand this suggestion improves the existing resource?
13+
14+
### Prerequisites to suggest a creation of a new documentation or a resource
15+
16+
- [ ] Did you already check there is not a similar example for what you are trying to do? Only after you say `yes`, please continue.
17+
- [ ] Does your contribution contain step-by-step instructions, not just code examples?
18+
- [ ] Did you make sure that the instructions are repeatable and easily understandable by everyone?
19+
20+
If you went through all of these required steps, great! Please follow the instructions below to make a new contribution.
21+
22+
## Submitting a pull request
23+
24+
1. Fork and clone the repository
25+
1. Create a new branch: `git checkout -b my-branch-name`
26+
1. Push to your fork and [submit a pull request][pr]
27+
1. Pat yourself on the back and wait for your pull request to be reviewed and merged.
28+
29+
Here are a few things you can do that will increase the likelihood of your pull request being accepted:
30+
31+
- Keep your change as focused as possible. If there are multiple changes you would like to make that are not dependent upon each other, consider submitting them as separate pull requests.
32+
- Write a [good commit message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html).
33+
34+
## Resources
35+
36+
- [How to Contribute to Open Source](https://opensource.guide/how-to-contribute/)
37+
- [Using Pull Requests](https://help.github.com/articles/about-pull-requests/)
38+
- [GitHub Help](https://help.github.com)

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
# copilot-demo-github-javascript
1+
# GitHub Copilot demos for Javascript
22

3-
### Rule # 1 for using Copilot...
3+
This repository contains a plethora of demos and step-by-step instructions for teaching GitHub Copilot's usage with Javascript.
44

5-
<img style="padding-top: 30px; margin-top: 30px;" src="docs/images/yogi.png">
5+
![Javascript Cover page](https://github.com/ps-copilot-sandbox/copilot-demo-github-javascript/blob/main/docs/images/javascript_cover.jpeg)
66

7-
<br><br>
87

98
This Repository provides Copilot Javascript (NodeJS) **demos & workshops** for developers.
109

@@ -23,3 +22,8 @@ Both contain the necessary steps to complete the exercise.
2322
| [NodeJS app that runs shell commands with Copilot Chat](demos/Command-Execution-WebApp-NodeJS) | A NodeJS webapp that runs shell commands with Copilot Chat |
2423
| [NodeJS app that runs shell commands without Copilot Chat](demos/Command-Execution-WebApp-NodeJS) | A NodeJS webapp that runs shell commands without Copilot Chat. Use this for customer `who does not want to use Copilot Chat or cannot` |
2524

25+
## Contribution
26+
27+
Do you want to contribute to the project? That is great! Please read [**CONTRIBUTING.md**](CONTRIBUTING.md) guide to make the contribution to this repository.
28+
29+
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
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>

demos/NodeJS-WebApp-Simple/app.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const express = require('express');
2+
const { Octokit } = require("@octokit/rest");
3+
const app = express();
4+
5+
6+
require('dotenv').config();
7+
app.use(express.urlencoded({ extended: true }));
8+
9+
app.set('view engine', 'ejs');
10+
11+
const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN });
12+
13+
async function addCollaborator(owner, repo, username) {
14+
try {
15+
const response = await octokit.rest.repos.addCollaborator({
16+
owner,
17+
repo,
18+
username,
19+
permissions: 'write' // 'pull', 'push', 'admin', or 'maintain'
20+
});
21+
22+
if (response.status === 201 || response.status === 204 ) {
23+
console.log(`User ${username} added as a collaborator to the repository ${owner}/${repo}`);
24+
} else {
25+
console.log(`There has been an error adding the collaborator: ${response.status}`);
26+
}
27+
} catch (error) {
28+
console.error(`Error adding collaborator: ${error}`);
29+
}
30+
}
31+
32+
app.get('/', (req, res) => {
33+
res.render('index');
34+
});
35+
36+
app.post('/add-collaborator', async (req, res) => {
37+
const { owner, repo, username } = req.body;
38+
39+
await addCollaborator(owner, repo, username);
40+
41+
res.send('Collaborator added successfully');
42+
});
43+
44+
// Start the server
45+
app.listen(3000, () => console.log('Server running on port 3000'));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Add Collaborator</title>
5+
</head>
6+
<body>
7+
<form action="/add-collaborator" method="POST">
8+
<label for="owner">Owner:</label><br>
9+
<input type="text" id="owner" name="owner"><br>
10+
<label for="repo">Repo:</label><br>
11+
<input type="text" id="repo" name="repo"><br>
12+
<label for="username">Username:</label><br>
13+
<input type="text" id="username" name="username"><br>
14+
<input type="submit" value="Add Collaborator">
15+
</form>
16+
</body>
17+
</html>

docs/images/javascript_cover.jpeg

301 KB
Loading

0 commit comments

Comments
 (0)