Skip to content

Commit

Permalink
feat: git transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel-Haeberli committed Aug 7, 2023
1 parent 07c5f81 commit a404649
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 10 deletions.
2 changes: 1 addition & 1 deletion momentum-core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ install-swagger:
go install github.com/swaggo/swag/cmd/swag@latest

setup-dev: dev-setup.sh
. ./dev-setup.sh
. ./dev-setup.sh
4 changes: 2 additions & 2 deletions momentum-core/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (m *MomentumConfig) checkMandatoryTemplates() error {
return nil
}

func (m *MomentumConfig) intitializeGitAccessToken() error {
func (m *MomentumConfig) initializeGitAccessToken() error {

m.transactionToken = new(gittransaction.Token)

Expand Down Expand Up @@ -141,7 +141,7 @@ func InitializeMomentumCore() (*MomentumConfig, error) {
config.deploymentTemplateFolderPath = utils.BuildPath(templatesDir, "deployments", "deploymentName")
config.deploymentTemplateFilePath = utils.BuildPath(templatesDir, "deployments", "deploymentName.yaml")

err = config.intitializeGitAccessToken()
err = config.initializeGitAccessToken()
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions momentum-core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func main() {
templateRouter := routers.NewTemplateRouter()
valueRouter := routers.NewValueRouter()
deploymentRouter := routers.NewDeploymentRouter(deploymentService, repositoryService, config)
stageRouter := routers.NewStageRouter(stageService)
applicationRouter := routers.NewApplicationRouter(applicationService)
stageRouter := routers.NewStageRouter(stageService, repositoryService, config)
applicationRouter := routers.NewApplicationRouter(applicationService, repositoryService, config)
repositoryRouter := routers.NewRepositoryRouter(repositoryService, applicationService, stageService, deploymentService)

dispatcher := NewDispatcher(config, repositoryRouter, applicationRouter, stageRouter, deploymentRouter, valueRouter, templateRouter)
Expand Down
39 changes: 36 additions & 3 deletions momentum-core/routers/application-router.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"momentum-core/services"
"net/http"

gittransaction "github.com/Joel-Haeberli/git-transaction"
"github.com/gin-gonic/gin"
)

Expand All @@ -14,13 +15,20 @@ const ROUTING_PATH_APPLICATION = VERSION + "/application"

type ApplicationRouter struct {
applicationService *services.ApplicationService
repositoryService *services.RepositoryService
config *config.MomentumConfig
}

func NewApplicationRouter(applicationService *services.ApplicationService) *ApplicationRouter {
func NewApplicationRouter(applicationService *services.ApplicationService,
repositoryService *services.RepositoryService,
config *config.MomentumConfig,
) *ApplicationRouter {

router := new(ApplicationRouter)

router.applicationService = applicationService
router.repositoryService = repositoryService
router.config = config

return router
}
Expand Down Expand Up @@ -89,7 +97,7 @@ func (a *ApplicationRouter) getApplication(c *gin.Context) {
// @Failure 404 {object} models.ApiError
// @Failure 500 {object} models.ApiError
// @Router /application [post]
func (d *ApplicationRouter) addApplication(c *gin.Context) {
func (a *ApplicationRouter) addApplication(c *gin.Context) {

traceId := config.LOGGER.TraceId()
request, err := models.ExtractApplicationCreateRequest(c)
Expand All @@ -99,12 +107,37 @@ func (d *ApplicationRouter) addApplication(c *gin.Context) {
return
}

application, err := d.applicationService.AddApplication(request, traceId)
repo, err := a.repositoryService.GetRepository(request.RepositoryName, traceId)
if err != nil {
c.IndentedJSON(http.StatusInternalServerError, models.NewApiError(err, http.StatusInternalServerError, c, traceId))
config.LOGGER.LogError(err.Error(), err, traceId)
return
}

ctx, transaction, err := gittransaction.New(gittransaction.SINGLEBRANCH, repo.Path, a.config.TransactionToken())

application, err := a.applicationService.AddApplication(request, traceId)
if err != nil {
c.IndentedJSON(http.StatusBadRequest, models.NewApiError(err, http.StatusBadRequest, c, traceId))
config.LOGGER.LogError(err.Error(), err, traceId)
return
}

err = transaction.Write(ctx)
if err != nil {
transaction.Rollback(ctx)
c.IndentedJSON(http.StatusInternalServerError, models.NewApiError(err, http.StatusInternalServerError, c, traceId))
config.LOGGER.LogError(err.Error(), err, traceId)
return
}

err = transaction.Commit(ctx)
if err != nil {
transaction.Rollback(ctx)
c.IndentedJSON(http.StatusInternalServerError, models.NewApiError(err, http.StatusInternalServerError, c, traceId))
config.LOGGER.LogError(err.Error(), err, traceId)
return
}

c.IndentedJSON(http.StatusOK, application)
}
2 changes: 2 additions & 0 deletions momentum-core/routers/deployment-router.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,15 @@ func (d *DeploymentRouter) addDeployment(c *gin.Context) {

err = transaction.Write(ctx)
if err != nil {
transaction.Rollback(ctx)
c.IndentedJSON(http.StatusInternalServerError, models.NewApiError(err, http.StatusInternalServerError, c, traceId))
config.LOGGER.LogError(err.Error(), err, traceId)
return
}

err = transaction.Commit(ctx)
if err != nil {
transaction.Rollback(ctx)
c.IndentedJSON(http.StatusInternalServerError, models.NewApiError(err, http.StatusInternalServerError, c, traceId))
config.LOGGER.LogError(err.Error(), err, traceId)
return
Expand Down
34 changes: 32 additions & 2 deletions momentum-core/routers/stage-router.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,26 @@ import (
"momentum-core/services"
"net/http"

gittransaction "github.com/Joel-Haeberli/git-transaction"
"github.com/gin-gonic/gin"
)

const ROUTING_PATH_STAGE_BY_ID = VERSION + "/repository/:repositoryName/app/stage/:stageId"
const ROUTING_PATH_STAGE = VERSION + "/stage"

type StageRouter struct {
stageService *services.StageService
stageService *services.StageService
repositoryService *services.RepositoryService
config *config.MomentumConfig
}

func NewStageRouter(stageService *services.StageService) *StageRouter {
func NewStageRouter(stageService *services.StageService, repositoryService *services.RepositoryService, config *config.MomentumConfig) *StageRouter {

router := new(StageRouter)

router.stageService = stageService
router.repositoryService = repositoryService
router.config = config

return router
}
Expand Down Expand Up @@ -82,12 +87,37 @@ func (sr *StageRouter) addStage(c *gin.Context) {
return
}

repo, err := sr.repositoryService.GetRepository(request.RepositoryName, traceId)
if err != nil {
c.IndentedJSON(http.StatusInternalServerError, models.NewApiError(err, http.StatusInternalServerError, c, traceId))
config.LOGGER.LogError(err.Error(), err, traceId)
return
}

ctx, transaction, err := gittransaction.New(gittransaction.SINGLEBRANCH, repo.Path, sr.config.TransactionToken())

stage, err := sr.stageService.AddStage(request, traceId)
if err != nil {
c.IndentedJSON(http.StatusBadRequest, models.NewApiError(err, http.StatusBadRequest, c, traceId))
config.LOGGER.LogError(err.Error(), err, traceId)
return
}

err = transaction.Write(ctx)
if err != nil {
transaction.Rollback(ctx)
c.IndentedJSON(http.StatusInternalServerError, models.NewApiError(err, http.StatusInternalServerError, c, traceId))
config.LOGGER.LogError(err.Error(), err, traceId)
return
}

err = transaction.Commit(ctx)
if err != nil {
transaction.Rollback(ctx)
c.IndentedJSON(http.StatusInternalServerError, models.NewApiError(err, http.StatusInternalServerError, c, traceId))
config.LOGGER.LogError(err.Error(), err, traceId)
return
}

c.IndentedJSON(http.StatusOK, stage)
}

0 comments on commit a404649

Please sign in to comment.