Skip to content

Commit f374fab

Browse files
committed
Updated Survey and removed dependency on cloud
1 parent 2888713 commit f374fab

File tree

10 files changed

+124
-119
lines changed

10 files changed

+124
-119
lines changed

app.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ default_permissions:
6262

6363
# Repository contents, commits, branches, downloads, releases, and merges.
6464
# https://developer.github.com/v3/apps/permissions/#permission-on-contents
65-
# contents: read
65+
contents: write
6666

6767
# Deployments and deployment statuses.
6868
# https://developer.github.com/v3/apps/permissions/#permission-on-deployments

index.js

Lines changed: 79 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
const dedent = require("dedent");
77
const fs = require("fs");
8-
const parse = require("csv-parse/lib/sync");
9-
const sql = require("mssql");
8+
const csvParser = require("csv-parser");
9+
const stream = require('stream');
1010
require("dotenv").config();
1111

1212
module.exports = (app) => {
@@ -16,9 +16,9 @@ module.exports = (app) => {
1616
app.on("pull_request.closed", async (context) => {
1717

1818
let pr_number = context.payload.pull_request.number;
19-
let detectedLanguage = "en";
2019
let pr_author = context.payload.pull_request.user.login;
21-
let organization_name = context.payload.repository.owner.login
20+
let organization_name = context.payload.repository.owner.login;
21+
let detectedLanguage = "en";
2222

2323
// read file that aligns with detected language
2424
const issue_body = fs.readFileSync(
@@ -64,7 +64,10 @@ module.exports = (app) => {
6464

6565
async function GetSurveyData(context) {
6666
let issue_body = context.payload.issue.body;
67-
let issue_id = context.payload.issue.id;
67+
let pctSelected = false;
68+
let pctValue = new Array();
69+
let freqSelected = false;
70+
let freqValue = new Array();
6871

6972
// save comment body if present
7073
let comment = null;
@@ -88,17 +91,9 @@ module.exports = (app) => {
8891
);
8992
});
9093

91-
// if there's a comment, insert it into the DB regardless of whether the user answered the survey or not
92-
if (comment) {
93-
await insertIntoDB(context, issue_id, pr_number, isCopilotUsed, null, null, comment);
94-
}
95-
9694
if (isCopilotUsed) {
97-
await insertIntoDB(context, issue_id, pr_number, isCopilotUsed, null, null, comment);
98-
9995
// loop through checkboxes and find the one that contains %
100-
let pctSelected = false;
101-
let pctValue = new Array();
96+
10297
for (const checkbox of checkboxes) {
10398
if (checkbox.includes("%")) {
10499
pctSelected = true;
@@ -108,14 +103,8 @@ module.exports = (app) => {
108103
app.log.info(copilotPercentage);
109104
}
110105
}
111-
if (pctSelected) {
112-
//if percentage is selected, insert into DB
113-
await insertIntoDB(context, issue_id, pr_number, isCopilotUsed, pctValue, null, comment);
114-
}
115106

116107
// loop through checkboxes and find the ones that do not contain % and are not Yes or No
117-
let freqSelected = false;
118-
let freqValue = new Array();
119108
for (const checkbox of checkboxes) {
120109
if (
121110
!checkbox.includes("%") &&
@@ -125,7 +114,8 @@ module.exports = (app) => {
125114
!checkbox.includes("Oui") &&
126115
!checkbox.includes("Não") &&
127116
!checkbox.includes("No") &&
128-
!checkbox.includes("Non")
117+
!checkbox.includes("Non") ||
118+
checkbox.includes("Not very much")
129119
) {
130120
freqSelected = true;
131121
frequencyValue = checkbox;
@@ -135,11 +125,6 @@ module.exports = (app) => {
135125
}
136126
}
137127

138-
if (freqSelected) {
139-
//if frequency is selected, insert into DB
140-
await insertIntoDB(context, issue_id, pr_number, isCopilotUsed, null, freqValue, comment);
141-
}
142-
143128
if( pctSelected && freqSelected ){
144129
// close the issue
145130
try {
@@ -163,7 +148,6 @@ module.exports = (app) => {
163148
);
164149
})
165150
) {
166-
await insertIntoDB(context, issue_id, pr_number, isCopilotUsed, null, null, comment);
167151

168152
if (comment) {
169153
try {
@@ -181,91 +165,118 @@ module.exports = (app) => {
181165
}
182166
}
183167

184-
return {
168+
let data = {
185169
enterprise_name: context.payload.enterprise ? context.payload.enterprise.name : '',
186170
organization_name: context.payload.organization ? context.payload.organization.login : '',
187171
repository_name: context.payload.repository ? context.payload.repository.name : '',
188172
issue_id: context.payload.issue ? context.payload.issue.id : '',
189173
issue_number: context.payload.issue ? context.payload.issue.number : '',
190-
PR_number: context.payload.pull_request ? context.payload.pull_request.number : '',
174+
PR_number: pr_number || '',
191175
assignee_name: context.payload.issue.assignee ? context.payload.issue.assignee.login : '',
192176
is_copilot_used: isCopilotUsed ? 1 : 0,
193177
saving_percentage: pctValue || '',
194178
usage_frequency: freqValue || '',
195179
comment: comment || '',
196180
created_at: context.payload.issue ? context.payload.issue.created_at : '',
197181
completed_at: context.payload.issue ? context.payload.issue.updated_at : ''
198-
}
182+
};
183+
184+
return data;
199185

200186
}
201187

202188
async function insertIntoFile(context) {
189+
let fileContent = "";
190+
let results = [];
191+
let resultString = "";
192+
203193
try {
194+
195+
fileContent = await GetSurveyData(context);
196+
204197
// Try to get the file
205198
let file = await context.octokit.repos.getContent({
206199
owner: context.payload.repository.owner.login,
207200
repo: context.payload.repository.name,
208201
path: "results.csv",
209202
});
203+
210204
// If the file exists, get its contents
211205
let fileContents = Buffer.from(file.data.content, "base64").toString();
212206
// If the file contents are not empty, parse the CSV
213207
if (fileContents.length > 0) {
214-
// parse a csv into an array
215-
let result = parse(fileContents, {
216-
columns: true,
217-
skip_empty_lines: true,
208+
// create a readable stream
209+
let readableStream = new stream.Readable();
210+
readableStream.push(fileContents);
211+
readableStream.push(null);
212+
213+
await new Promise((resolve, reject) => {
214+
readableStream
215+
.pipe(csvParser())
216+
.on('data', (data) => results.push(data))
217+
.on('end', () => {
218+
app.log.info(results);
219+
resolve();
220+
})
221+
.on('error', reject);
218222
});
219-
app.log.info(result);
220-
}
221223

222-
//check if the issue_id already exists in the array
223-
let issue_id_index = result.findIndex((row) => {
224-
return row.issue_id == issue_id;
225-
});
226224

227-
if(issue_id_index != -1){
228-
// if the issue_id exists, update the row in the array
229-
result[issue_id_index] = GetSurveyData(context);
230-
}else{
231-
// if the issue_id does not exist, push the row into the array
232-
result.push(GetSurveyData(context));
233-
}
225+
//check if the issue_id already exists in the array
226+
let issue_id_index = results.findIndex((row) => {
227+
return parseInt(row.issue_id) == parseInt(context.payload.issue.id);
228+
});
234229

235-
// convert the array back into a csv
236-
let csv = parse(result, { header: true });
237-
// convert the csv into a string
238-
let csvString = csv.join("\n");
239-
// convert the string into a buffer
240-
let csvBuffer = Buffer.from(csvString, "utf8");
241-
// encode the buffer into base64
242-
let csvEncoded = csvBuffer.toString("base64");
243-
244-
// commit the file to the repo
245-
await context.octokit.repos.createOrUpdateFileContents({
246-
owner: context.payload.repository.owner.login,
247-
repo: context.payload.repository.name,
248-
path: "results.csv",
249-
message: 'update',
250-
content: csvEncoded,
251-
sha: file.data.sha,
252-
});
230+
if(issue_id_index != -1){
231+
// save previous comments
232+
if (results[issue_id_index].comment) {
233+
fileContent.comment = results[issue_id_index].comment + ' || ' + fileContent.comment;
234+
}
235+
236+
// if the issue_id exists, update the row in the array results
237+
results[issue_id_index] = fileContent;
238+
}else{
239+
// if the issue_id does not exist, push the row into the array
240+
results.push(fileContent);
241+
}
242+
243+
resultString = Object.keys(results[0]).join(',') + '\n';
244+
results.forEach((row) => {
245+
resultString += Object.values(row).join(',') + '\n';
246+
});
247+
248+
app.log.info("CSV String:\n" + resultString);
253249

250+
// commit the file to the repo
251+
await context.octokit.repos.createOrUpdateFileContents({
252+
owner: context.payload.repository.owner.login,
253+
repo: context.payload.repository.name,
254+
path: "results.csv",
255+
message: 'Update results.csv',
256+
content: Buffer.from(resultString).toString('base64'),
257+
sha: file.data.sha,
258+
});
259+
260+
app.log.info("File updated successfully\n " + Buffer.from(resultString).toString('base64'));
261+
262+
}
254263
} catch (error) {
255264
// If the file does not exist, create it
256265
if (error.status === 404) {
266+
let completeData = 'enterprise_name,organization_name,repository_name,issue_id,issue_number,PR_number,assignee_name,is_copilot_used,saving_percentage,usage_frequency,comment,created_at,completed_at\n'
267+
+ Object.values(fileContent).join(',');
257268
await context.octokit.repos.createOrUpdateFileContents({
258269
owner: context.payload.repository.owner.login,
259270
repo: context.payload.repository.name,
260271
path: "results.csv",
261272
message: 'initial commit',
262-
content: 'enterprise_name, organization_name, repository_name, issue_id, issue_number, PR_number, assignee_name, is_copilot_used, saving_percentage, usage_frequency, comment, created_at, completed_at\n' + GetSurveyData(context)
273+
content: Buffer.from(completeData).toString('base64'),
263274
});
264-
} else {
265-
// If the error is not a 404 (not found), log it
275+
}else{
266276
app.log.error(error);
267277
}
268278
}
279+
269280

270281
}
271282

issue_template/copilot-usage-en.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ For Pull Request XXX:
66
- [ ] No
77
- [ ] Yes
88

9-
2. Compared to your previous experience coding WITHOUT using Copilot (This number represents 100%)
10-
11-
***How much less time did the coding take during this PR with Copilot?***
9+
2. Compared to your previous experience coding WITHOUT using Copilot (This number represents 100%) ***How much less time did the coding take during this PR with Copilot?***
1210

1311
(Example: The PR would normally take 5 days, but only took 4 days with Copilot then the answer is 20%)
1412
- [ ] 0%

issue_template/copilot-usage-es.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ Para el Pull Request XXX:
66
- [ ] No
77
- [ ] Si
88

9-
2. En comparación con su experiencia anterior codificando sin usar Copilot (este número representa el 100%)
10-
11-
***¿Cuánto menos tiempo tomó la codificación durante este PR con Copilot?***
9+
2. En comparación con su experiencia anterior codificando sin usar Copilot (este número representa el 100%) ***¿Cuánto menos tiempo tomó la codificación durante este PR con Copilot?***
1210

1311
(Ejemplo: el PR normalmente tomaría 5 días, pero solo tomó 4 días con Copilot, entonces la respuesta es 20%)
1412
- [ ] 0%

issue_template/copilot-usage-fr.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ Pour le Pull Request XXX :
66
- [ ] Non
77
- [ ] Oui
88

9-
2. Par rapport à votre expérience précédente de codage SANS utiliser Copilot (ce nombre représente 100 %)
10-
11-
***Combien de temps le codage a-t-il pris pendant ce PR avec Copilot ?***
9+
2. Par rapport à votre expérience précédente de codage SANS utiliser Copilot (ce nombre représente 100 %) ***Combien de temps le codage a-t-il pris pendant ce PR avec Copilot ?***
1210

1311
(Exemple : le PR prendrait normalement 5 jours, mais n'a pris que 4 jours avec Copilot alors la réponse est 20 %)
1412
- [ ] 0%

issue_template/copilot-usage-pt.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ Para o Pull Request XXX:
66
- [ ] Não
77
- [ ] Sim
88

9-
2. Em comparação com sua experiência anterior de codificação SEM usar o Copilot (este número representa 100%)
10-
11-
***Quanto menos tempo demorou a codificação durante este PR com o Copilot?***
9+
2. Em comparação com sua experiência anterior de codificação SEM usar o Copilot (este número representa 100%) ***Quanto menos tempo demorou a codificação durante este PR com o Copilot?***
1210

1311
(Exemplo: o PR normalmente levaria 5 dias, mas demorou apenas 4 dias com o Copilot, então a resposta é 20%)
1412
- [ ] 0%

package-lock.json

Lines changed: 14 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,14 @@
1616
"test": "jest --detectOpenHandles"
1717
},
1818
"dependencies": {
19-
"@azure/ai-language-text": "^1.1.0",
20-
"@azure/ai-text-analytics": "^5.1.0",
21-
"ai-text-analytics": "^0.0.1-security",
22-
"applicationinsights": "^2.7.3",
23-
"csv-parse": "^5.5.2",
19+
"csv-parser": "^3.0.0",
2420
"dedent": "^1.5.1",
2521
"dotenv": "^16.3.1",
26-
"mssql": "^9.2.0",
2722
"probot": "^12.3.1"
2823
},
2924
"devDependencies": {
3025
"jest": "^29.6.4",
3126
"nock": "^13.3.3",
32-
"semver": ">= 7.5.4",
3327
"smee-client": "^1.2.2"
3428
},
3529
"engines": {

test/fixtures/issue_body.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ For Pull Request #44:
66
- [ ] No
77
- [ ] Yes
88

9-
2. Compared to your previous experience coding WITHOUT using Copilot (This number represents 100%)
10-
11-
***How much less time did the coding take during this PR with Copilot?***
9+
2. Compared to your previous experience coding WITHOUT using Copilot (This number represents 100%) ***How much less time did the coding take during this PR with Copilot?***
1210

1311
(Example: The PR would normally take 5 days, but only took 4 days with Copilot then the answer is 20%)
1412
- [ ] 0%

0 commit comments

Comments
 (0)