Skip to content

Commit

Permalink
Merge pull request #118 from mcanoy/archived-projects
Browse files Browse the repository at this point in the history
allow hooks to be updated after archive if needed
  • Loading branch information
mcanoy authored Feb 8, 2021
2 parents 7d4913c + a29c908 commit acee9e1
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/redhat/labs/lodestar/models/ConfigMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import java.nio.file.attribute.FileTime;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -18,6 +21,7 @@
@NoArgsConstructor
@AllArgsConstructor
public class ConfigMap {
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigMap.class);

private Path path;
private String filePath;
Expand All @@ -43,6 +47,7 @@ public boolean updateMountedFile() {
content = Optional.of(new String(Files.readAllBytes(path), StandardCharsets.UTF_8));
return true;
} catch (IOException e) {
LOGGER.error("Error updating mounted file %{} {} {} ", lastModifiedTime, path, filePath);
content = Optional.empty();
}

Expand All @@ -68,6 +73,7 @@ private boolean isModified() {
return true;
}
} catch (IOException e) {
LOGGER.error("Error calculating isModified %{} {}", lastModifiedTime, path);
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,11 @@ public class HookConfig {

@JsonbProperty("token")
private String token;

/**
* Should the webhook be enabled after an engagement is archived
*/
@JsonbProperty("enabledAfterArchive")
private boolean enabledAfterArchive;

}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void updateWebHooksInGitLab() {
List<Project> projects = projectService.getProjectsByGroup(engagementRepositoryId, true);
LOGGER.debug("number of projects found: {}", projects.size());
projects.stream().filter(project -> project.getName().equals(IAC))
.filter(project -> !engagementIsArchived(project)).forEach(project -> {
.forEach(project -> {

Integer projectId = project.getId();

Expand All @@ -131,9 +131,12 @@ void updateWebHooksInGitLab() {

// remove existing webhooks for project
hookService.deleteProjectHooks(projectId);

boolean isArchived = isEngagementArchived(project);

// create hooks from configuration
hookConfigs.stream().forEach(hookC -> {
hookConfigs.stream().filter(config -> config.isEnabledAfterArchive() || !isArchived)
.forEach(hookC -> {
Hook hook = Hook.builder().projectId(projectId).pushEvents(true).url(hookC.getBaseUrl())
.token(hookC.getToken()).build();
LOGGER.debug("\tcreating webhook {}", hook.getUrl());
Expand All @@ -154,7 +157,7 @@ void updateWebHooksInGitLab() {
* @param project
* @return
*/
boolean engagementIsArchived(Project project) {
boolean isEngagementArchived(Project project) {

Optional<Engagement> engagement = engagementService.getEngagement(project, false, false);
if (engagement.isPresent() && null != engagement.get().getArchiveDate()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public Response createProjectHook(Integer projectId, Hook hook) {
return Response.status(Status.CREATED).build();
}

return null;
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
}

@Override
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/com/redhat/labs/lodestar/model/ProjectTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.redhat.labs.lodestar.model;

import java.util.ArrayList;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import com.redhat.labs.lodestar.models.gitlab.Project;

class ProjectTest {

@Test
void testPreserve() {

Project project = new Project();
Assertions.assertNull(project.getTagList());

project.setTagList(new ArrayList<>());
Assertions.assertEquals(0, project.getTagList().size());

project.preserve();

Assertions.assertEquals(1, project.getTagList().size());
Assertions.assertEquals("DO_NOT_DELETE", project.getTagList().get(0));

//Handle 2nd call properly
project.preserve();

Assertions.assertEquals(1, project.getTagList().size());
Assertions.assertEquals("DO_NOT_DELETE", project.getTagList().get(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,15 @@ void testGetHookFileSuccess() {
.body(is("\n[\n" +
" {\n" +
" \"baseUrl\": \"https://labs.com/webhooks/\",\n" +
" \"enabledAfterArchive\": false,\n" +
" \"name\": \"labs\",\n" +
" \"pushEvent\": true,\n" +
" \"pushEventsBranchFilter\": \"master\",\n" +
" \"token\": \"abc\"\n" +
" },\n" +
" {\n" +
" \"baseUrl\": \"https://rht.com/hooks/\",\n" +
" \"enabledAfterArchive\": true,\n" +
" \"name\": \"rht\",\n" +
" \"pushEvent\": true,\n" +
" \"pushEventsBranchFilter\": \"master\",\n" +
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/webhooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
pushEvent: true
pushEventsBranchFilter: master
token: def
enabledAfterArchive: true

0 comments on commit acee9e1

Please sign in to comment.