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

Commit 1ce222d

Browse files
committed
Merge branch 'master' into update-dependencies
2 parents 256f486 + 1321a83 commit 1ce222d

3 files changed

Lines changed: 77 additions & 8 deletions

File tree

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,25 +171,25 @@ public void collect(Collector collector){
171171
enabledRepos = enabledRepos.stream().filter(repo -> GithubRepoMatcher.orgNameMatcher(repo.getRepoUrl(), searchCriteria[1])).collect(Collectors.toList());
172172
}
173173
}
174+
LOG.info("GitHubCollectorTask:collect start, total enabledRepos=" + enabledRepos.size());
175+
LOG.warn("error threshold = " + gitHubSettings.getErrorThreshold());
174176
collectProcess(collector, enabledRepos );
177+
175178
}
176179

177180

178181
@SuppressWarnings({"PMD.AvoidDeeplyNestedIfStmts"})
179182
public void collectProcess(Collector collector, List<GitHubRepo> enabledRepos) {
180-
181-
logBanner("Starting...");
182183
long start = System.currentTimeMillis();
183184
int repoCount = 0;
184185
int commitCount = 0;
185186
int pullCount = 0;
186187
int issueCount = 0;
187188
count.set(0);
188189

189-
LOG.info("GitHubCollectorTask:collect start, total enabledRepos=" + enabledRepos.size());
190-
LOG.warn("error threshold = " + gitHubSettings.getErrorThreshold());
191190
for (GitHubRepo repo : enabledRepos) {
192191
repoCount++;
192+
long repoStart = System.currentTimeMillis();
193193
String repoUrl = repo==null?"null":(repo.getRepoUrl() + "/tree/" + repo.getBranch());
194194
String statusString = "UNKNOWN";
195195
long lastUpdated = repo==null?0:repo.getLastUpdated();
@@ -241,7 +241,7 @@ public void collectProcess(Collector collector, List<GitHubRepo> enabledRepos) {
241241
repo.setLastUpdated(System.currentTimeMillis());
242242
// if everything went alright, there should be no error!
243243
repo.getErrors().clear();
244-
statusString = "SUCCESS, pulls=" + pullCount1 + ", commits=" + pullCount1 + ", issues=" + issueCount1;
244+
statusString = "SUCCESS, pulls=" + pullCount1 + ", commits=" + commitCount1 + ", issues=" + issueCount1;
245245
} catch (HttpStatusCodeException hc) {
246246
LOG.error("Error fetching commits for:" + repo.getRepoUrl(), hc);
247247
statusString = "EXCEPTION, " + hc.getClass().getCanonicalName();
@@ -275,16 +275,19 @@ public void collectProcess(Collector collector, List<GitHubRepo> enabledRepos) {
275275
LOG.error("Unexpected exception when collecting url=" + repoUrl, e);
276276
} finally {
277277
String age = readableAge(lastUpdated, start);
278-
LOG.info(String.format("%d of %d, repository=%s, lastUpdated=%d [%s], status=%s",
279-
repoCount, enabledRepos.size(), repoUrl, lastUpdated, age, statusString));
280-
278+
long repoEnd = System.currentTimeMillis();
279+
long repoProcSeconds = (repoEnd - repoStart) / 1000;
280+
LOG.info(String.format("%d of %d, repository=%s, itemProcessSeconds=%d, lastUpdated=%d [%s], status=%s",
281+
repoCount, enabledRepos.size(), repoUrl, repoProcSeconds, lastUpdated, age, statusString));
281282
}
282283
}
283284
long end = System.currentTimeMillis();
284285
long elapsedSeconds = (end - start) / 1000;
285286
count.set(commitCount);
286287
LOG.info(String.format("GitHubCollectorTask:collect stop, totalProcessSeconds=%d, totalRepoCount=%d, totalNewPulls=%d, totalNewCommits=%d totalNewIssues=%d",
287288
elapsedSeconds, repoCount, pullCount, commitCount, issueCount));
289+
290+
collector.setLastExecutionRecordCount(repoCount+pullCount+commitCount+issueCount);
288291
}
289292

290293

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
public class GitHubController {
29+
private static final Log LOG = LogFactory.getLog(GitHubController.class);
30+
31+
private final BaseCollectorRepository<Collector> collectorRepository;
32+
private final GitHubRepoRepository gitHubRepoRepository;
33+
private final GitHubCollectorTask gitHubCollectorTask;
34+
private static final String GITHUB_COLLECTOR_NAME = "GitHub";
35+
36+
@Autowired
37+
public GitHubController(BaseCollectorRepository<Collector> collectorRepository,
38+
GitHubRepoRepository gitHubRepoRepository,
39+
GitHubCollectorTask gitHubCollectorTask) {
40+
this.collectorRepository = collectorRepository;
41+
this.gitHubRepoRepository = gitHubRepoRepository;
42+
this.gitHubCollectorTask = gitHubCollectorTask;
43+
}
44+
45+
@RequestMapping(value = "/refresh", method = GET, produces = APPLICATION_JSON_VALUE)
46+
public ResponseEntity<String> refresh(@Valid String url) throws HygieiaException {
47+
if (Objects.isNull(url)) return ResponseEntity.status(HttpStatus.OK).body("URL is null");
48+
Collector collector = collectorRepository.findByName(GITHUB_COLLECTOR_NAME);
49+
if (Objects.isNull(collector))
50+
return ResponseEntity.status(HttpStatus.OK).body(GITHUB_COLLECTOR_NAME + " collector is not found");
51+
List<GitHubRepo> repos = gitHubRepoRepository.findByCollectorIdAndUrl(collector.getId(), url);
52+
if (CollectionUtils.isEmpty(repos))
53+
return ResponseEntity.status(HttpStatus.OK).body("No GitHub repos found for URL: " + url);
54+
int count = 0;
55+
gitHubCollectorTask.collectProcess(collector, repos);
56+
57+
return ResponseEntity
58+
.status(HttpStatus.OK)
59+
.body(GITHUB_COLLECTOR_NAME + " refresh completed successfully for repo URL: " + url
60+
+ " Records updated: " + collector.getLastExecutionRecordCount());
61+
}
62+
63+
}

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
}

0 commit comments

Comments
 (0)