GithubGraphQLService.java

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
    // language=GraphQL
52
    String query =
53
        """
54
                                query getDefaultBranch($owner: String!, $repo: String!) {
55
                                  repository(owner: $owner, name: $repo) {
56
                                    defaultBranchRef {
57
                                      name
58
                                    }
59
                                  }
60
                                }
61
                                """;
62
63 1 1. getDefaultBranchName : replaced return value with "" for edu/ucsb/cs156/frontiers/services/GithubGraphQLService::getDefaultBranchName → KILLED
    return graphQlClient
64
        .mutate()
65
        .header("Authorization", "Bearer " + githubToken)
66
        .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
67
        .build()
68
        .document(query)
69
        .variable("owner", owner)
70
        .variable("repo", repo)
71
        .retrieveSync("repository.defaultBranchRef.name")
72
        .toEntity(String.class);
73
  }
74
75
  public String getCommits(
76
      Course course, String owner, String repo, String branch, int first, String after)
77
      throws JsonProcessingException,
78
          NoSuchAlgorithmException,
79
          InvalidKeySpecException,
80
          NoLinkedOrganizationException {
81
    log.info(
82
        "getCommits called with course.getId(): {} owner: {}, repo: {}, branch: {}, first: {}, after: {}",
83
        course.getId(),
84
        owner,
85
        repo,
86
        branch,
87
        first,
88
        after);
89
    String githubToken = jwtService.getInstallationToken(course);
90
91
    log.info("githubToken: {}", githubToken);
92
    // language=GraphQL
93
    String query =
94
        """
95
            query GetBranchCommits($owner: String!, $repo: String!, $branch: String!, $first: Int!, $after: String) {
96
              repository(owner: $owner, name: $repo) {
97
                ref(qualifiedName: $branch) {
98
                  target {
99
                    ... on Commit {
100
                      history(first: $first, after: $after) {
101
                        pageInfo {
102
                          hasNextPage
103
                          endCursor
104
                        }
105
                        edges {
106
                          node {
107
                            oid
108
                            url
109
                            messageHeadline
110
                            committedDate
111
                            author {
112
                              name
113
                              email
114
                              user {
115
                                login
116
                              }
117
                            }
118
                            committer {
119
                              name
120
                              email
121
                              user {
122
                                login
123
                              }
124
                            }
125
                          }
126
                        }
127
                      }
128
                    }
129
                  }
130
                }
131
              }
132
            }
133
            """;
134
135
    GraphQlResponse response =
136
        graphQlClient
137
            .mutate()
138
            .header("Authorization", "Bearer " + githubToken)
139
            .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
140
            .build()
141
            .document(query)
142
            .variable("owner", owner)
143
            .variable("repo", repo)
144
            .variable("branch", branch)
145
            .variable("first", first)
146
            .variable("after", after)
147
            .executeSync();
148
149
    Map<String, Object> data = response.getData();
150
    String jsonData = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(data);
151 1 1. getCommits : replaced return value with "" for edu/ucsb/cs156/frontiers/services/GithubGraphQLService::getCommits → KILLED
    return jsonData;
152
  }
153
}

Mutations

63

1.1
Location : getDefaultBranchName
Killed by : edu.ucsb.cs156.frontiers.services.GithubGraphQLServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.GithubGraphQLServiceTests]/[method:testGetDefaultBranchName()]
replaced return value with "" for edu/ucsb/cs156/frontiers/services/GithubGraphQLService::getDefaultBranchName → KILLED

151

1.1
Location : getCommits
Killed by : edu.ucsb.cs156.frontiers.services.GithubGraphQLServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.GithubGraphQLServiceTests]/[method:testGetCommits()]
replaced return value with "" for edu/ucsb/cs156/frontiers/services/GithubGraphQLService::getCommits → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0