-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
249 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/org/qualityannotate/api/coderepository/CodeStructuredExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.qualityannotate.api.coderepository; | ||
|
||
import org.qualityannotate.api.coderepository.api.CodeStructuredApi; | ||
import org.qualityannotate.api.qualitytool.MetricsAndIssues; | ||
|
||
public final class CodeStructuredExecutor { | ||
private CodeStructuredExecutor() { | ||
|
||
} | ||
|
||
public static void run(CodeStructuredApi codeStructuredApi, MetricsAndIssues metricsAndIssues) throws Exception { | ||
codeStructuredApi.update(metricsAndIssues); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
src/main/java/org/qualityannotate/api/coderepository/api/CodeApi.java
This file was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
src/main/java/org/qualityannotate/api/coderepository/api/CodeStructuredApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.qualityannotate.api.coderepository.api; | ||
|
||
import java.io.IOException; | ||
|
||
import org.qualityannotate.api.qualitytool.MetricsAndIssues; | ||
|
||
/** | ||
* Abstraction for the API of Code repositories like Gitlab, github, bitbucket, etc. | ||
* Can be used best for APIs which are structured and know about issues with severity. So github-checks | ||
* api and bitbucket-report api are well suited. | ||
*/ | ||
public interface CodeStructuredApi { | ||
void update(MetricsAndIssues metricsAndIssues) throws IOException; | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/org/qualityannotate/api/coderepository/api/CodeTextApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.qualityannotate.api.coderepository.api; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Abstraction for the API of Code repositories like Gitlab, github, bitbucket, etc. | ||
* Can be used best for APIs which are only text-based. So github-checks api and bitbucket-report api should use a | ||
* different interface. | ||
*/ | ||
public interface CodeTextApi { | ||
Optional<CodeTextMainComment> getMainComment() throws IOException; | ||
|
||
void createMainComment(String comment) throws IOException; | ||
|
||
List<CodeTextFileComment> listFileComments() throws IOException; | ||
|
||
void createFileComment(String file, Integer line, String comment) throws IOException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/main/java/org/qualityannotate/coderepo/github/client/GithubStructuredApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package org.qualityannotate.coderepo.github.client; | ||
|
||
import org.kohsuke.github.*; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.qualityannotate.api.coderepository.api.CodeStructuredApi; | ||
import org.qualityannotate.api.qualitytool.Issue; | ||
import org.qualityannotate.api.qualitytool.MetricsAndIssues; | ||
import org.qualityannotate.coderepo.github.GithubConfig; | ||
import org.qualityannotate.core.CommentProcessor; | ||
|
||
public class GithubStructuredApi implements CodeStructuredApi { | ||
public static final String NAME = "qualityannotate"; | ||
private final GitHubBuilder gitHubBuilder; | ||
private final String project; | ||
private final int pullRequestId; | ||
/** | ||
* Guaranteed to be not null by {@link #init()} | ||
*/ | ||
private GitHub github = null; | ||
|
||
public GithubStructuredApi(GithubConfig config) { | ||
gitHubBuilder = new GitHubBuilder().withOAuthToken(config.token()); | ||
this.project = config.project(); | ||
this.pullRequestId = config.pullRequest(); | ||
} | ||
|
||
private static Optional<GHCheckRun> findCheckRun(GHRepository repository, GHPullRequest pullRequest) | ||
throws IOException { | ||
PagedIterator<GHCheckRun> iterator = repository.getCheckRuns(pullRequest.getHead().getSha()).iterator(); | ||
while (iterator.hasNext()) { | ||
GHCheckRun next = iterator.next(); | ||
if (next.getName().equals(NAME)) { | ||
return Optional.of(next); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
private void init() throws IOException { | ||
if (github == null) { | ||
github = gitHubBuilder.build(); | ||
github.checkApiUrlValidity(); | ||
} | ||
} | ||
|
||
@Override | ||
public void update(MetricsAndIssues metricsAndIssues) throws IOException { | ||
init(); | ||
GHRepository repository = github.getRepository(project); | ||
GHPullRequest pullRequest = repository.getPullRequest(pullRequestId); | ||
Optional<GHCheckRun> checkRunOpt = findCheckRun(repository, pullRequest); | ||
if (checkRunOpt.isPresent()) { | ||
GHCheckRun checkRun = checkRunOpt.get(); | ||
GHCheckRunBuilder checkRunBuilder = checkRun.update(); | ||
updateCheckRun(checkRunBuilder, metricsAndIssues); | ||
} else { | ||
GHCheckRunBuilder checkRunBuilder = repository.createCheckRun(NAME, pullRequest.getHead().getSha()); | ||
updateCheckRun(checkRunBuilder, metricsAndIssues); | ||
} | ||
} | ||
|
||
private void updateCheckRun(GHCheckRunBuilder checkRunBuilder, MetricsAndIssues metricsAndIssues) | ||
throws IOException { | ||
checkRunBuilder.withDetailsURL(metricsAndIssues.globalMetrics().url()); | ||
checkRunBuilder.withStatus(GHCheckRun.Status.COMPLETED); | ||
checkRunBuilder.withConclusion(GHCheckRun.Conclusion.NEUTRAL); | ||
GHCheckRunBuilder.Output output = new GHCheckRunBuilder.Output("Result", | ||
CommentProcessor.createGlobalComment(metricsAndIssues.globalMetrics()).markdown()); | ||
List<Issue> issues = metricsAndIssues.issues(); | ||
for (Issue issue : issues) { | ||
output.add(new GHCheckRunBuilder.Annotation(issue.fileName(), issue.lineNumber(), | ||
mapSeverity(issue.severityEnum()), CommentProcessor.createComment(issue).comment().markdown())); | ||
} | ||
checkRunBuilder.add(output); | ||
checkRunBuilder.create(); | ||
} | ||
|
||
private GHCheckRun.AnnotationLevel mapSeverity(Issue.Severity severity) { | ||
return switch (severity) { | ||
case LOW -> GHCheckRun.AnnotationLevel.NOTICE; | ||
case MEDIUM -> GHCheckRun.AnnotationLevel.WARNING; | ||
case HIGH -> GHCheckRun.AnnotationLevel.FAILURE; | ||
}; | ||
} | ||
} |
Oops, something went wrong.