Skip to content
This repository was archived by the owner on Oct 2, 2023. It is now read-only.

Commit d46e9ea

Browse files
committed
Added GitHubController, updated core version,
removed RestOperationsSupplier to fix restClient implementation, logging improvements with total record count,
1 parent 225c564 commit d46e9ea

6 files changed

Lines changed: 75 additions & 30 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<repository.name>hygieia-scm-github-graphql-collector</repository.name>
5151
<apache.rat.plugin.version>0.13</apache.rat.plugin.version>
5252
<bc.version>3.0.1</bc.version>
53-
<com.capitalone.dashboard.core.version>3.7.24</com.capitalone.dashboard.core.version>
53+
<com.capitalone.dashboard.core.version>3.9.5</com.capitalone.dashboard.core.version>
5454
<commons.codec.version>1.14</commons.codec.version>
5555
<commons.io.version>2.4</commons.io.version>
5656
<commons.lang.version>3.10</commons.lang.version>

src/main/java/com/capitalone/dashboard/collector/GitHubCollectorTask.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,23 +168,22 @@ public void collect(Collector collector){
168168
enabledRepos = enabledRepos.stream().filter(repo -> GithubRepoMatcher.orgNameMatcher(repo.getRepoUrl(), searchCriteria[1])).collect(Collectors.toList());
169169
}
170170
}
171+
LOG.info("GitHubCollectorTask:collect start, total enabledRepos=" + enabledRepos.size());
172+
LOG.warn("error threshold = " + gitHubSettings.getErrorThreshold());
171173
collectProcess(collector, enabledRepos );
174+
172175
}
173176

174177

175178

