forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeReviewProvider.swift
More file actions
57 lines (50 loc) · 2.15 KB
/
CodeReviewProvider.swift
File metadata and controls
57 lines (50 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import ChatAPIService
import ConversationServiceProvider
import Foundation
import Logger
import GitHelper
public struct CodeReviewServiceProvider {
public var conversationServiceProvider: (any ConversationServiceProvider)?
}
public struct CodeReviewProvider {
public static func invoke(
_ request: CodeReviewRequest,
context: CodeReviewServiceProvider
) async -> (fileComments: [CodeReviewResponse.FileComment], errorMessage: String?) {
var fileComments: [CodeReviewResponse.FileComment] = []
var errorMessage: String?
do {
if let result = try await requestReviewChanges(request.fileChange.selectedChanges, context: context) {
for comment in result.comments {
guard let change = request.fileChange.selectedChanges.first(where: { $0.uri == comment.uri }) else {
continue
}
if let index = fileComments.firstIndex(where: { $0.uri == comment.uri }) {
var currentFileComments = fileComments[index]
currentFileComments.comments.append(comment)
fileComments[index] = currentFileComments
} else {
fileComments.append(
.init(uri: change.uri, originalContent: change.originalContent, comments: [comment])
)
}
}
}
} catch {
Logger.gitHubCopilot.error("Failed to review change: \(error)")
errorMessage = "Oops, failed to review changes."
}
return (fileComments, errorMessage)
}
private static func requestReviewChanges(
_ changes: [PRChange],
context: CodeReviewServiceProvider
) async throws -> CodeReviewResult? {
return try await context.conversationServiceProvider?
.reviewChanges(
changes.map {
.init(uri: $0.uri, path: $0.path, baseContent: $0.baseContent, headContent: $0.headContent)
}
)
}
}