Skip to content

Commit

Permalink
Invoke Webhook after Deployment (if defined)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brutus5000 committed Feb 23, 2019
1 parent a9ce1f0 commit 080f83e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ before_install:
install:
- git clone https://github.com/FAForever/faf-stack.git faf-stack
&& pushd faf-stack
&& git checkout a1ef34cb64343225d6793e7af1d8fa0c93a7b3e6
&& git checkout 22bd4a3b8766106c2203cc3a0aa1eae155e72240
&& cp -r config.template config
&& ./scripts/init-db.sh
&& popd
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/faforever/api/data/domain/FeaturedMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class FeaturedMod {
private Boolean allowOverride;
private String fileExtension;
private String bireusUrl;
private String deploymentWebhook;

@Id
@Column(name = "id")
Expand Down Expand Up @@ -82,6 +83,11 @@ public String getFileExtension() {
return fileExtension;
}

@Column(name = "deployment_webhook ")
public String getDeploymentWebhook() {
return deploymentWebhook;
}

@Transient
@ComputedAttribute
// Enriched by FeaturedModEnricher
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import javax.transaction.Transactional;
import javax.transaction.Transactional.TxType;
Expand Down Expand Up @@ -62,17 +64,19 @@ public class LegacyFeaturedModDeploymentTask implements Runnable {
private final GitWrapper gitWrapper;
private final FeaturedModService featuredModService;
private final FafApiProperties apiProperties;
private final RestTemplate restTemplate;

@Setter
private FeaturedMod featuredMod;

@Setter
private Consumer<String> statusDescriptionListener;

public LegacyFeaturedModDeploymentTask(GitWrapper gitWrapper, FeaturedModService featuredModService, FafApiProperties apiProperties) {
public LegacyFeaturedModDeploymentTask(GitWrapper gitWrapper, FeaturedModService featuredModService, FafApiProperties apiProperties, RestTemplate restTemplate) {
this.gitWrapper = gitWrapper;
this.featuredModService = featuredModService;
this.apiProperties = apiProperties;
this.restTemplate = restTemplate;
}

@Override
Expand Down Expand Up @@ -112,10 +116,25 @@ public void run() {
files.forEach(this::finalizeFile);

updateDatabase(files, version, modName);
invokeDeploymentWebhook(featuredMod);

log.info("Deployment of '{}' version '{}' was successful", modName, version);
}

void invokeDeploymentWebhook(FeaturedMod featuredMod) {
if (featuredMod.getDeploymentWebhook() == null) {
log.debug("No deployment webhook configured.");
return;
}

log.debug("Invoking deployment webhook on: {}", featuredMod.getDeploymentWebhook());
try {
restTemplate.getForObject(featuredMod.getDeploymentWebhook(), String.class);
} catch (RestClientException e) {
log.error("Invoking webhook failed on: {}", featuredMod.getDeploymentWebhook(), e);
}
}

/**
* Creates a ForgedAlliance.exe which contains the specified version number, if the file is specified for the current
* featured mod.
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/config/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ faf-api:
challonge:
key: ${CHALLONGE_KEY:}
database:
schema-version: ${DATABASE_SCHEMA_VERSION:62}
schema-version: ${DATABASE_SCHEMA_VERSION:64}
mautic:
base-url: ${MAUTIC_BASE_URL:false}
client-id: ${MAUTIC_CLIENT_ID:false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.io.RandomAccessFile;
Expand All @@ -34,6 +36,7 @@
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
Expand All @@ -48,6 +51,9 @@ public class LegacyFeaturedModDeploymentTaskTest {
private GitWrapper gitWrapper;
@Mock
private FeaturedModService featuredModService;
@Mock
private RestTemplate restTemplate;

private FafApiProperties properties;

@Before
Expand All @@ -57,7 +63,7 @@ public void setUp() throws Exception {
deployment.setRepositoriesDirectory(repositoriesFolder.getRoot().getAbsolutePath());
deployment.setFeaturedModsTargetDirectory(targetFolder.getRoot().getAbsolutePath());

instance = new LegacyFeaturedModDeploymentTask(gitWrapper, featuredModService, properties);
instance = new LegacyFeaturedModDeploymentTask(gitWrapper, featuredModService, properties, restTemplate);
}

@Test(expected = IllegalStateException.class)
Expand Down Expand Up @@ -106,7 +112,8 @@ public void testRun() throws Exception {
.setFileExtension("nx3")
.setTechnicalName("faf")
.setAllowOverride(true)
.setGitUrl("git@example.com/FAForever/faf"));
.setGitUrl("git@example.com/FAForever/faf")
.setDeploymentWebhook("someUrl"));

Mockito.doAnswer(invocation -> {
Path repoFolder = invocation.getArgument(0);
Expand Down Expand Up @@ -137,6 +144,7 @@ public void testRun() throws Exception {

ArgumentCaptor<List<FeaturedModFile>> filesCaptor = ArgumentCaptor.forClass(List.class);
verify(featuredModService).save(eq("faf"), eq((short) 1337), filesCaptor.capture());
verify(restTemplate).getForObject("someUrl", String.class);

List<FeaturedModFile> files = filesCaptor.getValue();
files.sort(Comparator.comparing(FeaturedModFile::getFileId));
Expand All @@ -155,6 +163,33 @@ public void testRun() throws Exception {
assertThat(Files.exists(targetFolder.getRoot().toPath().resolve("updates_faf_files/ForgedAlliance.1337.exe")), is(true));
}

@Test
public void testInvokeDeploymentWebhookSkipped() {
instance.invokeDeploymentWebhook(new FeaturedMod());
verifyNoMoreInteractions(restTemplate);
}

@Test
public void testInvokeDeploymentWebhookInvokedSuccess() {
String someUrl = "someUrl";
instance.invokeDeploymentWebhook(new FeaturedMod().setDeploymentWebhook(someUrl));

verify(restTemplate).getForObject(someUrl, String.class);
verifyNoMoreInteractions(restTemplate);
}

@Test
public void testInvokeDeploymentWebhookResilience() {
String someUrl = "someUrl";

when(restTemplate.getForObject(anyString(), eq(String.class))).thenThrow(new RestClientException("error"));

instance.invokeDeploymentWebhook(new FeaturedMod().setDeploymentWebhook(someUrl));

verify(restTemplate).getForObject(someUrl, String.class);
verifyNoMoreInteractions(restTemplate);
}

private void createDummyExe(Path file) throws IOException {
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file.toFile(), "rw")) {
randomAccessFile.setLength(12_444_928);
Expand Down

0 comments on commit 080f83e

Please sign in to comment.