176179
@SuppressWarnings({"PMD.AvoidDeeplyNestedIfStmts"})
177180
public void collectProcess(Collector collector, List<GitHubRepo> enabledRepos) {
178-
179-
logBanner("Starting...");
180181
long start = System.currentTimeMillis();
181182
int repoCount = 0;
182183
int commitCount = 0;
183184
int pullCount = 0;
184185
int issueCount = 0;
185186

186-
LOG.info("GitHubCollectorTask:collect start, total enabledRepos=" + enabledRepos.size());
187-
LOG.warn("error threshold = " + gitHubSettings.getErrorThreshold());
188187
for (GitHubRepo repo : enabledRepos) {
189188
repoCount++;
190189
String repoUrl = repo==null?"null":(repo.getRepoUrl() + "/tree/" + repo.getBranch());
@@ -281,6 +280,8 @@ public void collectProcess(Collector collector, List<GitHubRepo> enabledRepos) {
281280
long elapsedSeconds = (end - start) / 1000;
282281
LOG.info(String.format("GitHubCollectorTask:collect stop, totalProcessSeconds=%d, totalRepoCount=%d, totalNewPulls=%d, totalNewCommits=%d totalNewIssues=%d",
283282
elapsedSeconds, repoCount, pullCount, commitCount, issueCount));
283+
284+
collector.setLastExecutionRecordCount(repoCount+pullCount+commitCount+issueCount);
284285
}
285286

286287

src/main/java/com/capitalone/dashboard/collector/RestOperationsSupplier.java

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.capitalone.dashboard.controller;
2+
3+
import com.capitalone.dashboard.collector.GitHubCollectorTask;
4+
import com.capitalone.dashboard.misc.HygieiaException;
5+
import com.capitalone.dashboard.model.Collector;
6+
import com.capitalone.dashboard.model.GitHubRepo;
7+
import com.capitalone.dashboard.repository.BaseCollectorRepository;
8+
import com.capitalone.dashboard.repository.GitHubRepoRepository;
9+
import org.apache.commons.collections.CollectionUtils;
10+
import org.apache.commons.logging.Log;
11+
import org.apache.commons.logging.LogFactory;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.http.HttpStatus;
14+
import org.springframework.http.ResponseEntity;
15+
import org.springframework.validation.annotation.Validated;
16+
import org.springframework.web.bind.annotation.RequestMapping;
17+
import org.springframework.web.bind.annotation.RestController;
18+
19+
import javax.validation.Valid;
20+
import java.util.List;
21+
import java.util.Objects;
22+
23+
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
24+
import static org.springframework.web.bind.annotation.RequestMethod.GET;
25+
26+
@RestController
27+
@Validated
28+
@RequestMapping("/github")
29+
public class GitHubController {
30+
private static final Log LOG = LogFactory.getLog(GitHubController.class);
31+
32+
private final BaseCollectorRepository<Collector> collectorRepository;
33+
private final GitHubRepoRepository gitHubRepoRepository;
34+
private final GitHubCollectorTask gitHubCollectorTask;
35+
private static final String GITHUB_COLLECTOR_NAME = "GitHub";
36+
37+
@Autowired
38+
public GitHubController(BaseCollectorRepository<Collector> collectorRepository,
39+
GitHubRepoRepository gitHubRepoRepository,
40+
GitHubCollectorTask gitHubCollectorTask) {
41+
this.collectorRepository = collectorRepository;
42+
this.gitHubRepoRepository = gitHubRepoRepository;
43+
this.gitHubCollectorTask = gitHubCollectorTask;
44+
}
45+
46+
@RequestMapping(value = "/refresh", method = GET, produces = APPLICATION_JSON_VALUE)
47+
public ResponseEntity<String> refresh(@Valid String url) throws HygieiaException {
48+
if (Objects.isNull(url)) return ResponseEntity.status(HttpStatus.OK).body("URL is null");
49+
Collector collector = collectorRepository.findByName(GITHUB_COLLECTOR_NAME);
50+
if (Objects.isNull(collector))
51+
return ResponseEntity.status(HttpStatus.OK).body(GITHUB_COLLECTOR_NAME + " collector is not found");
52+
List<GitHubRepo> repos = gitHubRepoRepository.findByCollectorIdAndUrl(collector.getId(), url);
53+
if (CollectionUtils.isEmpty(repos))
54+
return ResponseEntity.status(HttpStatus.OK).body("No GitHub repos found for URL: " + url);
55+
int count = 0;
56+
gitHubCollectorTask.collectProcess(collector, repos);
57+
58+
return ResponseEntity
59+
.status(HttpStatus.OK)
60+
.body(GITHUB_COLLECTOR_NAME + " refresh completed successfully for repo URL: " + url
61+
+ " Records updated: " + collector.getLastExecutionRecordCount());
62+
}
63+
64+
}

src/main/java/com/capitalone/dashboard/repository/GitHubRepoRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ public interface GitHubRepoRepository extends BaseCollectorItemRepository<GitHub
1010

1111
@Query(value="{ 'collectorId' : ?0, enabled: true}")
1212
List<GitHubRepo> findEnabledGitHubRepos(ObjectId collectorId);
13+
14+
@Query(value="{ 'collectorId' : ?0, options.url : ?1}")
15+
List<GitHubRepo> findByCollectorIdAndUrl(ObjectId collectorId, String url);
1316
}

src/test/java/com/capitalone/dashboard/collector/DefaultGitHubClientTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.capitalone.dashboard.collector;
22

33
import com.capitalone.dashboard.client.RestClient;
4+
import com.capitalone.dashboard.client.RestOperationsSupplier;
45
import com.capitalone.dashboard.misc.HygieiaException;
56
import com.capitalone.dashboard.model.GitHubRepo;
67
import com.capitalone.dashboard.model.GitHubParsed;
78
import com.capitalone.dashboard.collector.DefaultGitHubClient.RedirectedStatus;
8-
import com.capitalone.dashboard.util.Supplier;
99
import org.junit.Before;
1010
import org.junit.Test;
1111
import org.junit.runner.RunWith;
@@ -27,7 +27,7 @@
2727
public class DefaultGitHubClientTest {
2828

2929
@Mock
30-
private Supplier<RestOperations> restOperationsSupplier;
30+
private RestOperationsSupplier restOperationsSupplier;
3131
@Mock private RestOperations rest;
3232
private GitHubSettings settings;
3333
private DefaultGitHubClient defaultGitHubClient;

0 commit comments

Comments
 (0)