Skip to content

Commit

Permalink
fix current status in gitlab (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcanoy authored May 17, 2022
1 parent b15654e commit 4034ce6
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ public List<Engagement> getEngagements(PageFilter pageFilter, Set<String> region
String query = "";
Map<String, Object> params = new HashMap<>();

if(regions.size() > 0) {
if(!regions.isEmpty()) {
query = REGION;
params.put("region", regions);
}

if(types.size() > 0) {
if(!types.isEmpty()) {
query = query.length() > 0 ? query + " and " + TYPE : TYPE;
params.put("engagementType", types);
}
Expand Down Expand Up @@ -113,7 +113,7 @@ private Query queryEngagements(Set<String> regions, Set<String> types, String ca
String query = "";
Map<String, Object> params = new HashMap<>();

if(regions.size() > 0) {
if(!regions.isEmpty()) {
query = REGION;
params.put("region", regions);
}
Expand All @@ -123,7 +123,7 @@ private Query queryEngagements(Set<String> regions, Set<String> types, String ca
params.put("category", category);
}

if(types.size() > 0) {
if(!types.isEmpty()) {
query = query.length() > 0 ? query + " and " + TYPE : TYPE;
params.put("engagementType", types);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,11 @@ public long refreshCategories(List<Engagement> engagements) {
String content = new String(file.getDecodedContentAsBytes(), StandardCharsets.UTF_8);
List<Category> categories = json.fromJson(content, Category.class);
if(categories == null) {
LOGGER.error("Category null " + e.getUuid());
LOGGER.error("Category null {}", e.getUuid());
} else {
categoryService.refresh(categories);
categoryCount += categories.size();
}
categoryService.refresh(categories);
categoryCount += categories.size();
} catch (GitLabApiException ex) {
if(ex.getHttpStatus() != 404) {
throw new EngagementGitlabException(ex.getHttpStatus(), ex.getReason(), "Engagement File Not Retrieved " + e.getProjectId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.eclipse.microprofile.rest.client.inject.*;

import javax.ws.rs.*;
import javax.ws.rs.core.*;
import java.util.*;

@Retry(maxRetries = 5, delay = 1200, retryOn = NoHttpResponseException.class, abortOn = WebApplicationException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ public boolean update(Engagement engagement, boolean updateGitlab, boolean categ

if (diff.hasChanges() || initialFieldUpdated) {
updated = true;

if(engagement.getCurrentState() != engagement.getState()) {
engagement.setCurrentState(engagement.getState());
bus.publish(UPDATE_STATUS, engagement);
}

engagementRepository.update(engagement);

if (updateGitlab) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import java.util.*;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import com.google.gson.*;
import com.redhat.labs.lodestar.engagements.model.EngagementState;
import com.redhat.labs.lodestar.engagements.utils.JsonMarshaller;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.gitlab4j.api.models.Group;
Expand Down Expand Up @@ -47,6 +49,19 @@ public class GitlabService {
String lodestarTagFormat;

Gson gson = new GsonBuilder().setPrettyPrinting().create();

List<String> statusPossibilities;

@PostConstruct
void setPossibleStatuses() {
statusPossibilities = new ArrayList<>();

for(EngagementState state : EngagementState.values()) {
statusPossibilities.add(String.format(lodestarTagFormat, state));
}

LOGGER.debug("states {}", statusPossibilities);
}

/**
* Creates:
Expand Down Expand Up @@ -163,24 +178,11 @@ public void updateWebhooks(String message) {
public void updateStatus(Engagement engagement) {
Optional<Project> p = gitlabApiClient.getProject(engagement.getProjectId());
if(p.isPresent()) {
boolean isSet = false;
List<String> tags = p.get().getTagList();
String stateTag = String.format(lodestarTagFormat, engagement.getCurrentState());
tags.removeAll(statusPossibilities);

for(int i=0; i<tags.size(); i++) {
if(tags.get(i).equals(stateTag)) {
tags.set(i, stateTag);
isSet = true;
}
}

if(!tags.contains(lodestarTag)) {
tags.add(lodestarTag);
}

if(!isSet) {
tags.add(stateTag);
}
String stateTag = String.format(lodestarTagFormat, engagement.getCurrentState());
tags.add(stateTag);

gitlabApiClient.updateProject(p.get());
}
Expand Down Expand Up @@ -236,8 +238,7 @@ private void changeName(Project existing, Engagement engagement) {
if(currentCustomerGroupOption.isEmpty()) {
throw new EngagementException(String.format("Current customer group was not found %s", currentPath));
}

Group engagementCurrentGroup = currentEngagementGroupOption.get();

Group customerCurrentGroup = currentCustomerGroupOption.get();

boolean customerChanged = !customerCurrentGroup.getName().equals(engagement.getName()) &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.redhat.labs.lodestar.engagements.utils;

import com.mongodb.client.model.Sorts;
import io.quarkus.panache.common.Sort;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down Expand Up @@ -74,7 +73,6 @@ public Bson getBsonSort() {
}
direction = sortFields.length == 2 ? sortFields[1] : "";
querySort.add(isDescending(direction) ? descending(sortFields[0]) : ascending(sortFields[0]));
orderBy(querySort);
}

querySort.add(ascending("uuid"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ void testDBCheck() {
assertEquals(2, repository.findAll().stream().count());
}

@Test
void testLastUpdate() {
engagementService.checkLastUpdate();

List<Engagement> engagements = engagementService.getEngagements();
Engagement engagement = engagements.iterator().next();
String uuid = engagement.getUuid();
engagement.setLastUpdate(null);
repository.update(engagement);

engagementService.checkLastUpdate();
engagement = engagementService.getEngagement(uuid).orElse(null);
assertTrue(engagement.getLastUpdate() != null);
}

@Test
void testStatusTimer() {
String uuid = "status-change";
Expand Down

0 comments on commit 4034ce6

Please sign in to comment.