Skip to content

Commit

Permalink
feat: workflow ascode from template edit with merge request (#5154)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlt authored May 18, 2020
1 parent d1a3a18 commit 35db63f
Show file tree
Hide file tree
Showing 75 changed files with 1,650 additions and 1,104 deletions.
4 changes: 4 additions & 0 deletions engine/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,10 @@ func (a *API) Serve(ctx context.Context) error {
return migrate.RefactorProjectIntegrationCrypto(ctx, a.DBConnectionFactory.GetDBMap())
}})

migrate.Add(ctx, sdk.Migration{Name: "AsCodeEventsWorkflowHolder", Release: "0.44.0", Blocker: false, Automatic: true, ExecFunc: func(ctx context.Context) error {
return migrate.RefactorAsCodeEventsWorkflowHolder(ctx, a.DBConnectionFactory.GetDBMap())
}})

isFreshInstall, errF := version.IsFreshInstall(a.mustDB())
if errF != nil {
return sdk.WrapError(errF, "Unable to check if it's a fresh installation of CDS")
Expand Down
4 changes: 1 addition & 3 deletions engine/api/api_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,6 @@ func (api *API) InitRouter() {
r.Handle("/project/{permProjectKey}/keys", Scope(sdk.AuthConsumerScopeProject), r.GET(api.getKeysInProjectHandler), r.POST(api.addKeyInProjectHandler))
r.Handle("/project/{permProjectKey}/keys/{name}", Scope(sdk.AuthConsumerScopeProject), r.DELETE(api.deleteKeyInProjectHandler))

// As Code
r.Handle("/project/{key}/ascode/events/resync", Scope(sdk.AuthConsumerScopeProject), r.POST(api.postResyncPRAsCodeHandler, EnableTracing()))

// Import Application
r.Handle("/project/{permProjectKey}/import/application", Scope(sdk.AuthConsumerScopeProject), r.POST(api.postApplicationImportHandler))
// Export Application
Expand Down Expand Up @@ -239,6 +236,7 @@ func (api *API) InitRouter() {
r.Handle("/project/{key}/workflows/{permWorkflowName}/eventsintegration/{integrationID}", Scope(sdk.AuthConsumerScopeProject), r.DELETE(api.deleteWorkflowEventsIntegrationHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/icon", Scope(sdk.AuthConsumerScopeProject), r.PUT(api.putWorkflowIconHandler), r.DELETE(api.deleteWorkflowIconHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/ascode", Scope(sdk.AuthConsumerScopeProject), r.POST(api.postWorkflowAsCodeHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/ascode/events/resync", Scope(sdk.AuthConsumerScopeProject), r.POST(api.postWorkflowAsCodeEventsResyncHandler, EnableTracing()))
r.Handle("/project/{key}/workflows/{permWorkflowName}/ascode/{uuid}", Scope(sdk.AuthConsumerScopeProject), r.GET(api.getWorkflowAsCodeHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/label", Scope(sdk.AuthConsumerScopeProject), r.POST(api.postWorkflowLabelHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/label/{labelID}", Scope(sdk.AuthConsumerScopeProject), r.DELETE(api.deleteWorkflowLabelHandler))
Expand Down
74 changes: 33 additions & 41 deletions engine/api/ascode.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (

"github.com/gorilla/mux"

"github.com/ovh/cds/engine/api/application"
"github.com/ovh/cds/engine/api/ascode/sync"
"github.com/ovh/cds/engine/api/ascode"
"github.com/ovh/cds/engine/api/event"
"github.com/ovh/cds/engine/api/operation"
"github.com/ovh/cds/engine/api/project"
Expand Down Expand Up @@ -88,12 +87,9 @@ func (api *API) postImportAsCodeHandler() service.Handler {
func (api *API) getImportAsCodeHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
uuid := vars["uuid"]

var ope = new(sdk.Operation)
ope.UUID = uuid
if err := operation.GetRepositoryOperation(ctx, api.mustDB(), ope); err != nil {
return sdk.WrapError(err, "Cannot get repository operation status")
ope, err := operation.GetRepositoryOperation(ctx, api.mustDB(), vars["uuid"])
if err != nil {
return sdk.WrapError(err, "cannot get repository operation status")
}
return service.WriteJSON(w, ope, http.StatusOK)
}
Expand Down Expand Up @@ -123,11 +119,9 @@ func (api *API) postPerformImportAsCodeHandler() service.Handler {
return sdk.WrapError(errp, "postPerformImportAsCodeHandler> Cannot load project %s", key)
}

var ope = new(sdk.Operation)
ope.UUID = uuid

if err := operation.GetRepositoryOperation(ctx, api.mustDB(), ope); err != nil {
return sdk.WrapError(err, "Unable to get repository operation")
ope, err := operation.GetRepositoryOperation(ctx, api.mustDB(), uuid)
if err != nil {
return sdk.WrapError(err, "unable to get repository operation")
}

if ope.Status != sdk.OperationStatusDone {
Expand Down Expand Up @@ -167,11 +161,14 @@ func (api *API) postPerformImportAsCodeHandler() service.Handler {
if opt.FromRepository != "" {
mods = append(mods, workflowtemplate.TemplateRequestModifiers.DefaultNameAndRepositories(ctx, api.mustDB(), api.Cache, *proj, opt.FromRepository))
}
wti, err := workflowtemplate.CheckAndExecuteTemplate(ctx, api.mustDB(), *consumer, *proj, &data, mods...)
var allMsg []sdk.Message
msgTemplate, wti, err := workflowtemplate.CheckAndExecuteTemplate(ctx, api.mustDB(), *consumer, *proj, &data, mods...)
allMsg = append(allMsg, msgTemplate...)
if err != nil {
return err
}
allMsg, wrkflw, _, err := workflow.Push(ctx, api.mustDB(), api.Cache, proj, data, opt, getAPIConsumer(ctx), project.DecryptWithBuiltinKey)
msgPush, wrkflw, _, err := workflow.Push(ctx, api.mustDB(), api.Cache, proj, data, opt, getAPIConsumer(ctx), project.DecryptWithBuiltinKey)
allMsg = append(allMsg, msgPush...)
if err != nil {
return sdk.WrapError(err, "unable to push workflow")
}
Expand Down Expand Up @@ -203,43 +200,38 @@ func (api *API) postPerformImportAsCodeHandler() service.Handler {
}
}

func (api *API) postResyncPRAsCodeHandler() service.Handler {
func (api *API) postWorkflowAsCodeEventsResyncHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
key := vars["key"]
appName := FormString(r, "appName")
fromRepo := FormString(r, "repo")
projectKey := vars["key"]
workflowName := vars["permWorkflowName"]

proj, errP := project.Load(api.mustDB(), key,
proj, err := project.Load(api.mustDB(), projectKey,
project.LoadOptions.WithApplicationWithDeploymentStrategies,
project.LoadOptions.WithPipelines,
project.LoadOptions.WithEnvironments,
project.LoadOptions.WithIntegrations,
project.LoadOptions.WithClearKeys)
if errP != nil {
return sdk.WrapError(errP, "unable to load project")
}
var app sdk.Application
switch {
case appName != "":
appP, err := application.LoadByName(api.mustDB(), key, appName)
if err != nil {
return err
}
app = *appP
case fromRepo != "":
wkf, err := workflow.LoadByRepo(ctx, api.Cache, api.mustDB(), *proj, fromRepo, workflow.LoadOptions{})
if err != nil {
return err
}
app = wkf.Applications[wkf.WorkflowData.Node.Context.ApplicationID]
default:
return sdk.WrapError(sdk.ErrWrongRequest, "Missing appName or repo query parameter")
project.LoadOptions.WithClearKeys,
)
if err != nil {
return err
}

if _, _, err := sync.SyncAsCodeEvent(ctx, api.mustDB(), api.Cache, *proj, app, getAPIConsumer(ctx).AuthentifiedUser); err != nil {
wf, err := workflow.Load(ctx, api.mustDB(), api.Cache, *proj, workflowName, workflow.LoadOptions{})
if err != nil {
return err
}

res, err := ascode.SyncEvents(ctx, api.mustDB(), api.Cache, *proj, *wf, getAPIConsumer(ctx).AuthentifiedUser)
if err != nil {
return err
}
if res.Merged {
if err := workflow.UpdateFromRepository(api.mustDB(), wf.ID, res.FromRepository); err != nil {
return err
}
}

return nil
}
}
168 changes: 0 additions & 168 deletions engine/api/ascode/ascode_pr.go

This file was deleted.

Loading

0 comments on commit 35db63f

Please sign in to comment.