Skip to content

Commit

Permalink
trigger new build
Browse files Browse the repository at this point in the history
  • Loading branch information
gy2006 committed Jan 6, 2025
1 parent b8958b8 commit dfd0904
Show file tree
Hide file tree
Showing 15 changed files with 183 additions and 35 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/flowci/build/business/CreateBuild.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.flowci.build.business;

import com.flowci.build.model.Build;
import com.flowci.common.model.Variables;

public interface CreateBuild {
Build invoke(Long flowId, Build.Trigger trigger, Variables inputs);
}
16 changes: 0 additions & 16 deletions src/main/java/com/flowci/build/business/CreateNewBuild.java

This file was deleted.

7 changes: 7 additions & 0 deletions src/main/java/com/flowci/build/business/FetchYamlFromGit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.flowci.build.business;

import com.flowci.build.model.Build;

public interface FetchYamlFromGit {
void invoke(Build build);
}
5 changes: 5 additions & 0 deletions src/main/java/com/flowci/build/business/WaitForAgent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.flowci.build.business;

public interface WaitForAgent {
void invoke(Long buildId);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.flowci.build.business.impl;

import com.flowci.build.business.CreateNewBuild;
import com.flowci.build.business.CreateBuild;
import com.flowci.build.business.TriggerBuild;
import com.flowci.build.model.Build;
import com.flowci.build.model.BuildYaml;
import com.flowci.build.repo.BuildRepo;
Expand All @@ -22,7 +23,7 @@
@Slf4j
@Component
@AllArgsConstructor
public class CreateNewBuildImpl implements CreateNewBuild {
public class CreateBuildImpl implements CreateBuild {

private final FetchFlow fetchFlow;
private final FetchFlowYamlContent fetchFlowYamlContent;
Expand All @@ -43,7 +44,7 @@ public Build invoke(Long flowId, Build.Trigger trigger, Variables inputs) {
: new HashSet<>(yamlObj.getAgents());

var build = new Build();
build.setFlowId(flowId);
build.setFlowId(flow.getId());
build.setTrigger(trigger);
build.setStatus(Build.Status.CREATED);
build.setAgentTags(agentTags.toArray(new String[0]));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.flowci.build.business.impl;

import com.flowci.build.business.FetchYamlFromGit;
import com.flowci.build.model.Build;
import org.springframework.stereotype.Component;

@Component
public class FetchYamlFromGitImpl implements FetchYamlFromGit {
@Override
public void invoke(Build build) {
// TODO:
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package com.flowci.build.business.impl;

import com.flowci.build.business.CreateNewBuild;
import com.flowci.build.business.CreateBuild;
import com.flowci.build.business.TriggerBuild;
import com.flowci.build.business.WaitForAgent;
import com.flowci.build.model.Build;
import com.flowci.common.model.Variables;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Component
@AllArgsConstructor
public class TriggerBuildImpl implements TriggerBuild {

private final CreateNewBuild createNewBuild;
private final CreateBuild createBuild;
private final WaitForAgent waitForAgent;

@Override
@Transactional
public Build invoke(Long flowId, Build.Trigger trigger, Variables inputs) {
var build = createNewBuild.invoke(flowId, trigger, inputs);

// put to queue, and wait

return null;
var build = createBuild.invoke(flowId, trigger, inputs);
waitForAgent.invoke(build.getId());
return build;
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/flowci/build/business/impl/WaitForAgentImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.flowci.build.business.impl;

import com.flowci.build.business.WaitForAgent;
import com.flowci.build.model.Build;
import com.flowci.build.repo.BuildRepo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@AllArgsConstructor
public class WaitForAgentImpl implements WaitForAgent {

private final BuildRepo buildRepo;

@Override
public void invoke(Long buildId) {
buildRepo.updateBuildStatusById(buildId, Build.Status.QUEUED);
}
}
6 changes: 4 additions & 2 deletions src/main/java/com/flowci/build/model/Build.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import com.flowci.common.model.EntityBase;
import io.hypersistence.utils.hibernate.type.array.StringArrayType;
import io.hypersistence.utils.hibernate.type.json.JsonType;
import jakarta.annotation.Nullable;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.Type;
import org.hibernate.generator.EventType;

import java.util.Set;
Expand Down Expand Up @@ -78,8 +80,8 @@ public enum Status {
@org.hibernate.annotations.Type(StringArrayType.class)
private String[] agentTags;

@Nullable
private String commitHash;
@Type(JsonType.class)
private GitRef gitRef;

/**
* Assigned agent id
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/flowci/build/model/BuildYaml.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.flowci.common.model.EntityBase;
import com.flowci.common.model.Variables;
import io.hypersistence.utils.hibernate.type.json.JsonType;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
Expand All @@ -20,7 +19,7 @@ public class BuildYaml extends EntityBase {
@Id
private Long id;

// ref to flow variables
// ref to flow variables and extra inputs
@Type(JsonType.class)
private Variables variables;

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/flowci/build/model/GitRef.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.flowci.build.model;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class GitRef {

private String commitHash;
}
8 changes: 8 additions & 0 deletions src/main/java/com/flowci/build/repo/BuildRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@

import com.flowci.build.model.Build;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public interface BuildRepo extends JpaRepository<Build, Long> {

@Transactional
@Modifying(clearAutomatically = true, flushAutomatically = true)
@Query("update Build b set b.status = ?2 where b.id= ?1")
void updateBuildStatusById(Long buildId, Build.Status status);
}
2 changes: 1 addition & 1 deletion src/main/resources/db/migration/V1__Init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ CREATE TABLE build
trigger varchar(20) NOT NULL,
status varchar(20) NOT NULL,
agent_tags varchar(20)[] NOT NULL,
commit_hash varchar(40),
git_ref json,
agent_id BIGINT,
created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL,
created_by BIGINT,
Expand Down
74 changes: 74 additions & 0 deletions src/test/java/com/flowci/build/business/CreateBuildTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.flowci.build.business;

import com.flowci.SpringTest;
import com.flowci.build.model.Build;
import com.flowci.build.model.BuildYaml;
import com.flowci.common.model.Variables;
import com.flowci.flow.business.FetchFlow;
import com.flowci.flow.business.FetchFlowYamlContent;
import com.flowci.flow.model.Flow;
import com.flowci.flow.model.FlowYaml;
import com.flowci.yaml.business.ParseYamlV2;
import com.flowci.yaml.model.FlowV2;
import org.instancio.Instancio;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;

import static com.flowci.TestUtils.newDummyInstance;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

class CreateBuildTest extends SpringTest {

@MockBean
private FetchFlow fetchFlow;

@MockBean
private FetchFlowYamlContent fetchFlowYamlContent;

@MockBean
private ParseYamlV2 parseYamlV2;

@Autowired
private MockRepositoriesConfig mockRepositoriesConfig;

@Autowired
private CreateBuild createBuild;

@Test
void givenFlow_whenCreating_thenBuildIsCreated() {
var mockFlow = newDummyInstance(Flow.class).create();
when(fetchFlow.invoke(anyLong())).thenReturn(mockFlow);
var mockFlowYaml = newDummyInstance(FlowYaml.class).create();
when(fetchFlowYamlContent.invoke(anyLong())).thenReturn(mockFlowYaml.getYaml());
when(parseYamlV2.invoke(anyString())).thenReturn(Instancio.of(FlowV2.class).create());

var mockBuildRepo = mockRepositoriesConfig.getBuildRepo();
var buildCaptor = ArgumentCaptor.forClass(Build.class);
when(mockBuildRepo.save(buildCaptor.capture()))
.thenAnswer(opt -> opt.getArgument(0));

var mockBuildYamlRepo = mockRepositoriesConfig.getBuildYamlRepo();
var buildYamlCaptor = ArgumentCaptor.forClass(BuildYaml.class);
when(mockBuildYamlRepo.save(buildYamlCaptor.capture()))
.thenAnswer(opt -> opt.getArgument(0));

var inputs = new Variables();
inputs.put("v1", "hello");
inputs.put("v2", "world");
createBuild.invoke(1L, Build.Trigger.API, inputs);

var build = buildCaptor.getValue();
assertEquals(mockFlow.getId(), build.getFlowId());
assertEquals(Build.Trigger.API, build.getTrigger());

var buildYaml = buildYamlCaptor.getValue();
assertEquals("hello", buildYaml.getVariables().get("v1"));
assertEquals("world", buildYaml.getVariables().get("v2"));
assertEquals(mockFlowYaml.getYaml(), buildYaml.getYaml());
}
}
21 changes: 19 additions & 2 deletions src/test/java/com/flowci/build/repo/BuildRepoTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.flowci.build.repo;


import com.flowci.SpringTestWithDB;
import com.flowci.build.model.Build;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static com.flowci.TestUtils.newDummyInstance;
import static org.instancio.Select.field;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class BuildRepoTest extends SpringTestWithDB {
Expand All @@ -29,6 +29,23 @@ void givenBuild_whenSaving_thenBuildIsSaved() {
var saved = buildRepo.save(build);
assertNotNull(saved.getBuildDate());
assertNotNull(saved.getBuildSequence());
assertNotNull(saved.getBuildSequence());
}

@Test
void giveBuild_whenUpdateStatus_thenStatusIsUpdated() {
var mockFlowId = 100L;
var build = newDummyInstance(Build.class)
.set(field(Build::getFlowId), mockFlowId)
.ignore(field(Build::getId))
.ignore(field(Build::getBuildDate))
.ignore(field(Build::getBuildSequence))
.ignore(field(Build::getBuildAlias))
.create();

var saved = buildRepo.save(build);
assertNotNull(saved.getId());

buildRepo.updateBuildStatusById(saved.getId(), Build.Status.SUCCESS);
assertEquals(Build.Status.SUCCESS, buildRepo.findById(saved.getId()).get().getStatus());
}
}

0 comments on commit dfd0904

Please sign in to comment.