Skip to content

Commit

Permalink
add build repository with auto generated build date,sequence and alise
Browse files Browse the repository at this point in the history
  • Loading branch information
gy2006 committed Dec 22, 2024
1 parent f4c7f9c commit fe1acc0
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/main/java/com/flowci/build/business/TriggerBuild.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.flowci.build.business;

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

public interface TriggerBuild {
/**
* Trigger a new build
*
* @param flowId flow id
* @param trigger trigger
* @param inputs extra variables
* @return build object
*/
Build invoke(Long flowId, Build.Trigger trigger, Variables inputs);
}
28 changes: 28 additions & 0 deletions src/main/java/com/flowci/build/business/impl/TriggerBuildImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.flowci.build.business.impl;

import com.flowci.build.business.TriggerBuild;
import com.flowci.build.model.Build;
import com.flowci.build.repo.BuildRepo;
import com.flowci.build.repo.BuildYamlRepo;
import com.flowci.common.model.Variables;
import com.flowci.flow.business.FetchFlow;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

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

private final FetchFlow fetchFlow;

private final BuildRepo buildRepo;

private final BuildYamlRepo buildYamlRepo;

@Override
public Build invoke(Long flowId, Build.Trigger trigger, Variables inputs) {
return null;
}
}
82 changes: 82 additions & 0 deletions src/main/java/com/flowci/build/model/Build.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.flowci.build.model;

import com.flowci.common.model.EntityBase;
import jakarta.annotation.Nullable;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.Generated;
import org.hibernate.generator.EventType;

import java.util.Set;

@Data
@EqualsAndHashCode(callSuper = false, of = {"id"})
@Entity
@Table(name = "build")
public class Build extends EntityBase {

public enum Trigger {
MANUAL,
API,
SCHEDULED,
GIT_PUSH,
GIT_PR_OPEN,
GIT_PR_CLOSE,
GIT_TAG,
}

public enum Status {
CREATED, // init status
LOADING, // when need to loading yaml from git
QUEUED, // been put to job queue and waiting for agent
ASSIGNED, // assigned to an agent
RUNNING, // agent start to execute the flow
CANCELLING, // will be cancelled, but waiting for response from agent
SUCCESS,
FAILURE,
CANCELLED,
TIMEOUT
}

public static final Set<Status> FINAL_STATUS = Set.of(
Status.SUCCESS,
Status.FAILURE,
Status.CANCELLED,
Status.TIMEOUT
);

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "build_id_gen")
@SequenceGenerator(name = "build_id_gen", sequenceName = "build_id_sequence", allocationSize = 1)
private Long id;

private Long flowId;

// auto generated from db, YYYYMMMDD as integer
@Generated(event = EventType.INSERT)
@Column(name = "build_date", insertable = false, updatable = false, nullable = false)
private Integer buildDate;

// auto generated from db, sequence ref to build date
@Generated(event = EventType.INSERT)
@Column(name = "build_sequence", insertable = false, updatable = false, nullable = false)
private Long buildSequence;

// auto generated from db, <buildDate>.<buildSequence>
@Generated(event = EventType.INSERT)
@Column(name = "build_alias", insertable = false, updatable = false, nullable = false)
private String buildAlias;

@Enumerated(EnumType.STRING)
private Trigger trigger;

@Enumerated(EnumType.STRING)
private Status status;

@Nullable
private String commitHash;

@Nullable
private Long agentId;
}
26 changes: 26 additions & 0 deletions src/main/java/com/flowci/build/model/BuildYaml.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.flowci.build.model;

import com.flowci.common.model.EntityBase;
import com.flowci.common.model.Variables;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false, of = "buildId")
@Entity
@Table(name = "build_yaml")
public class BuildYaml extends EntityBase {

@Id
private Long id;

// ref to flow variables
@Convert(converter = Variables.AttributeConverter.class)
private Variables variables;

private String yaml;
}
1 change: 1 addition & 0 deletions src/main/java/com/flowci/build/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package com.flowci.build;
9 changes: 9 additions & 0 deletions src/main/java/com/flowci/build/repo/BuildRepo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.flowci.build.repo;

