1 | package edu.ucsb.cs156.frontiers.services; | |
2 | ||
3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
4 | import com.fasterxml.jackson.databind.ObjectMapper; | |
5 | import edu.ucsb.cs156.frontiers.entities.Course; | |
6 | import edu.ucsb.cs156.frontiers.errors.NoLinkedOrganizationException; | |
7 | import java.security.NoSuchAlgorithmException; | |
8 | import java.security.spec.InvalidKeySpecException; | |
9 | import java.util.Map; | |
10 | import lombok.extern.slf4j.Slf4j; | |
11 | import org.springframework.graphql.GraphQlResponse; | |
12 | import org.springframework.graphql.client.HttpSyncGraphQlClient; | |
13 | import org.springframework.http.HttpHeaders; | |
14 | import org.springframework.http.MediaType; | |
15 | import org.springframework.stereotype.Service; | |
16 | ||
17 | @Service | |
18 | @Slf4j | |
19 | public class GithubGraphQLService { | |
20 | ||
21 | private final HttpSyncGraphQlClient graphQlClient; | |
22 | ||
23 | private final JwtService jwtService; | |
24 | ||
25 | public GithubGraphQLService(HttpSyncGraphQlClient graphQlClient, JwtService jwtService) { | |
26 | this.jwtService = jwtService; | |
27 | this.graphQlClient = graphQlClient; | |
28 | } | |
29 | ||
30 | /** | |
31 | * Retrieves the name of the default branch for a given GitHub repository. | |
32 | * | |
33 | * @param owner The owner (username or organization) of the repository. | |
34 | * @param repo The name of the repository. | |
35 | * @return A Mono emitting the default branch name, or an empty Mono if not found. | |
36 | */ | |
37 | public String getDefaultBranchName(Course course, String owner, String repo) | |
38 | throws JsonProcessingException, | |
39 | NoSuchAlgorithmException, | |
40 | InvalidKeySpecException, | |
41 | NoLinkedOrganizationException { | |
42 | log.info( | |
43 | "getDefaultBranchName called with course.getId(): {} owner: {}, repo: {}", | |
44 | course.getId(), | |
45 | owner, | |
46 | repo); | |
47 | String githubToken = jwtService.getInstallationToken(course); | |
48 | ||
49 | log.info("githubToken: {}", githubToken); | |
50 | ||
51 | String query = | |
52 | """ | |
53 | query getDefaultBranch($owner: String!, $repo: String!) { | |
54 | repository(owner: $owner, name: $repo) { | |
55 | defaultBranchRef { | |
56 | name | |
57 | } | |
58 | } | |
59 | } | |
60 | """; | |
61 | ||
62 |
1
1. getDefaultBranchName : replaced return value with "" for edu/ucsb/cs156/frontiers/services/GithubGraphQLService::getDefaultBranchName → KILLED |
return graphQlClient |
63 | .mutate() | |
64 | .header("Authorization", "Bearer " + githubToken) | |
65 | .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) | |
66 | .build() | |
67 | .document(query) | |
68 | .variable("owner", owner) | |
69 | .variable("repo", repo) | |
70 | .retrieveSync("repository.defaultBranchRef.name") | |
71 | .toEntity(String.class); | |
72 | } | |
73 | ||
74 | public String getCommits( | |
75 | Course course, String owner, String repo, String branch, int first, String after) | |
76 | throws JsonProcessingException, | |
77 | NoSuchAlgorithmException, | |
78 | InvalidKeySpecException, | |
79 | NoLinkedOrganizationException { | |
80 | log.info( | |
81 | "getCommits called with course.getId(): {} owner: {}, repo: {}, branch: {}, first: {}, after: {}", | |
82 | course.getId(), | |
83 | owner, | |
84 | repo, | |
85 | branch, | |
86 | first, | |
87 | after); | |
88 | String githubToken = jwtService.getInstallationToken(course); | |
89 | ||
90 | log.info("githubToken: {}", githubToken); | |
91 | ||
92 | String query = | |
93 | """ | |
94 | query GetBranchCommits($owner: String!, $repo: String!, $branch: String!, $first: Int!, $after: String) { | |
95 | repository(owner: $owner, name: $repo) { | |
96 | ref(qualifiedName: $branch) { | |
97 | target { | |
98 | ... on Commit { | |
99 | history(first: $first, after: $after) { | |
100 | pageInfo { | |
101 | hasNextPage | |
102 | endCursor | |
103 | } | |
104 | edges { | |
105 | node { | |
106 | oid | |
107 | url | |
108 | messageHeadline | |
109 | committedDate | |
110 | author { | |
111 | name | |
112 | email | |
113 | user { | |
114 | login | |
115 | } | |
116 | } | |
117 | committer { | |
118 | name | |
119 | email | |
120 | user { | |
121 | login | |
122 | } | |
123 | } | |
124 | } | |
125 | } | |
126 | } | |
127 | } | |
128 | } | |
129 | } | |
130 | } | |
131 | } | |
132 | """; | |
133 | ||
134 | GraphQlResponse response = | |
135 | graphQlClient | |
136 | .mutate() | |
137 | .header("Authorization", "Bearer " + githubToken) | |
138 | .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) | |
139 | .build() | |
140 | .document(query) | |
141 | .variable("owner", owner) | |
142 | .variable("repo", repo) | |
143 | .variable("branch", branch) | |
144 | .variable("first", first) | |
145 | .variable("after", after) | |
146 | .executeSync(); | |
147 | ||
148 | Map<String, Object> data = response.getData(); | |
149 | String jsonData = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(data); | |
150 |
1
1. getCommits : replaced return value with "" for edu/ucsb/cs156/frontiers/services/GithubGraphQLService::getCommits → KILLED |
return jsonData; |
151 | } | |
152 | } | |
Mutations | ||
62 |
1.1 |
|
150 |
1.1 |