Skip to content

Commit 3085220

Browse files
committed
Add GitHub survey update functionality and new survey route
1 parent 9fbfe6b commit 3085220

File tree

5 files changed

+71
-44
lines changed

5 files changed

+71
-44
lines changed

backend/src/controllers/survey.controller.ts

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,16 @@ import app from '../index.js';
66

77
class SurveyController {
88
async createSurvey(req: Request, res: Response): Promise<void> {
9-
let survey: Survey;
109
try {
11-
const _survey = await surveyService.updateSurvey({
10+
const survey = await surveyService.createSurvey({
1211
...req.body,
1312
status: 'completed'
1413
})
15-
if (!_survey) throw new Error('Survey not found');
16-
survey = _survey;
1714
res.status(201).json(survey);
1815
} catch (error) {
1916
res.status(500).json(error);
2017
return;
2118
}
22-
try {
23-
const { installation, octokit } = await app.github.getInstallation(survey.org);
24-
const surveyUrl = new URL(`copilot/surveys/${survey.id}`, app.baseUrl);
25-
26-
if (!survey.repo || !survey.org || !survey.prNumber) {
27-
logger.warn('Cannot process survey comment: missing survey data');
28-
return;
29-
}
30-
const comments = await octokit.rest.issues.listComments({
31-
owner: survey.org,
32-
repo: survey.repo,
33-
issue_number: survey.prNumber
34-
});
35-
const comment = comments.data.find(comment => comment.user?.login.startsWith(installation.app_slug));
36-
if (comment) {
37-
octokit.rest.issues.updateComment({
38-
owner: survey.org,
39-
repo: survey.repo,
40-
comment_id: comment.id,
41-
body: `Thanks for filling out the [copilot survey](${surveyUrl.toString()}) @${survey.userId}!`
42-
});
43-
} else {
44-
logger.info(`No comment found for survey from ${survey.org}`);
45-
}
46-
} catch (error) {
47-
logger.error('Error updating survey comment', error);
48-
throw error;
49-
}
5019
}
5120

5221
async getAllSurveys(req: Request, res: Response): Promise<void> {
@@ -96,6 +65,50 @@ class SurveyController {
9665
}
9766
}
9867

68+
async updateSurveyGitHub(req: Request, res: Response): Promise<void> {
69+
let survey: Survey;
70+
try {
71+
const _survey = await surveyService.updateSurvey({
72+
...req.body,
73+
status: 'completed'
74+
})
75+
if (!_survey) throw new Error('Survey not found');
76+
survey = _survey;
77+
res.status(201).json(survey);
78+
} catch (error) {
79+
res.status(500).json(error);
80+
return;
81+
}
82+
try {
83+
const { installation, octokit } = await app.github.getInstallation(survey.org);
84+
const surveyUrl = new URL(`copilot/surveys/${survey.id}`, app.baseUrl);
85+
86+
if (!survey.repo || !survey.org || !survey.prNumber) {
87+
logger.warn('Cannot process survey comment: missing survey data');
88+
return;
89+
}
90+
const comments = await octokit.rest.issues.listComments({
91+
owner: survey.org,
92+
repo: survey.repo,
93+
issue_number: survey.prNumber
94+
});
95+
const comment = comments.data.find(comment => comment.user?.login.startsWith(installation.app_slug));
96+
if (comment) {
97+
octokit.rest.issues.updateComment({
98+
owner: survey.org,
99+
repo: survey.repo,
100+
comment_id: comment.id,
101+
body: `Thanks for filling out the [copilot survey](${surveyUrl.toString()}) @${survey.userId}!`
102+
});
103+
} else {
104+
logger.info(`No comment found for survey from ${survey.org}`);
105+
}
106+
} catch (error) {
107+
logger.error('Error updating survey comment', error);
108+
throw error;
109+
}
110+
}
111+
99112
async deleteSurvey(req: Request, res: Response): Promise<void> {
100113
try {
101114
const { id } = req.params;

backend/src/routes/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ router.get('/', (req: Request, res: Response) => {
1717
router.get('/survey', surveyController.getAllSurveys);
1818
router.post('/survey', surveyController.createSurvey);
1919
router.get('/survey/:id', surveyController.getSurveyById);
20-
router.put('/survey/:id', surveyController.updateSurvey);
20+
router.put('/survey/:id', surveyController.updateSurvey); // put github survey logic here
2121
router.delete('/survey/:id', surveyController.deleteSurvey);
22+
router.put('/survey/:id/github', surveyController.updateSurveyGitHub);
2223

2324
router.get('/usage', usageController.getUsage);
2425

frontend/src/app/app.routes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const routes: Routes = [
3131
{ path: 'copilot/seats', component: CopilotSeatsComponent, title: 'Seats' },
3232
{ path: 'copilot/seats/:id', component: CopilotSeatComponent, title: 'Seat' },
3333
{ path: 'copilot/surveys', component: CopilotSurveysComponent, title: 'Surveys' },
34+
{ path: 'copilot/surveys/new', component: NewCopilotSurveyComponent, title: 'New Survey' },
3435
{ path: 'copilot/surveys/new/:id', component: NewCopilotSurveyComponent, title: 'New Survey' },
3536
{ path: 'copilot/surveys/:id', component: CopilotSurveyComponent, title: 'Survey' },
3637
{ path: 'copilot/predictive-modeling', component: PredictiveModelingComponent, title: 'Predictive Modeling' },

frontend/src/app/main/copilot/copilot-surveys/new-copilot-survey/new-copilot-survey.component.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Component, forwardRef, OnInit } from '@angular/core';
22
import { AppModule } from '../../../../app.module';
33
import { FormBuilder, FormControl, FormGroup, NG_VALUE_ACCESSOR, Validators } from '@angular/forms';
44
import { CopilotSurveyService } from '../../../../services/api/copilot-survey.service';
5-
import { ActivatedRoute, Params } from '@angular/router';
5+
import { ActivatedRoute, Params, Router } from '@angular/router';
66

77
@Component({
88
selector: 'app-copilot-survey',
@@ -38,7 +38,8 @@ export class NewCopilotSurveyComponent implements OnInit {
3838
constructor(
3939
private fb: FormBuilder,
4040
private copilotSurveyService: CopilotSurveyService,
41-
private route: ActivatedRoute
41+
private route: ActivatedRoute,
42+
private router: Router
4243
) {
4344
const id = Number(this.route.snapshot.paramMap.get('id'));
4445
this.id = isNaN(id) ? 0 : id
@@ -156,7 +157,7 @@ export class NewCopilotSurveyComponent implements OnInit {
156157

157158
onSubmit() {
158159
const { org, repo, prNumber } = this.parseGitHubPRUrl(this.params['url']);
159-
this.copilotSurveyService.createSurvey({
160+
const survey = {
160161
id: this.id,
161162
userId: this.params['author'],
162163
org,
@@ -166,14 +167,21 @@ export class NewCopilotSurveyComponent implements OnInit {
166167
percentTimeSaved: Number(this.surveyForm.value.percentTimeSaved),
167168
reason: this.surveyForm.value.reason,
168169
timeUsedFor: this.surveyForm.value.timeUsedFor
169-
}).subscribe(() => {
170-
const redirectUrl = this.params['url'];
171-
if (redirectUrl && redirectUrl.startsWith('https://github.com/')) {
172-
window.location.href = redirectUrl;
173-
} else {
174-
console.error('Unauthorized URL:', redirectUrl);
175-
}
176-
});
170+
};
171+
if (!this.id) {
172+
this.copilotSurveyService.createSurvey(survey).subscribe(() => {
173+
this.router.navigate(['/copilot/surveys']);
174+
});
175+
} else {
176+
this.copilotSurveyService.createSurveyGitHub(survey).subscribe(() => {
177+
const redirectUrl = this.params['url'];
178+
if (redirectUrl && redirectUrl.startsWith('https://github.com/')) {
179+
window.location.href = redirectUrl;
180+
} else {
181+
console.error('Unauthorized URL:', redirectUrl);
182+
}
183+
});
184+
}
177185
}
178186

179187
formatPercent(value: number): string {

frontend/src/app/services/api/copilot-survey.service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export class CopilotSurveyService {
3030
return this.http.post(this.apiUrl, survey);
3131
}
3232

33+
createSurveyGitHub(survey: Survey) {
34+
return this.http.post(`${this.apiUrl}/${survey.id}/github`, survey);
35+
}
36+
3337
getAllSurveys(org?: string) {
3438
return this.http.get<Survey[]>(this.apiUrl, {
3539
params: org ? { org } : undefined

0 commit comments

Comments
 (0)