import com.flowci.build.model.Build;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface BuildRepo extends JpaRepository<Build, Long> {
}
9 changes: 9 additions & 0 deletions src/main/java/com/flowci/build/repo/BuildYamlRepo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.flowci.build.repo;

import com.flowci.build.model.BuildYaml;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface BuildYamlRepo extends JpaRepository<BuildYaml, Long> {
}
67 changes: 64 additions & 3 deletions src/main/resources/db/migration/V1__Init.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
DROP SEQUENCE IF EXISTS flows_id_sequence;
CREATE SEQUENCE flows_id_sequence START 10000 INCREMENT 1;
-- flow related tables --

CREATE TABLE flows
(
Expand All @@ -16,6 +15,9 @@ CREATE TABLE flows
updated_by BIGINT
);

DROP SEQUENCE IF EXISTS flows_id_sequence;
CREATE SEQUENCE flows_id_sequence START 10000 INCREMENT 1 OWNED BY flows.id;

CREATE TABLE flows_yaml
(
id BIGINT PRIMARY KEY,
Expand All @@ -34,5 +36,64 @@ CREATE TABLE flows_user
created_by BIGINT,
updated_at TIMESTAMP WITHOUT TIME ZONE NOT NULL,
updated_by BIGINT,
PRIMARY KEY(flow_id, user_id)
PRIMARY KEY (flow_id, user_id)
);

-- build related tables --

CREATE TABLE build
(
id BIGSERIAL PRIMARY KEY,
flow_id BIGINT NOT NULL,
build_date INTEGER NOT NULL,
build_sequence BIGINT NOT NULL,
build_alias varchar(50) NOT NULL,
trigger varchar(20) NOT NULL,
status varchar(20) NOT NULL,
commit_hash varchar(40),
agent_id BIGINT,
created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL,
created_by BIGINT,
updated_at TIMESTAMP WITHOUT TIME ZONE NOT NULL,
updated_by BIGINT,
UNIQUE (flow_id, build_date, build_sequence)
);

DROP SEQUENCE IF EXISTS build_id_sequence;
CREATE SEQUENCE build_id_sequence START 10000 INCREMENT 1 OWNED BY build.id;

-- trigger function to create build_date, build_sequence and build_alias on insert
CREATE OR REPLACE FUNCTION auto_build_sequence() RETURNS trigger AS
$build_sequence$
BEGIN
SELECT to_char(current_date, 'YYYYMMDD')::INTEGER INTO NEW.build_date;

SELECT COALESCE(MAX(build_sequence) + 1, 1)
INTO NEW.build_sequence
FROM "build"
WHERE flow_id = NEW.flow_id
AND build_date = NEW.build_date;

SELECT concat(NEW.build_date, '.', NEW.build_sequence) INTO NEW.build_alias;

RETURN NEW;
END;
$build_sequence$ LANGUAGE plpgsql;

-- add trigger on insert
CREATE TRIGGER build_sequence_trigger
BEFORE INSERT
ON "build"
FOR EACH ROW
EXECUTE PROCEDURE auto_build_sequence();

CREATE TABLE build_yaml
(
id BIGINT PRIMARY KEY,
variables TEXT NOT NULL,
yaml TEXT NOT NULL,
created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL,
created_by BIGINT,
updated_at TIMESTAMP WITHOUT TIME ZONE NOT NULL,
updated_by BIGINT
);
1 change: 1 addition & 0 deletions src/test/java/com/flowci/SpringTestWithDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ static void setupDb(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgres::getJdbcUrl);
registry.add("spring.datasource.username", postgres::getUsername);
registry.add("spring.datasource.password", postgres::getPassword);
registry.add("spring.jpa.show-sql", () -> "true");
}
}
34 changes: 34 additions & 0 deletions src/test/java/com/flowci/build/repo/BuildRepoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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.assertNotNull;

class BuildRepoTest extends SpringTestWithDB {

@Autowired
private BuildRepo buildRepo;

@Test
void givenBuild_whenSaving_thenBuildIsSaved() {
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.getBuildDate());
assertNotNull(saved.getBuildSequence());
assertNotNull(saved.getBuildSequence());
}
}

0 comments on commit fe1acc0

Please sign in to comment.