Skip to content

Commit 5e59bc4

Browse files
committed
Adding a new branch for results.csv and updating packages
1 parent 2139c21 commit 5e59bc4

File tree

5 files changed

+107
-1610
lines changed

5 files changed

+107
-1610
lines changed

.env.example

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,4 @@ PRIVATE_KEY=
44
WEBHOOK_SECRET=development
55

66
# Use `trace` to get verbose logging or `info` to show less
7-
LOG_LEVEL=debug
8-
9-
LANGUAGE_API_ENDPOINT=
10-
LANGUAGE_API_KEY=
11-
APPLICATIONINSIGHTS_CONNECTION_STRING=
12-
DATABASE_CONNECTION_STRING=
7+
LOG_LEVEL=debug

index.js

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const dedent = require("dedent");
77
const fs = require("fs");
88
const csvParser = require("csv-parser");
99
const stream = require('stream');
10+
const BranchName = "copilot-survey-engine-results";
1011
require("dotenv").config();
1112

1213
module.exports = (app) => {
@@ -72,7 +73,7 @@ module.exports = (app) => {
7273
// save comment body if present
7374
let comment = null;
7475
if(context.payload.comment) {
75-
comment = context.payload.comment.body;
76+
comment = context.payload.comment.body.replace(/\n/g, ' ').trim();
7677
}
7778

7879
// find regex [0-9]\+ in issue_body and get first result
@@ -199,6 +200,7 @@ module.exports = (app) => {
199200
owner: context.payload.repository.owner.login,
200201
repo: context.payload.repository.name,
201202
path: "results.csv",
203+
ref: BranchName,
202204
});
203205

204206
// If the file exists, get its contents
@@ -247,14 +249,16 @@ module.exports = (app) => {
247249

248250
app.log.info("CSV String:\n" + resultString);
249251

250-
// commit the file to the repo
252+
// Commit the file to the repo in a new branch
253+
await createBranch(context);
251254
await context.octokit.repos.createOrUpdateFileContents({
252255
owner: context.payload.repository.owner.login,
253256
repo: context.payload.repository.name,
254257
path: "results.csv",
255258
message: 'Update results.csv',
256-
content: Buffer.from(resultString).toString('base64'),
259+
content: Buffer.from(resultString).toString('base64'),
257260
sha: file.data.sha,
261+
branch: BranchName,
258262
});
259263

260264
app.log.info("File updated successfully\n " + Buffer.from(resultString).toString('base64'));
@@ -265,18 +269,61 @@ module.exports = (app) => {
265269
if (error.status === 404) {
266270
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'
267271
+ Object.values(fileContent).join(',');
272+
await createBranch(context);
268273
await context.octokit.repos.createOrUpdateFileContents({
269274
owner: context.payload.repository.owner.login,
270275
repo: context.payload.repository.name,
271276
path: "results.csv",
272277
message: 'initial commit',
273278
content: Buffer.from(completeData).toString('base64'),
279+
branch: BranchName,
274280
});
275-
}else{
281+
} else {
276282
app.log.error(error);
277283
}
278284
}
279285

286+
async function createBranch(context) {
287+
// Step 1: Get reference to the default branch
288+
let ref;
289+
try {
290+
// Try to get the 'main' branch
291+
const { data: mainRef } = await context.octokit.git.getRef({
292+
owner: context.payload.repository.owner.login,
293+
repo: context.payload.repository.name,
294+
ref: 'heads/main',
295+
});
296+
ref = mainRef;
297+
} catch (error) {
298+
// If 'main' branch does not exist, try to get the 'master' branch
299+
if (error.status === 404) {
300+
const { data: masterRef } = await context.octokit.git.getRef({
301+
owner: context.payload.repository.owner.login,
302+
repo: context.payload.repository.name,
303+
ref: 'heads/master',
304+
});
305+
ref = masterRef;
306+
} else {
307+
app.log.error(error);
308+
}
309+
}
310+
311+
// Step 2: Create a new branch from the default branch
312+
try {
313+
await context.octokit.git.createRef({
314+
owner: context.payload.repository.owner.login,
315+
repo: context.payload.repository.name,
316+
ref: `refs/heads/${BranchName}`,
317+
sha: ref.object.sha,
318+
});
319+
} catch (error) {
320+
if (error.status === 422) {
321+
app.log.info(`Branch ${BranchName} already exists`);
322+
} else {
323+
app.log.error(error);
324+
}
325+
}
326+
}
280327

281328
}
282329

0 commit comments

Comments
 (0)