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

Commit

Permalink
added endpoint to remedy issue of Hygieia reporting the wrong repo (#82)
Browse files Browse the repository at this point in the history
* added endpoint to fix gitRequests with the wrong collectorItemId and added logging

* added endpoint to fix gitRequests with the wrong collectorItemId and added logging update

* Increment POM
  • Loading branch information
dcanar9 authored Jun 27, 2022
1 parent 31dc0b8 commit cc42335
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</description>
<url>https://github.com/Hygieia/${repository.name}</url>
<packaging>jar</packaging>
<version>3.3.13-SNAPSHOT</version>
<version>3.3.14-SNAPSHOT</version>


<parent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ public void fireGraphQL(GitHubRepo repo, boolean firstRun, Map<Long, String> exi
dummyCommitPaging.setLastPage(false);

JSONObject query = buildQuery(true, firstRun, false, gitHubParsed, repo, dummyCommitPaging, dummyPRPaging, dummyIssuePaging, offSetMinutes);
LOG.info(String.format("Repo=%s, FireGrapQL BuildQuery String=%s", repo.getRepoUrl(), query.toString()));
int loopCount = 1;
while (!alldone) {
LOG.debug(String.format("Executing loop %d for %s/%s", loopCount, gitHubParsed.getOrgName(), gitHubParsed.getRepoName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,11 +476,14 @@ private int processPRorIssueList(GitHubRepo repo, List<GitRequest> existingList,
pullNumbers.add("...");
}
}
if (existing == null) {
entry.setCollectorItemId(repo.getId());
count++;
} else {
if (existing != null) {
LOG.info(String.format("Existing PR - collectorItemId=%s, RepoURL=%s, RepoBranch=%s\nEntryURL=%s, EntryBranch=%s", repo.getId().toString(), repo.getRepoUrl(), repo.getBranch(), entry.getScmUrl(), entry.getScmBranch()));
entry.setId(existing.getId());
}else {
count++;
LOG.info(String.format("Non Existing PR - collectorItemId=%s, RepoURL=%s, RepoBranch=%s\nEntryURL=%s, EntryBranch=%s", repo.getId().toString(), repo.getRepoUrl(), repo.getBranch(), entry.getScmUrl(), entry.getScmBranch()));
}
if (repo.getRepoUrl().equalsIgnoreCase(entry.getScmUrl()) && repo.getBranch().equalsIgnoreCase(entry.getScmBranch())){
entry.setCollectorItemId(repo.getId());
}
gitRequestRepository.save(entry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.capitalone.dashboard.model.webhook.github.GitHubRepo;
import com.capitalone.dashboard.repository.BaseCollectorRepository;
import com.capitalone.dashboard.repository.GitHubRepoRepository;
import com.capitalone.dashboard.request.SyncPRRequest;
import com.capitalone.dashboard.service.GitHubService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
Expand All @@ -14,6 +15,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -23,6 +25,7 @@

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@RestController
@Validated
Expand Down Expand Up @@ -69,4 +72,9 @@ public ResponseEntity<String> refresh(@Valid String url) throws HygieiaException
public ResponseEntity<String> cleanup() throws HygieiaException {
return gitHubService.cleanup();
}

@RequestMapping(value = "/syncPullRequest", consumes = APPLICATION_JSON_VALUE,method = POST, produces = APPLICATION_JSON_VALUE)
public ResponseEntity<String> syncPullRequest(@Valid @RequestBody SyncPRRequest request) {
return gitHubService.syncPullRequest(request.getTitle(), request.getRepoUrl(), request.getBranch(), request.getLimit());
}
}
41 changes: 41 additions & 0 deletions src/main/java/com/capitalone/dashboard/request/SyncPRRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.capitalone.dashboard.request;

public class SyncPRRequest extends BaseRequest {
private String title;
private String repoUrl;
private String branch;
private int limit;


public String getRepoUrl() {
return repoUrl;
}

public void setRepoUrl(String repoUrl) {
this.repoUrl = repoUrl;
}

public String getBranch() {
return branch;
}

public void setBranch(String branch) {
this.branch = branch;
}

public int getLimit() {
return limit;
}

public void setLimit(int limit) {
this.limit = limit;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

public interface GitHubService {
ResponseEntity<String> cleanup();

ResponseEntity<String> syncPullRequest(String title, String repo, String branch, int limit);
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,46 @@
package com.capitalone.dashboard.service;

import com.capitalone.dashboard.collector.GitHubCollectorTask;
import com.capitalone.dashboard.model.GitHubCollector;
import com.capitalone.dashboard.model.*;
import com.capitalone.dashboard.model.webhook.github.GitHubRepo;
import com.capitalone.dashboard.repository.BaseCollectorRepository;
import com.capitalone.dashboard.repository.GitHubRepoRepository;
import com.capitalone.dashboard.repository.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Component
public class GitHubServiceImpl implements GitHubService {
private static final Log LOG = LogFactory.getLog(GitHubServiceImpl.class);

private final BaseCollectorRepository<GitHubCollector> collectorRepository;
private final GitHubRepoRepository gitHubRepoRepository;
private final GitRequestRepository gitRequestRepository;
private final ComponentRepository componentRepository;
private final DashboardRepository dashboardRepository;
private final CollectorItemRepository collectorItemRepository;
private static final String GITHUB_COLLECTOR_NAME = "GitHub";

@Autowired
public GitHubServiceImpl(BaseCollectorRepository<GitHubCollector> collectorRepository,
GitHubRepoRepository gitHubRepoRepository,
GitHubRepoRepository gitHubRepoRepository, GitRequestRepository gitRequestRepository,
ComponentRepository componentRepository, DashboardRepository dashboardRepository,
CollectorItemRepository collectorItemRepository,
GitHubCollectorTask gitHubCollectorTask) {
this.collectorRepository = collectorRepository;
this.gitHubRepoRepository = gitHubRepoRepository;
this.gitRequestRepository = gitRequestRepository;
this.componentRepository = componentRepository;
this.dashboardRepository = dashboardRepository;
this.collectorItemRepository = collectorItemRepository;
}

public ResponseEntity<String> cleanup() {
Expand All @@ -46,4 +57,53 @@ public ResponseEntity<String> cleanup() {
.status(HttpStatus.OK)
.body(GITHUB_COLLECTOR_NAME + " cleanup - " + count + " obsolete GitHub repo's deleted. ");
}

public ResponseEntity<String> syncPullRequest(String title, String repoUrl, String branch, int limit){
ObjectId githubCollectorId = collectorRepository.findByName(GITHUB_COLLECTOR_NAME).getId();

// get the collector item id from the collectorItems should result in only one
CollectorItem collectorItem = collectorItemRepository.findRepoByUrlAndBranch(githubCollectorId, repoUrl, branch);
if(Objects.isNull(collectorItem)){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(String.format("Unable to find collector item for repo: %s", repoUrl));
}
ObjectId collectorItemID = collectorItem.getId();

// go to that collectorItemID in components and get all the SCM urls
List<Dashboard> dashboard = dashboardRepository.findByTitle(title);
if(dashboard.isEmpty()){
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(String.format("Unable to find dashboard for title: %s", title));
}
ObjectId componentId = dashboard.get(0).getApplication().getComponents().get(0).getId();

com.capitalone.dashboard.model.Component component = componentRepository.findOne(componentId);
if(Objects.isNull(component)){
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(String.format("Unable to find component with componentId: %s", componentId));
}
//to lower case so we can ignore case when comparing for gitRequestsToFix
List<String> SCMs = component.getCollectorItems().get(CollectorType.SCM).stream().map(scm -> scm.getOptions().get("url").toString().toLowerCase()).collect(Collectors.toList());

// get all git requests with collectorItemID
List<GitRequest> allGitRequests = gitRequestRepository.findByCollectorItemIdAndRequestType(collectorItemID, "pull");
if (allGitRequests.isEmpty()){
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(String.format("Unable to find any gitRequests with the collectorItemId: %s", collectorItem));
}

// filter list of gitRequests, if scmUrl not in SCMs list add that gitRequest to the list so we can fix later
List<GitRequest> gitRequestsToFix = allGitRequests.stream().filter(GR -> !SCMs.contains(GR.getScmUrl().toLowerCase())).collect(Collectors.toList());

// iterate through the git requests with the wrong collectorItemId and correct them
for (GitRequest gr: gitRequestsToFix) {
CollectorItem collItem = collectorItemRepository.findRepoByUrlAndBranch(githubCollectorId, gr.getScmUrl(), gr.getScmBranch());
if (Objects.isNull(collItem)){
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(String.format("Unable to find collector item for repo: %s", repoUrl));
}
gr.setCollectorItemId(collItem.getId());
gitRequestRepository.save(gr);
LOG.info(String.format("GitRequest with wrong collectorItemId: %s \tcorrectCollectorItemId: %s", gr.getScmUrl(), collItem.getId().toString()));
}
String responseString = String.format("Successfully corrected the following gitRequests with URLs: %s", gitRequestsToFix.stream().
map(gr -> gr.getScmUrl().toString()).collect(Collectors.joining(", ")));
return ResponseEntity.status(HttpStatus.OK).body(responseString);

}
}

0 comments on commit cc42335

Please sign in to comment.