From 1058d9696961cb393755774dd424f9d703214bce Mon Sep 17 00:00:00 2001 From: Easton Crupper <65553218+ecrupper@users.noreply.github.com> Date: Wed, 10 Apr 2024 12:46:28 -0400 Subject: [PATCH] refactor(repo)!: nested repository with migration from types (#1095) --- api/admin/repo.go | 5 +- api/auth/login.go | 4 +- api/auth/logout.go | 4 +- api/auth/redirect.go | 4 +- api/build/approve.go | 16 +- api/build/auto_cancel.go | 7 +- api/build/compile_publish.go | 38 +- api/build/create.go | 4 +- api/build/enqueue.go | 4 +- api/build/get_id.go | 3 +- api/build/graph.go | 6 +- api/build/plan.go | 3 +- api/build/restart.go | 4 +- api/build/update.go | 8 +- api/pipeline/compile.go | 4 +- api/pipeline/expand.go | 4 +- api/pipeline/template.go | 15 +- api/pipeline/validate.go | 4 +- api/repo/chown.go | 2 +- api/repo/create.go | 13 +- api/repo/repair.go | 7 +- api/repo/update.go | 17 +- api/scm/sync_org.go | 6 +- api/step/plan.go | 13 +- api/step/update.go | 8 +- api/types/actions/comment.go | 83 +++ api/types/actions/comment_test.go | 108 +++ api/types/actions/deploy.go | 54 ++ api/types/actions/deploy_test.go | 98 +++ api/types/actions/pull.go | 203 ++++++ api/types/actions/pull_test.go | 148 ++++ api/types/actions/push.go | 143 ++++ api/types/actions/push_test.go | 136 ++++ api/types/actions/schedule.go | 54 ++ api/types/actions/schedule_test.go | 98 +++ api/types/events.go | 338 +++++++++ api/types/events_test.go | 424 +++++++++++ api/types/executor.go | 227 ++++++ api/types/executor_test.go | 241 +++++++ api/types/repo.go | 668 ++++++++++++++++++ api/types/repo_test.go | 371 ++++++++++ api/types/string.go | 90 +++ api/types/string_test.go | 57 ++ api/types/worker_test.go | 2 +- api/user/get_source.go | 8 +- api/webhook/post.go | 29 +- cmd/vela-server/metadata.go | 22 +- cmd/vela-server/schedule.go | 6 +- compiler/engine.go | 7 +- compiler/native/compile.go | 3 +- compiler/native/compile_test.go | 175 ++--- compiler/native/environment.go | 5 +- compiler/native/environment_test.go | 62 +- compiler/native/expand_test.go | 9 +- compiler/native/native.go | 11 +- compiler/native/native_test.go | 15 +- compiler/native/parse_test.go | 10 +- compiler/native/transform_test.go | 22 +- database/build/build_test.go | 6 +- database/build/count_org_test.go | 9 +- database/build/count_repo.go | 4 +- database/build/count_repo_test.go | 2 +- database/build/get_repo.go | 3 +- database/build/get_repo_test.go | 2 +- database/build/interface.go | 11 +- database/build/last_repo.go | 3 +- database/build/last_repo_test.go | 2 +- database/build/list_org_test.go | 9 +- database/build/list_pending_running_repo.go | 3 +- .../build/list_pending_running_repo_test.go | 9 +- database/build/list_pending_running_test.go | 5 +- database/build/list_repo.go | 3 +- database/build/list_repo_test.go | 2 +- database/deployment/count_repo.go | 4 +- database/deployment/count_repo_test.go | 2 +- database/deployment/deployment_test.go | 6 +- database/deployment/get_repo.go | 3 +- database/deployment/get_repo_test.go | 2 +- database/deployment/interface.go | 7 +- database/deployment/list_repo.go | 3 +- database/hook/count_repo.go | 4 +- database/hook/count_repo_test.go | 2 +- database/hook/get_repo.go | 3 +- database/hook/get_repo_test.go | 2 +- database/hook/hook_test.go | 6 +- database/hook/interface.go | 9 +- database/hook/last_repo.go | 3 +- database/hook/last_repo_test.go | 2 +- database/hook/list_repo.go | 3 +- database/hook/list_repo_test.go | 2 +- database/integration_test.go | 72 +- database/pipeline/count_repo.go | 4 +- database/pipeline/count_repo_test.go | 4 +- database/pipeline/get_repo.go | 3 +- database/pipeline/get_repo_test.go | 3 +- database/pipeline/interface.go | 7 +- database/pipeline/list_repo.go | 3 +- database/pipeline/list_repo_test.go | 3 +- database/repo/count_org_test.go | 4 +- database/repo/count_test.go | 4 +- database/repo/count_user_test.go | 4 +- database/repo/create.go | 21 +- database/repo/create_test.go | 4 +- database/repo/delete.go | 9 +- database/repo/delete_test.go | 2 +- database/repo/get.go | 21 +- database/repo/get_org.go | 27 +- database/repo/get_org_test.go | 38 +- database/repo/get_test.go | 32 +- database/repo/interface.go | 17 +- database/repo/list.go | 22 +- database/repo/list_org.go | 25 +- database/repo/list_org_test.go | 52 +- database/repo/list_test.go | 40 +- database/repo/list_user.go | 22 +- database/repo/list_user_test.go | 53 +- database/repo/repo.go | 328 +++++++++ database/repo/repo_test.go | 26 +- database/repo/update.go | 21 +- database/repo/update_test.go | 6 +- database/schedule/count_repo.go | 5 +- database/schedule/get_repo.go | 4 +- database/schedule/interface.go | 7 +- database/schedule/list_repo.go | 4 +- database/schedule/schedule_test.go | 6 +- database/secret/count_repo.go | 4 +- database/secret/count_repo_test.go | 2 +- database/secret/count_team_test.go | 2 +- database/secret/get_repo.go | 3 +- database/secret/get_repo_test.go | 2 +- database/secret/interface.go | 7 +- database/secret/list_repo.go | 3 +- database/secret/list_repo_test.go | 2 +- database/secret/secret_test.go | 6 +- go.mod | 4 +- go.sum | 4 +- internal/metadata.go | 44 ++ internal/token/compose.go | 4 +- internal/token/compose_test.go | 6 +- internal/webhook.go | 70 ++ internal/webhook_test.go | 111 +++ mock/server/repo.go | 18 +- mock/server/repo_test.go | 4 +- queue/models/item.go | 31 + queue/models/item_test.go | 123 ++++ queue/redis/length_test.go | 5 +- queue/redis/pop.go | 6 +- queue/redis/pop_test.go | 13 +- queue/redis/push_test.go | 5 +- queue/redis/redis_test.go | 70 +- queue/service.go | 4 +- router/middleware/build/build_test.go | 26 +- router/middleware/executors/context.go | 8 +- router/middleware/executors/context_test.go | 11 +- router/middleware/executors/executor_test.go | 7 +- router/middleware/executors/executors.go | 6 +- router/middleware/header.go | 6 +- router/middleware/header_test.go | 14 +- router/middleware/logger_test.go | 8 +- router/middleware/metadata.go | 5 +- router/middleware/metadata_test.go | 9 +- router/middleware/org/org_test.go | 6 +- router/middleware/perm/perm.go | 34 +- router/middleware/perm/perm_test.go | 141 ++-- router/middleware/pipeline/pipeline.go | 4 +- router/middleware/pipeline/pipeline_test.go | 34 +- router/middleware/repo/context.go | 8 +- router/middleware/repo/context_test.go | 7 +- router/middleware/repo/repo.go | 4 +- router/middleware/repo/repo_test.go | 25 +- router/middleware/service/service_test.go | 30 +- router/middleware/step/step_test.go | 36 +- scm/github/changeset.go | 14 +- scm/github/changeset_test.go | 11 +- scm/github/deployment.go | 9 +- scm/github/deployment_test.go | 3 +- scm/github/repo.go | 31 +- scm/github/repo_test.go | 49 +- scm/github/webhook.go | 61 +- scm/github/webhook_test.go | 103 +-- scm/service.go | 37 +- secret/native/count.go | 4 +- secret/native/get.go | 3 +- secret/native/list.go | 3 +- util/encryption.go | 96 +++ util/encryption_test.go | 99 +++ util/util.go | 4 +- 187 files changed, 5673 insertions(+), 1005 deletions(-) create mode 100644 api/types/actions/comment.go create mode 100644 api/types/actions/comment_test.go create mode 100644 api/types/actions/deploy.go create mode 100644 api/types/actions/deploy_test.go create mode 100644 api/types/actions/pull.go create mode 100644 api/types/actions/pull_test.go create mode 100644 api/types/actions/push.go create mode 100644 api/types/actions/push_test.go create mode 100644 api/types/actions/schedule.go create mode 100644 api/types/actions/schedule_test.go create mode 100644 api/types/events.go create mode 100644 api/types/events_test.go create mode 100644 api/types/executor.go create mode 100644 api/types/executor_test.go create mode 100644 api/types/repo.go create mode 100644 api/types/repo_test.go create mode 100644 api/types/string.go create mode 100644 api/types/string_test.go create mode 100644 internal/metadata.go create mode 100644 internal/webhook.go create mode 100644 internal/webhook_test.go create mode 100644 queue/models/item.go create mode 100644 queue/models/item_test.go create mode 100644 util/encryption.go create mode 100644 util/encryption_test.go diff --git a/api/admin/repo.go b/api/admin/repo.go index 11814a8a0..46eb3ccc2 100644 --- a/api/admin/repo.go +++ b/api/admin/repo.go @@ -7,11 +7,10 @@ import ( "fmt" "net/http" + "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" "github.com/go-vela/server/util" - "github.com/go-vela/types/library" - "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" ) @@ -55,7 +54,7 @@ func UpdateRepo(c *gin.Context) { ctx := c.Request.Context() // capture body from API request - input := new(library.Repo) + input := new(types.Repo) err := c.Bind(input) if err != nil { diff --git a/api/auth/login.go b/api/auth/login.go index 041d844f3..a6e026fc2 100644 --- a/api/auth/login.go +++ b/api/auth/login.go @@ -8,8 +8,8 @@ import ( "net/url" "github.com/gin-gonic/gin" + "github.com/go-vela/server/internal" "github.com/go-vela/server/util" - "github.com/go-vela/types" "github.com/sirupsen/logrus" ) @@ -38,7 +38,7 @@ import ( // process a user logging in to Vela. func Login(c *gin.Context) { // load the metadata - m := c.MustGet("metadata").(*types.Metadata) + m := c.MustGet("metadata").(*internal.Metadata) // capture query params t := util.FormParameter(c, "type") diff --git a/api/auth/logout.go b/api/auth/logout.go index 454d2b9ed..2c1cf9a33 100644 --- a/api/auth/logout.go +++ b/api/auth/logout.go @@ -8,10 +8,10 @@ import ( "net/url" "github.com/go-vela/server/database" + "github.com/go-vela/server/internal" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" - "github.com/go-vela/types" "github.com/go-vela/types/constants" "github.com/gin-gonic/gin" @@ -41,7 +41,7 @@ import ( // refresh token cookie. func Logout(c *gin.Context) { // grab the metadata to help deal with the cookie - m := c.MustGet("metadata").(*types.Metadata) + m := c.MustGet("metadata").(*internal.Metadata) // capture middleware values u := user.Retrieve(c) ctx := c.Request.Context() diff --git a/api/auth/redirect.go b/api/auth/redirect.go index 473c7a612..2e96b3482 100644 --- a/api/auth/redirect.go +++ b/api/auth/redirect.go @@ -7,8 +7,8 @@ import ( "net/http" "github.com/gin-gonic/gin" + "github.com/go-vela/server/internal" "github.com/go-vela/server/util" - "github.com/go-vela/types" "github.com/sirupsen/logrus" ) @@ -68,7 +68,7 @@ import ( // This will only handle non-headless flows (ie. web or cli). func GetAuthRedirect(c *gin.Context) { // load the metadata - m := c.MustGet("metadata").(*types.Metadata) + m := c.MustGet("metadata").(*internal.Metadata) logrus.Info("redirecting for final auth flow destination") diff --git a/api/build/approve.go b/api/build/approve.go index 5b41616b5..30006ab1c 100644 --- a/api/build/approve.go +++ b/api/build/approve.go @@ -11,12 +11,12 @@ import ( "github.com/gin-gonic/gin" "github.com/go-vela/server/database" "github.com/go-vela/server/queue" + "github.com/go-vela/server/queue/models" "github.com/go-vela/server/router/middleware/build" "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" - "github.com/go-vela/types" "github.com/go-vela/types/constants" "github.com/sirupsen/logrus" ) @@ -105,23 +105,13 @@ func ApproveBuild(c *gin.Context) { logger.Debugf("user %s approved build %s/%d for execution", u.GetName(), r.GetFullName(), b.GetNumber()) - // send API call to capture the repo owner - owner, err := database.FromContext(c).GetUser(ctx, r.GetUserID()) - if err != nil { - retErr := fmt.Errorf("unable to get owner for %s: %w", r.GetFullName(), err) - - util.HandleError(c, http.StatusBadRequest, retErr) - - return - } - // set fields b.SetStatus(constants.StatusPending) b.SetApprovedAt(time.Now().Unix()) b.SetApprovedBy(u.GetName()) // update the build in the db - _, err = database.FromContext(c).UpdateBuild(ctx, b) + _, err := database.FromContext(c).UpdateBuild(ctx, b) if err != nil { logrus.Errorf("Failed to update build %d during publish to queue for %s: %v", b.GetNumber(), r.GetFullName(), err) } @@ -131,7 +121,7 @@ func ApproveBuild(c *gin.Context) { ctx, queue.FromGinContext(c), database.FromContext(c), - types.ToItem(b, r, owner), + models.ToItem(b, r), b.GetHost(), ) diff --git a/api/build/auto_cancel.go b/api/build/auto_cancel.go index 0f200498c..53dc1b3eb 100644 --- a/api/build/auto_cancel.go +++ b/api/build/auto_cancel.go @@ -12,6 +12,7 @@ import ( "time" "github.com/gin-gonic/gin" + "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" "github.com/go-vela/server/internal/token" "github.com/go-vela/types/constants" @@ -21,7 +22,7 @@ import ( // AutoCancel is a helper function that checks to see if any pending or running // builds for the repo can be replaced by the current build. -func AutoCancel(c *gin.Context, b *library.Build, rB *library.Build, r *library.Repo, cancelOpts *pipeline.CancelOptions) (bool, error) { +func AutoCancel(c *gin.Context, b *library.Build, rB *library.Build, r *types.Repo, cancelOpts *pipeline.CancelOptions) (bool, error) { // if build is the current build, continue if rB.GetID() == b.GetID() { return false, nil @@ -73,8 +74,8 @@ func AutoCancel(c *gin.Context, b *library.Build, rB *library.Build, r *library. // cancelRunning is a helper function that determines the executor currently running a build and sends an API call // to that executor's worker to cancel the build. -func cancelRunning(c *gin.Context, b *library.Build, r *library.Repo) error { - e := new([]library.Executor) +func cancelRunning(c *gin.Context, b *library.Build, r *types.Repo) error { + e := new([]types.Executor) // retrieve the worker w, err := database.FromContext(c).GetWorkerForHostname(c, b.GetHost()) if err != nil { diff --git a/api/build/compile_publish.go b/api/build/compile_publish.go index f60dcaa56..e23e6d8b3 100644 --- a/api/build/compile_publish.go +++ b/api/build/compile_publish.go @@ -11,12 +11,14 @@ import ( "strings" "time" + "github.com/go-vela/server/api/types" "github.com/go-vela/server/compiler" "github.com/go-vela/server/database" + "github.com/go-vela/server/internal" "github.com/go-vela/server/queue" + "github.com/go-vela/server/queue/models" "github.com/go-vela/server/scm" "github.com/go-vela/server/util" - "github.com/go-vela/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/library" "github.com/go-vela/types/pipeline" @@ -26,8 +28,8 @@ import ( // CompileAndPublishConfig is a struct that contains information for the CompileAndPublish function. type CompileAndPublishConfig struct { Build *library.Build - Repo *library.Repo - Metadata *types.Metadata + Repo *types.Repo + Metadata *internal.Metadata BaseErr string Source string Comment string @@ -47,27 +49,17 @@ func CompileAndPublish( scm scm.Service, compiler compiler.Engine, queue queue.Service, -) (*pipeline.Build, *types.Item, error) { +) (*pipeline.Build, *models.Item, error) { logrus.Debugf("generating queue items for build %s/%d", cfg.Repo.GetFullName(), cfg.Build.GetNumber()) // assign variables from form for readibility r := cfg.Repo + u := cfg.Repo.GetOwner() b := cfg.Build baseErr := cfg.BaseErr - // send API call to capture repo owner - logrus.Debugf("capturing owner of repository %s", cfg.Repo.GetFullName()) - - u, err := database.GetUser(c, r.GetUserID()) - if err != nil { - retErr := fmt.Errorf("%s: failed to get owner for %s: %w", baseErr, r.GetFullName(), err) - util.HandleError(c, http.StatusBadRequest, retErr) - - return nil, nil, retErr - } - // confirm current repo owner has at least write access to repo (needed for status update later) - _, err = scm.RepoAccess(c, u.GetName(), u.GetToken(), r.GetOrg(), r.GetName()) + _, err := scm.RepoAccess(c, u.GetName(), u.GetToken(), r.GetOrg(), r.GetName()) if err != nil { retErr := fmt.Errorf("unable to publish build to queue: repository owner %s no longer has write access to repository %s", u.GetName(), r.GetFullName()) util.HandleError(c, http.StatusUnauthorized, retErr) @@ -97,7 +89,7 @@ func CompileAndPublish( return nil, nil, retErr } - commit, branch, baseref, headref, err := scm.GetPullRequest(c, u, r, prNum) + commit, branch, baseref, headref, err := scm.GetPullRequest(c, r, prNum) if err != nil { retErr := fmt.Errorf("%s: failed to get pull request info for %s: %w", baseErr, r.GetFullName(), err) util.HandleError(c, http.StatusInternalServerError, retErr) @@ -114,7 +106,7 @@ func CompileAndPublish( // if the source is from a schedule, fetch the commit sha from schedule branch (same as build branch at this moment) if strings.EqualFold(cfg.Source, "schedule") { // send API call to capture the commit sha for the branch - _, commit, err := scm.GetBranch(c, u, r, b.GetBranch()) + _, commit, err := scm.GetBranch(c, r, b.GetBranch()) if err != nil { retErr := fmt.Errorf("failed to get commit for repo %s on %s branch: %w", r.GetFullName(), r.GetBranch(), err) util.HandleError(c, http.StatusInternalServerError, retErr) @@ -170,7 +162,7 @@ func CompileAndPublish( !strings.EqualFold(b.GetEvent(), constants.EventPull) && !strings.EqualFold(b.GetEvent(), constants.EventDelete) { // send API call to capture list of files changed for the commit - files, err = scm.Changeset(c, u, r, b.GetCommit()) + files, err = scm.Changeset(c, r, b.GetCommit()) if err != nil { retErr := fmt.Errorf("%s: failed to get changeset for %s: %w", baseErr, r.GetFullName(), err) util.HandleError(c, http.StatusInternalServerError, retErr) @@ -182,7 +174,7 @@ func CompileAndPublish( // check if the build event is a pull_request if strings.EqualFold(b.GetEvent(), constants.EventPull) && prNum > 0 { // send API call to capture list of files changed for the pull request - files, err = scm.ChangesetPR(c, u, r, prNum) + files, err = scm.ChangesetPR(c, r, prNum) if err != nil { retErr := fmt.Errorf("%s: failed to get changeset for %s: %w", baseErr, r.GetFullName(), err) util.HandleError(c, http.StatusInternalServerError, retErr) @@ -201,7 +193,7 @@ func CompileAndPublish( // variable to store the pipeline type for the repository pipelineType = r.GetPipelineType() // variable to store updated repository record - repo *library.Repo + repo *types.Repo ) // implement a loop to process asynchronous operations with a retry limit @@ -324,7 +316,7 @@ func CompileAndPublish( } return nil, - &types.Item{ + &models.Item{ Build: b, }, errors.New(skip) @@ -448,7 +440,7 @@ func CompileAndPublish( return nil, nil, retErr } - return p, types.ToItem(b, repo, u), nil + return p, models.ToItem(b, repo), nil } // getPRNumberFromBuild is a helper function to diff --git a/api/build/create.go b/api/build/create.go index 1f1463ba3..08027545e 100644 --- a/api/build/create.go +++ b/api/build/create.go @@ -10,13 +10,13 @@ import ( "github.com/gin-gonic/gin" "github.com/go-vela/server/compiler" "github.com/go-vela/server/database" + "github.com/go-vela/server/internal" "github.com/go-vela/server/queue" "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/scm" "github.com/go-vela/server/util" - "github.com/go-vela/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/library" "github.com/sirupsen/logrus" @@ -74,7 +74,7 @@ import ( // CreateBuild represents the API handler to create a build in the configured backend. func CreateBuild(c *gin.Context) { // capture middleware values - m := c.MustGet("metadata").(*types.Metadata) + m := c.MustGet("metadata").(*internal.Metadata) o := org.Retrieve(c) r := repo.Retrieve(c) u := user.Retrieve(c) diff --git a/api/build/enqueue.go b/api/build/enqueue.go index be8cb2971..4edce866c 100644 --- a/api/build/enqueue.go +++ b/api/build/enqueue.go @@ -9,12 +9,12 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/queue" - "github.com/go-vela/types" + "github.com/go-vela/server/queue/models" "github.com/sirupsen/logrus" ) // Enqueue is a helper function that pushes a queue item (build, repo, user) to the queue. -func Enqueue(ctx context.Context, queue queue.Service, db database.Interface, item *types.Item, route string) { +func Enqueue(ctx context.Context, queue queue.Service, db database.Interface, item *models.Item, route string) { logrus.Infof("Converting queue item to json for build %d for %s", item.Build.GetNumber(), item.Repo.GetFullName()) byteItem, err := json.Marshal(item) diff --git a/api/build/get_id.go b/api/build/get_id.go index 44cfbdbea..b3adbea95 100644 --- a/api/build/get_id.go +++ b/api/build/get_id.go @@ -8,6 +8,7 @@ import ( "strconv" "github.com/gin-gonic/gin" + "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/scm" @@ -51,7 +52,7 @@ func GetBuildByID(c *gin.Context) { // Variables that will hold the library types of the build and repo var ( b *library.Build - r *library.Repo + r *types.Repo ) // Capture user from middleware diff --git a/api/build/graph.go b/api/build/graph.go index dde6e5343..fe6c9cf0d 100644 --- a/api/build/graph.go +++ b/api/build/graph.go @@ -11,13 +11,13 @@ import ( "github.com/gin-gonic/gin" "github.com/go-vela/server/compiler" "github.com/go-vela/server/database" + "github.com/go-vela/server/internal" "github.com/go-vela/server/router/middleware/build" "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/scm" "github.com/go-vela/server/util" - "github.com/go-vela/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/library" "github.com/go-vela/types/pipeline" @@ -142,7 +142,7 @@ func GetBuildGraph(c *gin.Context) { o := org.Retrieve(c) r := repo.Retrieve(c) u := user.Retrieve(c) - m := c.MustGet("metadata").(*types.Metadata) + m := c.MustGet("metadata").(*internal.Metadata) ctx := c.Request.Context() // update engine logger with API metadata @@ -194,7 +194,7 @@ func GetBuildGraph(c *gin.Context) { // check if the build event is not pull_request if !strings.EqualFold(b.GetEvent(), constants.EventPull) { // send API call to capture list of files changed for the commit - files, err = scm.FromContext(c).Changeset(ctx, u, r, b.GetCommit()) + files, err = scm.FromContext(c).Changeset(ctx, r, b.GetCommit()) if err != nil { retErr := fmt.Errorf("%s: failed to get changeset for %s: %w", baseErr, r.GetFullName(), err) diff --git a/api/build/plan.go b/api/build/plan.go index 28f54e981..d34a776ff 100644 --- a/api/build/plan.go +++ b/api/build/plan.go @@ -9,6 +9,7 @@ import ( "github.com/go-vela/server/api/service" "github.com/go-vela/server/api/step" + "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" "github.com/go-vela/server/scm" "github.com/go-vela/types/library" @@ -20,7 +21,7 @@ import ( // and services, for the build in the configured backend. // TODO: // - return build and error. -func PlanBuild(ctx context.Context, database database.Interface, scm scm.Service, p *pipeline.Build, b *library.Build, r *library.Repo) error { +func PlanBuild(ctx context.Context, database database.Interface, scm scm.Service, p *pipeline.Build, b *library.Build, r *types.Repo) error { // update fields in build object b.SetCreated(time.Now().UTC().Unix()) diff --git a/api/build/restart.go b/api/build/restart.go index 8b32fdc56..6043b4fde 100644 --- a/api/build/restart.go +++ b/api/build/restart.go @@ -10,6 +10,7 @@ import ( "github.com/gin-gonic/gin" "github.com/go-vela/server/compiler" "github.com/go-vela/server/database" + "github.com/go-vela/server/internal" "github.com/go-vela/server/queue" "github.com/go-vela/server/router/middleware/build" "github.com/go-vela/server/router/middleware/claims" @@ -18,7 +19,6 @@ import ( "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/scm" "github.com/go-vela/server/util" - "github.com/go-vela/types" "github.com/go-vela/types/constants" "github.com/sirupsen/logrus" ) @@ -73,7 +73,7 @@ import ( // RestartBuild represents the API handler to restart an existing build in the configured backend. func RestartBuild(c *gin.Context) { // capture middleware values - m := c.MustGet("metadata").(*types.Metadata) + m := c.MustGet("metadata").(*internal.Metadata) cl := claims.Retrieve(c) b := build.Retrieve(c) o := org.Retrieve(c) diff --git a/api/build/update.go b/api/build/update.go index c9fb30aa8..6a09e397f 100644 --- a/api/build/update.go +++ b/api/build/update.go @@ -168,14 +168,8 @@ func UpdateBuild(c *gin.Context) { b.GetStatus() == constants.StatusCanceled || b.GetStatus() == constants.StatusKilled || b.GetStatus() == constants.StatusError) && b.GetEvent() != constants.EventSchedule { - // send API call to capture the repo owner - u, err := database.FromContext(c).GetUser(ctx, r.GetUserID()) - if err != nil { - logrus.Errorf("unable to get owner for build %s: %v", entry, err) - } - // send API call to set the status on the commit - err = scm.FromContext(c).Status(ctx, u, b, r.GetOrg(), r.GetName()) + err = scm.FromContext(c).Status(ctx, r.GetOwner(), b, r.GetOrg(), r.GetName()) if err != nil { logrus.Errorf("unable to set commit status for build %s: %v", entry, err) } diff --git a/api/pipeline/compile.go b/api/pipeline/compile.go index 2b5782fe7..c3027edcf 100644 --- a/api/pipeline/compile.go +++ b/api/pipeline/compile.go @@ -9,12 +9,12 @@ import ( "github.com/gin-gonic/gin" "github.com/go-vela/server/compiler" + "github.com/go-vela/server/internal" "github.com/go-vela/server/router/middleware/org" pMiddleware "github.com/go-vela/server/router/middleware/pipeline" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" - "github.com/go-vela/types" "github.com/go-vela/types/pipeline" "github.com/sirupsen/logrus" ) @@ -71,7 +71,7 @@ import ( // expand and compile a pipeline configuration. func CompilePipeline(c *gin.Context) { // capture middleware values - m := c.MustGet("metadata").(*types.Metadata) + m := c.MustGet("metadata").(*internal.Metadata) o := org.Retrieve(c) p := pMiddleware.Retrieve(c) r := repo.Retrieve(c) diff --git a/api/pipeline/expand.go b/api/pipeline/expand.go index 948dcf1fd..a47ca84d6 100644 --- a/api/pipeline/expand.go +++ b/api/pipeline/expand.go @@ -9,12 +9,12 @@ import ( "github.com/gin-gonic/gin" "github.com/go-vela/server/compiler" + "github.com/go-vela/server/internal" "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/pipeline" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" - "github.com/go-vela/types" "github.com/sirupsen/logrus" ) @@ -71,7 +71,7 @@ import ( // expand a pipeline configuration. func ExpandPipeline(c *gin.Context) { // capture middleware values - m := c.MustGet("metadata").(*types.Metadata) + m := c.MustGet("metadata").(*internal.Metadata) o := org.Retrieve(c) p := pipeline.Retrieve(c) r := repo.Retrieve(c) diff --git a/api/pipeline/template.go b/api/pipeline/template.go index 9716b6328..8114d7f72 100644 --- a/api/pipeline/template.go +++ b/api/pipeline/template.go @@ -10,14 +10,13 @@ import ( "github.com/gin-gonic/gin" "github.com/go-vela/server/compiler" "github.com/go-vela/server/compiler/registry/github" - "github.com/go-vela/server/database" + "github.com/go-vela/server/internal" "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/pipeline" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/scm" "github.com/go-vela/server/util" - "github.com/go-vela/types" "github.com/go-vela/types/library" "github.com/go-vela/types/yaml" "github.com/sirupsen/logrus" @@ -75,7 +74,7 @@ import ( // map of templates utilized by a pipeline configuration. func GetTemplates(c *gin.Context) { // capture middleware values - m := c.MustGet("metadata").(*types.Metadata) + m := c.MustGet("metadata").(*internal.Metadata) o := org.Retrieve(c) p := pipeline.Retrieve(c) r := repo.Retrieve(c) @@ -105,14 +104,6 @@ func GetTemplates(c *gin.Context) { return } - // send API call to capture the repo owner - user, err := database.FromContext(c).GetUser(ctx, r.GetUserID()) - if err != nil { - util.HandleError(c, http.StatusBadRequest, fmt.Errorf("unable to get owner for %s: %w", r.GetFullName(), err)) - - return - } - baseErr := fmt.Sprintf("unable to set template links for %s", entry) templates := make(map[string]*library.Template) @@ -144,7 +135,7 @@ func GetTemplates(c *gin.Context) { } // retrieve link to template file from github - link, err := scm.FromContext(c).GetHTMLURL(ctx, user, src.Org, src.Repo, src.Name, src.Ref) + link, err := scm.FromContext(c).GetHTMLURL(ctx, r.GetOwner(), src.Org, src.Repo, src.Name, src.Ref) if err != nil { util.HandleError(c, http.StatusBadRequest, fmt.Errorf("%s: unable to get html url for %s/%s/%s/@%s: %w", baseErr, src.Org, src.Repo, src.Name, src.Ref, err)) diff --git a/api/pipeline/validate.go b/api/pipeline/validate.go index fbaf1116b..2754b24c9 100644 --- a/api/pipeline/validate.go +++ b/api/pipeline/validate.go @@ -8,12 +8,12 @@ import ( "github.com/gin-gonic/gin" "github.com/go-vela/server/compiler" + "github.com/go-vela/server/internal" "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/pipeline" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" - "github.com/go-vela/types" "github.com/sirupsen/logrus" ) @@ -69,7 +69,7 @@ import ( // expand and validate a pipeline configuration. func ValidatePipeline(c *gin.Context) { // capture middleware values - m := c.MustGet("metadata").(*types.Metadata) + m := c.MustGet("metadata").(*internal.Metadata) o := org.Retrieve(c) p := pipeline.Retrieve(c) r := repo.Retrieve(c) diff --git a/api/repo/chown.go b/api/repo/chown.go index 8061b2de6..9e1bf98ab 100644 --- a/api/repo/chown.go +++ b/api/repo/chown.go @@ -64,7 +64,7 @@ func ChownRepo(c *gin.Context) { }).Infof("changing owner of repo %s to %s", r.GetFullName(), u.GetName()) // update repo owner - r.SetUserID(u.GetID()) + r.SetOwner(u) // send API call to update the repo _, err := database.FromContext(c).UpdateRepo(ctx, r) diff --git a/api/repo/create.go b/api/repo/create.go index 59ccbf857..bc0c1103d 100644 --- a/api/repo/create.go +++ b/api/repo/create.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/gin-gonic/gin" + "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/scm" @@ -80,7 +81,7 @@ func CreateRepo(c *gin.Context) { ctx := c.Request.Context() // capture body from API request - input := new(library.Repo) + input := new(types.Repo) err := c.Bind(input) if err != nil { @@ -111,7 +112,7 @@ func CreateRepo(c *gin.Context) { } // update fields in repo object - r.SetUserID(u.GetID()) + r.SetOwner(u) // set the active field based off the input provided if input.Active == nil { @@ -275,7 +276,7 @@ func CreateRepo(c *gin.Context) { // if the repo exists but is inactive if len(dbRepo.GetOrg()) > 0 && !dbRepo.GetActive() { // update the repo owner - dbRepo.SetUserID(u.GetID()) + dbRepo.SetOwner(u) // update the default branch dbRepo.SetBranch(r.GetBranch()) // activate the repo @@ -323,12 +324,12 @@ func CreateRepo(c *gin.Context) { // defaultAllowedEvents is a helper function that generates an Events struct that results // from an admin-provided `sliceDefaults` or an admin-provided `maskDefaults`. If the admin // supplies a mask, that will be the default. Otherwise, it will be the legacy event list. -func defaultAllowedEvents(sliceDefaults []string, maskDefaults int64) *library.Events { +func defaultAllowedEvents(sliceDefaults []string, maskDefaults int64) *types.Events { if maskDefaults > 0 { - return library.NewEventsFromMask(maskDefaults) + return types.NewEventsFromMask(maskDefaults) } - events := new(library.Events) + events := new(types.Events) for _, event := range sliceDefaults { switch event { diff --git a/api/repo/repair.go b/api/repo/repair.go index 4a792283d..9d6e99b32 100644 --- a/api/repo/repair.go +++ b/api/repo/repair.go @@ -4,17 +4,18 @@ package repo import ( "fmt" + "net/http" + "github.com/gin-gonic/gin" wh "github.com/go-vela/server/api/webhook" "github.com/go-vela/server/database" + "github.com/go-vela/server/internal" "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/scm" "github.com/go-vela/server/util" - "github.com/go-vela/types" "github.com/sirupsen/logrus" - "net/http" ) // swagger:operation PATCH /api/v1/repos/{org}/{repo}/repair repos RepairRepo @@ -56,7 +57,7 @@ func RepairRepo(c *gin.Context) { u := user.Retrieve(c) ctx := c.Request.Context() // capture middleware values - m := c.MustGet("metadata").(*types.Metadata) + m := c.MustGet("metadata").(*internal.Metadata) // update engine logger with API metadata // diff --git a/api/repo/update.go b/api/repo/update.go index efdb1a366..db179371a 100644 --- a/api/repo/update.go +++ b/api/repo/update.go @@ -9,6 +9,7 @@ import ( "strings" "github.com/gin-gonic/gin" + "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" @@ -16,7 +17,6 @@ import ( "github.com/go-vela/server/scm" "github.com/go-vela/server/util" "github.com/go-vela/types/constants" - "github.com/go-vela/types/library" "github.com/google/uuid" "github.com/sirupsen/logrus" ) @@ -89,7 +89,7 @@ func UpdateRepo(c *gin.Context) { }).Infof("updating repo %s", r.GetFullName()) // capture body from API request - input := new(library.Repo) + input := new(types.Repo) err := c.Bind(input) if err != nil { @@ -249,26 +249,19 @@ func UpdateRepo(c *gin.Context) { return } - // if user is platform admin, fetch the repo owner token to make changes to webhook + // if user is platform admin, use repo owner token to make changes to webhook if u.GetAdmin() { // capture admin name for logging admn := u.GetName() - u, err = database.FromContext(c).GetUser(ctx, r.GetUserID()) - if err != nil { - retErr := fmt.Errorf("unable to get repo owner of %s for platform admin webhook update: %w", r.GetFullName(), err) - - util.HandleError(c, http.StatusInternalServerError, retErr) - - return - } - // log admin override update repo hook logrus.WithFields(logrus.Fields{ "org": o, "repo": r.GetName(), "user": u.GetName(), }).Infof("platform admin %s updating repo webhook events for repo %s", admn, r.GetFullName()) + + u = r.GetOwner() } // update webhook with new events _, err = scm.FromContext(c).Update(ctx, u, r, lastHook.GetWebhookID()) diff --git a/api/scm/sync_org.go b/api/scm/sync_org.go index fb0c425f3..0c5628bef 100644 --- a/api/scm/sync_org.go +++ b/api/scm/sync_org.go @@ -7,12 +7,12 @@ import ( "net/http" "github.com/gin-gonic/gin" + "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/scm" "github.com/go-vela/server/util" - "github.com/go-vela/types/library" "github.com/sirupsen/logrus" ) @@ -99,7 +99,7 @@ func SyncReposForOrg(c *gin.Context) { return } - repos := []*library.Repo{} + repos := []*types.Repo{} page := 0 // capture all repos belonging to a certain org in database for orgRepos := int64(0); orgRepos < t; orgRepos += 100 { @@ -117,7 +117,7 @@ func SyncReposForOrg(c *gin.Context) { page++ } - var results []*library.Repo + var results []*types.Repo // iterate through captured repos and check if they are in GitHub for _, repo := range repos { diff --git a/api/step/plan.go b/api/step/plan.go index 58ecc98f5..eb7d9dda9 100644 --- a/api/step/plan.go +++ b/api/step/plan.go @@ -7,6 +7,7 @@ import ( "fmt" "time" + "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" "github.com/go-vela/server/scm" "github.com/go-vela/types/constants" @@ -18,7 +19,7 @@ import ( // PlanSteps is a helper function to plan all steps // in the build for execution. This creates the steps // for the build in the configured backend. -func PlanSteps(ctx context.Context, database database.Interface, scm scm.Service, p *pipeline.Build, b *library.Build, repo *library.Repo) ([]*library.Step, error) { +func PlanSteps(ctx context.Context, database database.Interface, scm scm.Service, p *pipeline.Build, b *library.Build, repo *types.Repo) ([]*library.Step, error) { // variable to store planned steps steps := []*library.Step{} @@ -49,7 +50,7 @@ func PlanSteps(ctx context.Context, database database.Interface, scm scm.Service return steps, nil } -func planStep(ctx context.Context, database database.Interface, scm scm.Service, b *library.Build, c *pipeline.Container, repo *library.Repo, stage string) (*library.Step, error) { +func planStep(ctx context.Context, database database.Interface, scm scm.Service, b *library.Build, c *pipeline.Container, repo *types.Repo, stage string) (*library.Step, error) { // create the step object s := new(library.Step) s.SetBuildID(b.GetID()) @@ -90,14 +91,8 @@ func planStep(ctx context.Context, database database.Interface, scm scm.Service, } if len(s.GetReportAs()) > 0 { - // send API call to capture the repo owner - u, err := database.GetUser(ctx, repo.GetUserID()) - if err != nil { - logrus.Errorf("unable to get owner for build: %v", err) - } - // send API call to set the status on the commit - err = scm.StepStatus(ctx, u, b, s, repo.GetOrg(), repo.GetName()) + err = scm.StepStatus(ctx, repo.GetOwner(), b, s, repo.GetOrg(), repo.GetName()) if err != nil { logrus.Errorf("unable to set commit status for build: %v", err) } diff --git a/api/step/update.go b/api/step/update.go index bdfedc8ee..7dc97efac 100644 --- a/api/step/update.go +++ b/api/step/update.go @@ -168,14 +168,8 @@ func UpdateStep(c *gin.Context) { s.GetStatus() == constants.StatusError) && (b.GetEvent() != constants.EventSchedule) && (len(s.GetReportAs()) > 0) { - // send API call to capture the repo owner - u, err := database.FromContext(c).GetUser(ctx, r.GetUserID()) - if err != nil { - logrus.Errorf("unable to get owner for build %s: %v", entry, err) - } - // send API call to set the status on the commit - err = scm.FromContext(c).StepStatus(ctx, u, b, s, r.GetOrg(), r.GetName()) + err = scm.FromContext(c).StepStatus(ctx, r.GetOwner(), b, s, r.GetOrg(), r.GetName()) if err != nil { logrus.Errorf("unable to set commit status for build %s: %v", entry, err) } diff --git a/api/types/actions/comment.go b/api/types/actions/comment.go new file mode 100644 index 000000000..552aa3800 --- /dev/null +++ b/api/types/actions/comment.go @@ -0,0 +1,83 @@ +// SPDX-License-Identifier: Apache-2.0 + +package actions + +import "github.com/go-vela/types/constants" + +// Comment is the API representation of the various actions associated +// with the comment event webhook from the SCM. +type Comment struct { + Created *bool `json:"created"` + Edited *bool `json:"edited"` +} + +// FromMask returns the Comment type resulting from the provided integer mask. +func (a *Comment) FromMask(mask int64) *Comment { + a.SetCreated(mask&constants.AllowCommentCreate > 0) + a.SetEdited(mask&constants.AllowCommentEdit > 0) + + return a +} + +// ToMask returns the integer mask of the values for the Comment set. +func (a *Comment) ToMask() int64 { + mask := int64(0) + + if a.GetCreated() { + mask = mask | constants.AllowCommentCreate + } + + if a.GetEdited() { + mask = mask | constants.AllowCommentEdit + } + + return mask +} + +// GetCreated returns the Created field from the provided Comment. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (a *Comment) GetCreated() bool { + // return zero value if Events type or Created field is nil + if a == nil || a.Created == nil { + return false + } + + return *a.Created +} + +// GetEdited returns the Edited field from the provided Comment. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (a *Comment) GetEdited() bool { + // return zero value if Events type or Edited field is nil + if a == nil || a.Edited == nil { + return false + } + + return *a.Edited +} + +// SetCreated sets the Comment Created field. +// +// When the provided Events type is nil, it +// will set nothing and immediately return. +func (a *Comment) SetCreated(v bool) { + // return if Events type is nil + if a == nil { + return + } + + a.Created = &v +} + +// SetEdited sets the Comment Edited field. +// +// When the provided Events type is nil, it +// will set nothing and immediately return. +func (a *Comment) SetEdited(v bool) { + // return if Events type is nil + if a == nil { + return + } + + a.Edited = &v +} diff --git a/api/types/actions/comment_test.go b/api/types/actions/comment_test.go new file mode 100644 index 000000000..1ffc465b8 --- /dev/null +++ b/api/types/actions/comment_test.go @@ -0,0 +1,108 @@ +// SPDX-License-Identifier: Apache-2.0 + +package actions + +import ( + "reflect" + "testing" + + "github.com/go-vela/types/constants" +) + +func TestTypes_Comment_Getters(t *testing.T) { + // setup tests + tests := []struct { + actions *Comment + want *Comment + }{ + { + actions: testComment(), + want: testComment(), + }, + { + actions: new(Comment), + want: new(Comment), + }, + } + + // run tests + for _, test := range tests { + if test.actions.GetCreated() != test.want.GetCreated() { + t.Errorf("GetCreated is %v, want %v", test.actions.GetCreated(), test.want.GetCreated()) + } + + if test.actions.GetEdited() != test.want.GetEdited() { + t.Errorf("GetEdited is %v, want %v", test.actions.GetEdited(), test.want.GetEdited()) + } + } +} + +func TestTypes_Comment_Setters(t *testing.T) { + // setup types + var a *Comment + + // setup tests + tests := []struct { + actions *Comment + want *Comment + }{ + { + actions: testComment(), + want: testComment(), + }, + { + actions: a, + want: new(Comment), + }, + } + + // run tests + for _, test := range tests { + test.actions.SetCreated(test.want.GetCreated()) + test.actions.SetEdited(test.want.GetEdited()) + + if test.actions.GetCreated() != test.want.GetCreated() { + t.Errorf("SetCreated is %v, want %v", test.actions.GetCreated(), test.want.GetCreated()) + } + + if test.actions.GetEdited() != test.want.GetEdited() { + t.Errorf("SetEdited is %v, want %v", test.actions.GetEdited(), test.want.GetEdited()) + } + } +} + +func TestTypes_Comment_FromMask(t *testing.T) { + // setup types + mask := testMask() + + want := testComment() + + // run test + got := new(Comment).FromMask(mask) + + if !reflect.DeepEqual(got, want) { + t.Errorf("FromMask is %v, want %v", got, want) + } +} + +func TestTypes_Comment_ToMask(t *testing.T) { + // setup types + actions := testComment() + + want := int64(constants.AllowCommentCreate) + + // run test + got := actions.ToMask() + + if want != got { + t.Errorf("ToMask is %v, want %v", got, want) + } +} + +func testComment() *Comment { + comment := new(Comment) + comment.SetCreated(true) + comment.SetEdited(false) + + return comment +} diff --git a/api/types/actions/deploy.go b/api/types/actions/deploy.go new file mode 100644 index 000000000..5dd7a4242 --- /dev/null +++ b/api/types/actions/deploy.go @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: Apache-2.0 +// +//nolint:dupl // similar code to schedule.go +package actions + +import "github.com/go-vela/types/constants" + +// Deploy is the API representation of the various actions associated +// with the deploy event webhook from the SCM. +type Deploy struct { + Created *bool `json:"created"` +} + +// FromMask returns the Deploy type resulting from the provided integer mask. +func (a *Deploy) FromMask(mask int64) *Deploy { + a.SetCreated(mask&constants.AllowDeployCreate > 0) + + return a +} + +// ToMask returns the integer mask of the values for the Deploy set. +func (a *Deploy) ToMask() int64 { + mask := int64(0) + + if a.GetCreated() { + mask = mask | constants.AllowDeployCreate + } + + return mask +} + +// GetCreated returns the Created field from the provided Deploy. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (a *Deploy) GetCreated() bool { + // return zero value if Deploy type or Created field is nil + if a == nil || a.Created == nil { + return false + } + + return *a.Created +} + +// SetCreated sets the Deploy Created field. +// +// When the provided Deploy type is nil, it +// will set nothing and immediately return. +func (a *Deploy) SetCreated(v bool) { + // return if Deploy type is nil + if a == nil { + return + } + + a.Created = &v +} diff --git a/api/types/actions/deploy_test.go b/api/types/actions/deploy_test.go new file mode 100644 index 000000000..373407c7f --- /dev/null +++ b/api/types/actions/deploy_test.go @@ -0,0 +1,98 @@ +// SPDX-License-Identifier: Apache-2.0 + +package actions + +import ( + "reflect" + "testing" + + "github.com/go-vela/types/constants" +) + +func TestTypes_Deploy_Getters(t *testing.T) { + // setup tests + tests := []struct { + actions *Deploy + want *Deploy + }{ + { + actions: testDeploy(), + want: testDeploy(), + }, + { + actions: new(Deploy), + want: new(Deploy), + }, + } + + // run tests + for _, test := range tests { + if test.actions.GetCreated() != test.want.GetCreated() { + t.Errorf("GetCreated is %v, want %v", test.actions.GetCreated(), test.want.GetCreated()) + } + } +} + +func TestTypes_Deploy_Setters(t *testing.T) { + // setup types + var a *Deploy + + // setup tests + tests := []struct { + actions *Deploy + want *Deploy + }{ + { + actions: testDeploy(), + want: testDeploy(), + }, + { + actions: a, + want: new(Deploy), + }, + } + + // run tests + for _, test := range tests { + test.actions.SetCreated(test.want.GetCreated()) + + if test.actions.GetCreated() != test.want.GetCreated() { + t.Errorf("SetCreated is %v, want %v", test.actions.GetCreated(), test.want.GetCreated()) + } + } +} + +func TestTypes_Deploy_FromMask(t *testing.T) { + // setup types + mask := testMask() + + want := testDeploy() + + // run test + got := new(Deploy).FromMask(mask) + + if !reflect.DeepEqual(got, want) { + t.Errorf("FromMask is %v, want %v", got, want) + } +} + +func TestTypes_Deploy_ToMask(t *testing.T) { + // setup types + actions := testDeploy() + + want := int64(constants.AllowDeployCreate) + + // run test + got := actions.ToMask() + + if want != got { + t.Errorf("ToMask is %v, want %v", got, want) + } +} + +func testDeploy() *Deploy { + deploy := new(Deploy) + deploy.SetCreated(true) + + return deploy +} diff --git a/api/types/actions/pull.go b/api/types/actions/pull.go new file mode 100644 index 000000000..fd35fe7eb --- /dev/null +++ b/api/types/actions/pull.go @@ -0,0 +1,203 @@ +// SPDX-License-Identifier: Apache-2.0 + +package actions + +import "github.com/go-vela/types/constants" + +// Pull is the API representation of the various actions associated +// with the pull_request event webhook from the SCM. +type Pull struct { + Opened *bool `json:"opened"` + Edited *bool `json:"edited"` + Synchronize *bool `json:"synchronize"` + Reopened *bool `json:"reopened"` + Labeled *bool `json:"labeled"` + Unlabeled *bool `json:"unlabeled"` +} + +// FromMask returns the Pull type resulting from the provided integer mask. +func (a *Pull) FromMask(mask int64) *Pull { + a.SetOpened(mask&constants.AllowPullOpen > 0) + a.SetSynchronize(mask&constants.AllowPullSync > 0) + a.SetEdited(mask&constants.AllowPullEdit > 0) + a.SetReopened(mask&constants.AllowPullReopen > 0) + a.SetLabeled(mask&constants.AllowPullLabel > 0) + a.SetUnlabeled(mask&constants.AllowPullUnlabel > 0) + + return a +} + +// ToMask returns the integer mask of the values for the Pull set. +func (a *Pull) ToMask() int64 { + mask := int64(0) + + if a.GetOpened() { + mask = mask | constants.AllowPullOpen + } + + if a.GetSynchronize() { + mask = mask | constants.AllowPullSync + } + + if a.GetEdited() { + mask = mask | constants.AllowPullEdit + } + + if a.GetReopened() { + mask = mask | constants.AllowPullReopen + } + + if a.GetLabeled() { + mask = mask | constants.AllowPullLabel + } + + if a.GetUnlabeled() { + mask = mask | constants.AllowPullUnlabel + } + + return mask +} + +// GetOpened returns the Opened field from the provided Pull. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (a *Pull) GetOpened() bool { + // return zero value if Pull type or Opened field is nil + if a == nil || a.Opened == nil { + return false + } + + return *a.Opened +} + +// GetSynchronize returns the Synchronize field from the provided Pull. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (a *Pull) GetSynchronize() bool { + // return zero value if Pull type or Synchronize field is nil + if a == nil || a.Synchronize == nil { + return false + } + + return *a.Synchronize +} + +// GetEdited returns the Edited field from the provided Pull. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (a *Pull) GetEdited() bool { + // return zero value if Pull type or Edited field is nil + if a == nil || a.Edited == nil { + return false + } + + return *a.Edited +} + +// GetReopened returns the Reopened field from the provided Pull. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (a *Pull) GetReopened() bool { + // return zero value if Pull type or Reopened field is nil + if a == nil || a.Reopened == nil { + return false + } + + return *a.Reopened +} + +// GetLabeled returns the Labeled field from the provided Pull. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (a *Pull) GetLabeled() bool { + // return zero value if Pull type or Labeled field is nil + if a == nil || a.Labeled == nil { + return false + } + + return *a.Labeled +} + +// GetUnlabeled returns the Unlabeled field from the provided Pull. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (a *Pull) GetUnlabeled() bool { + // return zero value if Pull type or Unlabeled field is nil + if a == nil || a.Unlabeled == nil { + return false + } + + return *a.Unlabeled +} + +// SetOpened sets the Pull Opened field. +// +// When the provided Pull type is nil, it +// will set nothing and immediately return. +func (a *Pull) SetOpened(v bool) { + // return if Pull type is nil + if a == nil { + return + } + + a.Opened = &v +} + +// SetSynchronize sets the Pull Synchronize field. +// +// When the provided Pull type is nil, it +// will set nothing and immediately return. +func (a *Pull) SetSynchronize(v bool) { + // return if Pull type is nil + if a == nil { + return + } + + a.Synchronize = &v +} + +// SetEdited sets the Pull Edited field. +// +// When the provided Pull type is nil, it +// will set nothing and immediately return. +func (a *Pull) SetEdited(v bool) { + // return if Pull type is nil + if a == nil { + return + } + + a.Edited = &v +} + +// SetReopened sets the Pull Reopened field. +// +// When the provided Pull type is nil, it +// will set nothing and immediately return. +func (a *Pull) SetReopened(v bool) { + // return if Pull type is nil + if a == nil { + return + } + + a.Reopened = &v +} + +// SetLabeled sets the Pull Labeled field. +// +// When the provided Pull type is nil, it +// will set nothing and immediately return. +func (a *Pull) SetLabeled(v bool) { + // return if Pull type is nil + if a == nil { + return + } + + a.Labeled = &v +} + +// SetUnlabeled sets the Pull Unlabeled field. +// +// When the provided Pull type is nil, it +// will set nothing and immediately return. +func (a *Pull) SetUnlabeled(v bool) { + // return if Pull type is nil + if a == nil { + return + } + + a.Unlabeled = &v +} diff --git a/api/types/actions/pull_test.go b/api/types/actions/pull_test.go new file mode 100644 index 000000000..0ff17d6ad --- /dev/null +++ b/api/types/actions/pull_test.go @@ -0,0 +1,148 @@ +// SPDX-License-Identifier: Apache-2.0 + +package actions + +import ( + "reflect" + "testing" + + "github.com/go-vela/types/constants" +) + +func TestActions_Pull_Getters(t *testing.T) { + // setup tests + tests := []struct { + actions *Pull + want *Pull + }{ + { + actions: testPull(), + want: testPull(), + }, + { + actions: new(Pull), + want: new(Pull), + }, + } + + // run tests + for _, test := range tests { + if test.actions.GetOpened() != test.want.GetOpened() { + t.Errorf("GetOpened is %v, want %v", test.actions.GetOpened(), test.want.GetOpened()) + } + + if test.actions.GetSynchronize() != test.want.GetSynchronize() { + t.Errorf("GetSynchronize is %v, want %v", test.actions.GetSynchronize(), test.want.GetSynchronize()) + } + + if test.actions.GetEdited() != test.want.GetEdited() { + t.Errorf("GetEdited is %v, want %v", test.actions.GetEdited(), test.want.GetEdited()) + } + + if test.actions.GetReopened() != test.want.GetReopened() { + t.Errorf("GetReopened is %v, want %v", test.actions.GetReopened(), test.want.GetReopened()) + } + + if test.actions.GetLabeled() != test.want.GetLabeled() { + t.Errorf("GetLabeled is %v, want %v", test.actions.GetLabeled(), test.want.GetLabeled()) + } + + if test.actions.GetUnlabeled() != test.want.GetUnlabeled() { + t.Errorf("GetUnlabeled is %v, want %v", test.actions.GetUnlabeled(), test.want.GetUnlabeled()) + } + } +} + +func TestActions_Pull_Setters(t *testing.T) { + // setup types + var a *Pull + + // setup tests + tests := []struct { + actions *Pull + want *Pull + }{ + { + actions: testPull(), + want: testPull(), + }, + { + actions: a, + want: new(Pull), + }, + } + + // run tests + for _, test := range tests { + test.actions.SetOpened(test.want.GetOpened()) + test.actions.SetSynchronize(test.want.GetSynchronize()) + test.actions.SetEdited(test.want.GetEdited()) + test.actions.SetReopened(test.want.GetReopened()) + test.actions.SetLabeled(test.want.GetLabeled()) + test.actions.SetUnlabeled(test.want.GetUnlabeled()) + + if test.actions.GetOpened() != test.want.GetOpened() { + t.Errorf("SetOpened is %v, want %v", test.actions.GetOpened(), test.want.GetOpened()) + } + + if test.actions.GetSynchronize() != test.want.GetSynchronize() { + t.Errorf("SetSynchronize is %v, want %v", test.actions.GetSynchronize(), test.want.GetSynchronize()) + } + + if test.actions.GetEdited() != test.want.GetEdited() { + t.Errorf("SetEdited is %v, want %v", test.actions.GetEdited(), test.want.GetEdited()) + } + + if test.actions.GetReopened() != test.want.GetReopened() { + t.Errorf("SetReopened is %v, want %v", test.actions.GetReopened(), test.want.GetReopened()) + } + + if test.actions.GetLabeled() != test.want.GetLabeled() { + t.Errorf("SetLabeled is %v, want %v", test.actions.GetLabeled(), test.want.GetLabeled()) + } + + if test.actions.GetUnlabeled() != test.want.GetUnlabeled() { + t.Errorf("SetUnlabeled is %v, want %v", test.actions.GetUnlabeled(), test.want.GetUnlabeled()) + } + } +} + +func TestActions_Pull_FromMask(t *testing.T) { + // setup types + mask := testMask() + + want := testPull() + + // run test + got := new(Pull).FromMask(mask) + + if !reflect.DeepEqual(got, want) { + t.Errorf("FromMask is %v, want %v", got, want) + } +} + +func TestActions_Pull_ToMask(t *testing.T) { + // setup types + actions := testPull() + + want := int64(constants.AllowPullOpen | constants.AllowPullSync | constants.AllowPullReopen | constants.AllowPullUnlabel) + + // run test + got := actions.ToMask() + + if want != got { + t.Errorf("ToMask is %v, want %v", got, want) + } +} + +func testPull() *Pull { + pr := new(Pull) + pr.SetOpened(true) + pr.SetSynchronize(true) + pr.SetEdited(false) + pr.SetReopened(true) + pr.SetLabeled(false) + pr.SetUnlabeled(true) + + return pr +} diff --git a/api/types/actions/push.go b/api/types/actions/push.go new file mode 100644 index 000000000..6d5b24a6f --- /dev/null +++ b/api/types/actions/push.go @@ -0,0 +1,143 @@ +// SPDX-License-Identifier: Apache-2.0 + +package actions + +import "github.com/go-vela/types/constants" + +// Push is the API representation of the various actions associated +// with the push event webhook from the SCM. +type Push struct { + Branch *bool `json:"branch"` + Tag *bool `json:"tag"` + DeleteBranch *bool `json:"delete_branch"` + DeleteTag *bool `json:"delete_tag"` +} + +// FromMask returns the Push type resulting from the provided integer mask. +func (a *Push) FromMask(mask int64) *Push { + a.SetBranch(mask&constants.AllowPushBranch > 0) + a.SetTag(mask&constants.AllowPushTag > 0) + a.SetDeleteBranch(mask&constants.AllowPushDeleteBranch > 0) + a.SetDeleteTag(mask&constants.AllowPushDeleteTag > 0) + + return a +} + +// ToMask returns the integer mask of the values for the Push set. +func (a *Push) ToMask() int64 { + mask := int64(0) + + if a.GetBranch() { + mask = mask | constants.AllowPushBranch + } + + if a.GetTag() { + mask = mask | constants.AllowPushTag + } + + if a.GetDeleteBranch() { + mask = mask | constants.AllowPushDeleteBranch + } + + if a.GetDeleteTag() { + mask = mask | constants.AllowPushDeleteTag + } + + return mask +} + +// GetBranch returns the Branch field from the provided Push. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (a *Push) GetBranch() bool { + // return zero value if Push type or Branch field is nil + if a == nil || a.Branch == nil { + return false + } + + return *a.Branch +} + +// GetTag returns the Tag field from the provided Push. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (a *Push) GetTag() bool { + // return zero value if Push type or Tag field is nil + if a == nil || a.Tag == nil { + return false + } + + return *a.Tag +} + +// GetDeleteBranch returns the DeleteBranch field from the provided Push. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (a *Push) GetDeleteBranch() bool { + // return zero value if Push type or DeleteBranch field is nil + if a == nil || a.DeleteBranch == nil { + return false + } + + return *a.DeleteBranch +} + +// GetDeleteTag returns the DeleteTag field from the provided Push. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (a *Push) GetDeleteTag() bool { + // return zero value if Push type or DeleteTag field is nil + if a == nil || a.DeleteTag == nil { + return false + } + + return *a.DeleteTag +} + +// SetBranch sets the Push Branch field. +// +// When the provided Push type is nil, it +// will set nothing and immediately return. +func (a *Push) SetBranch(v bool) { + // return if Events type is nil + if a == nil { + return + } + + a.Branch = &v +} + +// SetTag sets the Push Tag field. +// +// When the provided Push type is nil, it +// will set nothing and immediately return. +func (a *Push) SetTag(v bool) { + // return if Events type is nil + if a == nil { + return + } + + a.Tag = &v +} + +// SetDeleteBranch sets the Push DeleteBranch field. +// +// When the provided Push type is nil, it +// will set nothing and immediately return. +func (a *Push) SetDeleteBranch(v bool) { + // return if Events type is nil + if a == nil { + return + } + + a.DeleteBranch = &v +} + +// SetDeleteTag sets the Push DeleteTag field. +// +// When the provided Push type is nil, it +// will set nothing and immediately return. +func (a *Push) SetDeleteTag(v bool) { + // return if Events type is nil + if a == nil { + return + } + + a.DeleteTag = &v +} diff --git a/api/types/actions/push_test.go b/api/types/actions/push_test.go new file mode 100644 index 000000000..259f7db2b --- /dev/null +++ b/api/types/actions/push_test.go @@ -0,0 +1,136 @@ +// SPDX-License-Identifier: Apache-2.0 + +package actions + +import ( + "reflect" + "testing" + + "github.com/go-vela/types/constants" +) + +func TestTypes_Push_Getters(t *testing.T) { + // setup tests + tests := []struct { + actions *Push + want *Push + }{ + { + actions: testPush(), + want: testPush(), + }, + { + actions: new(Push), + want: new(Push), + }, + } + + // run tests + for _, test := range tests { + if test.actions.GetBranch() != test.want.GetBranch() { + t.Errorf("GetBranch is %v, want %v", test.actions.GetBranch(), test.want.GetBranch()) + } + + if test.actions.GetTag() != test.want.GetTag() { + t.Errorf("GetTag is %v, want %v", test.actions.GetTag(), test.want.GetTag()) + } + } +} + +func TestTypes_Push_Setters(t *testing.T) { + // setup types + var a *Push + + // setup tests + tests := []struct { + actions *Push + want *Push + }{ + { + actions: testPush(), + want: testPush(), + }, + { + actions: a, + want: new(Push), + }, + } + + // run tests + for _, test := range tests { + test.actions.SetBranch(test.want.GetBranch()) + test.actions.SetTag(test.want.GetTag()) + test.actions.SetDeleteBranch(test.want.GetDeleteBranch()) + test.actions.SetDeleteTag(test.want.GetDeleteTag()) + + if test.actions.GetBranch() != test.want.GetBranch() { + t.Errorf("SetBranch is %v, want %v", test.actions.GetBranch(), test.want.GetBranch()) + } + + if test.actions.GetTag() != test.want.GetTag() { + t.Errorf("SetTag is %v, want %v", test.actions.GetTag(), test.want.GetTag()) + } + + if test.actions.GetDeleteBranch() != test.want.GetDeleteBranch() { + t.Errorf("SetDeleteBranch is %v, want %v", test.actions.GetDeleteBranch(), test.want.GetDeleteBranch()) + } + + if test.actions.GetDeleteTag() != test.want.GetDeleteTag() { + t.Errorf("SetDeleteTag is %v, want %v", test.actions.GetDeleteTag(), test.want.GetDeleteTag()) + } + } +} + +func TestTypes_Push_FromMask(t *testing.T) { + // setup types + mask := testMask() + + want := testPush() + + // run test + got := new(Push).FromMask(mask) + + if !reflect.DeepEqual(got, want) { + t.Errorf("FromMask is %v, want %v", got, want) + } +} + +func TestTypes_Push_ToMask(t *testing.T) { + // setup types + actions := testPush() + + want := int64(constants.AllowPushBranch | constants.AllowPushTag | constants.AllowPushDeleteBranch | constants.AllowPushDeleteTag) + + // run test + got := actions.ToMask() + + if want != got { + t.Errorf("ToMask is %v, want %v", got, want) + } +} + +func testPush() *Push { + push := new(Push) + push.SetBranch(true) + push.SetTag(true) + push.SetDeleteBranch(true) + push.SetDeleteTag(true) + + return push +} + +func testMask() int64 { + return int64( + constants.AllowPushBranch | + constants.AllowPushTag | + constants.AllowPushDeleteBranch | + constants.AllowPushDeleteTag | + constants.AllowPullOpen | + constants.AllowPullSync | + constants.AllowPullReopen | + constants.AllowPullUnlabel | + constants.AllowDeployCreate | + constants.AllowCommentCreate | + constants.AllowSchedule, + ) +} diff --git a/api/types/actions/schedule.go b/api/types/actions/schedule.go new file mode 100644 index 000000000..bc4588d2f --- /dev/null +++ b/api/types/actions/schedule.go @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: Apache-2.0 +// +//nolint:dupl // similar code to deploy.go +package actions + +import "github.com/go-vela/types/constants" + +// Schedule is the API representation of the various actions associated +// with the schedule event. +type Schedule struct { + Run *bool `json:"run"` +} + +// FromMask returns the Schedule type resulting from the provided integer mask. +func (a *Schedule) FromMask(mask int64) *Schedule { + a.SetRun(mask&constants.AllowSchedule > 0) + + return a +} + +// ToMask returns the integer mask of the values for the Schedule set. +func (a *Schedule) ToMask() int64 { + mask := int64(0) + + if a.GetRun() { + mask = mask | constants.AllowSchedule + } + + return mask +} + +// GetRun returns the Run field from the provided Schedule. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (a *Schedule) GetRun() bool { + // return zero value if Schedule type or Run field is nil + if a == nil || a.Run == nil { + return false + } + + return *a.Run +} + +// SetRun sets the Schedule Run field. +// +// When the provided Schedule type is nil, it +// will set nothing and immediately return. +func (a *Schedule) SetRun(v bool) { + // return if Schedule type is nil + if a == nil { + return + } + + a.Run = &v +} diff --git a/api/types/actions/schedule_test.go b/api/types/actions/schedule_test.go new file mode 100644 index 000000000..8a06b0d75 --- /dev/null +++ b/api/types/actions/schedule_test.go @@ -0,0 +1,98 @@ +// SPDX-License-Identifier: Apache-2.0 + +package actions + +import ( + "reflect" + "testing" + + "github.com/go-vela/types/constants" +) + +func TestTypes_Schedule_Getters(t *testing.T) { + // setup tests + tests := []struct { + actions *Schedule + want *Schedule + }{ + { + actions: testSchedule(), + want: testSchedule(), + }, + { + actions: new(Schedule), + want: new(Schedule), + }, + } + + // run tests + for _, test := range tests { + if test.actions.GetRun() != test.want.GetRun() { + t.Errorf("GetRun is %v, want %v", test.actions.GetRun(), test.want.GetRun()) + } + } +} + +func TestTypes_Schedule_Setters(t *testing.T) { + // setup types + var a *Schedule + + // setup tests + tests := []struct { + actions *Schedule + want *Schedule + }{ + { + actions: testSchedule(), + want: testSchedule(), + }, + { + actions: a, + want: new(Schedule), + }, + } + + // run tests + for _, test := range tests { + test.actions.SetRun(test.want.GetRun()) + + if test.actions.GetRun() != test.want.GetRun() { + t.Errorf("SetRun is %v, want %v", test.actions.GetRun(), test.want.GetRun()) + } + } +} + +func TestTypes_Schedule_FromMask(t *testing.T) { + // setup types + mask := testMask() + + want := testSchedule() + + // run test + got := new(Schedule).FromMask(mask) + + if !reflect.DeepEqual(got, want) { + t.Errorf("FromMask is %v, want %v", got, want) + } +} + +func TestTypes_Schedule_ToMask(t *testing.T) { + // setup types + actions := testSchedule() + + want := int64(constants.AllowSchedule) + + // run test + got := actions.ToMask() + + if want != got { + t.Errorf("ToMask is %v, want %v", got, want) + } +} + +func testSchedule() *Schedule { + schedule := new(Schedule) + schedule.SetRun(true) + + return schedule +} diff --git a/api/types/events.go b/api/types/events.go new file mode 100644 index 000000000..9c4885667 --- /dev/null +++ b/api/types/events.go @@ -0,0 +1,338 @@ +// SPDX-License-Identifier: Apache-2.0 + +package types + +import ( + "fmt" + + "github.com/go-vela/types/constants" + "github.com/go-vela/types/library/actions" +) + +// Events is the library representation of the various events that generate a +// webhook from the SCM. +type Events struct { + Push *actions.Push `json:"push"` + PullRequest *actions.Pull `json:"pull_request"` + Deployment *actions.Deploy `json:"deployment"` + Comment *actions.Comment `json:"comment"` + Schedule *actions.Schedule `json:"schedule"` +} + +// NewEventsFromMask is an instatiation function for the Events type that +// takes in an event mask integer value and populates the nested Events struct. +func NewEventsFromMask(mask int64) *Events { + pushActions := new(actions.Push).FromMask(mask) + pullActions := new(actions.Pull).FromMask(mask) + deployActions := new(actions.Deploy).FromMask(mask) + commentActions := new(actions.Comment).FromMask(mask) + scheduleActions := new(actions.Schedule).FromMask(mask) + + e := new(Events) + + e.SetPush(pushActions) + e.SetPullRequest(pullActions) + e.SetDeployment(deployActions) + e.SetComment(commentActions) + e.SetSchedule(scheduleActions) + + return e +} + +// NewEventsFromSlice is an instantiation function for the Events type that +// takes in a slice of event strings and populates the nested Events struct. +func NewEventsFromSlice(events []string) (*Events, error) { + mask := int64(0) + + // iterate through all events provided + for _, event := range events { + switch event { + // push actions + case constants.EventPush, constants.EventPush + ":branch": + mask = mask | constants.AllowPushBranch + case constants.EventTag, constants.EventPush + ":" + constants.EventTag: + mask = mask | constants.AllowPushTag + case constants.EventDelete + ":" + constants.ActionBranch: + mask = mask | constants.AllowPushDeleteBranch + case constants.EventDelete + ":" + constants.ActionTag: + mask = mask | constants.AllowPushDeleteTag + case constants.EventDelete: + mask = mask | constants.AllowPushDeleteBranch | constants.AllowPushDeleteTag + + // pull_request actions + case constants.EventPull, constants.EventPullAlternate: + mask = mask | constants.AllowPullOpen | constants.AllowPullSync | constants.AllowPullReopen + case constants.EventPull + ":" + constants.ActionOpened: + mask = mask | constants.AllowPullOpen + case constants.EventPull + ":" + constants.ActionEdited: + mask = mask | constants.AllowPullEdit + case constants.EventPull + ":" + constants.ActionSynchronize: + mask = mask | constants.AllowPullSync + case constants.EventPull + ":" + constants.ActionReopened: + mask = mask | constants.AllowPullReopen + case constants.EventPull + ":" + constants.ActionLabeled: + mask = mask | constants.AllowPullLabel + case constants.EventPull + ":" + constants.ActionUnlabeled: + mask = mask | constants.AllowPullUnlabel + + // deployment actions + case constants.EventDeploy, constants.EventDeployAlternate, constants.EventDeploy + ":" + constants.ActionCreated: + mask = mask | constants.AllowDeployCreate + + // comment actions + case constants.EventComment: + mask = mask | constants.AllowCommentCreate | constants.AllowCommentEdit + case constants.EventComment + ":" + constants.ActionCreated: + mask = mask | constants.AllowCommentCreate + case constants.EventComment + ":" + constants.ActionEdited: + mask = mask | constants.AllowCommentEdit + + // schedule actions + case constants.EventSchedule, constants.EventSchedule + ":" + constants.ActionRun: + mask = mask | constants.AllowSchedule + + default: + return nil, fmt.Errorf("invalid event provided: %s", event) + } + } + + return NewEventsFromMask(mask), nil +} + +// Allowed determines whether or not an event + action is allowed based on whether +// its event:action is set to true in the Events struct. +func (e *Events) Allowed(event, action string) bool { + allowed := false + + // if there is an action, create `event:action` comparator string + if len(action) > 0 { + event = event + ":" + action + } + + switch event { + case constants.EventPush: + allowed = e.GetPush().GetBranch() + case constants.EventPull + ":" + constants.ActionOpened: + allowed = e.GetPullRequest().GetOpened() + case constants.EventPull + ":" + constants.ActionSynchronize: + allowed = e.GetPullRequest().GetSynchronize() + case constants.EventPull + ":" + constants.ActionEdited: + allowed = e.GetPullRequest().GetEdited() + case constants.EventPull + ":" + constants.ActionReopened: + allowed = e.GetPullRequest().GetReopened() + case constants.EventPull + ":" + constants.ActionLabeled: + allowed = e.GetPullRequest().GetLabeled() + case constants.EventPull + ":" + constants.ActionUnlabeled: + allowed = e.GetPullRequest().GetUnlabeled() + case constants.EventTag: + allowed = e.GetPush().GetTag() + case constants.EventComment + ":" + constants.ActionCreated: + allowed = e.GetComment().GetCreated() + case constants.EventComment + ":" + constants.ActionEdited: + allowed = e.GetComment().GetEdited() + case constants.EventDeploy: + allowed = e.GetDeployment().GetCreated() + case constants.EventSchedule: + allowed = e.GetSchedule().GetRun() + case constants.EventDelete + ":" + constants.ActionBranch: + allowed = e.GetPush().GetDeleteBranch() + case constants.EventDelete + ":" + constants.ActionTag: + allowed = e.GetPush().GetDeleteTag() + } + + return allowed +} + +// List is an Events method that generates a comma-separated list of event:action +// combinations that are allowed for the repo. +func (e *Events) List() []string { + eventSlice := []string{} + + if e.GetPush().GetBranch() { + eventSlice = append(eventSlice, constants.EventPush) + } + + if e.GetPullRequest().GetOpened() { + eventSlice = append(eventSlice, constants.EventPull+":"+constants.ActionOpened) + } + + if e.GetPullRequest().GetSynchronize() { + eventSlice = append(eventSlice, constants.EventPull+":"+constants.ActionSynchronize) + } + + if e.GetPullRequest().GetEdited() { + eventSlice = append(eventSlice, constants.EventPull+":"+constants.ActionEdited) + } + + if e.GetPullRequest().GetReopened() { + eventSlice = append(eventSlice, constants.EventPull+":"+constants.ActionReopened) + } + + if e.GetPullRequest().GetLabeled() { + eventSlice = append(eventSlice, constants.EventPull+":"+constants.ActionLabeled) + } + + if e.GetPullRequest().GetUnlabeled() { + eventSlice = append(eventSlice, constants.EventPull+":"+constants.ActionUnlabeled) + } + + if e.GetPush().GetTag() { + eventSlice = append(eventSlice, constants.EventTag) + } + + if e.GetDeployment().GetCreated() { + eventSlice = append(eventSlice, constants.EventDeploy) + } + + if e.GetComment().GetCreated() { + eventSlice = append(eventSlice, constants.EventComment+":"+constants.ActionCreated) + } + + if e.GetComment().GetEdited() { + eventSlice = append(eventSlice, constants.EventComment+":"+constants.ActionEdited) + } + + if e.GetSchedule().GetRun() { + eventSlice = append(eventSlice, constants.EventSchedule) + } + + if e.GetPush().GetDeleteBranch() { + eventSlice = append(eventSlice, constants.EventDelete+":"+constants.ActionBranch) + } + + if e.GetPush().GetDeleteTag() { + eventSlice = append(eventSlice, constants.EventDelete+":"+constants.ActionTag) + } + + return eventSlice +} + +// ToDatabase is an Events method that converts a nested Events struct into an integer event mask. +func (e *Events) ToDatabase() int64 { + return 0 | + e.GetPush().ToMask() | + e.GetPullRequest().ToMask() | + e.GetComment().ToMask() | + e.GetDeployment().ToMask() | + e.GetSchedule().ToMask() +} + +// GetPush returns the Push field from the provided Events. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (e *Events) GetPush() *actions.Push { + // return zero value if Events type or Push field is nil + if e == nil || e.Push == nil { + return new(actions.Push) + } + + return e.Push +} + +// GetPullRequest returns the PullRequest field from the provided Events. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (e *Events) GetPullRequest() *actions.Pull { + // return zero value if Events type or PullRequest field is nil + if e == nil || e.PullRequest == nil { + return new(actions.Pull) + } + + return e.PullRequest +} + +// GetDeployment returns the Deployment field from the provided Events. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (e *Events) GetDeployment() *actions.Deploy { + // return zero value if Events type or Deployment field is nil + if e == nil || e.Deployment == nil { + return new(actions.Deploy) + } + + return e.Deployment +} + +// GetComment returns the Comment field from the provided Events. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (e *Events) GetComment() *actions.Comment { + // return zero value if Events type or Comment field is nil + if e == nil || e.Comment == nil { + return new(actions.Comment) + } + + return e.Comment +} + +// GetSchedule returns the Schedule field from the provided Events. If the object is nil, +// or the field within the object is nil, it returns the zero value instead. +func (e *Events) GetSchedule() *actions.Schedule { + // return zero value if Events type or Schedule field is nil + if e == nil || e.Schedule == nil { + return new(actions.Schedule) + } + + return e.Schedule +} + +// SetPush sets the Events Push field. +// +// When the provided Events type is nil, it +// will set nothing and immediately return. +func (e *Events) SetPush(v *actions.Push) { + // return if Events type is nil + if e == nil { + return + } + + e.Push = v +} + +// SetPullRequest sets the Events PullRequest field. +// +// When the provided Events type is nil, it +// will set nothing and immediately return. +func (e *Events) SetPullRequest(v *actions.Pull) { + // return if Events type is nil + if e == nil { + return + } + + e.PullRequest = v +} + +// SetDeployment sets the Events Deployment field. +// +// When the provided Events type is nil, it +// will set nothing and immediately return. +func (e *Events) SetDeployment(v *actions.Deploy) { + // return if Events type is nil + if e == nil { + return + } + + e.Deployment = v +} + +// SetComment sets the Events Comment field. +// +// When the provided Events type is nil, it +// will set nothing and immediately return. +func (e *Events) SetComment(v *actions.Comment) { + // return if Events type is nil + if e == nil { + return + } + + e.Comment = v +} + +// SetSchedule sets the Events Schedule field. +// +// When the provided Events type is nil, it +// will set nothing and immediately return. +func (e *Events) SetSchedule(v *actions.Schedule) { + // return if Events type is nil + if e == nil { + return + } + + e.Schedule = v +} diff --git a/api/types/events_test.go b/api/types/events_test.go new file mode 100644 index 000000000..e01d8d1f8 --- /dev/null +++ b/api/types/events_test.go @@ -0,0 +1,424 @@ +// SPDX-License-Identifier: Apache-2.0 + +package types + +import ( + "reflect" + "testing" + + "github.com/go-vela/types/constants" + "github.com/go-vela/types/library/actions" + "github.com/google/go-cmp/cmp" +) + +func TestTypes_Events_Getters(t *testing.T) { + // setup types + eventsOne, eventsTwo := testEvents() + + // setup tests + tests := []struct { + events *Events + want *Events + }{ + { + events: eventsOne, + want: eventsOne, + }, + { + events: eventsTwo, + want: eventsTwo, + }, + { + events: new(Events), + want: new(Events), + }, + } + + // run tests + for _, test := range tests { + if !reflect.DeepEqual(test.events.GetPush(), test.want.GetPush()) { + t.Errorf("GetPush is %v, want %v", test.events.GetPush(), test.want.GetPush()) + } + + if !reflect.DeepEqual(test.events.GetPullRequest(), test.want.GetPullRequest()) { + t.Errorf("GetPullRequest is %v, want %v", test.events.GetPush(), test.want.GetPush()) + } + + if !reflect.DeepEqual(test.events.GetDeployment(), test.want.GetDeployment()) { + t.Errorf("GetDeployment is %v, want %v", test.events.GetPush(), test.want.GetPush()) + } + + if !reflect.DeepEqual(test.events.GetComment(), test.want.GetComment()) { + t.Errorf("GetComment is %v, want %v", test.events.GetPush(), test.want.GetPush()) + } + + if !reflect.DeepEqual(test.events.GetSchedule(), test.want.GetSchedule()) { + t.Errorf("GetSchedule is %v, want %v", test.events.GetSchedule(), test.want.GetSchedule()) + } + } +} + +func TestTypes_Events_Setters(t *testing.T) { + // setup types + var e *Events + + eventsOne, eventsTwo := testEvents() + + // setup tests + tests := []struct { + events *Events + want *Events + }{ + { + events: eventsOne, + want: eventsOne, + }, + { + events: eventsTwo, + want: eventsTwo, + }, + { + events: e, + want: new(Events), + }, + } + + // run tests + for _, test := range tests { + test.events.SetPush(test.want.GetPush()) + test.events.SetPullRequest(test.want.GetPullRequest()) + test.events.SetDeployment(test.want.GetDeployment()) + test.events.SetComment(test.want.GetComment()) + test.events.SetSchedule(test.want.GetSchedule()) + + if !reflect.DeepEqual(test.events.GetPush(), test.want.GetPush()) { + t.Errorf("SetPush is %v, want %v", test.events.GetPush(), test.want.GetPush()) + } + + if !reflect.DeepEqual(test.events.GetPullRequest(), test.want.GetPullRequest()) { + t.Errorf("SetPullRequest is %v, want %v", test.events.GetPullRequest(), test.want.GetPullRequest()) + } + + if !reflect.DeepEqual(test.events.GetDeployment(), test.want.GetDeployment()) { + t.Errorf("SetDeployment is %v, want %v", test.events.GetDeployment(), test.want.GetDeployment()) + } + + if !reflect.DeepEqual(test.events.GetComment(), test.want.GetComment()) { + t.Errorf("SetComment is %v, want %v", test.events.GetComment(), test.want.GetComment()) + } + + if !reflect.DeepEqual(test.events.GetSchedule(), test.want.GetSchedule()) { + t.Errorf("SetSchedule is %v, want %v", test.events.GetSchedule(), test.want.GetSchedule()) + } + } +} + +func TestTypes_Events_List(t *testing.T) { + // setup types + eventsOne, eventsTwo := testEvents() + + wantOne := []string{ + "push", + "pull_request:opened", + "pull_request:synchronize", + "pull_request:reopened", + "pull_request:unlabeled", + "tag", + "comment:created", + "schedule", + "delete:branch", + } + + wantTwo := []string{ + "pull_request:edited", + "pull_request:labeled", + "deployment", + "comment:edited", + "delete:tag", + } + + // run test + gotOne := eventsOne.List() + + if diff := cmp.Diff(wantOne, gotOne); diff != "" { + t.Errorf("(List: -want +got):\n%s", diff) + } + + gotTwo := eventsTwo.List() + + if diff := cmp.Diff(wantTwo, gotTwo); diff != "" { + t.Errorf("(List Inverse: -want +got):\n%s", diff) + } +} + +func TestTypes_Events_NewEventsFromMask_ToDatabase(t *testing.T) { + // setup mask + maskOne := int64( + constants.AllowPushBranch | + constants.AllowPushTag | + constants.AllowPushDeleteBranch | + constants.AllowPullOpen | + constants.AllowPullSync | + constants.AllowPullReopen | + constants.AllowPullUnlabel | + constants.AllowCommentCreate | + constants.AllowSchedule, + ) + + maskTwo := int64( + constants.AllowPushDeleteTag | + constants.AllowPullEdit | + constants.AllowCommentEdit | + constants.AllowPullLabel | + constants.AllowDeployCreate, + ) + + wantOne, wantTwo := testEvents() + + // run test + gotOne := NewEventsFromMask(maskOne) + + if diff := cmp.Diff(wantOne, gotOne); diff != "" { + t.Errorf("(NewEventsFromMask: -want +got):\n%s", diff) + } + + gotTwo := NewEventsFromMask(maskTwo) + + if diff := cmp.Diff(wantTwo, gotTwo); diff != "" { + t.Errorf("(NewEventsFromMask Inverse: -want +got):\n%s", diff) + } + + // ensure ToDatabase maps back to masks + if gotOne.ToDatabase() != maskOne { + t.Errorf("ToDatabase returned %d, want %d", gotOne.ToDatabase(), maskOne) + } + + if gotTwo.ToDatabase() != maskTwo { + t.Errorf("ToDatabase returned %d, want %d", gotTwo.ToDatabase(), maskTwo) + } +} + +func Test_NewEventsFromSlice(t *testing.T) { + // setup types + tBool := true + fBool := false + + e1, e2 := testEvents() + + // setup tests + tests := []struct { + name string + events []string + want *Events + failure bool + }{ + { + name: "action specific events to e1", + events: []string{"push:branch", "push:tag", "delete:branch", "pull_request:opened", "pull_request:synchronize", "pull_request:reopened", "comment:created", "schedule:run", "pull_request:unlabeled"}, + want: e1, + failure: false, + }, + { + name: "action specific events to e2", + events: []string{"delete:tag", "pull_request:edited", "deployment:created", "comment:edited", "pull_request:labeled"}, + want: e2, + failure: false, + }, + { + name: "general events", + events: []string{"push", "pull", "deploy", "comment", "schedule", "tag", "delete"}, + want: &Events{ + Push: &actions.Push{ + Branch: &tBool, + Tag: &tBool, + DeleteBranch: &tBool, + DeleteTag: &tBool, + }, + PullRequest: &actions.Pull{ + Opened: &tBool, + Reopened: &tBool, + Edited: &fBool, + Synchronize: &tBool, + Labeled: &fBool, + Unlabeled: &fBool, + }, + Deployment: &actions.Deploy{ + Created: &tBool, + }, + Comment: &actions.Comment{ + Created: &tBool, + Edited: &tBool, + }, + Schedule: &actions.Schedule{ + Run: &tBool, + }, + }, + failure: false, + }, + { + name: "double events", + events: []string{"push", "push:branch", "pull_request", "pull_request:opened"}, + want: &Events{ + Push: &actions.Push{ + Branch: &tBool, + Tag: &fBool, + DeleteBranch: &fBool, + DeleteTag: &fBool, + }, + PullRequest: &actions.Pull{ + Opened: &tBool, + Reopened: &tBool, + Edited: &fBool, + Synchronize: &tBool, + Labeled: &fBool, + Unlabeled: &fBool, + }, + Deployment: &actions.Deploy{ + Created: &fBool, + }, + Comment: &actions.Comment{ + Created: &fBool, + Edited: &fBool, + }, + Schedule: &actions.Schedule{ + Run: &fBool, + }, + }, + failure: false, + }, + { + name: "empty events", + events: []string{}, + want: NewEventsFromMask(0), + }, + { + name: "invalid events", + events: []string{"foo:bar"}, + want: nil, + failure: true, + }, + } + + // run tests + for _, test := range tests { + got, err := NewEventsFromSlice(test.events) + + if test.failure { + if err == nil { + t.Errorf("NewEventsFromSlice should have returned err") + } + + continue + } + + if err != nil { + t.Errorf("NewEventsFromSlice returned err: %v", err) + } + + if diff := cmp.Diff(test.want, got); diff != "" { + t.Errorf("PopulateEvents failed for %s mismatch (-want +got):\n%s", test.name, diff) + } + } +} + +func TestTypes_Events_Allowed(t *testing.T) { + // setup types + eventsOne, eventsTwo := testEvents() + + // setup tests + tests := []struct { + event string + action string + want bool + }{ + {event: "push", want: true}, + {event: "tag", want: true}, + {event: "pull_request", action: "opened", want: true}, + {event: "pull_request", action: "synchronize", want: true}, + {event: "pull_request", action: "edited", want: false}, + {event: "pull_request", action: "reopened", want: true}, + {event: "pull_request", action: "labeled", want: false}, + {event: "pull_request", action: "unlabeled", want: true}, + {event: "deployment", want: false}, + {event: "comment", action: "created", want: true}, + {event: "comment", action: "edited", want: false}, + {event: "schedule", want: true}, + {event: "delete", action: "branch", want: true}, + {event: "delete", action: "tag", want: false}, + } + + for _, test := range tests { + gotOne := eventsOne.Allowed(test.event, test.action) + gotTwo := eventsTwo.Allowed(test.event, test.action) + + if gotOne != test.want { + t.Errorf("Allowed for %s/%s is %v, want %v", test.event, test.action, gotOne, test.want) + } + + if gotTwo == test.want { + t.Errorf("Allowed Inverse for %s/%s is %v, want %v", test.event, test.action, gotTwo, !test.want) + } + } +} + +// testEvents is a helper test function that returns an Events struct and its inverse for unit test coverage. +func testEvents() (*Events, *Events) { + tBool := true + fBool := false + + e1 := &Events{ + Push: &actions.Push{ + Branch: &tBool, + Tag: &tBool, + DeleteBranch: &tBool, + DeleteTag: &fBool, + }, + PullRequest: &actions.Pull{ + Opened: &tBool, + Synchronize: &tBool, + Edited: &fBool, + Reopened: &tBool, + Labeled: &fBool, + Unlabeled: &tBool, + }, + Deployment: &actions.Deploy{ + Created: &fBool, + }, + Comment: &actions.Comment{ + Created: &tBool, + Edited: &fBool, + }, + Schedule: &actions.Schedule{ + Run: &tBool, + }, + } + + e2 := &Events{ + Push: &actions.Push{ + Branch: &fBool, + Tag: &fBool, + DeleteBranch: &fBool, + DeleteTag: &tBool, + }, + PullRequest: &actions.Pull{ + Opened: &fBool, + Synchronize: &fBool, + Edited: &tBool, + Reopened: &fBool, + Labeled: &tBool, + Unlabeled: &fBool, + }, + Deployment: &actions.Deploy{ + Created: &tBool, + }, + Comment: &actions.Comment{ + Created: &fBool, + Edited: &tBool, + }, + Schedule: &actions.Schedule{ + Run: &fBool, + }, + } + + return e1, e2 +} diff --git a/api/types/executor.go b/api/types/executor.go new file mode 100644 index 000000000..d1dce126c --- /dev/null +++ b/api/types/executor.go @@ -0,0 +1,227 @@ +// SPDX-License-Identifier: Apache-2.0 + +package types + +import ( + "fmt" + "strings" + + "github.com/go-vela/types/library" + "github.com/go-vela/types/pipeline" +) + +// Executor is the library representation of an executor for a worker. +// +// swagger:model Executor +type Executor struct { + ID *int64 `json:"id,omitempty"` + Host *string `json:"host,omitempty"` + Runtime *string `json:"runtime,omitempty"` + Distribution *string `json:"distribution,omitempty"` + Build *library.Build `json:"build,omitempty"` + Repo *Repo `json:"repo,omitempty"` + Pipeline *pipeline.Build `json:"pipeline,omitempty"` +} + +// GetID returns the ID field. +// +// When the provided Executor type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (e *Executor) GetID() int64 { + // return zero value if Executor type or ID field is nil + if e == nil || e.ID == nil { + return 0 + } + + return *e.ID +} + +// GetHost returns the Host field. +// +// When the provided Executor type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (e *Executor) GetHost() string { + // return zero value if Executor type or Host field is nil + if e == nil || e.Host == nil { + return "" + } + + return *e.Host +} + +// GetRuntime returns the Runtime field. +// +// When the provided Executor type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (e *Executor) GetRuntime() string { + // return zero value if Executor type or Runtime field is nil + if e == nil || e.Runtime == nil { + return "" + } + + return *e.Runtime +} + +// GetDistribution returns the Distribution field. +// +// When the provided Executor type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (e *Executor) GetDistribution() string { + // return zero value if Executor type or Distribution field is nil + if e == nil || e.Distribution == nil { + return "" + } + + return *e.Distribution +} + +// GetBuild returns the Build field. +// +// When the provided Executor type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (e *Executor) GetBuild() library.Build { + // return zero value if Executor type or Build field is nil + if e == nil || e.Build == nil { + return library.Build{} + } + + return *e.Build +} + +// GetRepo returns the Repo field. +// +// When the provided Executor type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (e *Executor) GetRepo() Repo { + // return zero value if Executor type or Repo field is nil + if e == nil || e.Repo == nil { + return Repo{} + } + + return *e.Repo +} + +// GetPipeline returns the Pipeline field. +// +// When the provided Executor type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (e *Executor) GetPipeline() pipeline.Build { + // return zero value if Executor type or Pipeline field is nil + if e == nil || e.Pipeline == nil { + return pipeline.Build{} + } + + return *e.Pipeline +} + +// SetID sets the ID field. +// +// When the provided Executor type is nil, it +// will set nothing and immediately return. +func (e *Executor) SetID(v int64) { + // return if Executor type is nil + if e == nil { + return + } + + e.ID = &v +} + +// SetHost sets the Host field. +// +// When the provided Executor type is nil, it +// will set nothing and immediately return. +func (e *Executor) SetHost(v string) { + // return if Executor type is nil + if e == nil { + return + } + + e.Host = &v +} + +// SetRuntime sets the Runtime field. +// +// When the provided Executor type is nil, it +// will set nothing and immediately return. +func (e *Executor) SetRuntime(v string) { + // return if Executor type is nil + if e == nil { + return + } + + e.Runtime = &v +} + +// SetDistribution sets the Distribution field. +// +// When the provided Executor type is nil, it +// will set nothing and immediately return. +func (e *Executor) SetDistribution(v string) { + // return if Executor type is nil + if e == nil { + return + } + + e.Distribution = &v +} + +// SetBuild sets the Build field. +// +// When the provided Executor type is nil, it +// will set nothing and immediately return. +func (e *Executor) SetBuild(v library.Build) { + // return if Executor type is nil + if e == nil { + return + } + + e.Build = &v +} + +// SetRepo sets the Repo field. +// +// When the provided Executor type is nil, it +// will set nothing and immediately return. +func (e *Executor) SetRepo(v Repo) { + // return if Executor type is nil + if e == nil { + return + } + + e.Repo = &v +} + +// SetPipeline sets the pipeline Build field. +// +// When the provided Executor type is nil, it +// will set nothing and immediately return. +func (e *Executor) SetPipeline(v pipeline.Build) { + // return if Executor type is nil + if e == nil { + return + } + + e.Pipeline = &v +} + +// String implements the Stringer interface for the Executor type. +func (e *Executor) String() string { + return fmt.Sprintf(`{ + Build: %s, + Distribution: %s, + Host: %s, + ID: %d, + Repo: %v, + Runtime: %s, + Pipeline: %v, +}`, + strings.ReplaceAll(e.Build.String(), " ", " "), + e.GetDistribution(), + e.GetHost(), + e.GetID(), + strings.ReplaceAll(e.Repo.String(), " ", " "), + e.GetRuntime(), + e.GetPipeline(), + ) +} diff --git a/api/types/executor_test.go b/api/types/executor_test.go new file mode 100644 index 000000000..72dbac4e4 --- /dev/null +++ b/api/types/executor_test.go @@ -0,0 +1,241 @@ +// SPDX-License-Identifier: Apache-2.0 + +package types + +import ( + "fmt" + "reflect" + "strings" + "testing" + + "github.com/go-vela/types/library" + "github.com/go-vela/types/pipeline" + "github.com/go-vela/types/raw" +) + +func TestTypes_Executor_Getters(t *testing.T) { + // setup tests + tests := []struct { + executor *Executor + want *Executor + }{ + { + executor: testExecutor(), + want: testExecutor(), + }, + { + executor: new(Executor), + want: new(Executor), + }, + } + + // run tests + for _, test := range tests { + if test.executor.GetID() != test.want.GetID() { + t.Errorf("GetID is %v, want %v", test.executor.GetID(), test.want.GetID()) + } + + if test.executor.GetHost() != test.want.GetHost() { + t.Errorf("GetHost is %v, want %v", test.executor.GetHost(), test.want.GetHost()) + } + + if test.executor.GetRuntime() != test.want.GetRuntime() { + t.Errorf("GetRuntime is %v, want %v", test.executor.GetRuntime(), test.want.GetRuntime()) + } + + if test.executor.GetDistribution() != test.want.GetDistribution() { + t.Errorf("GetDistribution is %v, want %v", test.executor.GetDistribution(), test.want.GetDistribution()) + } + + if !reflect.DeepEqual(test.executor.GetBuild(), test.want.GetBuild()) { + t.Errorf("GetBuild is %v, want %v", test.executor.GetBuild(), test.want.GetBuild()) + } + + if !reflect.DeepEqual(test.executor.GetRepo(), test.want.GetRepo()) { + t.Errorf("GetRepo is %v, want %v", test.executor.GetRepo(), test.want.GetRepo()) + } + + if !reflect.DeepEqual(test.executor.GetPipeline(), test.want.GetPipeline()) { + t.Errorf("GetPipeline is %v, want %v", test.executor.GetPipeline(), test.want.GetPipeline()) + } + } +} + +func TestTypes_Executor_Setters(t *testing.T) { + // setup types + var e *Executor + + // setup tests + tests := []struct { + executor *Executor + want *Executor + }{ + { + executor: testExecutor(), + want: testExecutor(), + }, + { + executor: e, + want: new(Executor), + }, + } + + // run tests + for _, test := range tests { + test.executor.SetID(test.want.GetID()) + test.executor.SetHost(test.want.GetHost()) + test.executor.SetRuntime(test.want.GetRuntime()) + test.executor.SetDistribution(test.want.GetDistribution()) + test.executor.SetBuild(test.want.GetBuild()) + test.executor.SetRepo(test.want.GetRepo()) + test.executor.SetPipeline(test.want.GetPipeline()) + + if test.executor.GetID() != test.want.GetID() { + t.Errorf("SetID is %v, want %v", test.executor.GetID(), test.want.GetID()) + } + + if test.executor.GetHost() != test.want.GetHost() { + t.Errorf("SetHost is %v, want %v", test.executor.GetHost(), test.want.GetHost()) + } + + if test.executor.GetRuntime() != test.want.GetRuntime() { + t.Errorf("SetRuntime is %v, want %v", test.executor.GetRuntime(), test.want.GetRuntime()) + } + + if test.executor.GetDistribution() != test.want.GetDistribution() { + t.Errorf("SetDistribution is %v, want %v", test.executor.GetDistribution(), test.want.GetDistribution()) + } + + if !reflect.DeepEqual(test.executor.GetBuild(), test.want.GetBuild()) { + t.Errorf("SetBuild is %v, want %v", test.executor.GetBuild(), test.want.GetBuild()) + } + + if !reflect.DeepEqual(test.executor.GetRepo(), test.want.GetRepo()) { + t.Errorf("SetRepo is %v, want %v", test.executor.GetRepo(), test.want.GetRepo()) + } + + if !reflect.DeepEqual(test.executor.GetPipeline(), test.want.GetPipeline()) { + t.Errorf("SetPipeline is %v, want %v", test.executor.GetPipeline(), test.want.GetPipeline()) + } + } +} + +func TestTypes_Executor_String(t *testing.T) { + // setup types + e := testExecutor() + + want := fmt.Sprintf(`{ + Build: %s, + Distribution: %s, + Host: %s, + ID: %d, + Repo: %v, + Runtime: %s, + Pipeline: %v, +}`, + strings.ReplaceAll(e.Build.String(), " ", " "), + e.GetDistribution(), + e.GetHost(), + e.GetID(), + strings.ReplaceAll(e.Repo.String(), " ", " "), + e.GetRuntime(), + e.GetPipeline(), + ) + + // run test + got := e.String() + + if !reflect.DeepEqual(got, want) { + t.Errorf("String is %v, want %v", got, want) + } +} + +// testExecutor is a test helper function to create a Executor +// type with all fields set to a fake value. +func testExecutor() *Executor { + e := new(Executor) + + e.SetID(1) + e.SetHost("company.example.com") + e.SetRuntime("docker") + e.SetDistribution("linux") + e.SetBuild(*testBuild()) + e.SetRepo(*testRepo()) + e.SetPipeline(pipeline.Build{ + Version: "1", + ID: "github_octocat_1", + Steps: pipeline.ContainerSlice{ + { + ID: "step_github_octocat_1_init", + Directory: "/home/github/octocat", + Image: "#init", + Name: "init", + Number: 1, + Pull: "always", + }, + { + ID: "step_github_octocat_1_clone", + Directory: "/home/github/octocat", + Image: "target/vela-git:v0.3.0", + Name: "clone", + Number: 2, + Pull: "always", + }, + { + ID: "step_github_octocat_1_echo", + Commands: []string{"echo hello"}, + Directory: "/home/github/octocat", + Image: "alpine:latest", + Name: "echo", + Number: 3, + Pull: "always", + }, + }, + }) + + return e +} + +// testBuild is a test helper function to create a Build +// type with all fields set to a fake value. +// +// TODO: remove this function once the Build type is moved to server. +func testBuild() *library.Build { + b := new(library.Build) + + b.SetID(1) + b.SetRepoID(1) + b.SetPipelineID(1) + b.SetNumber(1) + b.SetParent(1) + b.SetEvent("push") + b.SetStatus("running") + b.SetError("") + b.SetEnqueued(1563474077) + b.SetCreated(1563474076) + b.SetStarted(1563474078) + b.SetFinished(1563474079) + b.SetDeploy("") + b.SetDeployNumber(0) + b.SetDeployPayload(raw.StringSliceMap{"foo": "test1"}) + b.SetClone("https://github.com/github/octocat.git") + b.SetSource("https://github.com/github/octocat/48afb5bdc41ad69bf22588491333f7cf71135163") + b.SetTitle("push received from https://github.com/github/octocat") + b.SetMessage("First commit...") + b.SetCommit("48afb5bdc41ad69bf22588491333f7cf71135163") + b.SetSender("OctoKitty") + b.SetAuthor("OctoKitty") + b.SetEmail("OctoKitty@github.com") + b.SetLink("https://example.company.com/github/octocat/1") + b.SetBranch("main") + b.SetRef("refs/heads/main") + b.SetBaseRef("") + b.SetHeadRef("changes") + b.SetHost("example.company.com") + b.SetRuntime("docker") + b.SetDistribution("linux") + b.SetApprovedAt(1563474076) + b.SetApprovedBy("OctoCat") + + return b +} diff --git a/api/types/repo.go b/api/types/repo.go new file mode 100644 index 000000000..fe0ad1028 --- /dev/null +++ b/api/types/repo.go @@ -0,0 +1,668 @@ +// SPDX-License-Identifier: Apache-2.0 + +package types + +import ( + "fmt" + "strings" + + "github.com/go-vela/types/library" +) + +// Repo is the API representation of a repo. +// +// swagger:model Repo +type Repo struct { + ID *int64 `json:"id,omitempty"` + Owner *library.User `json:"owner,omitempty"` + Hash *string `json:"-"` + Org *string `json:"org,omitempty"` + Name *string `json:"name,omitempty"` + FullName *string `json:"full_name,omitempty"` + Link *string `json:"link,omitempty"` + Clone *string `json:"clone,omitempty"` + Branch *string `json:"branch,omitempty"` + Topics *[]string `json:"topics,omitempty"` + BuildLimit *int64 `json:"build_limit,omitempty"` + Timeout *int64 `json:"timeout,omitempty"` + Counter *int `json:"counter,omitempty"` + Visibility *string `json:"visibility,omitempty"` + Private *bool `json:"private,omitempty"` + Trusted *bool `json:"trusted,omitempty"` + Active *bool `json:"active,omitempty"` + AllowEvents *Events `json:"allow_events,omitempty"` + PipelineType *string `json:"pipeline_type,omitempty"` + PreviousName *string `json:"previous_name,omitempty"` + ApproveBuild *string `json:"approve_build,omitempty"` +} + +// Environment returns a list of environment variables +// provided from the fields of the Repo type. +func (r *Repo) Environment() map[string]string { + return map[string]string{ + "VELA_REPO_ACTIVE": ToString(r.GetActive()), + "VELA_REPO_ALLOW_EVENTS": strings.Join(r.GetAllowEvents().List()[:], ","), + "VELA_REPO_BRANCH": ToString(r.GetBranch()), + "VELA_REPO_TOPICS": strings.Join(r.GetTopics()[:], ","), + "VELA_REPO_BUILD_LIMIT": ToString(r.GetBuildLimit()), + "VELA_REPO_CLONE": ToString(r.GetClone()), + "VELA_REPO_FULL_NAME": ToString(r.GetFullName()), + "VELA_REPO_LINK": ToString(r.GetLink()), + "VELA_REPO_NAME": ToString(r.GetName()), + "VELA_REPO_ORG": ToString(r.GetOrg()), + "VELA_REPO_PRIVATE": ToString(r.GetPrivate()), + "VELA_REPO_TIMEOUT": ToString(r.GetTimeout()), + "VELA_REPO_TRUSTED": ToString(r.GetTrusted()), + "VELA_REPO_VISIBILITY": ToString(r.GetVisibility()), + "VELA_REPO_PIPELINE_TYPE": ToString(r.GetPipelineType()), + "VELA_REPO_APPROVE_BUILD": ToString(r.GetApproveBuild()), + "VELA_REPO_OWNER": ToString(r.GetOwner().GetName()), + + // deprecated environment variables + "REPOSITORY_ACTIVE": ToString(r.GetActive()), + "REPOSITORY_ALLOW_EVENTS": strings.Join(r.GetAllowEvents().List()[:], ","), + "REPOSITORY_BRANCH": ToString(r.GetBranch()), + "REPOSITORY_CLONE": ToString(r.GetClone()), + "REPOSITORY_FULL_NAME": ToString(r.GetFullName()), + "REPOSITORY_LINK": ToString(r.GetLink()), + "REPOSITORY_NAME": ToString(r.GetName()), + "REPOSITORY_ORG": ToString(r.GetOrg()), + "REPOSITORY_PRIVATE": ToString(r.GetPrivate()), + "REPOSITORY_TIMEOUT": ToString(r.GetTimeout()), + "REPOSITORY_TRUSTED": ToString(r.GetTrusted()), + "REPOSITORY_VISIBILITY": ToString(r.GetVisibility()), + } +} + +// GetID returns the ID field. +// +// When the provided Repo type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (r *Repo) GetID() int64 { + // return zero value if Repo type or ID field is nil + if r == nil || r.ID == nil { + return 0 + } + + return *r.ID +} + +// GetOwner returns the Owner field. +// +// When the provided Repo type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (r *Repo) GetOwner() *library.User { + // return zero value if Repo type or Owner field is nil + if r == nil || r.Owner == nil { + return new(library.User) + } + + return r.Owner +} + +// GetHash returns the Hash field. +// +// When the provided Repo type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (r *Repo) GetHash() string { + // return zero value if Repo type or Hash field is nil + if r == nil || r.Hash == nil { + return "" + } + + return *r.Hash +} + +// GetOrg returns the Org field. +// +// When the provided Repo type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (r *Repo) GetOrg() string { + // return zero value if Repo type or Org field is nil + if r == nil || r.Org == nil { + return "" + } + + return *r.Org +} + +// GetName returns the Name field. +// +// When the provided Repo type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (r *Repo) GetName() string { + // return zero value if Repo type or Name field is nil + if r == nil || r.Name == nil { + return "" + } + + return *r.Name +} + +// GetFullName returns the FullName field. +// +// When the provided Repo type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (r *Repo) GetFullName() string { + // return zero value if Repo type or FullName field is nil + if r == nil || r.FullName == nil { + return "" + } + + return *r.FullName +} + +// GetLink returns the Link field. +// +// When the provided Repo type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (r *Repo) GetLink() string { + // return zero value if Repo type or Link field is nil + if r == nil || r.Link == nil { + return "" + } + + return *r.Link +} + +// GetClone returns the Clone field. +// +// When the provided Repo type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (r *Repo) GetClone() string { + // return zero value if Repo type or Clone field is nil + if r == nil || r.Clone == nil { + return "" + } + + return *r.Clone +} + +// GetBranch returns the Branch field. +// +// When the provided Repo type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (r *Repo) GetBranch() string { + // return zero value if Repo type or Branch field is nil + if r == nil || r.Branch == nil { + return "" + } + + return *r.Branch +} + +// GetTopics returns the Topics field. +// +// When the provided Repo type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (r *Repo) GetTopics() []string { + // return zero value if Repo type or Topics field is nil + if r == nil || r.Topics == nil { + return []string{} + } + + return *r.Topics +} + +// GetBuildLimit returns the BuildLimit field. +// +// When the provided Repo type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (r *Repo) GetBuildLimit() int64 { + // return zero value if Repo type or BuildLimit field is nil + if r == nil || r.BuildLimit == nil { + return 0 + } + + return *r.BuildLimit +} + +// GetTimeout returns the Timeout field. +// +// When the provided Repo type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (r *Repo) GetTimeout() int64 { + // return zero value if Repo type or Timeout field is nil + if r == nil || r.Timeout == nil { + return 0 + } + + return *r.Timeout +} + +// GetCounter returns the Counter field. +// +// When the provided Repo type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (r *Repo) GetCounter() int { + // return zero value if Repo type or Counter field is nil + if r == nil || r.Counter == nil { + return 0 + } + + return *r.Counter +} + +// GetVisibility returns the Visibility field. +// +// When the provided Repo type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (r *Repo) GetVisibility() string { + // return zero value if Repo type or Visibility field is nil + if r == nil || r.Visibility == nil { + return "" + } + + return *r.Visibility +} + +// GetPrivate returns the Private field. +// +// When the provided Repo type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (r *Repo) GetPrivate() bool { + // return zero value if Repo type or Private field is nil + if r == nil || r.Private == nil { + return false + } + + return *r.Private +} + +// GetTrusted returns the Trusted field. +// +// When the provided Repo type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (r *Repo) GetTrusted() bool { + // return zero value if Repo type or Trusted field is nil + if r == nil || r.Trusted == nil { + return false + } + + return *r.Trusted +} + +// GetActive returns the Active field. +// +// When the provided Repo type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (r *Repo) GetActive() bool { + // return zero value if Repo type or Active field is nil + if r == nil || r.Active == nil { + return false + } + + return *r.Active +} + +// GetAllowEvents returns the AllowEvents field. +// +// When the provided Repo type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (r *Repo) GetAllowEvents() *Events { + // return zero value if Repo type or AllowPull field is nil + if r == nil || r.AllowEvents == nil { + return new(Events) + } + + return r.AllowEvents +} + +// GetPipelineType returns the PipelineType field. +// +// When the provided Repo type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (r *Repo) GetPipelineType() string { + // return zero value if Repo type or PipelineType field is nil + if r == nil || r.PipelineType == nil { + return "" + } + + return *r.PipelineType +} + +// GetPreviousName returns the PreviousName field. +// +// When the provided Repo type is nil, or the field within +//  the type is nil, it returns the zero value for the field. +func (r *Repo) GetPreviousName() string { + // return zero value if Repo type or PreviousName field is nil + if r == nil || r.PreviousName == nil { + return "" + } + + return *r.PreviousName +} + +// GetApproveBuild returns the ApproveBuild field. +// +// When the provided Repo type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (r *Repo) GetApproveBuild() string { + // return zero value if Repo type or ApproveBuild field is nil + if r == nil || r.ApproveBuild == nil { + return "" + } + + return *r.ApproveBuild +} + +// SetID sets the ID field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetID(v int64) { + // return if Repo type is nil + if r == nil { + return + } + + r.ID = &v +} + +// SetOwner sets the Owner field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetOwner(v *library.User) { + // return if Repo type is nil + if r == nil { + return + } + + r.Owner = v +} + +// SetHash sets the Hash field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetHash(v string) { + // return if Repo type is nil + if r == nil { + return + } + + r.Hash = &v +} + +// SetOrg sets the Org field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetOrg(v string) { + // return if Repo type is nil + if r == nil { + return + } + + r.Org = &v +} + +// SetName sets the Name field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetName(v string) { + // return if Repo type is nil + if r == nil { + return + } + + r.Name = &v +} + +// SetFullName sets the FullName field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetFullName(v string) { + // return if Repo type is nil + if r == nil { + return + } + + r.FullName = &v +} + +// SetLink sets the Link field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetLink(v string) { + // return if Repo type is nil + if r == nil { + return + } + + r.Link = &v +} + +// SetClone sets the Clone field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetClone(v string) { + // return if Repo type is nil + if r == nil { + return + } + + r.Clone = &v +} + +// SetBranch sets the Branch field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetBranch(v string) { + // return if Repo type is nil + if r == nil { + return + } + + r.Branch = &v +} + +// SetTopics sets the Topics field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetTopics(v []string) { + // return if Repo type is nil + if r == nil { + return + } + + r.Topics = &v +} + +// SetBuildLimit sets the BuildLimit field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetBuildLimit(v int64) { + // return if Repo type is nil + if r == nil { + return + } + + r.BuildLimit = &v +} + +// SetTimeout sets the Timeout field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetTimeout(v int64) { + // return if Repo type is nil + if r == nil { + return + } + + r.Timeout = &v +} + +// SetCounter sets the Counter field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetCounter(v int) { + // return if Repo type is nil + if r == nil { + return + } + + r.Counter = &v +} + +// SetVisibility sets the Visibility field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetVisibility(v string) { + // return if Repo type is nil + if r == nil { + return + } + + r.Visibility = &v +} + +// SetPrivate sets the Private field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetPrivate(v bool) { + // return if Repo type is nil + if r == nil { + return + } + + r.Private = &v +} + +// SetTrusted sets the Trusted field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetTrusted(v bool) { + // return if Repo type is nil + if r == nil { + return + } + + r.Trusted = &v +} + +// SetActive sets the Active field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetActive(v bool) { + // return if Repo type is nil + if r == nil { + return + } + + r.Active = &v +} + +// SetAllowEvents sets the AllowEvents field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetAllowEvents(v *Events) { + // return if Repo type is nil + if r == nil { + return + } + + r.AllowEvents = v +} + +// SetPipelineType sets the PipelineType field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetPipelineType(v string) { + // return if Repo type is nil + if r == nil { + return + } + + r.PipelineType = &v +} + +// SetPreviousName sets the PreviousName field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetPreviousName(v string) { + // return if Repo type is nil + if r == nil { + return + } + + r.PreviousName = &v +} + +// SetApproveBuild sets the ApproveBuild field. +// +// When the provided Repo type is nil, it +// will set nothing and immediately return. +func (r *Repo) SetApproveBuild(v string) { + // return if Repo type is nil + if r == nil { + return + } + + r.ApproveBuild = &v +} + +// String implements the Stringer interface for the Repo type. +func (r *Repo) String() string { + return fmt.Sprintf(`{ + Active: %t, + AllowEvents: %s, + ApproveBuild: %s, + Branch: %s, + BuildLimit: %d, + Clone: %s, + Counter: %d, + FullName: %s, + ID: %d, + Link: %s, + Name: %s, + Org: %s, + Owner: %v, + PipelineType: %s, + PreviousName: %s, + Private: %t, + Timeout: %d, + Topics: %s, + Trusted: %t, + Visibility: %s +}`, + r.GetActive(), + r.GetAllowEvents().List(), + r.GetApproveBuild(), + r.GetBranch(), + r.GetBuildLimit(), + r.GetClone(), + r.GetCounter(), + r.GetFullName(), + r.GetID(), + r.GetLink(), + r.GetName(), + r.GetOrg(), + r.GetOwner(), + r.GetPipelineType(), + r.GetPreviousName(), + r.GetPrivate(), + r.GetTimeout(), + r.GetTopics(), + r.GetTrusted(), + r.GetVisibility(), + ) +} diff --git a/api/types/repo_test.go b/api/types/repo_test.go new file mode 100644 index 000000000..b0554d742 --- /dev/null +++ b/api/types/repo_test.go @@ -0,0 +1,371 @@ +// SPDX-License-Identifier: Apache-2.0 + +package types + +import ( + "fmt" + "reflect" + "testing" + + "github.com/go-vela/types/constants" + "github.com/go-vela/types/library" + "github.com/google/go-cmp/cmp" +) + +func TestTypes_Repo_Environment(t *testing.T) { + // setup types + want := map[string]string{ + "VELA_REPO_ACTIVE": "true", + "VELA_REPO_ALLOW_EVENTS": "push,pull_request:opened,pull_request:synchronize,pull_request:reopened,pull_request:unlabeled,tag,comment:created,schedule,delete:branch", + "VELA_REPO_BRANCH": "main", + "VELA_REPO_TOPICS": "cloud,security", + "VELA_REPO_BUILD_LIMIT": "10", + "VELA_REPO_CLONE": "https://github.com/github/octocat.git", + "VELA_REPO_FULL_NAME": "github/octocat", + "VELA_REPO_LINK": "https://github.com/github/octocat", + "VELA_REPO_NAME": "octocat", + "VELA_REPO_ORG": "github", + "VELA_REPO_PRIVATE": "false", + "VELA_REPO_TIMEOUT": "30", + "VELA_REPO_TRUSTED": "false", + "VELA_REPO_VISIBILITY": "public", + "VELA_REPO_PIPELINE_TYPE": "", + "VELA_REPO_APPROVE_BUILD": "never", + "VELA_REPO_OWNER": "octocat", + "REPOSITORY_ACTIVE": "true", + "REPOSITORY_ALLOW_EVENTS": "push,pull_request:opened,pull_request:synchronize,pull_request:reopened,pull_request:unlabeled,tag,comment:created,schedule,delete:branch", + "REPOSITORY_BRANCH": "main", + "REPOSITORY_CLONE": "https://github.com/github/octocat.git", + "REPOSITORY_FULL_NAME": "github/octocat", + "REPOSITORY_LINK": "https://github.com/github/octocat", + "REPOSITORY_NAME": "octocat", + "REPOSITORY_ORG": "github", + "REPOSITORY_PRIVATE": "false", + "REPOSITORY_TIMEOUT": "30", + "REPOSITORY_TRUSTED": "false", + "REPOSITORY_VISIBILITY": "public", + } + + // run test + got := testRepo().Environment() + + if diff := cmp.Diff(want, got); diff != "" { + t.Errorf("(Environment: -want +got):\n%s", diff) + } +} + +func TestTypes_Repo_Getters(t *testing.T) { + // setup tests + tests := []struct { + repo *Repo + want *Repo + }{ + { + repo: testRepo(), + want: testRepo(), + }, + { + repo: new(Repo), + want: new(Repo), + }, + } + + // run tests + for _, test := range tests { + if test.repo.GetID() != test.want.GetID() { + t.Errorf("GetID is %v, want %v", test.repo.GetID(), test.want.GetID()) + } + + if !reflect.DeepEqual(test.repo.GetOwner(), test.want.GetOwner()) { + t.Errorf("GetOwner is %v, want %v", test.repo.GetOwner(), test.want.GetOwner()) + } + + if test.repo.GetHash() != test.want.GetHash() { + t.Errorf("GetHash is %v, want %v", test.repo.GetHash(), test.want.GetHash()) + } + + if test.repo.GetOrg() != test.want.GetOrg() { + t.Errorf("GetOrg is %v, want %v", test.repo.GetOrg(), test.want.GetOrg()) + } + + if test.repo.GetName() != test.want.GetName() { + t.Errorf("GetName is %v, want %v", test.repo.GetName(), test.want.GetName()) + } + + if test.repo.GetFullName() != test.want.GetFullName() { + t.Errorf("GetFullName is %v, want %v", test.repo.GetFullName(), test.want.GetFullName()) + } + + if test.repo.GetLink() != test.want.GetLink() { + t.Errorf("GetLink is %v, want %v", test.repo.GetLink(), test.want.GetLink()) + } + + if test.repo.GetClone() != test.want.GetClone() { + t.Errorf("GetClone is %v, want %v", test.repo.GetClone(), test.want.GetClone()) + } + + if test.repo.GetBranch() != test.want.GetBranch() { + t.Errorf("GetBranch is %v, want %v", test.repo.GetBranch(), test.want.GetBranch()) + } + + if !reflect.DeepEqual(test.repo.GetTopics(), test.want.GetTopics()) { + t.Errorf("GetTopics is %v, want %v", test.repo.GetTopics(), test.want.GetTopics()) + } + + if test.repo.GetBuildLimit() != test.want.GetBuildLimit() { + t.Errorf("GetBuildLimit is %v, want %v", test.repo.GetBuildLimit(), test.want.GetBuildLimit()) + } + + if test.repo.GetTimeout() != test.want.GetTimeout() { + t.Errorf("GetTimeout is %v, want %v", test.repo.GetTimeout(), test.want.GetTimeout()) + } + + if test.repo.GetVisibility() != test.want.GetVisibility() { + t.Errorf("GetVisibility is %v, want %v", test.repo.GetVisibility(), test.want.GetVisibility()) + } + + if test.repo.GetPrivate() != test.want.GetPrivate() { + t.Errorf("GetPrivate is %v, want %v", test.repo.GetPrivate(), test.want.GetPrivate()) + } + + if test.repo.GetTrusted() != test.want.GetTrusted() { + t.Errorf("GetTrusted is %v, want %v", test.repo.GetTrusted(), test.want.GetTrusted()) + } + + if test.repo.GetActive() != test.want.GetActive() { + t.Errorf("GetActive is %v, want %v", test.repo.GetActive(), test.want.GetActive()) + } + + if !reflect.DeepEqual(test.repo.GetAllowEvents(), test.want.GetAllowEvents()) { + t.Errorf("GetRepo is %v, want %v", test.repo.GetAllowEvents(), test.want.GetAllowEvents()) + } + + if test.repo.GetPipelineType() != test.want.GetPipelineType() { + t.Errorf("GetPipelineType is %v, want %v", test.repo.GetPipelineType(), test.want.GetPipelineType()) + } + + if !reflect.DeepEqual(test.repo.GetPreviousName(), test.want.GetPreviousName()) { + t.Errorf("GetPreviousName is %v, want %v", test.repo.GetPreviousName(), test.want.GetPreviousName()) + } + + if test.repo.GetApproveBuild() != test.want.GetApproveBuild() { + t.Errorf("GetApproveForkBuild is %v, want %v", test.repo.GetApproveBuild(), test.want.GetApproveBuild()) + } + } +} + +func TestTypes_Repo_Setters(t *testing.T) { + // setup types + var r *Repo + + // setup tests + tests := []struct { + repo *Repo + want *Repo + }{ + { + repo: testRepo(), + want: testRepo(), + }, + { + repo: r, + want: new(Repo), + }, + } + + // run tests + for _, test := range tests { + test.repo.SetID(test.want.GetID()) + test.repo.SetOwner(test.want.GetOwner()) + test.repo.SetHash(test.want.GetHash()) + test.repo.SetOrg(test.want.GetOrg()) + test.repo.SetName(test.want.GetName()) + test.repo.SetFullName(test.want.GetFullName()) + test.repo.SetLink(test.want.GetLink()) + test.repo.SetClone(test.want.GetClone()) + test.repo.SetBranch(test.want.GetBranch()) + test.repo.SetTopics(test.want.GetTopics()) + test.repo.SetBuildLimit(test.want.GetBuildLimit()) + test.repo.SetTimeout(test.want.GetTimeout()) + test.repo.SetCounter(test.want.GetCounter()) + test.repo.SetVisibility(test.want.GetVisibility()) + test.repo.SetPrivate(test.want.GetPrivate()) + test.repo.SetTrusted(test.want.GetTrusted()) + test.repo.SetActive(test.want.GetActive()) + test.repo.SetAllowEvents(test.want.GetAllowEvents()) + test.repo.SetPipelineType(test.want.GetPipelineType()) + test.repo.SetPreviousName(test.want.GetPreviousName()) + test.repo.SetApproveBuild(test.want.GetApproveBuild()) + + if test.repo.GetID() != test.want.GetID() { + t.Errorf("SetID is %v, want %v", test.repo.GetID(), test.want.GetID()) + } + + if !reflect.DeepEqual(test.repo.GetOwner(), test.want.GetOwner()) { + t.Errorf("SetOwner is %v, want %v", test.repo.GetOwner(), test.want.GetOwner()) + } + + if test.repo.GetHash() != test.want.GetHash() { + t.Errorf("SetHash is %v, want %v", test.repo.GetHash(), test.want.GetHash()) + } + + if test.repo.GetOrg() != test.want.GetOrg() { + t.Errorf("SetOrg is %v, want %v", test.repo.GetOrg(), test.want.GetOrg()) + } + + if test.repo.GetName() != test.want.GetName() { + t.Errorf("SetName is %v, want %v", test.repo.GetName(), test.want.GetName()) + } + + if test.repo.GetFullName() != test.want.GetFullName() { + t.Errorf("SetFullName is %v, want %v", test.repo.GetFullName(), test.want.GetFullName()) + } + + if test.repo.GetLink() != test.want.GetLink() { + t.Errorf("SetLink is %v, want %v", test.repo.GetLink(), test.want.GetLink()) + } + + if test.repo.GetClone() != test.want.GetClone() { + t.Errorf("SetClone is %v, want %v", test.repo.GetClone(), test.want.GetClone()) + } + + if test.repo.GetBranch() != test.want.GetBranch() { + t.Errorf("SetBranch is %v, want %v", test.repo.GetBranch(), test.want.GetBranch()) + } + + if !reflect.DeepEqual(test.repo.GetTopics(), test.want.GetTopics()) { + t.Errorf("SetTopics is %v, want %v", test.repo.GetTopics(), test.want.GetTopics()) + } + + if test.repo.GetBuildLimit() != test.want.GetBuildLimit() { + t.Errorf("SetBuildLimit is %v, want %v", test.repo.GetBuildLimit(), test.want.GetBuildLimit()) + } + + if test.repo.GetTimeout() != test.want.GetTimeout() { + t.Errorf("SetTimeout is %v, want %v", test.repo.GetTimeout(), test.want.GetTimeout()) + } + + if test.repo.GetVisibility() != test.want.GetVisibility() { + t.Errorf("SetVisibility is %v, want %v", test.repo.GetVisibility(), test.want.GetVisibility()) + } + + if test.repo.GetPrivate() != test.want.GetPrivate() { + t.Errorf("SetPrivate is %v, want %v", test.repo.GetPrivate(), test.want.GetPrivate()) + } + + if test.repo.GetTrusted() != test.want.GetTrusted() { + t.Errorf("SetTrusted is %v, want %v", test.repo.GetTrusted(), test.want.GetTrusted()) + } + + if test.repo.GetActive() != test.want.GetActive() { + t.Errorf("SetActive is %v, want %v", test.repo.GetActive(), test.want.GetActive()) + } + + if !reflect.DeepEqual(test.repo.GetAllowEvents(), test.want.GetAllowEvents()) { + t.Errorf("GetRepo is %v, want %v", test.repo.GetAllowEvents(), test.want.GetAllowEvents()) + } + + if test.repo.GetPipelineType() != test.want.GetPipelineType() { + t.Errorf("SetPipelineType is %v, want %v", test.repo.GetPipelineType(), test.want.GetPipelineType()) + } + + if !reflect.DeepEqual(test.repo.GetPreviousName(), test.want.GetPreviousName()) { + t.Errorf("SetPreviousName is %v, want %v", test.repo.GetPreviousName(), test.want.GetPreviousName()) + } + + if test.repo.GetApproveBuild() != test.want.GetApproveBuild() { + t.Errorf("SetApproveForkBuild is %v, want %v", test.repo.GetApproveBuild(), test.want.GetApproveBuild()) + } + } +} + +func TestTypes_Repo_String(t *testing.T) { + // setup types + r := testRepo() + + want := fmt.Sprintf(`{ + Active: %t, + AllowEvents: %s, + ApproveBuild: %s, + Branch: %s, + BuildLimit: %d, + Clone: %s, + Counter: %d, + FullName: %s, + ID: %d, + Link: %s, + Name: %s, + Org: %s, + Owner: %v, + PipelineType: %s, + PreviousName: %s, + Private: %t, + Timeout: %d, + Topics: %s, + Trusted: %t, + Visibility: %s +}`, + r.GetActive(), + r.GetAllowEvents().List(), + r.GetApproveBuild(), + r.GetBranch(), + r.GetBuildLimit(), + r.GetClone(), + r.GetCounter(), + r.GetFullName(), + r.GetID(), + r.GetLink(), + r.GetName(), + r.GetOrg(), + r.GetOwner(), + r.GetPipelineType(), + r.GetPreviousName(), + r.GetPrivate(), + r.GetTimeout(), + r.GetTopics(), + r.GetTrusted(), + r.GetVisibility(), + ) + + // run test + got := r.String() + + if !reflect.DeepEqual(got, want) { + t.Errorf("String is %v, want %v", got, want) + } +} + +// testRepo is a test helper function to create a Repo +// type with all fields set to a fake value. +func testRepo() *Repo { + r := new(Repo) + + e, _ := testEvents() + + owner := new(library.User) + owner.SetID(1) + owner.SetName("octocat") + + r.SetID(1) + r.SetOwner(owner) + r.SetOrg("github") + r.SetName("octocat") + r.SetFullName("github/octocat") + r.SetLink("https://github.com/github/octocat") + r.SetClone("https://github.com/github/octocat.git") + r.SetBranch("main") + r.SetTopics([]string{"cloud", "security"}) + r.SetBuildLimit(10) + r.SetTimeout(30) + r.SetCounter(0) + r.SetVisibility("public") + r.SetPrivate(false) + r.SetTrusted(false) + r.SetActive(true) + r.SetAllowEvents(e) + r.SetPipelineType("") + r.SetPreviousName("") + r.SetApproveBuild(constants.ApproveNever) + + return r +} diff --git a/api/types/string.go b/api/types/string.go new file mode 100644 index 000000000..33ddacb59 --- /dev/null +++ b/api/types/string.go @@ -0,0 +1,90 @@ +// SPDX-License-Identifier: Apache-2.0 + +package types + +import ( + "encoding/base64" + "strconv" + "strings" + + "github.com/buildkite/yaml" + json "github.com/ghodss/yaml" +) + +// ToString is a helper function to convert +// the provided interface value to a string. +func ToString(v interface{}) string { + switch v := v.(type) { + case string: + return v + case bool: + return strconv.FormatBool(v) + case []byte: + return base64.StdEncoding.EncodeToString(v) + case float32: + return strconv.FormatFloat(float64(v), 'g', -1, 32) + case float64: + return strconv.FormatFloat(v, 'g', -1, 64) + case int: + return strconv.Itoa(v) + case int8: + return strconv.FormatInt(int64(v), 10) + case int16: + return strconv.FormatInt(int64(v), 10) + case int32: + return strconv.FormatInt(int64(v), 10) + case int64: + return strconv.FormatInt(v, 10) + case uint: + return strconv.FormatUint(uint64(v), 10) + case uint8: + return strconv.FormatUint(uint64(v), 10) + case uint16: + return strconv.FormatUint(uint64(v), 10) + case uint32: + return strconv.FormatUint(uint64(v), 10) + case uint64: + return strconv.FormatUint(v, 10) + case []interface{}: + return unmarshalSlice(v) + default: + return unmarshalMap(v) + } +} + +// helper function to unmarshal a parameter in map format. +func unmarshalMap(v interface{}) string { + yml, err := yaml.Marshal(v) + if err != nil { + return err.Error() + } + + out, err := json.YAMLToJSON(yml) + if err != nil { + return err.Error() + } + + return string(out) +} + +// helper function to unmarshal a parameter in slice format. +func unmarshalSlice(v interface{}) string { + out, err := yaml.Marshal(v) + if err != nil { + return err.Error() + } + + in := []string{} + + err = yaml.Unmarshal(out, &in) + if err == nil { + return strings.Join(in, ",") + } + + out, err = json.YAMLToJSON(out) + if err != nil { + return err.Error() + } + + return string(out) +} diff --git a/api/types/string_test.go b/api/types/string_test.go new file mode 100644 index 000000000..e362f0766 --- /dev/null +++ b/api/types/string_test.go @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: Apache-2.0 + +package types + +import ( + "reflect" + "testing" +) + +func TestTypes_ToString(t *testing.T) { + // setup tests + tests := []struct { + parameter interface{} + want interface{} + }{ + {parameter: "string", want: "string"}, // string + {parameter: true, want: "true"}, // bool + {parameter: []byte{1}, want: "AQ=="}, // []byte + {parameter: float32(1.1), want: "1.1"}, // float32 + {parameter: float64(1.1), want: "1.1"}, // float64 + {parameter: 1, want: "1"}, // int + {parameter: int8(1), want: "1"}, // int8 + {parameter: int16(1), want: "1"}, // int16 + {parameter: int32(1), want: "1"}, // int32 + {parameter: int64(1), want: "1"}, // int64 + {parameter: uint(1), want: "1"}, // uint + {parameter: uint8(1), want: "1"}, // uint8 + {parameter: uint16(1), want: "1"}, // uint16 + {parameter: uint32(1), want: "1"}, // uint32 + {parameter: uint64(1), want: "1"}, // uint64 + { // map + parameter: map[string]string{"hello": "world"}, + want: "{\"hello\":\"world\"}", + }, + { // slice + parameter: []interface{}{1, 2, 3}, + want: "1,2,3", + }, + { // slice complex + parameter: []interface{}{struct{ Foo string }{Foo: "bar"}}, + want: "[{\"foo\":\"bar\"}]", + }, + { // complex + parameter: []struct{ Foo string }{{"bar"}, {"baz"}}, + want: "[{\"foo\":\"bar\"},{\"foo\":\"baz\"}]", + }, + } + + // run tests + for _, test := range tests { + got := ToString(test.parameter) + + if !reflect.DeepEqual(got, test.want) { + t.Errorf("ToString is %v, want %v", got, test.want) + } + } +} diff --git a/api/types/worker_test.go b/api/types/worker_test.go index 19d2defb5..b1cc06fdb 100644 --- a/api/types/worker_test.go +++ b/api/types/worker_test.go @@ -58,7 +58,7 @@ func TestTypes_Worker_Getters(t *testing.T) { } if !reflect.DeepEqual(test.worker.GetRunningBuilds(), test.want.GetRunningBuilds()) { - t.Errorf("GetRunningBuildIDs is %v, want %v", test.worker.GetRunningBuilds(), test.want.GetRunningBuilds()) + t.Errorf("GetRunningBuilds is %v, want %v", test.worker.GetRunningBuilds(), test.want.GetRunningBuilds()) } if test.worker.GetLastBuildStartedAt() != test.want.GetLastBuildStartedAt() { diff --git a/api/user/get_source.go b/api/user/get_source.go index 6390c00e7..b52e3950f 100644 --- a/api/user/get_source.go +++ b/api/user/get_source.go @@ -7,11 +7,11 @@ import ( "net/http" "github.com/gin-gonic/gin" + "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/scm" "github.com/go-vela/server/util" - "github.com/go-vela/types/library" "github.com/sirupsen/logrus" ) @@ -49,8 +49,8 @@ func GetSourceRepos(c *gin.Context) { }).Infof("reading available SCM repos for user %s", u.GetName()) // variables to capture requested data - dbRepos := []*library.Repo{} - output := make(map[string][]library.Repo) + dbRepos := []*types.Repo{} + output := make(map[string][]types.Repo) // send API call to capture the list of repos for the user srcRepos, err := scm.FromContext(c).ListUserRepos(ctx, u) @@ -72,7 +72,7 @@ func GetSourceRepos(c *gin.Context) { active := false // library struct to omit optional fields - repo := library.Repo{ + repo := types.Repo{ Org: org, Name: name, Active: &active, diff --git a/api/webhook/post.go b/api/webhook/post.go index d33a201e9..3f1b3bba7 100644 --- a/api/webhook/post.go +++ b/api/webhook/post.go @@ -15,12 +15,13 @@ import ( "github.com/gin-gonic/gin" "github.com/go-vela/server/api/build" + "github.com/go-vela/server/api/types" "github.com/go-vela/server/compiler" "github.com/go-vela/server/database" + "github.com/go-vela/server/internal" "github.com/go-vela/server/queue" "github.com/go-vela/server/scm" "github.com/go-vela/server/util" - "github.com/go-vela/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/library" "github.com/sirupsen/logrus" @@ -74,7 +75,7 @@ func PostWebhook(c *gin.Context) { logrus.Info("webhook received") // capture middleware values - m := c.MustGet("metadata").(*types.Metadata) + m := c.MustGet("metadata").(*internal.Metadata) ctx := c.Request.Context() // duplicate request so we can perform operations on the request body @@ -307,8 +308,8 @@ func PostWebhook(c *gin.Context) { queue.FromContext(c), ) - // capture the build, repo, and user from the items - b, repo, u := item.Build, item.Repo, item.User + // capture the build and repo from the items + b, repo = item.Build, item.Repo // set hook build_id to the generated build id h.SetBuildID(b.GetID()) @@ -407,7 +408,7 @@ func PostWebhook(c *gin.Context) { switch repo.GetApproveBuild() { case constants.ApproveForkAlways: - err = gatekeepBuild(c, b, repo, u) + err = gatekeepBuild(c, b, repo) if err != nil { util.HandleError(c, http.StatusInternalServerError, err) } @@ -415,9 +416,9 @@ func PostWebhook(c *gin.Context) { return case constants.ApproveForkNoWrite: // determine if build sender has write access to parent repo. If not, this call will result in an error - _, err = scm.FromContext(c).RepoAccess(ctx, b.GetSender(), u.GetToken(), r.GetOrg(), r.GetName()) + _, err = scm.FromContext(c).RepoAccess(ctx, b.GetSender(), r.GetOwner().GetToken(), r.GetOrg(), r.GetName()) if err != nil { - err = gatekeepBuild(c, b, repo, u) + err = gatekeepBuild(c, b, repo) if err != nil { util.HandleError(c, http.StatusInternalServerError, err) } @@ -431,13 +432,13 @@ func PostWebhook(c *gin.Context) { // // NOTE: this call is cumbersome for repos with lots of contributors. Potential TODO: improve this if // GitHub adds a single-contributor API endpoint. - contributor, err := scm.FromContext(c).RepoContributor(ctx, u, b.GetSender(), r.GetOrg(), r.GetName()) + contributor, err := scm.FromContext(c).RepoContributor(ctx, r.GetOwner(), b.GetSender(), r.GetOrg(), r.GetName()) if err != nil { util.HandleError(c, http.StatusInternalServerError, err) } if !contributor { - err = gatekeepBuild(c, b, repo, u) + err = gatekeepBuild(c, b, repo) if err != nil { util.HandleError(c, http.StatusInternalServerError, err) } @@ -454,7 +455,7 @@ func PostWebhook(c *gin.Context) { } // send API call to set the status on the commit - err = scm.FromContext(c).Status(ctx, u, b, repo.GetOrg(), repo.GetName()) + err = scm.FromContext(c).Status(ctx, repo.GetOwner(), b, repo.GetOrg(), repo.GetName()) if err != nil { logrus.Errorf("unable to set commit status for %s/%d: %v", repo.GetFullName(), b.GetNumber(), err) } @@ -471,7 +472,7 @@ func PostWebhook(c *gin.Context) { // handleRepositoryEvent is a helper function that processes repository events from the SCM and updates // the database resources with any relevant changes resulting from the event, such as name changes, transfers, etc. -func handleRepositoryEvent(ctx context.Context, c *gin.Context, m *types.Metadata, h *library.Hook, r *library.Repo) (*library.Repo, error) { +func handleRepositoryEvent(ctx context.Context, c *gin.Context, m *internal.Metadata, h *library.Hook, r *types.Repo) (*types.Repo, error) { logrus.Debugf("webhook is repository event, making necessary updates to repo %s", r.GetFullName()) defer func() { @@ -563,7 +564,7 @@ func handleRepositoryEvent(ctx context.Context, c *gin.Context, m *types.Metadat // queries the database for the repo that matches that name and org, and updates // that repo to its new name in order to preserve it. It also updates the secrets // associated with that repo as well as build links for the UI. -func RenameRepository(ctx context.Context, h *library.Hook, r *library.Repo, c *gin.Context, m *types.Metadata) (*library.Repo, error) { +func RenameRepository(ctx context.Context, h *library.Hook, r *types.Repo, c *gin.Context, m *internal.Metadata) (*types.Repo, error) { logrus.Infof("renaming repository from %s to %s", r.GetPreviousName(), r.GetName()) // get any matching hook with the repo's unique webhook ID in the SCM @@ -688,7 +689,7 @@ func RenameRepository(ctx context.Context, h *library.Hook, r *library.Repo, c * // gatekeepBuild is a helper function that will set the status of a build to 'pending approval' and // send a status update to the SCM. -func gatekeepBuild(c *gin.Context, b *library.Build, r *library.Repo, u *library.User) error { +func gatekeepBuild(c *gin.Context, b *library.Build, r *types.Repo) error { logrus.Debugf("fork PR build %s/%d waiting for approval", r.GetFullName(), b.GetNumber()) b.SetStatus(constants.StatusPendingApproval) @@ -704,7 +705,7 @@ func gatekeepBuild(c *gin.Context, b *library.Build, r *library.Repo, u *library } // send API call to set the status on the commit - err = scm.FromContext(c).Status(c, u, b, r.GetOrg(), r.GetName()) + err = scm.FromContext(c).Status(c, r.GetOwner(), b, r.GetOrg(), r.GetName()) if err != nil { logrus.Errorf("unable to set commit status for %s/%d: %v", r.GetFullName(), b.GetNumber(), err) } diff --git a/cmd/vela-server/metadata.go b/cmd/vela-server/metadata.go index 88a29b23a..4e24723cb 100644 --- a/cmd/vela-server/metadata.go +++ b/cmd/vela-server/metadata.go @@ -5,7 +5,7 @@ package main import ( "net/url" - "github.com/go-vela/types" + "github.com/go-vela/server/internal" "github.com/sirupsen/logrus" @@ -13,10 +13,10 @@ import ( ) // helper function to setup the metadata from the CLI arguments. -func setupMetadata(c *cli.Context) (*types.Metadata, error) { +func setupMetadata(c *cli.Context) (*internal.Metadata, error) { logrus.Debug("Creating metadata from CLI configuration") - m := new(types.Metadata) + m := new(internal.Metadata) database, err := metadataDatabase(c) if err != nil { @@ -50,7 +50,7 @@ func setupMetadata(c *cli.Context) (*types.Metadata, error) { } // helper function to capture the database metadata from the CLI arguments. -func metadataDatabase(c *cli.Context) (*types.Database, error) { +func metadataDatabase(c *cli.Context) (*internal.Database, error) { logrus.Trace("Creating database metadata from CLI configuration") u, err := url.Parse(c.String("database.addr")) @@ -58,14 +58,14 @@ func metadataDatabase(c *cli.Context) (*types.Database, error) { return nil, err } - return &types.Database{ + return &internal.Database{ Driver: c.String("database.driver"), Host: u.Host, }, nil } // helper function to capture the queue metadata from the CLI arguments. -func metadataQueue(c *cli.Context) (*types.Queue, error) { +func metadataQueue(c *cli.Context) (*internal.Queue, error) { logrus.Trace("Creating queue metadata from CLI configuration") u, err := url.Parse(c.String("queue.addr")) @@ -73,14 +73,14 @@ func metadataQueue(c *cli.Context) (*types.Queue, error) { return nil, err } - return &types.Queue{ + return &internal.Queue{ Driver: c.String("queue.driver"), Host: u.Host, }, nil } // helper function to capture the source metadata from the CLI arguments. -func metadataSource(c *cli.Context) (*types.Source, error) { +func metadataSource(c *cli.Context) (*internal.Source, error) { logrus.Trace("Creating source metadata from CLI configuration") u, err := url.Parse(c.String("scm.addr")) @@ -88,7 +88,7 @@ func metadataSource(c *cli.Context) (*types.Source, error) { return nil, err } - return &types.Source{ + return &internal.Source{ Driver: c.String("scm.driver"), Host: u.Host, }, nil @@ -97,10 +97,10 @@ func metadataSource(c *cli.Context) (*types.Source, error) { // helper function to capture the Vela metadata from the CLI arguments. // //nolint:unparam // ignore unparam for now -func metadataVela(c *cli.Context) (*types.Vela, error) { +func metadataVela(c *cli.Context) (*internal.Vela, error) { logrus.Trace("Creating Vela metadata from CLI configuration") - vela := new(types.Vela) + vela := new(internal.Vela) if len(c.String("server-addr")) > 0 { vela.Address = c.String("server-addr") diff --git a/cmd/vela-server/schedule.go b/cmd/vela-server/schedule.go index 3e4ef799f..89a0abece 100644 --- a/cmd/vela-server/schedule.go +++ b/cmd/vela-server/schedule.go @@ -12,10 +12,10 @@ import ( "github.com/go-vela/server/api/build" "github.com/go-vela/server/compiler" "github.com/go-vela/server/database" + "github.com/go-vela/server/internal" "github.com/go-vela/server/queue" "github.com/go-vela/server/scm" "github.com/go-vela/server/util" - "github.com/go-vela/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/library" "github.com/sirupsen/logrus" @@ -29,7 +29,7 @@ const ( scheduleWait = "waiting to trigger build for schedule" ) -func processSchedules(ctx context.Context, start time.Time, compiler compiler.Engine, database database.Interface, metadata *types.Metadata, queue queue.Service, scm scm.Service, allowList []string) error { +func processSchedules(ctx context.Context, start time.Time, compiler compiler.Engine, database database.Interface, metadata *internal.Metadata, queue queue.Service, scm scm.Service, allowList []string) error { logrus.Infof("processing active schedules to create builds") // send API call to capture the list of active schedules @@ -134,7 +134,7 @@ func processSchedules(ctx context.Context, start time.Time, compiler compiler.En } // processSchedule will, given a schedule, process it and trigger a new build. -func processSchedule(ctx context.Context, s *library.Schedule, compiler compiler.Engine, database database.Interface, metadata *types.Metadata, queue queue.Service, scm scm.Service, allowList []string) error { +func processSchedule(ctx context.Context, s *library.Schedule, compiler compiler.Engine, database database.Interface, metadata *internal.Metadata, queue queue.Service, scm scm.Service, allowList []string) error { // send API call to capture the repo for the schedule r, err := database.GetRepo(ctx, s.GetRepoID()) if err != nil { diff --git a/compiler/engine.go b/compiler/engine.go index 566696b91..6ed310451 100644 --- a/compiler/engine.go +++ b/compiler/engine.go @@ -3,7 +3,8 @@ package compiler import ( - "github.com/go-vela/types" + api "github.com/go-vela/server/api/types" + "github.com/go-vela/server/internal" "github.com/go-vela/types/library" "github.com/go-vela/types/pipeline" "github.com/go-vela/types/raw" @@ -133,10 +134,10 @@ type Engine interface { WithLocalTemplates([]string) Engine // WithMetadata defines a function that sets // the compiler Metadata type in the Engine. - WithMetadata(*types.Metadata) Engine + WithMetadata(*internal.Metadata) Engine // WithRepo defines a function that sets // the library repo type in the Engine. - WithRepo(*library.Repo) Engine + WithRepo(*api.Repo) Engine // WithUser defines a function that sets // the library user type in the Engine. WithUser(*library.User) Engine diff --git a/compiler/native/compile.go b/compiler/native/compile.go index 1c02e4d7a..cbea8185e 100644 --- a/compiler/native/compile.go +++ b/compiler/native/compile.go @@ -15,6 +15,7 @@ import ( "github.com/go-vela/types/constants" yml "github.com/buildkite/yaml" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/library" "github.com/go-vela/types/pipeline" @@ -502,7 +503,7 @@ func errorHandler(resp *http.Response, err error, attempts int) (*http.Response, } // modifyConfig sends the configuration to external http endpoint for modification. -func (c *client) modifyConfig(build *yaml.Build, libraryBuild *library.Build, repo *library.Repo) (*yaml.Build, error) { +func (c *client) modifyConfig(build *yaml.Build, libraryBuild *library.Build, repo *api.Repo) (*yaml.Build, error) { // create request to send to endpoint data, err := yml.Marshal(build) if err != nil { diff --git a/compiler/native/compile_test.go b/compiler/native/compile_test.go index 4fb45b2dd..4d77cd148 100644 --- a/compiler/native/compile_test.go +++ b/compiler/native/compile_test.go @@ -10,6 +10,8 @@ import ( "os" "path/filepath" + api "github.com/go-vela/server/api/types" + "github.com/go-vela/server/internal" "github.com/go-vela/types/constants" "github.com/go-vela/types/raw" @@ -23,7 +25,6 @@ import ( "github.com/go-vela/types/library" "github.com/go-vela/types/yaml" - "github.com/go-vela/types" "github.com/go-vela/types/pipeline" "github.com/gin-gonic/gin" @@ -39,21 +40,21 @@ func TestNative_Compile_StagesPipeline(t *testing.T) { set.Int("max-template-depth", 5, "doc") c := cli.NewContext(nil, set, nil) - m := &types.Metadata{ - Database: &types.Database{ + m := &internal.Metadata{ + Database: &internal.Database{ Driver: "foo", Host: "foo", }, - Queue: &types.Queue{ + Queue: &internal.Queue{ Channel: "foo", Driver: "foo", Host: "foo", }, - Source: &types.Source{ + Source: &internal.Source{ Driver: "foo", Host: "foo", }, - Vela: &types.Vela{ + Vela: &internal.Vela{ Address: "foo", WebAddress: "foo", }, @@ -308,7 +309,7 @@ func TestNative_Compile_StagesPipeline_Modification(t *testing.T) { type args struct { endpoint string libraryBuild *library.Build - repo *library.Repo + repo *api.Repo } tests := []struct { @@ -318,12 +319,12 @@ func TestNative_Compile_StagesPipeline_Modification(t *testing.T) { }{ {"bad url", args{ libraryBuild: &library.Build{Number: &number, Author: &author}, - repo: &library.Repo{Name: &name}, + repo: &api.Repo{Name: &name}, endpoint: "bad", }, true}, {"invalid return", args{ libraryBuild: &library.Build{Number: &number, Author: &author}, - repo: &library.Repo{Name: &name}, + repo: &api.Repo{Name: &name}, endpoint: fmt.Sprintf("%s/%s", s.URL, "config/bad"), }, true}, } @@ -335,7 +336,7 @@ func TestNative_Compile_StagesPipeline_Modification(t *testing.T) { Timeout: 1 * time.Second, Endpoint: tt.args.endpoint, }, - repo: &library.Repo{Name: &author}, + repo: &api.Repo{Name: &author}, build: &library.Build{Author: &name, Number: &number}, } _, _, err := compiler.Compile(yaml) @@ -376,7 +377,7 @@ func TestNative_Compile_StepsPipeline_Modification(t *testing.T) { type args struct { endpoint string libraryBuild *library.Build - repo *library.Repo + repo *api.Repo } tests := []struct { @@ -386,12 +387,12 @@ func TestNative_Compile_StepsPipeline_Modification(t *testing.T) { }{ {"bad url", args{ libraryBuild: &library.Build{Number: &number, Author: &author}, - repo: &library.Repo{Name: &name}, + repo: &api.Repo{Name: &name}, endpoint: "bad", }, true}, {"invalid return", args{ libraryBuild: &library.Build{Number: &number, Author: &author}, - repo: &library.Repo{Name: &name}, + repo: &api.Repo{Name: &name}, endpoint: fmt.Sprintf("%s/%s", s.URL, "config/bad"), }, true}, } @@ -422,21 +423,21 @@ func TestNative_Compile_StepsPipeline(t *testing.T) { set.Int("max-template-depth", 5, "doc") c := cli.NewContext(nil, set, nil) - m := &types.Metadata{ - Database: &types.Database{ + m := &internal.Metadata{ + Database: &internal.Database{ Driver: "foo", Host: "foo", }, - Queue: &types.Queue{ + Queue: &internal.Queue{ Channel: "foo", Driver: "foo", Host: "foo", }, - Source: &types.Source{ + Source: &internal.Source{ Driver: "foo", Host: "foo", }, - Vela: &types.Vela{ + Vela: &internal.Vela{ Address: "foo", WebAddress: "foo", }, @@ -633,21 +634,21 @@ func TestNative_Compile_StagesPipelineTemplate(t *testing.T) { set.Int("max-template-depth", 5, "doc") c := cli.NewContext(nil, set, nil) - m := &types.Metadata{ - Database: &types.Database{ + m := &internal.Metadata{ + Database: &internal.Database{ Driver: "foo", Host: "foo", }, - Queue: &types.Queue{ + Queue: &internal.Queue{ Channel: "foo", Driver: "foo", Host: "foo", }, - Source: &types.Source{ + Source: &internal.Source{ Driver: "foo", Host: "foo", }, - Vela: &types.Vela{ + Vela: &internal.Vela{ Address: "foo", WebAddress: "foo", }, @@ -904,21 +905,21 @@ func TestNative_Compile_StepsPipelineTemplate(t *testing.T) { set.Int("max-template-depth", 5, "doc") c := cli.NewContext(nil, set, nil) - m := &types.Metadata{ - Database: &types.Database{ + m := &internal.Metadata{ + Database: &internal.Database{ Driver: "foo", Host: "foo", }, - Queue: &types.Queue{ + Queue: &internal.Queue{ Channel: "foo", Driver: "foo", Host: "foo", }, - Source: &types.Source{ + Source: &internal.Source{ Driver: "foo", Host: "foo", }, - Vela: &types.Vela{ + Vela: &internal.Vela{ Address: "foo", WebAddress: "foo", }, @@ -1138,21 +1139,21 @@ func TestNative_Compile_StepsPipelineTemplate_VelaFunction_TemplateName(t *testi set.Int("max-template-depth", 5, "doc") c := cli.NewContext(nil, set, nil) - m := &types.Metadata{ - Database: &types.Database{ + m := &internal.Metadata{ + Database: &internal.Database{ Driver: "foo", Host: "foo", }, - Queue: &types.Queue{ + Queue: &internal.Queue{ Channel: "foo", Driver: "foo", Host: "foo", }, - Source: &types.Source{ + Source: &internal.Source{ Driver: "foo", Host: "foo", }, - Vela: &types.Vela{ + Vela: &internal.Vela{ Address: "foo", WebAddress: "foo", }, @@ -1259,21 +1260,21 @@ func TestNative_Compile_StepsPipelineTemplate_VelaFunction_TemplateName_Inline(t set.Int("max-template-depth", 5, "doc") c := cli.NewContext(nil, set, nil) - m := &types.Metadata{ - Database: &types.Database{ + m := &internal.Metadata{ + Database: &internal.Database{ Driver: "foo", Host: "foo", }, - Queue: &types.Queue{ + Queue: &internal.Queue{ Channel: "foo", Driver: "foo", Host: "foo", }, - Source: &types.Source{ + Source: &internal.Source{ Driver: "foo", Host: "foo", }, - Vela: &types.Vela{ + Vela: &internal.Vela{ Address: "foo", WebAddress: "foo", }, @@ -1378,21 +1379,21 @@ func TestNative_Compile_InvalidType(t *testing.T) { set.Int("max-template-depth", 5, "doc") c := cli.NewContext(nil, set, nil) - m := &types.Metadata{ - Database: &types.Database{ + m := &internal.Metadata{ + Database: &internal.Database{ Driver: "foo", Host: "foo", }, - Queue: &types.Queue{ + Queue: &internal.Queue{ Channel: "foo", Driver: "foo", Host: "foo", }, - Source: &types.Source{ + Source: &internal.Source{ Driver: "foo", Host: "foo", }, - Vela: &types.Vela{ + Vela: &internal.Vela{ Address: "foo", WebAddress: "foo", }, @@ -1435,21 +1436,21 @@ func TestNative_Compile_Clone(t *testing.T) { set.Int("max-template-depth", 5, "doc") c := cli.NewContext(nil, set, nil) - m := &types.Metadata{ - Database: &types.Database{ + m := &internal.Metadata{ + Database: &internal.Database{ Driver: "foo", Host: "foo", }, - Queue: &types.Queue{ + Queue: &internal.Queue{ Channel: "foo", Driver: "foo", Host: "foo", }, - Source: &types.Source{ + Source: &internal.Source{ Driver: "foo", Host: "foo", }, - Vela: &types.Vela{ + Vela: &internal.Vela{ Address: "foo", WebAddress: "foo", }, @@ -1629,21 +1630,21 @@ func TestNative_Compile_Pipeline_Type(t *testing.T) { set.Int("max-template-depth", 5, "doc") c := cli.NewContext(nil, set, nil) - m := &types.Metadata{ - Database: &types.Database{ + m := &internal.Metadata{ + Database: &internal.Database{ Driver: "foo", Host: "foo", }, - Queue: &types.Queue{ + Queue: &internal.Queue{ Channel: "foo", Driver: "foo", Host: "foo", }, - Source: &types.Source{ + Source: &internal.Source{ Driver: "foo", Host: "foo", }, - Vela: &types.Vela{ + Vela: &internal.Vela{ Address: "foo", WebAddress: "foo", }, @@ -1695,10 +1696,10 @@ func TestNative_Compile_Pipeline_Type(t *testing.T) { goPipelineType := "go" - goFooEnv := environment(nil, m, &library.Repo{PipelineType: &goPipelineType}, nil) + goFooEnv := environment(nil, m, &api.Repo{PipelineType: &goPipelineType}, nil) goFooEnv["PARAMETER_REGISTRY"] = "foo" - defaultGoEnv := environment(nil, m, &library.Repo{PipelineType: &goPipelineType}, nil) + defaultGoEnv := environment(nil, m, &api.Repo{PipelineType: &goPipelineType}, nil) wantGo := &pipeline.Build{ Version: "1", ID: "__0", @@ -1741,10 +1742,10 @@ func TestNative_Compile_Pipeline_Type(t *testing.T) { starPipelineType := "starlark" - starlarkFooEnv := environment(nil, m, &library.Repo{PipelineType: &starPipelineType}, nil) + starlarkFooEnv := environment(nil, m, &api.Repo{PipelineType: &starPipelineType}, nil) starlarkFooEnv["PARAMETER_REGISTRY"] = "foo" - defaultStarlarkEnv := environment(nil, m, &library.Repo{PipelineType: &starPipelineType}, nil) + defaultStarlarkEnv := environment(nil, m, &api.Repo{PipelineType: &starPipelineType}, nil) wantStarlark := &pipeline.Build{ Version: "1", ID: "__0", @@ -1815,7 +1816,9 @@ func TestNative_Compile_Pipeline_Type(t *testing.T) { } compiler.WithMetadata(m) - compiler.WithRepo(&library.Repo{PipelineType: &tt.args.pipelineType}) + + pipelineType := tt.args.pipelineType + compiler.WithRepo(&api.Repo{PipelineType: &pipelineType}) got, _, err := compiler.Compile(yaml) if err != nil { @@ -1848,7 +1851,7 @@ func TestNative_Compile_NoStepsorStages(t *testing.T) { t.Errorf("Creating compiler returned err: %v", err) } - compiler.repo = &library.Repo{Name: &author} + compiler.repo = &api.Repo{Name: &author} compiler.build = &library.Build{Author: &name, Number: &number} got, _, err := compiler.Compile(yaml) @@ -1880,7 +1883,7 @@ func TestNative_Compile_StepsandStages(t *testing.T) { t.Errorf("Creating compiler returned err: %v", err) } - compiler.repo = &library.Repo{Name: &author} + compiler.repo = &api.Repo{Name: &author} compiler.build = &library.Build{Author: &name, Number: &number} got, _, err := compiler.Compile(yaml) @@ -1923,21 +1926,21 @@ func Test_client_modifyConfig(t *testing.T) { c.JSON(http.StatusOK, body) }) - m := &types.Metadata{ - Database: &types.Database{ + m := &internal.Metadata{ + Database: &internal.Database{ Driver: "foo", Host: "foo", }, - Queue: &types.Queue{ + Queue: &internal.Queue{ Channel: "foo", Driver: "foo", Host: "foo", }, - Source: &types.Source{ + Source: &internal.Source{ Driver: "foo", Host: "foo", }, - Vela: &types.Vela{ + Vela: &internal.Vela{ Address: "foo", WebAddress: "foo", }, @@ -2083,7 +2086,7 @@ func Test_client_modifyConfig(t *testing.T) { endpoint string build *yaml.Build libraryBuild *library.Build - repo *library.Repo + repo *api.Repo } tests := []struct { @@ -2095,37 +2098,37 @@ func Test_client_modifyConfig(t *testing.T) { {"unmodified", args{ build: want, libraryBuild: &library.Build{Number: &number, Author: &author}, - repo: &library.Repo{Name: &name}, + repo: &api.Repo{Name: &name}, endpoint: fmt.Sprintf("%s/%s", s.URL, "config/unmodified"), }, want, false}, {"modified", args{ build: want, libraryBuild: &library.Build{Number: &number, Author: &author}, - repo: &library.Repo{Name: &name}, + repo: &api.Repo{Name: &name}, endpoint: fmt.Sprintf("%s/%s", s.URL, "config/modified"), }, want2, false}, {"invalid endpoint", args{ build: want, libraryBuild: &library.Build{Number: &number, Author: &author}, - repo: &library.Repo{Name: &name}, + repo: &api.Repo{Name: &name}, endpoint: "bad", }, nil, true}, {"unauthorized endpoint", args{ build: want, libraryBuild: &library.Build{Number: &number, Author: &author}, - repo: &library.Repo{Name: &name}, + repo: &api.Repo{Name: &name}, endpoint: fmt.Sprintf("%s/%s", s.URL, "config/unauthorized"), }, nil, true}, {"timeout endpoint", args{ build: want, libraryBuild: &library.Build{Number: &number, Author: &author}, - repo: &library.Repo{Name: &name}, + repo: &api.Repo{Name: &name}, endpoint: fmt.Sprintf("%s/%s", s.URL, "config/timeout"), }, nil, true}, {"empty payload", args{ build: want, libraryBuild: &library.Build{Number: &number, Author: &author}, - repo: &library.Repo{Name: &name}, + repo: &api.Repo{Name: &name}, endpoint: fmt.Sprintf("%s/%s", s.URL, "config/empty"), }, nil, true}, } @@ -2166,7 +2169,7 @@ func convertFileToGithubResponse(file string) (github.RepositoryContent, error) return content, nil } -func generateTestEnv(command string, m *types.Metadata, pipelineType string) map[string]string { +func generateTestEnv(command string, m *internal.Metadata, pipelineType string) map[string]string { output := environment(nil, m, nil, nil) output["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{command}) output["HOME"] = "/root" @@ -2204,21 +2207,21 @@ func Test_Compile_Inline(t *testing.T) { set.Int("max-template-depth", 5, "doc") c := cli.NewContext(nil, set, nil) - m := &types.Metadata{ - Database: &types.Database{ + m := &internal.Metadata{ + Database: &internal.Database{ Driver: "foo", Host: "foo", }, - Queue: &types.Queue{ + Queue: &internal.Queue{ Channel: "foo", Driver: "foo", Host: "foo", }, - Source: &types.Source{ + Source: &internal.Source{ Driver: "foo", Host: "foo", }, - Vela: &types.Vela{ + Vela: &internal.Vela{ Address: "foo", WebAddress: "foo", }, @@ -2964,7 +2967,8 @@ func Test_Compile_Inline(t *testing.T) { compiler.WithMetadata(m) if tt.args.pipelineType != "" { - compiler.WithRepo(&library.Repo{PipelineType: &tt.args.pipelineType}) + pipelineType := tt.args.pipelineType + compiler.WithRepo(&api.Repo{PipelineType: &pipelineType}) } got, _, err := compiler.Compile(yaml) @@ -3021,21 +3025,21 @@ func Test_CompileLite(t *testing.T) { set.Int("max-template-depth", 5, "doc") c := cli.NewContext(nil, set, nil) - m := &types.Metadata{ - Database: &types.Database{ + m := &internal.Metadata{ + Database: &internal.Database{ Driver: "foo", Host: "foo", }, - Queue: &types.Queue{ + Queue: &internal.Queue{ Channel: "foo", Driver: "foo", Host: "foo", }, - Source: &types.Source{ + Source: &internal.Source{ Driver: "foo", Host: "foo", }, - Vela: &types.Vela{ + Vela: &internal.Vela{ Address: "foo", WebAddress: "foo", }, @@ -3833,7 +3837,8 @@ func Test_CompileLite(t *testing.T) { compiler.WithMetadata(m) if tt.args.pipelineType != "" { - compiler.WithRepo(&library.Repo{PipelineType: &tt.args.pipelineType}) + pipelineType := tt.args.pipelineType + compiler.WithRepo(&api.Repo{PipelineType: &pipelineType}) } yaml, err := os.ReadFile(tt.args.file) diff --git a/compiler/native/environment.go b/compiler/native/environment.go index 5841825b3..b6d9571b0 100644 --- a/compiler/native/environment.go +++ b/compiler/native/environment.go @@ -7,7 +7,8 @@ import ( "os" "strings" - "github.com/go-vela/types" + api "github.com/go-vela/server/api/types" + "github.com/go-vela/server/internal" "github.com/go-vela/types/constants" "github.com/go-vela/types/library" "github.com/go-vela/types/raw" @@ -281,7 +282,7 @@ func appendMap(originalMap, otherMap map[string]string) map[string]string { } // helper function that creates the standard set of environment variables for a pipeline. -func environment(b *library.Build, m *types.Metadata, r *library.Repo, u *library.User) map[string]string { +func environment(b *library.Build, m *internal.Metadata, r *api.Repo, u *library.User) map[string]string { // set default workspace workspace := constants.WorkspaceDefault notImplemented := "TODO" diff --git a/compiler/native/environment_test.go b/compiler/native/environment_test.go index 01755972e..cfacbd880 100644 --- a/compiler/native/environment_test.go +++ b/compiler/native/environment_test.go @@ -8,10 +8,11 @@ import ( "strings" "testing" + api "github.com/go-vela/server/api/types" + "github.com/go-vela/server/internal" "github.com/go-vela/types/raw" "github.com/google/go-cmp/cmp" - "github.com/go-vela/types" "github.com/go-vela/types/library" "github.com/go-vela/types/yaml" @@ -186,6 +187,7 @@ func TestNative_EnvironmentSteps(t *testing.T) { "VELA_REPO_LINK": "", "VELA_REPO_NAME": "", "VELA_REPO_ORG": "", + "VELA_REPO_OWNER": "", "VELA_REPO_PIPELINE_TYPE": "", "VELA_REPO_PRIVATE": "false", "VELA_REPO_TIMEOUT": "0", @@ -360,6 +362,7 @@ func TestNative_EnvironmentServices(t *testing.T) { "VELA_REPO_LINK": "", "VELA_REPO_NAME": "", "VELA_REPO_ORG": "", + "VELA_REPO_OWNER": "", "VELA_REPO_PIPELINE_TYPE": "", "VELA_REPO_PRIVATE": "false", "VELA_REPO_TIMEOUT": "0", @@ -516,6 +519,7 @@ func TestNative_EnvironmentSecrets(t *testing.T) { "VELA_REPO_LINK": "", "VELA_REPO_NAME": "", "VELA_REPO_ORG": "", + "VELA_REPO_OWNER": "", "VELA_REPO_PIPELINE_TYPE": "", "VELA_REPO_PRIVATE": "false", "VELA_REPO_TIMEOUT": "0", @@ -575,8 +579,8 @@ func TestNative_environment(t *testing.T) { tests := []struct { w string b *library.Build - m *types.Metadata - r *library.Repo + m *internal.Metadata + r *api.Repo u *library.User want map[string]string }{ @@ -584,37 +588,37 @@ func TestNative_environment(t *testing.T) { { w: workspace, b: &library.Build{ID: &num64, RepoID: &num64, Number: &num, Parent: &num, Event: &push, Status: &str, Error: &str, Enqueued: &num64, Created: &num64, Started: &num64, Finished: &num64, Deploy: &str, Clone: &str, Source: &str, Title: &str, Message: &str, Commit: &str, Sender: &str, Author: &str, Branch: &str, Ref: &str, BaseRef: &str}, - m: &types.Metadata{Database: &types.Database{Driver: str, Host: str}, Queue: &types.Queue{Channel: str, Driver: str, Host: str}, Source: &types.Source{Driver: str, Host: str}, Vela: &types.Vela{Address: str, WebAddress: str}}, - r: &library.Repo{ID: &num64, UserID: &num64, Org: &str, Name: &str, FullName: &str, Link: &str, Clone: &str, Branch: &str, Topics: &topics, BuildLimit: &num64, Timeout: &num64, Visibility: &str, Private: &booL, Trusted: &booL, Active: &booL}, + m: &internal.Metadata{Database: &internal.Database{Driver: str, Host: str}, Queue: &internal.Queue{Channel: str, Driver: str, Host: str}, Source: &internal.Source{Driver: str, Host: str}, Vela: &internal.Vela{Address: str, WebAddress: str}}, + r: &api.Repo{ID: &num64, Owner: &library.User{ID: &num64, Name: &str, Token: &str, Active: &booL, Admin: &booL}, Org: &str, Name: &str, FullName: &str, Link: &str, Clone: &str, Branch: &str, Topics: &topics, BuildLimit: &num64, Timeout: &num64, Visibility: &str, Private: &booL, Trusted: &booL, Active: &booL}, u: &library.User{ID: &num64, Name: &str, Token: &str, Active: &booL, Admin: &booL}, - want: map[string]string{"BUILD_AUTHOR": "foo", "BUILD_AUTHOR_EMAIL": "", "BUILD_BASE_REF": "foo", "BUILD_BRANCH": "foo", "BUILD_CHANNEL": "foo", "BUILD_CLONE": "foo", "BUILD_COMMIT": "foo", "BUILD_CREATED": "1", "BUILD_ENQUEUED": "1", "BUILD_EVENT": "push", "BUILD_HOST": "", "BUILD_LINK": "", "BUILD_MESSAGE": "foo", "BUILD_NUMBER": "1", "BUILD_PARENT": "1", "BUILD_REF": "foo", "BUILD_SENDER": "foo", "BUILD_SOURCE": "foo", "BUILD_STARTED": "1", "BUILD_STATUS": "foo", "BUILD_TITLE": "foo", "BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "CI": "true", "REPOSITORY_ACTIVE": "false", "REPOSITORY_ALLOW_EVENTS": "", "REPOSITORY_BRANCH": "foo", "REPOSITORY_CLONE": "foo", "REPOSITORY_FULL_NAME": "foo", "REPOSITORY_LINK": "foo", "REPOSITORY_NAME": "foo", "REPOSITORY_ORG": "foo", "REPOSITORY_PRIVATE": "false", "REPOSITORY_TIMEOUT": "1", "REPOSITORY_TRUSTED": "false", "REPOSITORY_VISIBILITY": "foo", "VELA": "true", "VELA_ADDR": "foo", "VELA_BUILD_APPROVED_AT": "0", "VELA_BUILD_APPROVED_BY": "", "VELA_BUILD_AUTHOR": "foo", "VELA_BUILD_AUTHOR_EMAIL": "", "VELA_BUILD_BASE_REF": "foo", "VELA_BUILD_BRANCH": "foo", "VELA_BUILD_CHANNEL": "foo", "VELA_BUILD_CLONE": "foo", "VELA_BUILD_COMMIT": "foo", "VELA_BUILD_CREATED": "1", "VELA_BUILD_DISTRIBUTION": "", "VELA_BUILD_ENQUEUED": "1", "VELA_BUILD_EVENT": "push", "VELA_BUILD_EVENT_ACTION": "", "VELA_BUILD_HOST": "", "VELA_BUILD_LINK": "", "VELA_BUILD_MESSAGE": "foo", "VELA_BUILD_NUMBER": "1", "VELA_BUILD_PARENT": "1", "VELA_BUILD_REF": "foo", "VELA_BUILD_RUNTIME": "", "VELA_BUILD_SENDER": "foo", "VELA_BUILD_SOURCE": "foo", "VELA_BUILD_STARTED": "1", "VELA_BUILD_STATUS": "foo", "VELA_BUILD_TITLE": "foo", "VELA_BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_CHANNEL": "foo", "VELA_DATABASE": "foo", "VELA_DISTRIBUTION": "TODO", "VELA_HOST": "foo", "VELA_NETRC_MACHINE": "foo", "VELA_NETRC_PASSWORD": "foo", "VELA_NETRC_USERNAME": "x-oauth-basic", "VELA_QUEUE": "foo", "VELA_REPO_ACTIVE": "false", "VELA_REPO_ALLOW_EVENTS": "", "VELA_REPO_APPROVE_BUILD": "", "VELA_REPO_BRANCH": "foo", "VELA_REPO_TOPICS": "cloud,security", "VELA_REPO_BUILD_LIMIT": "1", "VELA_REPO_CLONE": "foo", "VELA_REPO_FULL_NAME": "foo", "VELA_REPO_LINK": "foo", "VELA_REPO_NAME": "foo", "VELA_REPO_ORG": "foo", "VELA_REPO_PIPELINE_TYPE": "", "VELA_REPO_PRIVATE": "false", "VELA_REPO_TIMEOUT": "1", "VELA_REPO_TRUSTED": "false", "VELA_REPO_VISIBILITY": "foo", "VELA_RUNTIME": "TODO", "VELA_SOURCE": "foo", "VELA_USER_ACTIVE": "false", "VELA_USER_ADMIN": "false", "VELA_USER_FAVORITES": "[]", "VELA_USER_NAME": "foo", "VELA_VERSION": "TODO", "VELA_WORKSPACE": "/vela/src/foo/foo/foo"}, + want: map[string]string{"BUILD_AUTHOR": "foo", "BUILD_AUTHOR_EMAIL": "", "BUILD_BASE_REF": "foo", "BUILD_BRANCH": "foo", "BUILD_CHANNEL": "foo", "BUILD_CLONE": "foo", "BUILD_COMMIT": "foo", "BUILD_CREATED": "1", "BUILD_ENQUEUED": "1", "BUILD_EVENT": "push", "BUILD_HOST": "", "BUILD_LINK": "", "BUILD_MESSAGE": "foo", "BUILD_NUMBER": "1", "BUILD_PARENT": "1", "BUILD_REF": "foo", "BUILD_SENDER": "foo", "BUILD_SOURCE": "foo", "BUILD_STARTED": "1", "BUILD_STATUS": "foo", "BUILD_TITLE": "foo", "BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "CI": "true", "REPOSITORY_ACTIVE": "false", "REPOSITORY_ALLOW_EVENTS": "", "REPOSITORY_BRANCH": "foo", "REPOSITORY_CLONE": "foo", "REPOSITORY_FULL_NAME": "foo", "REPOSITORY_LINK": "foo", "REPOSITORY_NAME": "foo", "REPOSITORY_ORG": "foo", "REPOSITORY_PRIVATE": "false", "REPOSITORY_TIMEOUT": "1", "REPOSITORY_TRUSTED": "false", "REPOSITORY_VISIBILITY": "foo", "VELA": "true", "VELA_ADDR": "foo", "VELA_BUILD_APPROVED_AT": "0", "VELA_BUILD_APPROVED_BY": "", "VELA_BUILD_AUTHOR": "foo", "VELA_BUILD_AUTHOR_EMAIL": "", "VELA_BUILD_BASE_REF": "foo", "VELA_BUILD_BRANCH": "foo", "VELA_BUILD_CHANNEL": "foo", "VELA_BUILD_CLONE": "foo", "VELA_BUILD_COMMIT": "foo", "VELA_BUILD_CREATED": "1", "VELA_BUILD_DISTRIBUTION": "", "VELA_BUILD_ENQUEUED": "1", "VELA_BUILD_EVENT": "push", "VELA_BUILD_EVENT_ACTION": "", "VELA_BUILD_HOST": "", "VELA_BUILD_LINK": "", "VELA_BUILD_MESSAGE": "foo", "VELA_BUILD_NUMBER": "1", "VELA_BUILD_PARENT": "1", "VELA_BUILD_REF": "foo", "VELA_BUILD_RUNTIME": "", "VELA_BUILD_SENDER": "foo", "VELA_BUILD_SOURCE": "foo", "VELA_BUILD_STARTED": "1", "VELA_BUILD_STATUS": "foo", "VELA_BUILD_TITLE": "foo", "VELA_BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_CHANNEL": "foo", "VELA_DATABASE": "foo", "VELA_DISTRIBUTION": "TODO", "VELA_HOST": "foo", "VELA_NETRC_MACHINE": "foo", "VELA_NETRC_PASSWORD": "foo", "VELA_NETRC_USERNAME": "x-oauth-basic", "VELA_QUEUE": "foo", "VELA_REPO_ACTIVE": "false", "VELA_REPO_ALLOW_EVENTS": "", "VELA_REPO_APPROVE_BUILD": "", "VELA_REPO_BRANCH": "foo", "VELA_REPO_TOPICS": "cloud,security", "VELA_REPO_BUILD_LIMIT": "1", "VELA_REPO_CLONE": "foo", "VELA_REPO_FULL_NAME": "foo", "VELA_REPO_LINK": "foo", "VELA_REPO_NAME": "foo", "VELA_REPO_ORG": "foo", "VELA_REPO_OWNER": "foo", "VELA_REPO_PIPELINE_TYPE": "", "VELA_REPO_PRIVATE": "false", "VELA_REPO_TIMEOUT": "1", "VELA_REPO_TRUSTED": "false", "VELA_REPO_VISIBILITY": "foo", "VELA_RUNTIME": "TODO", "VELA_SOURCE": "foo", "VELA_USER_ACTIVE": "false", "VELA_USER_ADMIN": "false", "VELA_USER_FAVORITES": "[]", "VELA_USER_NAME": "foo", "VELA_VERSION": "TODO", "VELA_WORKSPACE": "/vela/src/foo/foo/foo"}, }, // tag { w: workspace, b: &library.Build{ID: &num64, RepoID: &num64, Number: &num, Parent: &num, Event: &tag, Status: &str, Error: &str, Enqueued: &num64, Created: &num64, Started: &num64, Finished: &num64, Deploy: &str, Clone: &str, Source: &str, Title: &str, Message: &str, Commit: &str, Sender: &str, Author: &str, Branch: &str, Ref: &tagref, BaseRef: &str}, - m: &types.Metadata{Database: &types.Database{Driver: str, Host: str}, Queue: &types.Queue{Channel: str, Driver: str, Host: str}, Source: &types.Source{Driver: str, Host: str}, Vela: &types.Vela{Address: str, WebAddress: str}}, - r: &library.Repo{ID: &num64, UserID: &num64, Org: &str, Name: &str, FullName: &str, Link: &str, Clone: &str, Branch: &str, Topics: &topics, BuildLimit: &num64, Timeout: &num64, Visibility: &str, Private: &booL, Trusted: &booL, Active: &booL}, + m: &internal.Metadata{Database: &internal.Database{Driver: str, Host: str}, Queue: &internal.Queue{Channel: str, Driver: str, Host: str}, Source: &internal.Source{Driver: str, Host: str}, Vela: &internal.Vela{Address: str, WebAddress: str}}, + r: &api.Repo{ID: &num64, Owner: &library.User{ID: &num64, Name: &str, Token: &str, Active: &booL, Admin: &booL}, Org: &str, Name: &str, FullName: &str, Link: &str, Clone: &str, Branch: &str, Topics: &topics, BuildLimit: &num64, Timeout: &num64, Visibility: &str, Private: &booL, Trusted: &booL, Active: &booL}, u: &library.User{ID: &num64, Name: &str, Token: &str, Active: &booL, Admin: &booL}, - want: map[string]string{"BUILD_AUTHOR": "foo", "BUILD_AUTHOR_EMAIL": "", "BUILD_BASE_REF": "foo", "BUILD_BRANCH": "foo", "BUILD_CHANNEL": "foo", "BUILD_CLONE": "foo", "BUILD_COMMIT": "foo", "BUILD_CREATED": "1", "BUILD_ENQUEUED": "1", "BUILD_EVENT": "tag", "BUILD_HOST": "", "BUILD_LINK": "", "BUILD_MESSAGE": "foo", "BUILD_NUMBER": "1", "BUILD_PARENT": "1", "BUILD_REF": "refs/tags/1", "BUILD_SENDER": "foo", "BUILD_SOURCE": "foo", "BUILD_STARTED": "1", "BUILD_STATUS": "foo", "BUILD_TAG": "1", "BUILD_TITLE": "foo", "BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "CI": "true", "REPOSITORY_ACTIVE": "false", "REPOSITORY_ALLOW_EVENTS": "", "REPOSITORY_BRANCH": "foo", "REPOSITORY_CLONE": "foo", "REPOSITORY_FULL_NAME": "foo", "REPOSITORY_LINK": "foo", "REPOSITORY_NAME": "foo", "REPOSITORY_ORG": "foo", "REPOSITORY_PRIVATE": "false", "REPOSITORY_TIMEOUT": "1", "REPOSITORY_TRUSTED": "false", "REPOSITORY_VISIBILITY": "foo", "VELA": "true", "VELA_ADDR": "foo", "VELA_BUILD_APPROVED_AT": "0", "VELA_BUILD_APPROVED_BY": "", "VELA_BUILD_AUTHOR": "foo", "VELA_BUILD_AUTHOR_EMAIL": "", "VELA_BUILD_BASE_REF": "foo", "VELA_BUILD_BRANCH": "foo", "VELA_BUILD_CHANNEL": "foo", "VELA_BUILD_CLONE": "foo", "VELA_BUILD_COMMIT": "foo", "VELA_BUILD_CREATED": "1", "VELA_BUILD_DISTRIBUTION": "", "VELA_BUILD_ENQUEUED": "1", "VELA_BUILD_EVENT": "tag", "VELA_BUILD_EVENT_ACTION": "", "VELA_BUILD_HOST": "", "VELA_BUILD_LINK": "", "VELA_BUILD_MESSAGE": "foo", "VELA_BUILD_NUMBER": "1", "VELA_BUILD_PARENT": "1", "VELA_BUILD_REF": "refs/tags/1", "VELA_BUILD_RUNTIME": "", "VELA_BUILD_SENDER": "foo", "VELA_BUILD_SOURCE": "foo", "VELA_BUILD_STARTED": "1", "VELA_BUILD_STATUS": "foo", "VELA_BUILD_TAG": "1", "VELA_BUILD_TITLE": "foo", "VELA_BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_CHANNEL": "foo", "VELA_DATABASE": "foo", "VELA_DISTRIBUTION": "TODO", "VELA_HOST": "foo", "VELA_NETRC_MACHINE": "foo", "VELA_NETRC_PASSWORD": "foo", "VELA_NETRC_USERNAME": "x-oauth-basic", "VELA_QUEUE": "foo", "VELA_REPO_ACTIVE": "false", "VELA_REPO_ALLOW_EVENTS": "", "VELA_REPO_APPROVE_BUILD": "", "VELA_REPO_BRANCH": "foo", "VELA_REPO_TOPICS": "cloud,security", "VELA_REPO_BUILD_LIMIT": "1", "VELA_REPO_CLONE": "foo", "VELA_REPO_FULL_NAME": "foo", "VELA_REPO_LINK": "foo", "VELA_REPO_NAME": "foo", "VELA_REPO_ORG": "foo", "VELA_REPO_PIPELINE_TYPE": "", "VELA_REPO_PRIVATE": "false", "VELA_REPO_TIMEOUT": "1", "VELA_REPO_TRUSTED": "false", "VELA_REPO_VISIBILITY": "foo", "VELA_RUNTIME": "TODO", "VELA_SOURCE": "foo", "VELA_USER_ACTIVE": "false", "VELA_USER_ADMIN": "false", "VELA_USER_FAVORITES": "[]", "VELA_USER_NAME": "foo", "VELA_VERSION": "TODO", "VELA_WORKSPACE": "/vela/src/foo/foo/foo"}, + want: map[string]string{"BUILD_AUTHOR": "foo", "BUILD_AUTHOR_EMAIL": "", "BUILD_BASE_REF": "foo", "BUILD_BRANCH": "foo", "BUILD_CHANNEL": "foo", "BUILD_CLONE": "foo", "BUILD_COMMIT": "foo", "BUILD_CREATED": "1", "BUILD_ENQUEUED": "1", "BUILD_EVENT": "tag", "BUILD_HOST": "", "BUILD_LINK": "", "BUILD_MESSAGE": "foo", "BUILD_NUMBER": "1", "BUILD_PARENT": "1", "BUILD_REF": "refs/tags/1", "BUILD_SENDER": "foo", "BUILD_SOURCE": "foo", "BUILD_STARTED": "1", "BUILD_STATUS": "foo", "BUILD_TAG": "1", "BUILD_TITLE": "foo", "BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "CI": "true", "REPOSITORY_ACTIVE": "false", "REPOSITORY_ALLOW_EVENTS": "", "REPOSITORY_BRANCH": "foo", "REPOSITORY_CLONE": "foo", "REPOSITORY_FULL_NAME": "foo", "REPOSITORY_LINK": "foo", "REPOSITORY_NAME": "foo", "REPOSITORY_ORG": "foo", "REPOSITORY_PRIVATE": "false", "REPOSITORY_TIMEOUT": "1", "REPOSITORY_TRUSTED": "false", "REPOSITORY_VISIBILITY": "foo", "VELA": "true", "VELA_ADDR": "foo", "VELA_BUILD_APPROVED_AT": "0", "VELA_BUILD_APPROVED_BY": "", "VELA_BUILD_AUTHOR": "foo", "VELA_BUILD_AUTHOR_EMAIL": "", "VELA_BUILD_BASE_REF": "foo", "VELA_BUILD_BRANCH": "foo", "VELA_BUILD_CHANNEL": "foo", "VELA_BUILD_CLONE": "foo", "VELA_BUILD_COMMIT": "foo", "VELA_BUILD_CREATED": "1", "VELA_BUILD_DISTRIBUTION": "", "VELA_BUILD_ENQUEUED": "1", "VELA_BUILD_EVENT": "tag", "VELA_BUILD_EVENT_ACTION": "", "VELA_BUILD_HOST": "", "VELA_BUILD_LINK": "", "VELA_BUILD_MESSAGE": "foo", "VELA_BUILD_NUMBER": "1", "VELA_BUILD_PARENT": "1", "VELA_BUILD_REF": "refs/tags/1", "VELA_BUILD_RUNTIME": "", "VELA_BUILD_SENDER": "foo", "VELA_BUILD_SOURCE": "foo", "VELA_BUILD_STARTED": "1", "VELA_BUILD_STATUS": "foo", "VELA_BUILD_TAG": "1", "VELA_BUILD_TITLE": "foo", "VELA_BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_CHANNEL": "foo", "VELA_DATABASE": "foo", "VELA_DISTRIBUTION": "TODO", "VELA_HOST": "foo", "VELA_NETRC_MACHINE": "foo", "VELA_NETRC_PASSWORD": "foo", "VELA_NETRC_USERNAME": "x-oauth-basic", "VELA_QUEUE": "foo", "VELA_REPO_ACTIVE": "false", "VELA_REPO_ALLOW_EVENTS": "", "VELA_REPO_APPROVE_BUILD": "", "VELA_REPO_BRANCH": "foo", "VELA_REPO_TOPICS": "cloud,security", "VELA_REPO_BUILD_LIMIT": "1", "VELA_REPO_CLONE": "foo", "VELA_REPO_FULL_NAME": "foo", "VELA_REPO_LINK": "foo", "VELA_REPO_NAME": "foo", "VELA_REPO_ORG": "foo", "VELA_REPO_OWNER": "foo", "VELA_REPO_PIPELINE_TYPE": "", "VELA_REPO_PRIVATE": "false", "VELA_REPO_TIMEOUT": "1", "VELA_REPO_TRUSTED": "false", "VELA_REPO_VISIBILITY": "foo", "VELA_RUNTIME": "TODO", "VELA_SOURCE": "foo", "VELA_USER_ACTIVE": "false", "VELA_USER_ADMIN": "false", "VELA_USER_FAVORITES": "[]", "VELA_USER_NAME": "foo", "VELA_VERSION": "TODO", "VELA_WORKSPACE": "/vela/src/foo/foo/foo"}, }, // pull_request { w: workspace, b: &library.Build{ID: &num64, RepoID: &num64, Number: &num, Parent: &num, Event: &pull, EventAction: &pullact, Status: &str, Error: &str, Enqueued: &num64, Created: &num64, Started: &num64, Finished: &num64, Deploy: &str, Clone: &str, Source: &str, Title: &str, Message: &str, Commit: &str, Sender: &str, Author: &str, Branch: &str, Ref: &pullref, BaseRef: &str}, - m: &types.Metadata{Database: &types.Database{Driver: str, Host: str}, Queue: &types.Queue{Channel: str, Driver: str, Host: str}, Source: &types.Source{Driver: str, Host: str}, Vela: &types.Vela{Address: str, WebAddress: str}}, - r: &library.Repo{ID: &num64, UserID: &num64, Org: &str, Name: &str, FullName: &str, Link: &str, Clone: &str, Branch: &str, Topics: &topics, BuildLimit: &num64, Timeout: &num64, Visibility: &str, Private: &booL, Trusted: &booL, Active: &booL}, + m: &internal.Metadata{Database: &internal.Database{Driver: str, Host: str}, Queue: &internal.Queue{Channel: str, Driver: str, Host: str}, Source: &internal.Source{Driver: str, Host: str}, Vela: &internal.Vela{Address: str, WebAddress: str}}, + r: &api.Repo{ID: &num64, Owner: &library.User{ID: &num64, Name: &str, Token: &str, Active: &booL, Admin: &booL}, Org: &str, Name: &str, FullName: &str, Link: &str, Clone: &str, Branch: &str, Topics: &topics, BuildLimit: &num64, Timeout: &num64, Visibility: &str, Private: &booL, Trusted: &booL, Active: &booL}, u: &library.User{ID: &num64, Name: &str, Token: &str, Active: &booL, Admin: &booL}, - want: map[string]string{"BUILD_AUTHOR": "foo", "BUILD_AUTHOR_EMAIL": "", "BUILD_BASE_REF": "foo", "BUILD_BRANCH": "foo", "BUILD_CHANNEL": "foo", "BUILD_CLONE": "foo", "BUILD_COMMIT": "foo", "BUILD_CREATED": "1", "BUILD_ENQUEUED": "1", "BUILD_EVENT": "pull_request", "BUILD_HOST": "", "BUILD_LINK": "", "BUILD_MESSAGE": "foo", "BUILD_NUMBER": "1", "BUILD_PARENT": "1", "BUILD_PULL_REQUEST_NUMBER": "1", "BUILD_REF": "refs/pull/1/head", "BUILD_SENDER": "foo", "BUILD_SOURCE": "foo", "BUILD_STARTED": "1", "BUILD_STATUS": "foo", "BUILD_TITLE": "foo", "BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "CI": "true", "REPOSITORY_ACTIVE": "false", "REPOSITORY_ALLOW_EVENTS": "", "REPOSITORY_BRANCH": "foo", "REPOSITORY_CLONE": "foo", "REPOSITORY_FULL_NAME": "foo", "REPOSITORY_LINK": "foo", "REPOSITORY_NAME": "foo", "REPOSITORY_ORG": "foo", "REPOSITORY_PRIVATE": "false", "REPOSITORY_TIMEOUT": "1", "REPOSITORY_TRUSTED": "false", "REPOSITORY_VISIBILITY": "foo", "VELA": "true", "VELA_ADDR": "foo", "VELA_BUILD_APPROVED_AT": "0", "VELA_BUILD_APPROVED_BY": "", "VELA_BUILD_AUTHOR": "foo", "VELA_BUILD_AUTHOR_EMAIL": "", "VELA_BUILD_BASE_REF": "foo", "VELA_BUILD_BRANCH": "foo", "VELA_BUILD_CHANNEL": "foo", "VELA_BUILD_CLONE": "foo", "VELA_BUILD_COMMIT": "foo", "VELA_BUILD_CREATED": "1", "VELA_BUILD_DISTRIBUTION": "", "VELA_BUILD_ENQUEUED": "1", "VELA_BUILD_EVENT": "pull_request", "VELA_BUILD_EVENT_ACTION": "opened", "VELA_BUILD_HOST": "", "VELA_BUILD_LINK": "", "VELA_BUILD_MESSAGE": "foo", "VELA_BUILD_NUMBER": "1", "VELA_BUILD_PARENT": "1", "VELA_BUILD_PULL_REQUEST": "1", "VELA_BUILD_REF": "refs/pull/1/head", "VELA_BUILD_RUNTIME": "", "VELA_BUILD_SENDER": "foo", "VELA_BUILD_SOURCE": "foo", "VELA_BUILD_STARTED": "1", "VELA_BUILD_STATUS": "foo", "VELA_BUILD_TITLE": "foo", "VELA_BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_CHANNEL": "foo", "VELA_DATABASE": "foo", "VELA_DISTRIBUTION": "TODO", "VELA_HOST": "foo", "VELA_NETRC_MACHINE": "foo", "VELA_NETRC_PASSWORD": "foo", "VELA_NETRC_USERNAME": "x-oauth-basic", "VELA_PULL_REQUEST": "1", "VELA_PULL_REQUEST_SOURCE": "", "VELA_PULL_REQUEST_TARGET": "foo", "VELA_QUEUE": "foo", "VELA_REPO_ACTIVE": "false", "VELA_REPO_ALLOW_EVENTS": "", "VELA_REPO_APPROVE_BUILD": "", "VELA_REPO_BRANCH": "foo", "VELA_REPO_TOPICS": "cloud,security", "VELA_REPO_BUILD_LIMIT": "1", "VELA_REPO_CLONE": "foo", "VELA_REPO_FULL_NAME": "foo", "VELA_REPO_LINK": "foo", "VELA_REPO_NAME": "foo", "VELA_REPO_ORG": "foo", "VELA_REPO_PIPELINE_TYPE": "", "VELA_REPO_PRIVATE": "false", "VELA_REPO_TIMEOUT": "1", "VELA_REPO_TRUSTED": "false", "VELA_REPO_VISIBILITY": "foo", "VELA_RUNTIME": "TODO", "VELA_SOURCE": "foo", "VELA_USER_ACTIVE": "false", "VELA_USER_ADMIN": "false", "VELA_USER_FAVORITES": "[]", "VELA_USER_NAME": "foo", "VELA_VERSION": "TODO", "VELA_WORKSPACE": "/vela/src/foo/foo/foo"}, + want: map[string]string{"BUILD_AUTHOR": "foo", "BUILD_AUTHOR_EMAIL": "", "BUILD_BASE_REF": "foo", "BUILD_BRANCH": "foo", "BUILD_CHANNEL": "foo", "BUILD_CLONE": "foo", "BUILD_COMMIT": "foo", "BUILD_CREATED": "1", "BUILD_ENQUEUED": "1", "BUILD_EVENT": "pull_request", "BUILD_HOST": "", "BUILD_LINK": "", "BUILD_MESSAGE": "foo", "BUILD_NUMBER": "1", "BUILD_PARENT": "1", "BUILD_PULL_REQUEST_NUMBER": "1", "BUILD_REF": "refs/pull/1/head", "BUILD_SENDER": "foo", "BUILD_SOURCE": "foo", "BUILD_STARTED": "1", "BUILD_STATUS": "foo", "BUILD_TITLE": "foo", "BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "CI": "true", "REPOSITORY_ACTIVE": "false", "REPOSITORY_ALLOW_EVENTS": "", "REPOSITORY_BRANCH": "foo", "REPOSITORY_CLONE": "foo", "REPOSITORY_FULL_NAME": "foo", "REPOSITORY_LINK": "foo", "REPOSITORY_NAME": "foo", "REPOSITORY_ORG": "foo", "REPOSITORY_PRIVATE": "false", "REPOSITORY_TIMEOUT": "1", "REPOSITORY_TRUSTED": "false", "REPOSITORY_VISIBILITY": "foo", "VELA": "true", "VELA_ADDR": "foo", "VELA_BUILD_APPROVED_AT": "0", "VELA_BUILD_APPROVED_BY": "", "VELA_BUILD_AUTHOR": "foo", "VELA_BUILD_AUTHOR_EMAIL": "", "VELA_BUILD_BASE_REF": "foo", "VELA_BUILD_BRANCH": "foo", "VELA_BUILD_CHANNEL": "foo", "VELA_BUILD_CLONE": "foo", "VELA_BUILD_COMMIT": "foo", "VELA_BUILD_CREATED": "1", "VELA_BUILD_DISTRIBUTION": "", "VELA_BUILD_ENQUEUED": "1", "VELA_BUILD_EVENT": "pull_request", "VELA_BUILD_EVENT_ACTION": "opened", "VELA_BUILD_HOST": "", "VELA_BUILD_LINK": "", "VELA_BUILD_MESSAGE": "foo", "VELA_BUILD_NUMBER": "1", "VELA_BUILD_PARENT": "1", "VELA_BUILD_PULL_REQUEST": "1", "VELA_BUILD_REF": "refs/pull/1/head", "VELA_BUILD_RUNTIME": "", "VELA_BUILD_SENDER": "foo", "VELA_BUILD_SOURCE": "foo", "VELA_BUILD_STARTED": "1", "VELA_BUILD_STATUS": "foo", "VELA_BUILD_TITLE": "foo", "VELA_BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_CHANNEL": "foo", "VELA_DATABASE": "foo", "VELA_DISTRIBUTION": "TODO", "VELA_HOST": "foo", "VELA_NETRC_MACHINE": "foo", "VELA_NETRC_PASSWORD": "foo", "VELA_NETRC_USERNAME": "x-oauth-basic", "VELA_PULL_REQUEST": "1", "VELA_PULL_REQUEST_SOURCE": "", "VELA_PULL_REQUEST_TARGET": "foo", "VELA_QUEUE": "foo", "VELA_REPO_ACTIVE": "false", "VELA_REPO_ALLOW_EVENTS": "", "VELA_REPO_APPROVE_BUILD": "", "VELA_REPO_BRANCH": "foo", "VELA_REPO_TOPICS": "cloud,security", "VELA_REPO_BUILD_LIMIT": "1", "VELA_REPO_CLONE": "foo", "VELA_REPO_FULL_NAME": "foo", "VELA_REPO_LINK": "foo", "VELA_REPO_NAME": "foo", "VELA_REPO_ORG": "foo", "VELA_REPO_OWNER": "foo", "VELA_REPO_PIPELINE_TYPE": "", "VELA_REPO_PRIVATE": "false", "VELA_REPO_TIMEOUT": "1", "VELA_REPO_TRUSTED": "false", "VELA_REPO_VISIBILITY": "foo", "VELA_RUNTIME": "TODO", "VELA_SOURCE": "foo", "VELA_USER_ACTIVE": "false", "VELA_USER_ADMIN": "false", "VELA_USER_FAVORITES": "[]", "VELA_USER_NAME": "foo", "VELA_VERSION": "TODO", "VELA_WORKSPACE": "/vela/src/foo/foo/foo"}, }, // deployment { w: workspace, b: &library.Build{ID: &num64, RepoID: &num64, Number: &num, Parent: &num, Event: &deploy, Status: &str, Error: &str, Enqueued: &num64, Created: &num64, Started: &num64, Finished: &num64, Deploy: &target, Clone: &str, Source: &str, Title: &str, Message: &str, Commit: &str, Sender: &str, Author: &str, Branch: &str, Ref: &pullref, BaseRef: &str}, - m: &types.Metadata{Database: &types.Database{Driver: str, Host: str}, Queue: &types.Queue{Channel: str, Driver: str, Host: str}, Source: &types.Source{Driver: str, Host: str}, Vela: &types.Vela{Address: str, WebAddress: str}}, - r: &library.Repo{ID: &num64, UserID: &num64, Org: &str, Name: &str, FullName: &str, Link: &str, Clone: &str, Branch: &str, Topics: &topics, BuildLimit: &num64, Timeout: &num64, Visibility: &str, Private: &booL, Trusted: &booL, Active: &booL}, + m: &internal.Metadata{Database: &internal.Database{Driver: str, Host: str}, Queue: &internal.Queue{Channel: str, Driver: str, Host: str}, Source: &internal.Source{Driver: str, Host: str}, Vela: &internal.Vela{Address: str, WebAddress: str}}, + r: &api.Repo{ID: &num64, Owner: &library.User{ID: &num64, Name: &str, Token: &str, Active: &booL, Admin: &booL}, Org: &str, Name: &str, FullName: &str, Link: &str, Clone: &str, Branch: &str, Topics: &topics, BuildLimit: &num64, Timeout: &num64, Visibility: &str, Private: &booL, Trusted: &booL, Active: &booL}, u: &library.User{ID: &num64, Name: &str, Token: &str, Active: &booL, Admin: &booL}, - want: map[string]string{"BUILD_AUTHOR": "foo", "BUILD_AUTHOR_EMAIL": "", "BUILD_BASE_REF": "foo", "BUILD_BRANCH": "foo", "BUILD_CHANNEL": "foo", "BUILD_CLONE": "foo", "BUILD_COMMIT": "foo", "BUILD_CREATED": "1", "BUILD_ENQUEUED": "1", "BUILD_EVENT": "deployment", "BUILD_HOST": "", "BUILD_LINK": "", "BUILD_MESSAGE": "foo", "BUILD_NUMBER": "1", "BUILD_PARENT": "1", "BUILD_REF": "refs/pull/1/head", "BUILD_SENDER": "foo", "BUILD_SOURCE": "foo", "BUILD_STARTED": "1", "BUILD_STATUS": "foo", "BUILD_TARGET": "production", "BUILD_TITLE": "foo", "BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "CI": "true", "REPOSITORY_ACTIVE": "false", "REPOSITORY_ALLOW_EVENTS": "", "REPOSITORY_BRANCH": "foo", "REPOSITORY_CLONE": "foo", "REPOSITORY_FULL_NAME": "foo", "REPOSITORY_LINK": "foo", "REPOSITORY_NAME": "foo", "REPOSITORY_ORG": "foo", "REPOSITORY_PRIVATE": "false", "REPOSITORY_TIMEOUT": "1", "REPOSITORY_TRUSTED": "false", "REPOSITORY_VISIBILITY": "foo", "VELA": "true", "VELA_ADDR": "foo", "VELA_BUILD_APPROVED_AT": "0", "VELA_BUILD_APPROVED_BY": "", "VELA_BUILD_AUTHOR": "foo", "VELA_BUILD_AUTHOR_EMAIL": "", "VELA_BUILD_BASE_REF": "foo", "VELA_BUILD_BRANCH": "foo", "VELA_BUILD_CHANNEL": "foo", "VELA_BUILD_CLONE": "foo", "VELA_BUILD_COMMIT": "foo", "VELA_BUILD_CREATED": "1", "VELA_BUILD_DISTRIBUTION": "", "VELA_BUILD_ENQUEUED": "1", "VELA_BUILD_EVENT": "deployment", "VELA_BUILD_EVENT_ACTION": "", "VELA_BUILD_HOST": "", "VELA_BUILD_LINK": "", "VELA_BUILD_MESSAGE": "foo", "VELA_BUILD_NUMBER": "1", "VELA_BUILD_PARENT": "1", "VELA_BUILD_REF": "refs/pull/1/head", "VELA_BUILD_RUNTIME": "", "VELA_BUILD_SENDER": "foo", "VELA_BUILD_SOURCE": "foo", "VELA_BUILD_STARTED": "1", "VELA_BUILD_STATUS": "foo", "VELA_BUILD_TARGET": "production", "VELA_BUILD_TITLE": "foo", "VELA_BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_CHANNEL": "foo", "VELA_DATABASE": "foo", "VELA_DEPLOYMENT": "production", "VELA_DEPLOYMENT_NUMBER": "0", "VELA_DISTRIBUTION": "TODO", "VELA_HOST": "foo", "VELA_NETRC_MACHINE": "foo", "VELA_NETRC_PASSWORD": "foo", "VELA_NETRC_USERNAME": "x-oauth-basic", "VELA_QUEUE": "foo", "VELA_REPO_ACTIVE": "false", "VELA_REPO_ALLOW_EVENTS": "", "VELA_REPO_APPROVE_BUILD": "", "VELA_REPO_BRANCH": "foo", "VELA_REPO_TOPICS": "cloud,security", "VELA_REPO_BUILD_LIMIT": "1", "VELA_REPO_CLONE": "foo", "VELA_REPO_FULL_NAME": "foo", "VELA_REPO_LINK": "foo", "VELA_REPO_NAME": "foo", "VELA_REPO_ORG": "foo", "VELA_REPO_PIPELINE_TYPE": "", "VELA_REPO_PRIVATE": "false", "VELA_REPO_TIMEOUT": "1", "VELA_REPO_TRUSTED": "false", "VELA_REPO_VISIBILITY": "foo", "VELA_RUNTIME": "TODO", "VELA_SOURCE": "foo", "VELA_USER_ACTIVE": "false", "VELA_USER_ADMIN": "false", "VELA_USER_FAVORITES": "[]", "VELA_USER_NAME": "foo", "VELA_VERSION": "TODO", "VELA_WORKSPACE": "/vela/src/foo/foo/foo"}, + want: map[string]string{"BUILD_AUTHOR": "foo", "BUILD_AUTHOR_EMAIL": "", "BUILD_BASE_REF": "foo", "BUILD_BRANCH": "foo", "BUILD_CHANNEL": "foo", "BUILD_CLONE": "foo", "BUILD_COMMIT": "foo", "BUILD_CREATED": "1", "BUILD_ENQUEUED": "1", "BUILD_EVENT": "deployment", "BUILD_HOST": "", "BUILD_LINK": "", "BUILD_MESSAGE": "foo", "BUILD_NUMBER": "1", "BUILD_PARENT": "1", "BUILD_REF": "refs/pull/1/head", "BUILD_SENDER": "foo", "BUILD_SOURCE": "foo", "BUILD_STARTED": "1", "BUILD_STATUS": "foo", "BUILD_TARGET": "production", "BUILD_TITLE": "foo", "BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "CI": "true", "REPOSITORY_ACTIVE": "false", "REPOSITORY_ALLOW_EVENTS": "", "REPOSITORY_BRANCH": "foo", "REPOSITORY_CLONE": "foo", "REPOSITORY_FULL_NAME": "foo", "REPOSITORY_LINK": "foo", "REPOSITORY_NAME": "foo", "REPOSITORY_ORG": "foo", "REPOSITORY_PRIVATE": "false", "REPOSITORY_TIMEOUT": "1", "REPOSITORY_TRUSTED": "false", "REPOSITORY_VISIBILITY": "foo", "VELA": "true", "VELA_ADDR": "foo", "VELA_BUILD_APPROVED_AT": "0", "VELA_BUILD_APPROVED_BY": "", "VELA_BUILD_AUTHOR": "foo", "VELA_BUILD_AUTHOR_EMAIL": "", "VELA_BUILD_BASE_REF": "foo", "VELA_BUILD_BRANCH": "foo", "VELA_BUILD_CHANNEL": "foo", "VELA_BUILD_CLONE": "foo", "VELA_BUILD_COMMIT": "foo", "VELA_BUILD_CREATED": "1", "VELA_BUILD_DISTRIBUTION": "", "VELA_BUILD_ENQUEUED": "1", "VELA_BUILD_EVENT": "deployment", "VELA_BUILD_EVENT_ACTION": "", "VELA_BUILD_HOST": "", "VELA_BUILD_LINK": "", "VELA_BUILD_MESSAGE": "foo", "VELA_BUILD_NUMBER": "1", "VELA_BUILD_PARENT": "1", "VELA_BUILD_REF": "refs/pull/1/head", "VELA_BUILD_RUNTIME": "", "VELA_BUILD_SENDER": "foo", "VELA_BUILD_SOURCE": "foo", "VELA_BUILD_STARTED": "1", "VELA_BUILD_STATUS": "foo", "VELA_BUILD_TARGET": "production", "VELA_BUILD_TITLE": "foo", "VELA_BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_CHANNEL": "foo", "VELA_DATABASE": "foo", "VELA_DEPLOYMENT": "production", "VELA_DEPLOYMENT_NUMBER": "0", "VELA_DISTRIBUTION": "TODO", "VELA_HOST": "foo", "VELA_NETRC_MACHINE": "foo", "VELA_NETRC_PASSWORD": "foo", "VELA_NETRC_USERNAME": "x-oauth-basic", "VELA_QUEUE": "foo", "VELA_REPO_ACTIVE": "false", "VELA_REPO_ALLOW_EVENTS": "", "VELA_REPO_APPROVE_BUILD": "", "VELA_REPO_BRANCH": "foo", "VELA_REPO_TOPICS": "cloud,security", "VELA_REPO_BUILD_LIMIT": "1", "VELA_REPO_CLONE": "foo", "VELA_REPO_FULL_NAME": "foo", "VELA_REPO_LINK": "foo", "VELA_REPO_NAME": "foo", "VELA_REPO_ORG": "foo", "VELA_REPO_OWNER": "foo", "VELA_REPO_PIPELINE_TYPE": "", "VELA_REPO_PRIVATE": "false", "VELA_REPO_TIMEOUT": "1", "VELA_REPO_TRUSTED": "false", "VELA_REPO_VISIBILITY": "foo", "VELA_RUNTIME": "TODO", "VELA_SOURCE": "foo", "VELA_USER_ACTIVE": "false", "VELA_USER_ADMIN": "false", "VELA_USER_FAVORITES": "[]", "VELA_USER_NAME": "foo", "VELA_VERSION": "TODO", "VELA_WORKSPACE": "/vela/src/foo/foo/foo"}, }, } @@ -688,8 +692,8 @@ func Test_client_EnvironmentBuild(t *testing.T) { type fields struct { build *library.Build - metadata *types.Metadata - repo *library.Repo + metadata *internal.Metadata + repo *api.Repo user *library.User } @@ -700,30 +704,30 @@ func Test_client_EnvironmentBuild(t *testing.T) { }{ {"push", fields{ build: &library.Build{ID: &num64, RepoID: &num64, Number: &num, Parent: &num, Event: &push, Status: &str, Error: &str, Enqueued: &num64, Created: &num64, Started: &num64, Finished: &num64, Deploy: &str, Clone: &str, Source: &str, Title: &str, Message: &str, Commit: &str, Sender: &str, Author: &str, Branch: &str, Ref: &str, BaseRef: &str}, - metadata: &types.Metadata{Database: &types.Database{Driver: str, Host: str}, Queue: &types.Queue{Channel: str, Driver: str, Host: str}, Source: &types.Source{Driver: str, Host: str}, Vela: &types.Vela{Address: str, WebAddress: str}}, - repo: &library.Repo{ID: &num64, UserID: &num64, Org: &str, Name: &str, FullName: &str, Link: &str, Clone: &str, Branch: &str, Topics: &topics, BuildLimit: &num64, Timeout: &num64, Visibility: &str, Private: &booL, Trusted: &booL, Active: &booL}, + metadata: &internal.Metadata{Database: &internal.Database{Driver: str, Host: str}, Queue: &internal.Queue{Channel: str, Driver: str, Host: str}, Source: &internal.Source{Driver: str, Host: str}, Vela: &internal.Vela{Address: str, WebAddress: str}}, + repo: &api.Repo{ID: &num64, Owner: &library.User{ID: &num64, Name: &str, Token: &str, Active: &booL, Admin: &booL}, Org: &str, Name: &str, FullName: &str, Link: &str, Clone: &str, Branch: &str, Topics: &topics, BuildLimit: &num64, Timeout: &num64, Visibility: &str, Private: &booL, Trusted: &booL, Active: &booL}, user: &library.User{ID: &num64, Name: &str, Token: &str, Active: &booL, Admin: &booL}, - }, map[string]string{"BUILD_AUTHOR": "foo", "BUILD_AUTHOR_EMAIL": "", "BUILD_BASE_REF": "foo", "BUILD_BRANCH": "foo", "BUILD_CHANNEL": "foo", "BUILD_CLONE": "foo", "BUILD_COMMIT": "foo", "BUILD_CREATED": "1", "BUILD_ENQUEUED": "1", "BUILD_EVENT": "push", "BUILD_HOST": "", "BUILD_LINK": "", "BUILD_MESSAGE": "foo", "BUILD_NUMBER": "1", "BUILD_PARENT": "1", "BUILD_REF": "foo", "BUILD_SENDER": "foo", "BUILD_SOURCE": "foo", "BUILD_STARTED": "1", "BUILD_STATUS": "foo", "BUILD_TITLE": "foo", "BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "CI": "true", "REPOSITORY_ACTIVE": "false", "REPOSITORY_ALLOW_EVENTS": "", "REPOSITORY_BRANCH": "foo", "REPOSITORY_CLONE": "foo", "REPOSITORY_FULL_NAME": "foo", "REPOSITORY_LINK": "foo", "REPOSITORY_NAME": "foo", "REPOSITORY_ORG": "foo", "REPOSITORY_PRIVATE": "false", "REPOSITORY_TIMEOUT": "1", "REPOSITORY_TRUSTED": "false", "REPOSITORY_VISIBILITY": "foo", "VELA": "true", "VELA_ADDR": "foo", "VELA_BUILD_APPROVED_AT": "0", "VELA_BUILD_APPROVED_BY": "", "VELA_BUILD_AUTHOR": "foo", "VELA_BUILD_AUTHOR_EMAIL": "", "VELA_BUILD_BASE_REF": "foo", "VELA_BUILD_BRANCH": "foo", "VELA_BUILD_CHANNEL": "foo", "VELA_BUILD_CLONE": "foo", "VELA_BUILD_COMMIT": "foo", "VELA_BUILD_CREATED": "1", "VELA_BUILD_DISTRIBUTION": "", "VELA_BUILD_ENQUEUED": "1", "VELA_BUILD_EVENT": "push", "VELA_BUILD_EVENT_ACTION": "", "VELA_BUILD_HOST": "", "VELA_BUILD_LINK": "", "VELA_BUILD_MESSAGE": "foo", "VELA_BUILD_NUMBER": "1", "VELA_BUILD_PARENT": "1", "VELA_BUILD_REF": "foo", "VELA_BUILD_RUNTIME": "", "VELA_BUILD_SENDER": "foo", "VELA_BUILD_SOURCE": "foo", "VELA_BUILD_STARTED": "1", "VELA_BUILD_STATUS": "foo", "VELA_BUILD_TITLE": "foo", "VELA_BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_CHANNEL": "foo", "VELA_DATABASE": "foo", "VELA_DISTRIBUTION": "TODO", "VELA_HOST": "foo", "VELA_NETRC_MACHINE": "foo", "VELA_NETRC_PASSWORD": "foo", "VELA_NETRC_USERNAME": "x-oauth-basic", "VELA_QUEUE": "foo", "VELA_REPO_ACTIVE": "false", "VELA_REPO_ALLOW_EVENTS": "", "VELA_REPO_APPROVE_BUILD": "", "VELA_REPO_BRANCH": "foo", "VELA_REPO_BUILD_LIMIT": "1", "VELA_REPO_CLONE": "foo", "VELA_REPO_FULL_NAME": "foo", "VELA_REPO_LINK": "foo", "VELA_REPO_NAME": "foo", "VELA_REPO_ORG": "foo", "VELA_REPO_PIPELINE_TYPE": "", "VELA_REPO_PRIVATE": "false", "VELA_REPO_TIMEOUT": "1", "VELA_REPO_TOPICS": "cloud,security", "VELA_REPO_TRUSTED": "false", "VELA_REPO_VISIBILITY": "foo", "VELA_RUNTIME": "TODO", "VELA_SOURCE": "foo", "VELA_USER_ACTIVE": "false", "VELA_USER_ADMIN": "false", "VELA_USER_FAVORITES": "[]", "VELA_USER_NAME": "foo", "VELA_VERSION": "TODO", "VELA_WORKSPACE": "/vela/src/foo/foo/foo"}}, + }, map[string]string{"BUILD_AUTHOR": "foo", "BUILD_AUTHOR_EMAIL": "", "BUILD_BASE_REF": "foo", "BUILD_BRANCH": "foo", "BUILD_CHANNEL": "foo", "BUILD_CLONE": "foo", "BUILD_COMMIT": "foo", "BUILD_CREATED": "1", "BUILD_ENQUEUED": "1", "BUILD_EVENT": "push", "BUILD_HOST": "", "BUILD_LINK": "", "BUILD_MESSAGE": "foo", "BUILD_NUMBER": "1", "BUILD_PARENT": "1", "BUILD_REF": "foo", "BUILD_SENDER": "foo", "BUILD_SOURCE": "foo", "BUILD_STARTED": "1", "BUILD_STATUS": "foo", "BUILD_TITLE": "foo", "BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "CI": "true", "REPOSITORY_ACTIVE": "false", "REPOSITORY_ALLOW_EVENTS": "", "REPOSITORY_BRANCH": "foo", "REPOSITORY_CLONE": "foo", "REPOSITORY_FULL_NAME": "foo", "REPOSITORY_LINK": "foo", "REPOSITORY_NAME": "foo", "REPOSITORY_ORG": "foo", "REPOSITORY_PRIVATE": "false", "REPOSITORY_TIMEOUT": "1", "REPOSITORY_TRUSTED": "false", "REPOSITORY_VISIBILITY": "foo", "VELA": "true", "VELA_ADDR": "foo", "VELA_BUILD_APPROVED_AT": "0", "VELA_BUILD_APPROVED_BY": "", "VELA_BUILD_AUTHOR": "foo", "VELA_BUILD_AUTHOR_EMAIL": "", "VELA_BUILD_BASE_REF": "foo", "VELA_BUILD_BRANCH": "foo", "VELA_BUILD_CHANNEL": "foo", "VELA_BUILD_CLONE": "foo", "VELA_BUILD_COMMIT": "foo", "VELA_BUILD_CREATED": "1", "VELA_BUILD_DISTRIBUTION": "", "VELA_BUILD_ENQUEUED": "1", "VELA_BUILD_EVENT": "push", "VELA_BUILD_EVENT_ACTION": "", "VELA_BUILD_HOST": "", "VELA_BUILD_LINK": "", "VELA_BUILD_MESSAGE": "foo", "VELA_BUILD_NUMBER": "1", "VELA_BUILD_PARENT": "1", "VELA_BUILD_REF": "foo", "VELA_BUILD_RUNTIME": "", "VELA_BUILD_SENDER": "foo", "VELA_BUILD_SOURCE": "foo", "VELA_BUILD_STARTED": "1", "VELA_BUILD_STATUS": "foo", "VELA_BUILD_TITLE": "foo", "VELA_BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_CHANNEL": "foo", "VELA_DATABASE": "foo", "VELA_DISTRIBUTION": "TODO", "VELA_HOST": "foo", "VELA_NETRC_MACHINE": "foo", "VELA_NETRC_PASSWORD": "foo", "VELA_NETRC_USERNAME": "x-oauth-basic", "VELA_QUEUE": "foo", "VELA_REPO_ACTIVE": "false", "VELA_REPO_ALLOW_EVENTS": "", "VELA_REPO_APPROVE_BUILD": "", "VELA_REPO_OWNER": "foo", "VELA_REPO_BRANCH": "foo", "VELA_REPO_BUILD_LIMIT": "1", "VELA_REPO_CLONE": "foo", "VELA_REPO_FULL_NAME": "foo", "VELA_REPO_LINK": "foo", "VELA_REPO_NAME": "foo", "VELA_REPO_ORG": "foo", "VELA_REPO_PIPELINE_TYPE": "", "VELA_REPO_PRIVATE": "false", "VELA_REPO_TIMEOUT": "1", "VELA_REPO_TOPICS": "cloud,security", "VELA_REPO_TRUSTED": "false", "VELA_REPO_VISIBILITY": "foo", "VELA_RUNTIME": "TODO", "VELA_SOURCE": "foo", "VELA_USER_ACTIVE": "false", "VELA_USER_ADMIN": "false", "VELA_USER_FAVORITES": "[]", "VELA_USER_NAME": "foo", "VELA_VERSION": "TODO", "VELA_WORKSPACE": "/vela/src/foo/foo/foo"}}, {"tag", fields{ build: &library.Build{ID: &num64, RepoID: &num64, Number: &num, Parent: &num, Event: &tag, Status: &str, Error: &str, Enqueued: &num64, Created: &num64, Started: &num64, Finished: &num64, Deploy: &str, Clone: &str, Source: &str, Title: &str, Message: &str, Commit: &str, Sender: &str, Author: &str, Branch: &str, Ref: &tagref, BaseRef: &str}, - metadata: &types.Metadata{Database: &types.Database{Driver: str, Host: str}, Queue: &types.Queue{Channel: str, Driver: str, Host: str}, Source: &types.Source{Driver: str, Host: str}, Vela: &types.Vela{Address: str, WebAddress: str}}, - repo: &library.Repo{ID: &num64, UserID: &num64, Org: &str, Name: &str, FullName: &str, Link: &str, Clone: &str, Branch: &str, Topics: &topics, BuildLimit: &num64, Timeout: &num64, Visibility: &str, Private: &booL, Trusted: &booL, Active: &booL}, + metadata: &internal.Metadata{Database: &internal.Database{Driver: str, Host: str}, Queue: &internal.Queue{Channel: str, Driver: str, Host: str}, Source: &internal.Source{Driver: str, Host: str}, Vela: &internal.Vela{Address: str, WebAddress: str}}, + repo: &api.Repo{ID: &num64, Owner: &library.User{ID: &num64, Name: &str, Token: &str, Active: &booL, Admin: &booL}, Org: &str, Name: &str, FullName: &str, Link: &str, Clone: &str, Branch: &str, Topics: &topics, BuildLimit: &num64, Timeout: &num64, Visibility: &str, Private: &booL, Trusted: &booL, Active: &booL}, user: &library.User{ID: &num64, Name: &str, Token: &str, Active: &booL, Admin: &booL}, - }, map[string]string{"BUILD_AUTHOR": "foo", "BUILD_AUTHOR_EMAIL": "", "BUILD_BASE_REF": "foo", "BUILD_BRANCH": "foo", "BUILD_CHANNEL": "foo", "BUILD_CLONE": "foo", "BUILD_COMMIT": "foo", "BUILD_CREATED": "1", "BUILD_ENQUEUED": "1", "BUILD_EVENT": "tag", "BUILD_HOST": "", "BUILD_LINK": "", "BUILD_MESSAGE": "foo", "BUILD_NUMBER": "1", "BUILD_PARENT": "1", "BUILD_REF": "refs/tags/1", "BUILD_SENDER": "foo", "BUILD_SOURCE": "foo", "BUILD_STARTED": "1", "BUILD_STATUS": "foo", "BUILD_TAG": "1", "BUILD_TITLE": "foo", "BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "CI": "true", "REPOSITORY_ACTIVE": "false", "REPOSITORY_ALLOW_EVENTS": "", "REPOSITORY_BRANCH": "foo", "REPOSITORY_CLONE": "foo", "REPOSITORY_FULL_NAME": "foo", "REPOSITORY_LINK": "foo", "REPOSITORY_NAME": "foo", "REPOSITORY_ORG": "foo", "REPOSITORY_PRIVATE": "false", "REPOSITORY_TIMEOUT": "1", "REPOSITORY_TRUSTED": "false", "REPOSITORY_VISIBILITY": "foo", "VELA": "true", "VELA_ADDR": "foo", "VELA_BUILD_APPROVED_AT": "0", "VELA_BUILD_APPROVED_BY": "", "VELA_BUILD_AUTHOR": "foo", "VELA_BUILD_AUTHOR_EMAIL": "", "VELA_BUILD_BASE_REF": "foo", "VELA_BUILD_BRANCH": "foo", "VELA_BUILD_CHANNEL": "foo", "VELA_BUILD_CLONE": "foo", "VELA_BUILD_COMMIT": "foo", "VELA_BUILD_CREATED": "1", "VELA_BUILD_DISTRIBUTION": "", "VELA_BUILD_ENQUEUED": "1", "VELA_BUILD_EVENT": "tag", "VELA_BUILD_EVENT_ACTION": "", "VELA_BUILD_HOST": "", "VELA_BUILD_LINK": "", "VELA_BUILD_MESSAGE": "foo", "VELA_BUILD_NUMBER": "1", "VELA_BUILD_PARENT": "1", "VELA_BUILD_REF": "refs/tags/1", "VELA_BUILD_RUNTIME": "", "VELA_BUILD_SENDER": "foo", "VELA_BUILD_SOURCE": "foo", "VELA_BUILD_STARTED": "1", "VELA_BUILD_STATUS": "foo", "VELA_BUILD_TAG": "1", "VELA_BUILD_TITLE": "foo", "VELA_BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_CHANNEL": "foo", "VELA_DATABASE": "foo", "VELA_DISTRIBUTION": "TODO", "VELA_HOST": "foo", "VELA_NETRC_MACHINE": "foo", "VELA_NETRC_PASSWORD": "foo", "VELA_NETRC_USERNAME": "x-oauth-basic", "VELA_QUEUE": "foo", "VELA_REPO_ACTIVE": "false", "VELA_REPO_ALLOW_EVENTS": "", "VELA_REPO_APPROVE_BUILD": "", "VELA_REPO_BRANCH": "foo", "VELA_REPO_BUILD_LIMIT": "1", "VELA_REPO_CLONE": "foo", "VELA_REPO_FULL_NAME": "foo", "VELA_REPO_LINK": "foo", "VELA_REPO_NAME": "foo", "VELA_REPO_ORG": "foo", "VELA_REPO_PIPELINE_TYPE": "", "VELA_REPO_PRIVATE": "false", "VELA_REPO_TIMEOUT": "1", "VELA_REPO_TOPICS": "cloud,security", "VELA_REPO_TRUSTED": "false", "VELA_REPO_VISIBILITY": "foo", "VELA_RUNTIME": "TODO", "VELA_SOURCE": "foo", "VELA_USER_ACTIVE": "false", "VELA_USER_ADMIN": "false", "VELA_USER_FAVORITES": "[]", "VELA_USER_NAME": "foo", "VELA_VERSION": "TODO", "VELA_WORKSPACE": "/vela/src/foo/foo/foo"}, + }, map[string]string{"BUILD_AUTHOR": "foo", "BUILD_AUTHOR_EMAIL": "", "BUILD_BASE_REF": "foo", "BUILD_BRANCH": "foo", "BUILD_CHANNEL": "foo", "BUILD_CLONE": "foo", "BUILD_COMMIT": "foo", "BUILD_CREATED": "1", "BUILD_ENQUEUED": "1", "BUILD_EVENT": "tag", "BUILD_HOST": "", "BUILD_LINK": "", "BUILD_MESSAGE": "foo", "BUILD_NUMBER": "1", "BUILD_PARENT": "1", "BUILD_REF": "refs/tags/1", "BUILD_SENDER": "foo", "BUILD_SOURCE": "foo", "BUILD_STARTED": "1", "BUILD_STATUS": "foo", "BUILD_TAG": "1", "BUILD_TITLE": "foo", "BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "CI": "true", "REPOSITORY_ACTIVE": "false", "REPOSITORY_ALLOW_EVENTS": "", "REPOSITORY_BRANCH": "foo", "REPOSITORY_CLONE": "foo", "REPOSITORY_FULL_NAME": "foo", "REPOSITORY_LINK": "foo", "REPOSITORY_NAME": "foo", "REPOSITORY_ORG": "foo", "REPOSITORY_PRIVATE": "false", "REPOSITORY_TIMEOUT": "1", "REPOSITORY_TRUSTED": "false", "REPOSITORY_VISIBILITY": "foo", "VELA": "true", "VELA_ADDR": "foo", "VELA_BUILD_APPROVED_AT": "0", "VELA_BUILD_APPROVED_BY": "", "VELA_BUILD_AUTHOR": "foo", "VELA_BUILD_AUTHOR_EMAIL": "", "VELA_BUILD_BASE_REF": "foo", "VELA_BUILD_BRANCH": "foo", "VELA_BUILD_CHANNEL": "foo", "VELA_BUILD_CLONE": "foo", "VELA_BUILD_COMMIT": "foo", "VELA_BUILD_CREATED": "1", "VELA_BUILD_DISTRIBUTION": "", "VELA_BUILD_ENQUEUED": "1", "VELA_BUILD_EVENT": "tag", "VELA_BUILD_EVENT_ACTION": "", "VELA_BUILD_HOST": "", "VELA_BUILD_LINK": "", "VELA_BUILD_MESSAGE": "foo", "VELA_BUILD_NUMBER": "1", "VELA_BUILD_PARENT": "1", "VELA_BUILD_REF": "refs/tags/1", "VELA_BUILD_RUNTIME": "", "VELA_BUILD_SENDER": "foo", "VELA_BUILD_SOURCE": "foo", "VELA_BUILD_STARTED": "1", "VELA_BUILD_STATUS": "foo", "VELA_BUILD_TAG": "1", "VELA_BUILD_TITLE": "foo", "VELA_BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_CHANNEL": "foo", "VELA_DATABASE": "foo", "VELA_DISTRIBUTION": "TODO", "VELA_HOST": "foo", "VELA_NETRC_MACHINE": "foo", "VELA_NETRC_PASSWORD": "foo", "VELA_NETRC_USERNAME": "x-oauth-basic", "VELA_QUEUE": "foo", "VELA_REPO_ACTIVE": "false", "VELA_REPO_ALLOW_EVENTS": "", "VELA_REPO_APPROVE_BUILD": "", "VELA_REPO_OWNER": "foo", "VELA_REPO_BRANCH": "foo", "VELA_REPO_BUILD_LIMIT": "1", "VELA_REPO_CLONE": "foo", "VELA_REPO_FULL_NAME": "foo", "VELA_REPO_LINK": "foo", "VELA_REPO_NAME": "foo", "VELA_REPO_ORG": "foo", "VELA_REPO_PIPELINE_TYPE": "", "VELA_REPO_PRIVATE": "false", "VELA_REPO_TIMEOUT": "1", "VELA_REPO_TOPICS": "cloud,security", "VELA_REPO_TRUSTED": "false", "VELA_REPO_VISIBILITY": "foo", "VELA_RUNTIME": "TODO", "VELA_SOURCE": "foo", "VELA_USER_ACTIVE": "false", "VELA_USER_ADMIN": "false", "VELA_USER_FAVORITES": "[]", "VELA_USER_NAME": "foo", "VELA_VERSION": "TODO", "VELA_WORKSPACE": "/vela/src/foo/foo/foo"}, }, {"pull_request", fields{ build: &library.Build{ID: &num64, RepoID: &num64, Number: &num, Parent: &num, Event: &pull, EventAction: &pullact, Status: &str, Error: &str, Enqueued: &num64, Created: &num64, Started: &num64, Finished: &num64, Deploy: &str, Clone: &str, Source: &str, Title: &str, Message: &str, Commit: &str, Sender: &str, Author: &str, Branch: &str, Ref: &pullref, BaseRef: &str}, - metadata: &types.Metadata{Database: &types.Database{Driver: str, Host: str}, Queue: &types.Queue{Channel: str, Driver: str, Host: str}, Source: &types.Source{Driver: str, Host: str}, Vela: &types.Vela{Address: str, WebAddress: str}}, - repo: &library.Repo{ID: &num64, UserID: &num64, Org: &str, Name: &str, FullName: &str, Link: &str, Clone: &str, Branch: &str, Topics: &topics, BuildLimit: &num64, Timeout: &num64, Visibility: &str, Private: &booL, Trusted: &booL, Active: &booL}, + metadata: &internal.Metadata{Database: &internal.Database{Driver: str, Host: str}, Queue: &internal.Queue{Channel: str, Driver: str, Host: str}, Source: &internal.Source{Driver: str, Host: str}, Vela: &internal.Vela{Address: str, WebAddress: str}}, + repo: &api.Repo{ID: &num64, Owner: &library.User{ID: &num64, Name: &str, Token: &str, Active: &booL, Admin: &booL}, Org: &str, Name: &str, FullName: &str, Link: &str, Clone: &str, Branch: &str, Topics: &topics, BuildLimit: &num64, Timeout: &num64, Visibility: &str, Private: &booL, Trusted: &booL, Active: &booL}, user: &library.User{ID: &num64, Name: &str, Token: &str, Active: &booL, Admin: &booL}, - }, map[string]string{"BUILD_AUTHOR": "foo", "BUILD_AUTHOR_EMAIL": "", "BUILD_BASE_REF": "foo", "BUILD_BRANCH": "foo", "BUILD_CHANNEL": "foo", "BUILD_CLONE": "foo", "BUILD_COMMIT": "foo", "BUILD_CREATED": "1", "BUILD_ENQUEUED": "1", "BUILD_EVENT": "pull_request", "BUILD_HOST": "", "BUILD_LINK": "", "BUILD_MESSAGE": "foo", "BUILD_NUMBER": "1", "BUILD_PARENT": "1", "BUILD_PULL_REQUEST_NUMBER": "1", "BUILD_REF": "refs/pull/1/head", "BUILD_SENDER": "foo", "BUILD_SOURCE": "foo", "BUILD_STARTED": "1", "BUILD_STATUS": "foo", "BUILD_TITLE": "foo", "BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "CI": "true", "REPOSITORY_ACTIVE": "false", "REPOSITORY_ALLOW_EVENTS": "", "REPOSITORY_BRANCH": "foo", "REPOSITORY_CLONE": "foo", "REPOSITORY_FULL_NAME": "foo", "REPOSITORY_LINK": "foo", "REPOSITORY_NAME": "foo", "REPOSITORY_ORG": "foo", "REPOSITORY_PRIVATE": "false", "REPOSITORY_TIMEOUT": "1", "REPOSITORY_TRUSTED": "false", "REPOSITORY_VISIBILITY": "foo", "VELA": "true", "VELA_ADDR": "foo", "VELA_BUILD_APPROVED_AT": "0", "VELA_BUILD_APPROVED_BY": "", "VELA_BUILD_AUTHOR": "foo", "VELA_BUILD_AUTHOR_EMAIL": "", "VELA_BUILD_BASE_REF": "foo", "VELA_BUILD_BRANCH": "foo", "VELA_BUILD_CHANNEL": "foo", "VELA_BUILD_CLONE": "foo", "VELA_BUILD_COMMIT": "foo", "VELA_BUILD_CREATED": "1", "VELA_BUILD_DISTRIBUTION": "", "VELA_BUILD_ENQUEUED": "1", "VELA_BUILD_EVENT": "pull_request", "VELA_BUILD_EVENT_ACTION": "opened", "VELA_BUILD_HOST": "", "VELA_BUILD_LINK": "", "VELA_BUILD_MESSAGE": "foo", "VELA_BUILD_NUMBER": "1", "VELA_BUILD_PARENT": "1", "VELA_BUILD_PULL_REQUEST": "1", "VELA_BUILD_REF": "refs/pull/1/head", "VELA_BUILD_RUNTIME": "", "VELA_BUILD_SENDER": "foo", "VELA_BUILD_SOURCE": "foo", "VELA_BUILD_STARTED": "1", "VELA_BUILD_STATUS": "foo", "VELA_BUILD_TITLE": "foo", "VELA_BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_CHANNEL": "foo", "VELA_DATABASE": "foo", "VELA_DISTRIBUTION": "TODO", "VELA_HOST": "foo", "VELA_NETRC_MACHINE": "foo", "VELA_NETRC_PASSWORD": "foo", "VELA_NETRC_USERNAME": "x-oauth-basic", "VELA_PULL_REQUEST": "1", "VELA_PULL_REQUEST_SOURCE": "", "VELA_PULL_REQUEST_TARGET": "foo", "VELA_QUEUE": "foo", "VELA_REPO_ACTIVE": "false", "VELA_REPO_ALLOW_EVENTS": "", "VELA_REPO_APPROVE_BUILD": "", "VELA_REPO_BRANCH": "foo", "VELA_REPO_BUILD_LIMIT": "1", "VELA_REPO_CLONE": "foo", "VELA_REPO_FULL_NAME": "foo", "VELA_REPO_LINK": "foo", "VELA_REPO_NAME": "foo", "VELA_REPO_ORG": "foo", "VELA_REPO_PIPELINE_TYPE": "", "VELA_REPO_PRIVATE": "false", "VELA_REPO_TIMEOUT": "1", "VELA_REPO_TOPICS": "cloud,security", "VELA_REPO_TRUSTED": "false", "VELA_REPO_VISIBILITY": "foo", "VELA_RUNTIME": "TODO", "VELA_SOURCE": "foo", "VELA_USER_ACTIVE": "false", "VELA_USER_ADMIN": "false", "VELA_USER_FAVORITES": "[]", "VELA_USER_NAME": "foo", "VELA_VERSION": "TODO", "VELA_WORKSPACE": "/vela/src/foo/foo/foo"}, + }, map[string]string{"BUILD_AUTHOR": "foo", "BUILD_AUTHOR_EMAIL": "", "BUILD_BASE_REF": "foo", "BUILD_BRANCH": "foo", "BUILD_CHANNEL": "foo", "BUILD_CLONE": "foo", "BUILD_COMMIT": "foo", "BUILD_CREATED": "1", "BUILD_ENQUEUED": "1", "BUILD_EVENT": "pull_request", "BUILD_HOST": "", "BUILD_LINK": "", "BUILD_MESSAGE": "foo", "BUILD_NUMBER": "1", "BUILD_PARENT": "1", "BUILD_PULL_REQUEST_NUMBER": "1", "BUILD_REF": "refs/pull/1/head", "BUILD_SENDER": "foo", "BUILD_SOURCE": "foo", "BUILD_STARTED": "1", "BUILD_STATUS": "foo", "BUILD_TITLE": "foo", "BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "CI": "true", "REPOSITORY_ACTIVE": "false", "REPOSITORY_ALLOW_EVENTS": "", "REPOSITORY_BRANCH": "foo", "REPOSITORY_CLONE": "foo", "REPOSITORY_FULL_NAME": "foo", "REPOSITORY_LINK": "foo", "REPOSITORY_NAME": "foo", "REPOSITORY_ORG": "foo", "REPOSITORY_PRIVATE": "false", "REPOSITORY_TIMEOUT": "1", "REPOSITORY_TRUSTED": "false", "REPOSITORY_VISIBILITY": "foo", "VELA": "true", "VELA_ADDR": "foo", "VELA_BUILD_APPROVED_AT": "0", "VELA_BUILD_APPROVED_BY": "", "VELA_BUILD_AUTHOR": "foo", "VELA_BUILD_AUTHOR_EMAIL": "", "VELA_BUILD_BASE_REF": "foo", "VELA_BUILD_BRANCH": "foo", "VELA_BUILD_CHANNEL": "foo", "VELA_BUILD_CLONE": "foo", "VELA_BUILD_COMMIT": "foo", "VELA_BUILD_CREATED": "1", "VELA_BUILD_DISTRIBUTION": "", "VELA_BUILD_ENQUEUED": "1", "VELA_BUILD_EVENT": "pull_request", "VELA_BUILD_EVENT_ACTION": "opened", "VELA_BUILD_HOST": "", "VELA_BUILD_LINK": "", "VELA_BUILD_MESSAGE": "foo", "VELA_BUILD_NUMBER": "1", "VELA_BUILD_PARENT": "1", "VELA_BUILD_PULL_REQUEST": "1", "VELA_BUILD_REF": "refs/pull/1/head", "VELA_BUILD_RUNTIME": "", "VELA_BUILD_SENDER": "foo", "VELA_BUILD_SOURCE": "foo", "VELA_BUILD_STARTED": "1", "VELA_BUILD_STATUS": "foo", "VELA_BUILD_TITLE": "foo", "VELA_BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_CHANNEL": "foo", "VELA_DATABASE": "foo", "VELA_DISTRIBUTION": "TODO", "VELA_HOST": "foo", "VELA_NETRC_MACHINE": "foo", "VELA_NETRC_PASSWORD": "foo", "VELA_NETRC_USERNAME": "x-oauth-basic", "VELA_PULL_REQUEST": "1", "VELA_PULL_REQUEST_SOURCE": "", "VELA_PULL_REQUEST_TARGET": "foo", "VELA_QUEUE": "foo", "VELA_REPO_ACTIVE": "false", "VELA_REPO_ALLOW_EVENTS": "", "VELA_REPO_APPROVE_BUILD": "", "VELA_REPO_OWNER": "foo", "VELA_REPO_BRANCH": "foo", "VELA_REPO_BUILD_LIMIT": "1", "VELA_REPO_CLONE": "foo", "VELA_REPO_FULL_NAME": "foo", "VELA_REPO_LINK": "foo", "VELA_REPO_NAME": "foo", "VELA_REPO_ORG": "foo", "VELA_REPO_PIPELINE_TYPE": "", "VELA_REPO_PRIVATE": "false", "VELA_REPO_TIMEOUT": "1", "VELA_REPO_TOPICS": "cloud,security", "VELA_REPO_TRUSTED": "false", "VELA_REPO_VISIBILITY": "foo", "VELA_RUNTIME": "TODO", "VELA_SOURCE": "foo", "VELA_USER_ACTIVE": "false", "VELA_USER_ADMIN": "false", "VELA_USER_FAVORITES": "[]", "VELA_USER_NAME": "foo", "VELA_VERSION": "TODO", "VELA_WORKSPACE": "/vela/src/foo/foo/foo"}, }, {"deployment", fields{ build: &library.Build{ID: &num64, RepoID: &num64, Number: &num, Parent: &num, Event: &deploy, Status: &str, Error: &str, Enqueued: &num64, Created: &num64, Started: &num64, Finished: &num64, Deploy: &target, Clone: &str, Source: &str, Title: &str, Message: &str, Commit: &str, Sender: &str, Author: &str, Branch: &str, Ref: &pullref, BaseRef: &str}, - metadata: &types.Metadata{Database: &types.Database{Driver: str, Host: str}, Queue: &types.Queue{Channel: str, Driver: str, Host: str}, Source: &types.Source{Driver: str, Host: str}, Vela: &types.Vela{Address: str, WebAddress: str}}, - repo: &library.Repo{ID: &num64, UserID: &num64, Org: &str, Name: &str, FullName: &str, Link: &str, Clone: &str, Branch: &str, Topics: &topics, BuildLimit: &num64, Timeout: &num64, Visibility: &str, Private: &booL, Trusted: &booL, Active: &booL}, + metadata: &internal.Metadata{Database: &internal.Database{Driver: str, Host: str}, Queue: &internal.Queue{Channel: str, Driver: str, Host: str}, Source: &internal.Source{Driver: str, Host: str}, Vela: &internal.Vela{Address: str, WebAddress: str}}, + repo: &api.Repo{ID: &num64, Owner: &library.User{ID: &num64, Name: &str, Token: &str, Active: &booL, Admin: &booL}, Org: &str, Name: &str, FullName: &str, Link: &str, Clone: &str, Branch: &str, Topics: &topics, BuildLimit: &num64, Timeout: &num64, Visibility: &str, Private: &booL, Trusted: &booL, Active: &booL}, user: &library.User{ID: &num64, Name: &str, Token: &str, Active: &booL, Admin: &booL}, - }, map[string]string{"BUILD_AUTHOR": "foo", "BUILD_AUTHOR_EMAIL": "", "BUILD_BASE_REF": "foo", "BUILD_BRANCH": "foo", "BUILD_CHANNEL": "foo", "BUILD_CLONE": "foo", "BUILD_COMMIT": "foo", "BUILD_CREATED": "1", "BUILD_ENQUEUED": "1", "BUILD_EVENT": "deployment", "BUILD_HOST": "", "BUILD_LINK": "", "BUILD_MESSAGE": "foo", "BUILD_NUMBER": "1", "BUILD_PARENT": "1", "BUILD_REF": "refs/pull/1/head", "BUILD_SENDER": "foo", "BUILD_SOURCE": "foo", "BUILD_STARTED": "1", "BUILD_STATUS": "foo", "BUILD_TARGET": "production", "BUILD_TITLE": "foo", "BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "CI": "true", "REPOSITORY_ACTIVE": "false", "REPOSITORY_ALLOW_EVENTS": "", "REPOSITORY_BRANCH": "foo", "REPOSITORY_CLONE": "foo", "REPOSITORY_FULL_NAME": "foo", "REPOSITORY_LINK": "foo", "REPOSITORY_NAME": "foo", "REPOSITORY_ORG": "foo", "REPOSITORY_PRIVATE": "false", "REPOSITORY_TIMEOUT": "1", "REPOSITORY_TRUSTED": "false", "REPOSITORY_VISIBILITY": "foo", "VELA": "true", "VELA_ADDR": "foo", "VELA_BUILD_APPROVED_AT": "0", "VELA_BUILD_APPROVED_BY": "", "VELA_BUILD_AUTHOR": "foo", "VELA_BUILD_AUTHOR_EMAIL": "", "VELA_BUILD_BASE_REF": "foo", "VELA_BUILD_BRANCH": "foo", "VELA_BUILD_CHANNEL": "foo", "VELA_BUILD_CLONE": "foo", "VELA_BUILD_COMMIT": "foo", "VELA_BUILD_CREATED": "1", "VELA_BUILD_DISTRIBUTION": "", "VELA_BUILD_ENQUEUED": "1", "VELA_BUILD_EVENT": "deployment", "VELA_BUILD_EVENT_ACTION": "", "VELA_BUILD_HOST": "", "VELA_BUILD_LINK": "", "VELA_BUILD_MESSAGE": "foo", "VELA_BUILD_NUMBER": "1", "VELA_BUILD_PARENT": "1", "VELA_BUILD_REF": "refs/pull/1/head", "VELA_BUILD_RUNTIME": "", "VELA_BUILD_SENDER": "foo", "VELA_BUILD_SOURCE": "foo", "VELA_BUILD_STARTED": "1", "VELA_BUILD_STATUS": "foo", "VELA_BUILD_TARGET": "production", "VELA_BUILD_TITLE": "foo", "VELA_BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_CHANNEL": "foo", "VELA_DATABASE": "foo", "VELA_DEPLOYMENT": "production", "VELA_DEPLOYMENT_NUMBER": "0", "VELA_DISTRIBUTION": "TODO", "VELA_HOST": "foo", "VELA_NETRC_MACHINE": "foo", "VELA_NETRC_PASSWORD": "foo", "VELA_NETRC_USERNAME": "x-oauth-basic", "VELA_QUEUE": "foo", "VELA_REPO_ACTIVE": "false", "VELA_REPO_ALLOW_EVENTS": "", "VELA_REPO_APPROVE_BUILD": "", "VELA_REPO_BRANCH": "foo", "VELA_REPO_BUILD_LIMIT": "1", "VELA_REPO_CLONE": "foo", "VELA_REPO_FULL_NAME": "foo", "VELA_REPO_LINK": "foo", "VELA_REPO_NAME": "foo", "VELA_REPO_ORG": "foo", "VELA_REPO_PIPELINE_TYPE": "", "VELA_REPO_PRIVATE": "false", "VELA_REPO_TIMEOUT": "1", "VELA_REPO_TOPICS": "cloud,security", "VELA_REPO_TRUSTED": "false", "VELA_REPO_VISIBILITY": "foo", "VELA_RUNTIME": "TODO", "VELA_SOURCE": "foo", "VELA_USER_ACTIVE": "false", "VELA_USER_ADMIN": "false", "VELA_USER_FAVORITES": "[]", "VELA_USER_NAME": "foo", "VELA_VERSION": "TODO", "VELA_WORKSPACE": "/vela/src/foo/foo/foo"}, + }, map[string]string{"BUILD_AUTHOR": "foo", "BUILD_AUTHOR_EMAIL": "", "BUILD_BASE_REF": "foo", "BUILD_BRANCH": "foo", "BUILD_CHANNEL": "foo", "BUILD_CLONE": "foo", "BUILD_COMMIT": "foo", "BUILD_CREATED": "1", "BUILD_ENQUEUED": "1", "BUILD_EVENT": "deployment", "BUILD_HOST": "", "BUILD_LINK": "", "BUILD_MESSAGE": "foo", "BUILD_NUMBER": "1", "BUILD_PARENT": "1", "BUILD_REF": "refs/pull/1/head", "BUILD_SENDER": "foo", "BUILD_SOURCE": "foo", "BUILD_STARTED": "1", "BUILD_STATUS": "foo", "BUILD_TARGET": "production", "BUILD_TITLE": "foo", "BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "CI": "true", "REPOSITORY_ACTIVE": "false", "REPOSITORY_ALLOW_EVENTS": "", "REPOSITORY_BRANCH": "foo", "REPOSITORY_CLONE": "foo", "REPOSITORY_FULL_NAME": "foo", "REPOSITORY_LINK": "foo", "REPOSITORY_NAME": "foo", "REPOSITORY_ORG": "foo", "REPOSITORY_PRIVATE": "false", "REPOSITORY_TIMEOUT": "1", "REPOSITORY_TRUSTED": "false", "REPOSITORY_VISIBILITY": "foo", "VELA": "true", "VELA_ADDR": "foo", "VELA_BUILD_APPROVED_AT": "0", "VELA_BUILD_APPROVED_BY": "", "VELA_BUILD_AUTHOR": "foo", "VELA_BUILD_AUTHOR_EMAIL": "", "VELA_BUILD_BASE_REF": "foo", "VELA_BUILD_BRANCH": "foo", "VELA_BUILD_CHANNEL": "foo", "VELA_BUILD_CLONE": "foo", "VELA_BUILD_COMMIT": "foo", "VELA_BUILD_CREATED": "1", "VELA_BUILD_DISTRIBUTION": "", "VELA_BUILD_ENQUEUED": "1", "VELA_BUILD_EVENT": "deployment", "VELA_BUILD_EVENT_ACTION": "", "VELA_BUILD_HOST": "", "VELA_BUILD_LINK": "", "VELA_BUILD_MESSAGE": "foo", "VELA_BUILD_NUMBER": "1", "VELA_BUILD_PARENT": "1", "VELA_BUILD_REF": "refs/pull/1/head", "VELA_BUILD_RUNTIME": "", "VELA_BUILD_SENDER": "foo", "VELA_BUILD_SOURCE": "foo", "VELA_BUILD_STARTED": "1", "VELA_BUILD_STATUS": "foo", "VELA_BUILD_TARGET": "production", "VELA_BUILD_TITLE": "foo", "VELA_BUILD_WORKSPACE": "/vela/src/foo/foo/foo", "VELA_CHANNEL": "foo", "VELA_DATABASE": "foo", "VELA_DEPLOYMENT": "production", "VELA_DEPLOYMENT_NUMBER": "0", "VELA_DISTRIBUTION": "TODO", "VELA_HOST": "foo", "VELA_NETRC_MACHINE": "foo", "VELA_NETRC_PASSWORD": "foo", "VELA_NETRC_USERNAME": "x-oauth-basic", "VELA_QUEUE": "foo", "VELA_REPO_ACTIVE": "false", "VELA_REPO_ALLOW_EVENTS": "", "VELA_REPO_APPROVE_BUILD": "", "VELA_REPO_OWNER": "foo", "VELA_REPO_BRANCH": "foo", "VELA_REPO_BUILD_LIMIT": "1", "VELA_REPO_CLONE": "foo", "VELA_REPO_FULL_NAME": "foo", "VELA_REPO_LINK": "foo", "VELA_REPO_NAME": "foo", "VELA_REPO_ORG": "foo", "VELA_REPO_PIPELINE_TYPE": "", "VELA_REPO_PRIVATE": "false", "VELA_REPO_TIMEOUT": "1", "VELA_REPO_TOPICS": "cloud,security", "VELA_REPO_TRUSTED": "false", "VELA_REPO_VISIBILITY": "foo", "VELA_RUNTIME": "TODO", "VELA_SOURCE": "foo", "VELA_USER_ACTIVE": "false", "VELA_USER_ADMIN": "false", "VELA_USER_FAVORITES": "[]", "VELA_USER_NAME": "foo", "VELA_VERSION": "TODO", "VELA_WORKSPACE": "/vela/src/foo/foo/foo"}, }, } for _, tt := range tests { diff --git a/compiler/native/expand_test.go b/compiler/native/expand_test.go index 6d9479a53..8400bd6a4 100644 --- a/compiler/native/expand_test.go +++ b/compiler/native/expand_test.go @@ -9,6 +9,7 @@ import ( "reflect" "testing" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/library" "github.com/go-vela/types/pipeline" "github.com/go-vela/types/raw" @@ -228,7 +229,7 @@ func TestNative_ExpandSteps(t *testing.T) { set.Int("max-template-depth", 5, "doc") c := cli.NewContext(nil, set, nil) - testRepo := new(library.Repo) + testRepo := new(api.Repo) testRepo.SetID(1) testRepo.SetOrg("foo") @@ -767,7 +768,7 @@ func TestNative_ExpandSteps_TemplateCallTemplate(t *testing.T) { testBuild.SetID(1) testBuild.SetCommit("123abc456def") - testRepo := new(library.Repo) + testRepo := new(api.Repo) testRepo.SetID(1) testRepo.SetOrg("foo") @@ -935,7 +936,7 @@ func TestNative_ExpandSteps_TemplateCallTemplate_CircularFail(t *testing.T) { testBuild.SetID(1) testBuild.SetCommit("123abc456def") - testRepo := new(library.Repo) + testRepo := new(api.Repo) testRepo.SetID(1) testRepo.SetOrg("foo") @@ -1021,7 +1022,7 @@ func TestNative_ExpandSteps_CallTemplateWithRenderInline(t *testing.T) { testBuild.SetID(1) testBuild.SetCommit("123abc456def") - testRepo := new(library.Repo) + testRepo := new(api.Repo) testRepo.SetID(1) testRepo.SetOrg("foo") diff --git a/compiler/native/native.go b/compiler/native/native.go index 048f22307..04db6b72c 100644 --- a/compiler/native/native.go +++ b/compiler/native/native.go @@ -5,12 +5,13 @@ package native import ( "time" + api "github.com/go-vela/server/api/types" "github.com/go-vela/server/compiler" + "github.com/go-vela/server/internal" "github.com/go-vela/server/compiler/registry" "github.com/go-vela/server/compiler/registry/github" - "github.com/go-vela/types" "github.com/go-vela/types/library" "github.com/sirupsen/logrus" @@ -39,8 +40,8 @@ type client struct { files []string local bool localTemplates []string - metadata *types.Metadata - repo *library.Repo + metadata *internal.Metadata + repo *api.Repo user *library.User labels []string } @@ -175,7 +176,7 @@ func (c *client) WithLocalTemplates(templates []string) compiler.Engine { } // WithMetadata sets the compiler metadata type in the Engine. -func (c *client) WithMetadata(m *types.Metadata) compiler.Engine { +func (c *client) WithMetadata(m *internal.Metadata) compiler.Engine { if m != nil { c.metadata = m } @@ -195,7 +196,7 @@ func (c *client) WithPrivateGitHub(url, token string) compiler.Engine { } // WithRepo sets the library repo type in the Engine. -func (c *client) WithRepo(r *library.Repo) compiler.Engine { +func (c *client) WithRepo(r *api.Repo) compiler.Engine { if r != nil { c.repo = r } diff --git a/compiler/native/native_test.go b/compiler/native/native_test.go index 489288ee7..db0859c02 100644 --- a/compiler/native/native_test.go +++ b/compiler/native/native_test.go @@ -7,9 +7,10 @@ import ( "reflect" "testing" + api "github.com/go-vela/server/api/types" "github.com/go-vela/server/compiler/registry/github" + "github.com/go-vela/server/internal" - "github.com/go-vela/types" "github.com/go-vela/types/library" "github.com/urfave/cli/v2" @@ -225,21 +226,21 @@ func TestNative_WithMetadata(t *testing.T) { set := flag.NewFlagSet("test", 0) c := cli.NewContext(nil, set, nil) - m := &types.Metadata{ - Database: &types.Database{ + m := &internal.Metadata{ + Database: &internal.Database{ Driver: "foo", Host: "foo", }, - Queue: &types.Queue{ + Queue: &internal.Queue{ Channel: "foo", Driver: "foo", Host: "foo", }, - Source: &types.Source{ + Source: &internal.Source{ Driver: "foo", Host: "foo", }, - Vela: &types.Vela{ + Vela: &internal.Vela{ Address: "foo", WebAddress: "foo", }, @@ -291,7 +292,7 @@ func TestNative_WithRepo(t *testing.T) { c := cli.NewContext(nil, set, nil) id := int64(1) - r := &library.Repo{ID: &id} + r := &api.Repo{ID: &id} want, _ := New(c) want.repo = r diff --git a/compiler/native/parse_test.go b/compiler/native/parse_test.go index 7176438e1..a24d3bf34 100644 --- a/compiler/native/parse_test.go +++ b/compiler/native/parse_test.go @@ -10,8 +10,8 @@ import ( "reflect" "testing" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" - "github.com/go-vela/types/library" "github.com/go-vela/types/raw" "github.com/go-vela/types/yaml" @@ -858,7 +858,7 @@ func TestNative_ParseString_Metadata(t *testing.T) { type FailReader struct{} -func (FailReader) Read(p []byte) (n int, err error) { +func (FailReader) Read(_ []byte) (n int, err error) { return 0, errors.New("this is a reader that fails when you try to read") } @@ -910,11 +910,13 @@ func Test_client_Parse(t *testing.T) { } var c *client - if tt.args.pipelineType == "nil" { + + pipelineType := tt.args.pipelineType + if pipelineType == "nil" { c = &client{} } else { c = &client{ - repo: &library.Repo{PipelineType: &tt.args.pipelineType}, + repo: &api.Repo{PipelineType: &pipelineType}, } } diff --git a/compiler/native/transform_test.go b/compiler/native/transform_test.go index 743f9a46c..82ec72d3d 100644 --- a/compiler/native/transform_test.go +++ b/compiler/native/transform_test.go @@ -7,7 +7,7 @@ import ( "reflect" "testing" - "github.com/go-vela/types" + "github.com/go-vela/server/internal" "github.com/go-vela/types/pipeline" "github.com/go-vela/types/yaml" @@ -19,21 +19,21 @@ func TestNative_TransformStages(t *testing.T) { set := flag.NewFlagSet("test", 0) c := cli.NewContext(nil, set, nil) - m := &types.Metadata{ - Database: &types.Database{ + m := &internal.Metadata{ + Database: &internal.Database{ Driver: "foo", Host: "foo", }, - Queue: &types.Queue{ + Queue: &internal.Queue{ Channel: "foo", Driver: "foo", Host: "foo", }, - Source: &types.Source{ + Source: &internal.Source{ Driver: "foo", Host: "foo", }, - Vela: &types.Vela{ + Vela: &internal.Vela{ Address: "foo", WebAddress: "foo", }, @@ -259,21 +259,21 @@ func TestNative_TransformSteps(t *testing.T) { set := flag.NewFlagSet("test", 0) c := cli.NewContext(nil, set, nil) - m := &types.Metadata{ - Database: &types.Database{ + m := &internal.Metadata{ + Database: &internal.Database{ Driver: "foo", Host: "foo", }, - Queue: &types.Queue{ + Queue: &internal.Queue{ Channel: "foo", Driver: "foo", Host: "foo", }, - Source: &types.Source{ + Source: &internal.Source{ Driver: "foo", Host: "foo", }, - Vela: &types.Vela{ + Vela: &internal.Vela{ Address: "foo", WebAddress: "foo", }, diff --git a/database/build/build_test.go b/database/build/build_test.go index 16910e5ac..419e9443c 100644 --- a/database/build/build_test.go +++ b/database/build/build_test.go @@ -10,6 +10,7 @@ import ( "time" "github.com/DATA-DOG/go-sqlmock" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/library" "github.com/go-vela/types/raw" "github.com/sirupsen/logrus" @@ -241,10 +242,9 @@ func testDeployment() *library.Deployment { // testRepo is a test helper function to create a library // Repo type with all fields set to their zero values. -func testRepo() *library.Repo { - return &library.Repo{ +func testRepo() *api.Repo { + return &api.Repo{ ID: new(int64), - UserID: new(int64), BuildLimit: new(int64), Timeout: new(int64), Counter: new(int), diff --git a/database/build/count_org_test.go b/database/build/count_org_test.go index 6b6283dbb..968b1d7c2 100644 --- a/database/build/count_org_test.go +++ b/database/build/count_org_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/DATA-DOG/go-sqlmock" + "github.com/go-vela/server/database/repo" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" ) @@ -30,7 +31,7 @@ func TestBuild_Engine_CountBuildsForOrg(t *testing.T) { _repoOne := testRepo() _repoOne.SetID(1) - _repoOne.SetUserID(1) + _repoOne.GetOwner().SetID(1) _repoOne.SetHash("baz") _repoOne.SetOrg("foo") _repoOne.SetName("bar") @@ -41,7 +42,7 @@ func TestBuild_Engine_CountBuildsForOrg(t *testing.T) { _repoTwo := testRepo() _repoTwo.SetID(2) - _repoTwo.SetUserID(1) + _repoTwo.GetOwner().SetID(1) _repoTwo.SetHash("bar") _repoTwo.SetOrg("foo") _repoTwo.SetName("baz") @@ -81,12 +82,12 @@ func TestBuild_Engine_CountBuildsForOrg(t *testing.T) { t.Errorf("unable to create repo table for sqlite: %v", err) } - err = _sqlite.client.Table(constants.TableRepo).Create(database.RepoFromLibrary(_repoOne)).Error + err = _sqlite.client.Table(constants.TableRepo).Create(repo.FromAPI(_repoOne)).Error if err != nil { t.Errorf("unable to create test repo for sqlite: %v", err) } - err = _sqlite.client.Table(constants.TableRepo).Create(database.RepoFromLibrary(_repoTwo)).Error + err = _sqlite.client.Table(constants.TableRepo).Create(repo.FromAPI(_repoTwo)).Error if err != nil { t.Errorf("unable to create test repo for sqlite: %v", err) } diff --git a/database/build/count_repo.go b/database/build/count_repo.go index 152d867a2..342e49491 100644 --- a/database/build/count_repo.go +++ b/database/build/count_repo.go @@ -5,13 +5,13 @@ package build import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" - "github.com/go-vela/types/library" "github.com/sirupsen/logrus" ) // CountBuildsForRepo gets the count of builds by repo ID from the database. -func (e *engine) CountBuildsForRepo(ctx context.Context, r *library.Repo, filters map[string]interface{}) (int64, error) { +func (e *engine) CountBuildsForRepo(ctx context.Context, r *api.Repo, filters map[string]interface{}) (int64, error) { e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), diff --git a/database/build/count_repo_test.go b/database/build/count_repo_test.go index ebf0dbe47..edbb77816 100644 --- a/database/build/count_repo_test.go +++ b/database/build/count_repo_test.go @@ -26,7 +26,7 @@ func TestBuild_Engine_CountBuildsForRepo(t *testing.T) { _repo := testRepo() _repo.SetID(1) - _repo.SetUserID(1) + _repo.GetOwner().SetID(1) _repo.SetHash("baz") _repo.SetOrg("foo") _repo.SetName("bar") diff --git a/database/build/get_repo.go b/database/build/get_repo.go index 3728b6232..17dfdcb41 100644 --- a/database/build/get_repo.go +++ b/database/build/get_repo.go @@ -5,6 +5,7 @@ package build import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" @@ -12,7 +13,7 @@ import ( ) // GetBuildForRepo gets a build by repo ID and number from the database. -func (e *engine) GetBuildForRepo(ctx context.Context, r *library.Repo, number int) (*library.Build, error) { +func (e *engine) GetBuildForRepo(ctx context.Context, r *api.Repo, number int) (*library.Build, error) { e.logger.WithFields(logrus.Fields{ "build": number, "org": r.GetOrg(), diff --git a/database/build/get_repo_test.go b/database/build/get_repo_test.go index c34fa8a2e..ca714d017 100644 --- a/database/build/get_repo_test.go +++ b/database/build/get_repo_test.go @@ -22,7 +22,7 @@ func TestBuild_Engine_GetBuildForRepo(t *testing.T) { _repo := testRepo() _repo.SetID(1) - _repo.SetUserID(1) + _repo.GetOwner().SetID(1) _repo.SetHash("baz") _repo.SetOrg("foo") _repo.SetName("bar") diff --git a/database/build/interface.go b/database/build/interface.go index e28d230b4..18c85d194 100644 --- a/database/build/interface.go +++ b/database/build/interface.go @@ -5,6 +5,7 @@ package build import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/library" ) @@ -35,7 +36,7 @@ type BuildInterface interface { // CountBuildsForOrg defines a function that gets the count of builds by org name. CountBuildsForOrg(context.Context, string, map[string]interface{}) (int64, error) // CountBuildsForRepo defines a function that gets the count of builds by repo ID. - CountBuildsForRepo(context.Context, *library.Repo, map[string]interface{}) (int64, error) + CountBuildsForRepo(context.Context, *api.Repo, map[string]interface{}) (int64, error) // CountBuildsForStatus defines a function that gets the count of builds by status. CountBuildsForStatus(context.Context, string, map[string]interface{}) (int64, error) // CreateBuild defines a function that creates a new build. @@ -45,19 +46,19 @@ type BuildInterface interface { // GetBuild defines a function that gets a build by ID. GetBuild(context.Context, int64) (*library.Build, error) // GetBuildForRepo defines a function that gets a build by repo ID and number. - GetBuildForRepo(context.Context, *library.Repo, int) (*library.Build, error) + GetBuildForRepo(context.Context, *api.Repo, int) (*library.Build, error) // LastBuildForRepo defines a function that gets the last build ran by repo ID and branch. - LastBuildForRepo(context.Context, *library.Repo, string) (*library.Build, error) + LastBuildForRepo(context.Context, *api.Repo, string) (*library.Build, error) // ListBuilds defines a function that gets a list of all builds. ListBuilds(context.Context) ([]*library.Build, error) // ListBuildsForOrg defines a function that gets a list of builds by org name. ListBuildsForOrg(context.Context, string, map[string]interface{}, int, int) ([]*library.Build, int64, error) // ListBuildsForRepo defines a function that gets a list of builds by repo ID. - ListBuildsForRepo(context.Context, *library.Repo, map[string]interface{}, int64, int64, int, int) ([]*library.Build, int64, error) + ListBuildsForRepo(context.Context, *api.Repo, map[string]interface{}, int64, int64, int, int) ([]*library.Build, int64, error) // ListPendingAndRunningBuilds defines a function that gets a list of pending and running builds. ListPendingAndRunningBuilds(context.Context, string) ([]*library.BuildQueue, error) // ListPendingAndRunningBuildsForRepo defines a function that gets a list of pending and running builds for a repo. - ListPendingAndRunningBuildsForRepo(context.Context, *library.Repo) ([]*library.Build, error) + ListPendingAndRunningBuildsForRepo(context.Context, *api.Repo) ([]*library.Build, error) // UpdateBuild defines a function that updates an existing build. UpdateBuild(context.Context, *library.Build) (*library.Build, error) } diff --git a/database/build/last_repo.go b/database/build/last_repo.go index 36b923379..d92e3eea5 100644 --- a/database/build/last_repo.go +++ b/database/build/last_repo.go @@ -6,6 +6,7 @@ import ( "context" "errors" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" @@ -15,7 +16,7 @@ import ( ) // LastBuildForRepo gets the last build by repo ID and branch from the database. -func (e *engine) LastBuildForRepo(ctx context.Context, r *library.Repo, branch string) (*library.Build, error) { +func (e *engine) LastBuildForRepo(ctx context.Context, r *api.Repo, branch string) (*library.Build, error) { e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), diff --git a/database/build/last_repo_test.go b/database/build/last_repo_test.go index 7baebe01d..5e3d9d216 100644 --- a/database/build/last_repo_test.go +++ b/database/build/last_repo_test.go @@ -22,7 +22,7 @@ func TestBuild_Engine_LastBuildForRepo(t *testing.T) { _repo := testRepo() _repo.SetID(1) - _repo.SetUserID(1) + _repo.GetOwner().SetID(1) _repo.SetHash("baz") _repo.SetOrg("foo") _repo.SetName("bar") diff --git a/database/build/list_org_test.go b/database/build/list_org_test.go index 718517d3d..6b8df4c19 100644 --- a/database/build/list_org_test.go +++ b/database/build/list_org_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/DATA-DOG/go-sqlmock" + "github.com/go-vela/server/database/repo" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" @@ -31,7 +32,7 @@ func TestBuild_Engine_ListBuildsForOrg(t *testing.T) { _repoOne := testRepo() _repoOne.SetID(1) - _repoOne.SetUserID(1) + _repoOne.GetOwner().SetID(1) _repoOne.SetHash("baz") _repoOne.SetOrg("foo") _repoOne.SetName("bar") @@ -42,7 +43,7 @@ func TestBuild_Engine_ListBuildsForOrg(t *testing.T) { _repoTwo := testRepo() _repoTwo.SetID(2) - _repoTwo.SetUserID(1) + _repoTwo.GetOwner().SetID(1) _repoTwo.SetHash("bar") _repoTwo.SetOrg("foo") _repoTwo.SetName("baz") @@ -108,12 +109,12 @@ func TestBuild_Engine_ListBuildsForOrg(t *testing.T) { t.Errorf("unable to create repo table for sqlite: %v", err) } - err = _sqlite.client.Table(constants.TableRepo).Create(database.RepoFromLibrary(_repoOne)).Error + err = _sqlite.client.Table(constants.TableRepo).Create(repo.FromAPI(_repoOne)).Error if err != nil { t.Errorf("unable to create test repo for sqlite: %v", err) } - err = _sqlite.client.Table(constants.TableRepo).Create(database.RepoFromLibrary(_repoTwo)).Error + err = _sqlite.client.Table(constants.TableRepo).Create(repo.FromAPI(_repoTwo)).Error if err != nil { t.Errorf("unable to create test repo for sqlite: %v", err) } diff --git a/database/build/list_pending_running_repo.go b/database/build/list_pending_running_repo.go index ad0963aee..119b78625 100644 --- a/database/build/list_pending_running_repo.go +++ b/database/build/list_pending_running_repo.go @@ -5,13 +5,14 @@ package build import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" ) // ListPendingAndRunningBuilds gets a list of all pending and running builds in the provided timeframe from the database. -func (e *engine) ListPendingAndRunningBuildsForRepo(ctx context.Context, repo *library.Repo) ([]*library.Build, error) { +func (e *engine) ListPendingAndRunningBuildsForRepo(ctx context.Context, repo *api.Repo) ([]*library.Build, error) { e.logger.Trace("listing all pending and running builds from the database") // variables to store query results and return value diff --git a/database/build/list_pending_running_repo_test.go b/database/build/list_pending_running_repo_test.go index 34993713b..d6b2d5a4f 100644 --- a/database/build/list_pending_running_repo_test.go +++ b/database/build/list_pending_running_repo_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/DATA-DOG/go-sqlmock" + "github.com/go-vela/server/database/repo" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" @@ -41,7 +42,7 @@ func TestBuild_Engine_ListPendingAndRunningBuildsForRepo(t *testing.T) { _repo := testRepo() _repo.SetID(1) - _repo.SetUserID(1) + _repo.GetOwner().SetID(1) _repo.SetHash("baz") _repo.SetOrg("foo") _repo.SetName("bar") @@ -50,7 +51,7 @@ func TestBuild_Engine_ListPendingAndRunningBuildsForRepo(t *testing.T) { _repoTwo := testRepo() _repoTwo.SetID(2) - _repoTwo.SetUserID(1) + _repoTwo.GetOwner().SetID(1) _repoTwo.SetHash("bazzy") _repoTwo.SetOrg("foo") _repoTwo.SetName("baz") @@ -92,12 +93,12 @@ func TestBuild_Engine_ListPendingAndRunningBuildsForRepo(t *testing.T) { t.Errorf("unable to create repo table for sqlite: %v", err) } - err = _sqlite.client.Table(constants.TableRepo).Create(database.RepoFromLibrary(_repo)).Error + err = _sqlite.client.Table(constants.TableRepo).Create(repo.FromAPI(_repo)).Error if err != nil { t.Errorf("unable to create test repo for sqlite: %v", err) } - err = _sqlite.client.Table(constants.TableRepo).Create(database.RepoFromLibrary(_repoTwo)).Error + err = _sqlite.client.Table(constants.TableRepo).Create(repo.FromAPI(_repoTwo)).Error if err != nil { t.Errorf("unable to create test repo for sqlite: %v", err) } diff --git a/database/build/list_pending_running_test.go b/database/build/list_pending_running_test.go index 9115745f3..b7144a815 100644 --- a/database/build/list_pending_running_test.go +++ b/database/build/list_pending_running_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/DATA-DOG/go-sqlmock" + "github.com/go-vela/server/database/repo" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" @@ -45,7 +46,7 @@ func TestBuild_Engine_ListPendingAndRunningBuilds(t *testing.T) { _repo := testRepo() _repo.SetID(1) - _repo.SetUserID(1) + _repo.GetOwner().SetID(1) _repo.SetHash("baz") _repo.SetOrg("foo") _repo.SetName("bar") @@ -79,7 +80,7 @@ func TestBuild_Engine_ListPendingAndRunningBuilds(t *testing.T) { t.Errorf("unable to create repo table for sqlite: %v", err) } - err = _sqlite.client.Table(constants.TableRepo).Create(database.RepoFromLibrary(_repo)).Error + err = _sqlite.client.Table(constants.TableRepo).Create(repo.FromAPI(_repo)).Error if err != nil { t.Errorf("unable to create test repo for sqlite: %v", err) } diff --git a/database/build/list_repo.go b/database/build/list_repo.go index 8b4386457..f8d7a0c00 100644 --- a/database/build/list_repo.go +++ b/database/build/list_repo.go @@ -5,6 +5,7 @@ package build import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" @@ -14,7 +15,7 @@ import ( // ListBuildsForRepo gets a list of builds by repo ID from the database. // //nolint:lll // ignore long line length due to variable names -func (e *engine) ListBuildsForRepo(ctx context.Context, r *library.Repo, filters map[string]interface{}, before, after int64, page, perPage int) ([]*library.Build, int64, error) { +func (e *engine) ListBuildsForRepo(ctx context.Context, r *api.Repo, filters map[string]interface{}, before, after int64, page, perPage int) ([]*library.Build, int64, error) { e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), diff --git a/database/build/list_repo_test.go b/database/build/list_repo_test.go index 4cb4622a5..90b54988a 100644 --- a/database/build/list_repo_test.go +++ b/database/build/list_repo_test.go @@ -30,7 +30,7 @@ func TestBuild_Engine_ListBuildsForRepo(t *testing.T) { _repo := testRepo() _repo.SetID(1) - _repo.SetUserID(1) + _repo.GetOwner().SetID(1) _repo.SetHash("baz") _repo.SetOrg("foo") _repo.SetName("bar") diff --git a/database/deployment/count_repo.go b/database/deployment/count_repo.go index 93a7c1ac8..c9eef1aad 100644 --- a/database/deployment/count_repo.go +++ b/database/deployment/count_repo.go @@ -5,13 +5,13 @@ package deployment import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" - "github.com/go-vela/types/library" "github.com/sirupsen/logrus" ) // CountDeploymentsForRepo gets the count of deployments by repo ID from the database. -func (e *engine) CountDeploymentsForRepo(ctx context.Context, r *library.Repo) (int64, error) { +func (e *engine) CountDeploymentsForRepo(ctx context.Context, r *api.Repo) (int64, error) { e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), diff --git a/database/deployment/count_repo_test.go b/database/deployment/count_repo_test.go index 6d3afece4..586ca6ffb 100644 --- a/database/deployment/count_repo_test.go +++ b/database/deployment/count_repo_test.go @@ -47,7 +47,7 @@ func TestDeployment_Engine_CountDeploymentsForRepo(t *testing.T) { _repo := testRepo() _repo.SetID(1) - _repo.SetUserID(1) + _repo.GetOwner().SetID(1) _repo.SetOrg("foo") _repo.SetName("bar") _repo.SetFullName("foo/bar") diff --git a/database/deployment/deployment_test.go b/database/deployment/deployment_test.go index 0184fbcce..60265211d 100644 --- a/database/deployment/deployment_test.go +++ b/database/deployment/deployment_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/DATA-DOG/go-sqlmock" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/library" "github.com/go-vela/types/raw" "github.com/sirupsen/logrus" @@ -188,10 +189,9 @@ func testDeployment() *library.Deployment { // testRepo is a test helper function to create a library // Repo type with all fields set to their zero values. -func testRepo() *library.Repo { - return &library.Repo{ +func testRepo() *api.Repo { + return &api.Repo{ ID: new(int64), - UserID: new(int64), BuildLimit: new(int64), Timeout: new(int64), Counter: new(int), diff --git a/database/deployment/get_repo.go b/database/deployment/get_repo.go index b240adc20..a359bbcb7 100644 --- a/database/deployment/get_repo.go +++ b/database/deployment/get_repo.go @@ -6,6 +6,7 @@ import ( "context" "strconv" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" @@ -13,7 +14,7 @@ import ( ) // GetDeploymentForRepo gets a deployment by repo ID and number from the database. -func (e *engine) GetDeploymentForRepo(ctx context.Context, r *library.Repo, number int64) (*library.Deployment, error) { +func (e *engine) GetDeploymentForRepo(ctx context.Context, r *api.Repo, number int64) (*library.Deployment, error) { e.logger.WithFields(logrus.Fields{ "deployment": number, "org": r.GetOrg(), diff --git a/database/deployment/get_repo_test.go b/database/deployment/get_repo_test.go index 5ddbb61d5..289b3ad87 100644 --- a/database/deployment/get_repo_test.go +++ b/database/deployment/get_repo_test.go @@ -32,7 +32,7 @@ func TestDeployment_Engine_GetDeploymentForRepo(t *testing.T) { _repo := testRepo() _repo.SetID(1) - _repo.SetUserID(1) + _repo.GetOwner().SetID(1) _repo.SetOrg("foo") _repo.SetName("bar") _repo.SetFullName("foo/bar") diff --git a/database/deployment/interface.go b/database/deployment/interface.go index 837aa2121..cd1f6cebc 100644 --- a/database/deployment/interface.go +++ b/database/deployment/interface.go @@ -5,6 +5,7 @@ package deployment import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/library" ) @@ -29,7 +30,7 @@ type DeploymentInterface interface { // CountDeployments defines a function that gets the count of all deployments. CountDeployments(context.Context) (int64, error) // CountDeploymentsForRepo defines a function that gets the count of deployments by repo ID. - CountDeploymentsForRepo(context.Context, *library.Repo) (int64, error) + CountDeploymentsForRepo(context.Context, *api.Repo) (int64, error) // CreateDeployment defines a function that creates a new deployment. CreateDeployment(context.Context, *library.Deployment) (*library.Deployment, error) // DeleteDeployment defines a function that deletes an existing deployment. @@ -37,11 +38,11 @@ type DeploymentInterface interface { // GetDeployment defines a function that gets a deployment by ID. GetDeployment(context.Context, int64) (*library.Deployment, error) // GetDeploymentForRepo defines a function that gets a deployment by repo ID and number. - GetDeploymentForRepo(context.Context, *library.Repo, int64) (*library.Deployment, error) + GetDeploymentForRepo(context.Context, *api.Repo, int64) (*library.Deployment, error) // ListDeployments defines a function that gets a list of all deployments. ListDeployments(context.Context) ([]*library.Deployment, error) // ListDeploymentsForRepo defines a function that gets a list of deployments by repo ID. - ListDeploymentsForRepo(context.Context, *library.Repo, int, int) ([]*library.Deployment, error) + ListDeploymentsForRepo(context.Context, *api.Repo, int, int) ([]*library.Deployment, error) // UpdateDeployment defines a function that updates an existing deployment. UpdateDeployment(context.Context, *library.Deployment) (*library.Deployment, error) } diff --git a/database/deployment/list_repo.go b/database/deployment/list_repo.go index 471da6cf0..76c973e6d 100644 --- a/database/deployment/list_repo.go +++ b/database/deployment/list_repo.go @@ -6,6 +6,7 @@ import ( "context" "strconv" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" @@ -13,7 +14,7 @@ import ( ) // ListDeploymentsForRepo gets a list of deployments by repo ID from the database. -func (e *engine) ListDeploymentsForRepo(ctx context.Context, r *library.Repo, page, perPage int) ([]*library.Deployment, error) { +func (e *engine) ListDeploymentsForRepo(ctx context.Context, r *api.Repo, page, perPage int) ([]*library.Deployment, error) { e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), diff --git a/database/hook/count_repo.go b/database/hook/count_repo.go index 7b536804f..28549dec9 100644 --- a/database/hook/count_repo.go +++ b/database/hook/count_repo.go @@ -5,13 +5,13 @@ package hook import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" - "github.com/go-vela/types/library" "github.com/sirupsen/logrus" ) // CountHooksForRepo gets the count of hooks by repo ID from the database. -func (e *engine) CountHooksForRepo(ctx context.Context, r *library.Repo) (int64, error) { +func (e *engine) CountHooksForRepo(ctx context.Context, r *api.Repo) (int64, error) { e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), diff --git a/database/hook/count_repo_test.go b/database/hook/count_repo_test.go index 87f6c97bb..cd1592f4e 100644 --- a/database/hook/count_repo_test.go +++ b/database/hook/count_repo_test.go @@ -30,7 +30,7 @@ func TestHook_Engine_CountHooksForRepo(t *testing.T) { _repo := testRepo() _repo.SetID(1) - _repo.SetUserID(1) + _repo.GetOwner().SetID(1) _repo.SetOrg("foo") _repo.SetName("bar") _repo.SetFullName("foo/bar") diff --git a/database/hook/get_repo.go b/database/hook/get_repo.go index 6182d86c9..af5d2b9bc 100644 --- a/database/hook/get_repo.go +++ b/database/hook/get_repo.go @@ -5,6 +5,7 @@ package hook import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" @@ -12,7 +13,7 @@ import ( ) // GetHookForRepo gets a hook by repo ID and number from the database. -func (e *engine) GetHookForRepo(ctx context.Context, r *library.Repo, number int) (*library.Hook, error) { +func (e *engine) GetHookForRepo(ctx context.Context, r *api.Repo, number int) (*library.Hook, error) { e.logger.WithFields(logrus.Fields{ "hook": number, "org": r.GetOrg(), diff --git a/database/hook/get_repo_test.go b/database/hook/get_repo_test.go index e7c33c2d8..f52eb1bd4 100644 --- a/database/hook/get_repo_test.go +++ b/database/hook/get_repo_test.go @@ -23,7 +23,7 @@ func TestHook_Engine_GetHookForRepo(t *testing.T) { _repo := testRepo() _repo.SetID(1) - _repo.SetUserID(1) + _repo.GetOwner().SetID(1) _repo.SetOrg("foo") _repo.SetName("bar") _repo.SetFullName("foo/bar") diff --git a/database/hook/hook_test.go b/database/hook/hook_test.go index fd358715f..50b689c00 100644 --- a/database/hook/hook_test.go +++ b/database/hook/hook_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/DATA-DOG/go-sqlmock" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/library" "github.com/sirupsen/logrus" @@ -187,10 +188,9 @@ func testHook() *library.Hook { // testRepo is a test helper function to create a library // Repo type with all fields set to their zero values. -func testRepo() *library.Repo { - return &library.Repo{ +func testRepo() *api.Repo { + return &api.Repo{ ID: new(int64), - UserID: new(int64), BuildLimit: new(int64), Timeout: new(int64), Counter: new(int), diff --git a/database/hook/interface.go b/database/hook/interface.go index 6a9cec8f6..674dca20c 100644 --- a/database/hook/interface.go +++ b/database/hook/interface.go @@ -5,6 +5,7 @@ package hook import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/library" ) @@ -29,7 +30,7 @@ type HookInterface interface { // CountHooks defines a function that gets the count of all hooks. CountHooks(context.Context) (int64, error) // CountHooksForRepo defines a function that gets the count of hooks by repo ID. - CountHooksForRepo(context.Context, *library.Repo) (int64, error) + CountHooksForRepo(context.Context, *api.Repo) (int64, error) // CreateHook defines a function that creates a new hook. CreateHook(context.Context, *library.Hook) (*library.Hook, error) // DeleteHook defines a function that deletes an existing hook. @@ -39,13 +40,13 @@ type HookInterface interface { // GetHookByWebhookID defines a function that gets any hook with a matching webhook_id. GetHookByWebhookID(context.Context, int64) (*library.Hook, error) // GetHookForRepo defines a function that gets a hook by repo ID and number. - GetHookForRepo(context.Context, *library.Repo, int) (*library.Hook, error) + GetHookForRepo(context.Context, *api.Repo, int) (*library.Hook, error) // LastHookForRepo defines a function that gets the last hook by repo ID. - LastHookForRepo(context.Context, *library.Repo) (*library.Hook, error) + LastHookForRepo(context.Context, *api.Repo) (*library.Hook, error) // ListHooks defines a function that gets a list of all hooks. ListHooks(context.Context) ([]*library.Hook, error) // ListHooksForRepo defines a function that gets a list of hooks by repo ID. - ListHooksForRepo(context.Context, *library.Repo, int, int) ([]*library.Hook, int64, error) + ListHooksForRepo(context.Context, *api.Repo, int, int) ([]*library.Hook, int64, error) // UpdateHook defines a function that updates an existing hook. UpdateHook(context.Context, *library.Hook) (*library.Hook, error) } diff --git a/database/hook/last_repo.go b/database/hook/last_repo.go index bf85be4dc..76fb7cbf0 100644 --- a/database/hook/last_repo.go +++ b/database/hook/last_repo.go @@ -6,6 +6,7 @@ import ( "context" "errors" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" @@ -15,7 +16,7 @@ import ( ) // LastHookForRepo gets the last hook by repo ID from the database. -func (e *engine) LastHookForRepo(ctx context.Context, r *library.Repo) (*library.Hook, error) { +func (e *engine) LastHookForRepo(ctx context.Context, r *api.Repo) (*library.Hook, error) { e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), diff --git a/database/hook/last_repo_test.go b/database/hook/last_repo_test.go index b7911f91d..d7409cc8e 100644 --- a/database/hook/last_repo_test.go +++ b/database/hook/last_repo_test.go @@ -23,7 +23,7 @@ func TestHook_Engine_LastHookForRepo(t *testing.T) { _repo := testRepo() _repo.SetID(1) - _repo.SetUserID(1) + _repo.GetOwner().SetID(1) _repo.SetOrg("foo") _repo.SetName("bar") _repo.SetFullName("foo/bar") diff --git a/database/hook/list_repo.go b/database/hook/list_repo.go index 28220b1dd..3a2ec497f 100644 --- a/database/hook/list_repo.go +++ b/database/hook/list_repo.go @@ -5,6 +5,7 @@ package hook import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" @@ -12,7 +13,7 @@ import ( ) // ListHooksForRepo gets a list of hooks by repo ID from the database. -func (e *engine) ListHooksForRepo(ctx context.Context, r *library.Repo, page, perPage int) ([]*library.Hook, int64, error) { +func (e *engine) ListHooksForRepo(ctx context.Context, r *api.Repo, page, perPage int) ([]*library.Hook, int64, error) { e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), diff --git a/database/hook/list_repo_test.go b/database/hook/list_repo_test.go index 38e72a93f..b2967a1f0 100644 --- a/database/hook/list_repo_test.go +++ b/database/hook/list_repo_test.go @@ -31,7 +31,7 @@ func TestHook_Engine_ListHooksForRepo(t *testing.T) { _repo := testRepo() _repo.SetID(1) - _repo.SetUserID(1) + _repo.GetOwner().SetID(1) _repo.SetOrg("foo") _repo.SetName("bar") _repo.SetFullName("foo/bar") diff --git a/database/integration_test.go b/database/integration_test.go index 0bde55005..e1a0c1432 100644 --- a/database/integration_test.go +++ b/database/integration_test.go @@ -38,7 +38,7 @@ type Resources struct { Hooks []*library.Hook Logs []*library.Log Pipelines []*library.Pipeline - Repos []*library.Repo + Repos []*api.Repo Schedules []*library.Schedule Secrets []*library.Secret Services []*library.Service @@ -994,6 +994,14 @@ func testRepos(t *testing.T, db Interface, resources *Resources) { methods[element.Method(i).Name] = false } + // create owners + for _, user := range resources.Users { + _, err := db.CreateUser(context.TODO(), user) + if err != nil { + t.Errorf("unable to create user %d: %v", user.GetID(), err) + } + } + // create the repos for _, repo := range resources.Repos { _, err := db.CreateRepo(context.TODO(), repo) @@ -1110,6 +1118,14 @@ func testRepos(t *testing.T, db Interface, resources *Resources) { } methods["DeleteRepo"] = true + // delete the owners + for _, user := range resources.Users { + err := db.DeleteUser(context.TODO(), user) + if err != nil { + t.Errorf("unable to delete user %d: %v", user.GetID(), err) + } + } + // ensure we called all the methods we expected to for method, called := range methods { if !called { @@ -2221,9 +2237,29 @@ func newResources() *Resources { pipelineTwo.SetTemplates(false) pipelineTwo.SetData([]byte("version: 1")) - repoOne := new(library.Repo) + userOne := new(library.User) + userOne.SetID(1) + userOne.SetName("octocat") + userOne.SetToken("superSecretToken") + userOne.SetRefreshToken("superSecretRefreshToken") + userOne.SetHash("MzM4N2MzMDAtNmY4Mi00OTA5LWFhZDAtNWIzMTlkNTJkODMy") + userOne.SetFavorites([]string{"github/octocat"}) + userOne.SetActive(true) + userOne.SetAdmin(false) + + userTwo := new(library.User) + userTwo.SetID(2) + userTwo.SetName("octokitty") + userTwo.SetToken("superSecretToken") + userTwo.SetRefreshToken("superSecretRefreshToken") + userTwo.SetHash("MzM4N2MzMDAtNmY4Mi00OTA5LWFhZDAtNWIzMTlkNTJkODMy") + userTwo.SetFavorites([]string{"github/octocat"}) + userTwo.SetActive(true) + userTwo.SetAdmin(false) + + repoOne := new(api.Repo) repoOne.SetID(1) - repoOne.SetUserID(1) + repoOne.SetOwner(userOne) repoOne.SetHash("MzM4N2MzMDAtNmY4Mi00OTA5LWFhZDAtNWIzMTlkNTJkODMy") repoOne.SetOrg("github") repoOne.SetName("octocat") @@ -2242,11 +2278,11 @@ func newResources() *Resources { repoOne.SetPipelineType("") repoOne.SetPreviousName("") repoOne.SetApproveBuild(constants.ApproveNever) - repoOne.SetAllowEvents(library.NewEventsFromMask(1)) + repoOne.SetAllowEvents(api.NewEventsFromMask(1)) - repoTwo := new(library.Repo) + repoTwo := new(api.Repo) repoTwo.SetID(2) - repoTwo.SetUserID(1) + repoTwo.SetOwner(userOne) repoTwo.SetHash("MzM4N2MzMDAtNmY4Mi00OTA5LWFhZDAtNWIzMTlkNTJkODMy") repoTwo.SetOrg("github") repoTwo.SetName("octokitty") @@ -2265,7 +2301,7 @@ func newResources() *Resources { repoTwo.SetPipelineType("") repoTwo.SetPreviousName("") repoTwo.SetApproveBuild(constants.ApproveForkAlways) - repoTwo.SetAllowEvents(library.NewEventsFromMask(1)) + repoTwo.SetAllowEvents(api.NewEventsFromMask(1)) scheduleOne := new(library.Schedule) scheduleOne.SetID(1) @@ -2416,26 +2452,6 @@ func newResources() *Resources { stepTwo.SetDistribution("linux") stepTwo.SetReportAs("test") - userOne := new(library.User) - userOne.SetID(1) - userOne.SetName("octocat") - userOne.SetToken("superSecretToken") - userOne.SetRefreshToken("superSecretRefreshToken") - userOne.SetHash("MzM4N2MzMDAtNmY4Mi00OTA5LWFhZDAtNWIzMTlkNTJkODMy") - userOne.SetFavorites([]string{"github/octocat"}) - userOne.SetActive(true) - userOne.SetAdmin(false) - - userTwo := new(library.User) - userTwo.SetID(2) - userTwo.SetName("octokitty") - userTwo.SetToken("superSecretToken") - userTwo.SetRefreshToken("superSecretRefreshToken") - userTwo.SetHash("MzM4N2MzMDAtNmY4Mi00OTA5LWFhZDAtNWIzMTlkNTJkODMy") - userTwo.SetFavorites([]string{"github/octocat"}) - userTwo.SetActive(true) - userTwo.SetAdmin(false) - _bPartialOne := new(library.Build) _bPartialOne.SetID(1) @@ -2477,7 +2493,7 @@ func newResources() *Resources { Hooks: []*library.Hook{hookOne, hookTwo, hookThree}, Logs: []*library.Log{logServiceOne, logServiceTwo, logStepOne, logStepTwo}, Pipelines: []*library.Pipeline{pipelineOne, pipelineTwo}, - Repos: []*library.Repo{repoOne, repoTwo}, + Repos: []*api.Repo{repoOne, repoTwo}, Schedules: []*library.Schedule{scheduleOne, scheduleTwo}, Secrets: []*library.Secret{secretOrg, secretRepo, secretShared}, Services: []*library.Service{serviceOne, serviceTwo}, diff --git a/database/pipeline/count_repo.go b/database/pipeline/count_repo.go index 549a9ecc9..00cecda46 100644 --- a/database/pipeline/count_repo.go +++ b/database/pipeline/count_repo.go @@ -5,13 +5,13 @@ package pipeline import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" - "github.com/go-vela/types/library" "github.com/sirupsen/logrus" ) // CountPipelinesForRepo gets the count of pipelines by repo ID from the database. -func (e *engine) CountPipelinesForRepo(ctx context.Context, r *library.Repo) (int64, error) { +func (e *engine) CountPipelinesForRepo(ctx context.Context, r *api.Repo) (int64, error) { e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), diff --git a/database/pipeline/count_repo_test.go b/database/pipeline/count_repo_test.go index e40888f3c..d4776cd77 100644 --- a/database/pipeline/count_repo_test.go +++ b/database/pipeline/count_repo_test.go @@ -8,7 +8,7 @@ import ( "testing" "github.com/DATA-DOG/go-sqlmock" - "github.com/go-vela/types/library" + api "github.com/go-vela/server/api/types" ) func TestPipeline_Engine_CountPipelinesForRepo(t *testing.T) { @@ -75,7 +75,7 @@ func TestPipeline_Engine_CountPipelinesForRepo(t *testing.T) { // run tests for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := test.database.CountPipelinesForRepo(context.TODO(), &library.Repo{ID: _pipelineOne.RepoID}) + got, err := test.database.CountPipelinesForRepo(context.TODO(), &api.Repo{ID: _pipelineOne.RepoID}) if test.failure { if err == nil { diff --git a/database/pipeline/get_repo.go b/database/pipeline/get_repo.go index 934c0dcd2..b5581df56 100644 --- a/database/pipeline/get_repo.go +++ b/database/pipeline/get_repo.go @@ -5,6 +5,7 @@ package pipeline import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" @@ -12,7 +13,7 @@ import ( ) // GetPipelineForRepo gets a pipeline by number and repo ID from the database. -func (e *engine) GetPipelineForRepo(ctx context.Context, commit string, r *library.Repo) (*library.Pipeline, error) { +func (e *engine) GetPipelineForRepo(ctx context.Context, commit string, r *api.Repo) (*library.Pipeline, error) { e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "pipeline": commit, diff --git a/database/pipeline/get_repo_test.go b/database/pipeline/get_repo_test.go index 82b9669da..fd4a5dea8 100644 --- a/database/pipeline/get_repo_test.go +++ b/database/pipeline/get_repo_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/DATA-DOG/go-sqlmock" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/library" ) @@ -65,7 +66,7 @@ func TestPipeline_Engine_GetPipelineForRepo(t *testing.T) { // run tests for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, err := test.database.GetPipelineForRepo(context.TODO(), "48afb5bdc41ad69bf22588491333f7cf71135163", &library.Repo{ID: _pipeline.RepoID}) + got, err := test.database.GetPipelineForRepo(context.TODO(), "48afb5bdc41ad69bf22588491333f7cf71135163", &api.Repo{ID: _pipeline.RepoID}) if test.failure { if err == nil { diff --git a/database/pipeline/interface.go b/database/pipeline/interface.go index 602cfbb1d..64666952e 100644 --- a/database/pipeline/interface.go +++ b/database/pipeline/interface.go @@ -5,6 +5,7 @@ package pipeline import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/library" ) @@ -29,7 +30,7 @@ type PipelineInterface interface { // CountPipelines defines a function that gets the count of all pipelines. CountPipelines(context.Context) (int64, error) // CountPipelinesForRepo defines a function that gets the count of pipelines by repo ID. - CountPipelinesForRepo(context.Context, *library.Repo) (int64, error) + CountPipelinesForRepo(context.Context, *api.Repo) (int64, error) // CreatePipeline defines a function that creates a new pipeline. CreatePipeline(context.Context, *library.Pipeline) (*library.Pipeline, error) // DeletePipeline defines a function that deletes an existing pipeline. @@ -37,11 +38,11 @@ type PipelineInterface interface { // GetPipeline defines a function that gets a pipeline by ID. GetPipeline(context.Context, int64) (*library.Pipeline, error) // GetPipelineForRepo defines a function that gets a pipeline by commit SHA and repo ID. - GetPipelineForRepo(context.Context, string, *library.Repo) (*library.Pipeline, error) + GetPipelineForRepo(context.Context, string, *api.Repo) (*library.Pipeline, error) // ListPipelines defines a function that gets a list of all pipelines. ListPipelines(context.Context) ([]*library.Pipeline, error) // ListPipelinesForRepo defines a function that gets a list of pipelines by repo ID. - ListPipelinesForRepo(context.Context, *library.Repo, int, int) ([]*library.Pipeline, int64, error) + ListPipelinesForRepo(context.Context, *api.Repo, int, int) ([]*library.Pipeline, int64, error) // UpdatePipeline defines a function that updates an existing pipeline. UpdatePipeline(context.Context, *library.Pipeline) (*library.Pipeline, error) } diff --git a/database/pipeline/list_repo.go b/database/pipeline/list_repo.go index f4ed90fde..f4ccc6a3e 100644 --- a/database/pipeline/list_repo.go +++ b/database/pipeline/list_repo.go @@ -5,6 +5,7 @@ package pipeline import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" @@ -14,7 +15,7 @@ import ( // ListPipelinesForRepo gets a list of pipelines by repo ID from the database. // //nolint:lll // ignore long line length due to variable names -func (e *engine) ListPipelinesForRepo(ctx context.Context, r *library.Repo, page, perPage int) ([]*library.Pipeline, int64, error) { +func (e *engine) ListPipelinesForRepo(ctx context.Context, r *api.Repo, page, perPage int) ([]*library.Pipeline, int64, error) { e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), diff --git a/database/pipeline/list_repo_test.go b/database/pipeline/list_repo_test.go index aec274702..909605f19 100644 --- a/database/pipeline/list_repo_test.go +++ b/database/pipeline/list_repo_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/DATA-DOG/go-sqlmock" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/library" ) @@ -86,7 +87,7 @@ func TestPipeline_Engine_ListPipelinesForRepo(t *testing.T) { // run tests for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, _, err := test.database.ListPipelinesForRepo(context.TODO(), &library.Repo{ID: _pipelineOne.RepoID}, 1, 10) + got, _, err := test.database.ListPipelinesForRepo(context.TODO(), &api.Repo{ID: _pipelineOne.RepoID}, 1, 10) if test.failure { if err == nil { diff --git a/database/repo/count_org_test.go b/database/repo/count_org_test.go index c89299773..e77b39e28 100644 --- a/database/repo/count_org_test.go +++ b/database/repo/count_org_test.go @@ -14,7 +14,7 @@ func TestRepo_Engine_CountReposForOrg(t *testing.T) { // setup types _repoOne := testRepo() _repoOne.SetID(1) - _repoOne.SetUserID(1) + _repoOne.GetOwner().SetID(1) _repoOne.SetHash("baz") _repoOne.SetOrg("foo") _repoOne.SetName("bar") @@ -23,7 +23,7 @@ func TestRepo_Engine_CountReposForOrg(t *testing.T) { _repoTwo := testRepo() _repoTwo.SetID(2) - _repoTwo.SetUserID(1) + _repoTwo.GetOwner().SetID(1) _repoTwo.SetHash("baz") _repoTwo.SetOrg("bar") _repoTwo.SetName("foo") diff --git a/database/repo/count_test.go b/database/repo/count_test.go index c399411d6..522779f92 100644 --- a/database/repo/count_test.go +++ b/database/repo/count_test.go @@ -14,7 +14,7 @@ func TestRepo_Engine_CountRepos(t *testing.T) { // setup types _repoOne := testRepo() _repoOne.SetID(1) - _repoOne.SetUserID(1) + _repoOne.GetOwner().SetID(1) _repoOne.SetHash("baz") _repoOne.SetOrg("foo") _repoOne.SetName("bar") @@ -23,7 +23,7 @@ func TestRepo_Engine_CountRepos(t *testing.T) { _repoTwo := testRepo() _repoTwo.SetID(2) - _repoTwo.SetUserID(1) + _repoTwo.GetOwner().SetID(1) _repoTwo.SetHash("baz") _repoTwo.SetOrg("bar") _repoTwo.SetName("foo") diff --git a/database/repo/count_user_test.go b/database/repo/count_user_test.go index a2b580a52..f62b43ff6 100644 --- a/database/repo/count_user_test.go +++ b/database/repo/count_user_test.go @@ -15,7 +15,7 @@ func TestRepo_Engine_CountReposForUser(t *testing.T) { // setup types _repoOne := testRepo() _repoOne.SetID(1) - _repoOne.SetUserID(1) + _repoOne.GetOwner().SetID(1) _repoOne.SetHash("baz") _repoOne.SetOrg("foo") _repoOne.SetName("bar") @@ -24,7 +24,7 @@ func TestRepo_Engine_CountReposForUser(t *testing.T) { _repoTwo := testRepo() _repoTwo.SetID(2) - _repoTwo.SetUserID(1) + _repoTwo.GetOwner().SetID(1) _repoTwo.SetHash("baz") _repoTwo.SetOrg("bar") _repoTwo.SetName("foo") diff --git a/database/repo/create.go b/database/repo/create.go index 578b54fb6..f716bf518 100644 --- a/database/repo/create.go +++ b/database/repo/create.go @@ -7,35 +7,28 @@ import ( "context" "fmt" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" - "github.com/go-vela/types/database" - "github.com/go-vela/types/library" "github.com/sirupsen/logrus" ) // CreateRepo creates a new repo in the database. -func (e *engine) CreateRepo(ctx context.Context, r *library.Repo) (*library.Repo, error) { +func (e *engine) CreateRepo(ctx context.Context, r *api.Repo) (*api.Repo, error) { e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), }).Tracef("creating repo %s in the database", r.GetFullName()) // cast the library type to database type - // - // https://pkg.go.dev/github.com/go-vela/types/database#RepoFromLibrary - repo := database.RepoFromLibrary(r) + repo := FromAPI(r) // validate the necessary fields are populated - // - // https://pkg.go.dev/github.com/go-vela/types/database#Repo.Validate err := repo.Validate() if err != nil { return nil, err } // encrypt the fields for the repo - // - // https://pkg.go.dev/github.com/go-vela/types/database#Repo.Encrypt err = repo.Encrypt(e.config.EncryptionKey) if err != nil { return nil, fmt.Errorf("unable to encrypt repo %s: %w", r.GetFullName(), err) @@ -53,8 +46,12 @@ func (e *engine) CreateRepo(ctx context.Context, r *library.Repo) (*library.Repo // only log to preserve backwards compatibility e.logger.Errorf("unable to decrypt repo %d: %v", r.GetID(), err) - return repo.ToLibrary(), nil + return repo.ToAPI(), nil } - return repo.ToLibrary(), nil + // set owner to provided owner if creation successful + result := repo.ToAPI() + result.SetOwner(r.GetOwner()) + + return result, nil } diff --git a/database/repo/create_test.go b/database/repo/create_test.go index 1a523b563..ddd3d209d 100644 --- a/database/repo/create_test.go +++ b/database/repo/create_test.go @@ -14,7 +14,7 @@ func TestRepo_Engine_CreateRepo(t *testing.T) { // setup types _repo := testRepo() _repo.SetID(1) - _repo.SetUserID(1) + _repo.GetOwner().SetID(1) _repo.SetHash("baz") _repo.SetOrg("foo") _repo.SetName("bar") @@ -75,7 +75,7 @@ VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$ t.Errorf("CreateRepo for %s returned err: %v", test.name, err) } - if diff := cmp.Diff(got, _repo); diff != "" { + if diff := cmp.Diff(_repo, got); diff != "" { t.Errorf("CreateRepo mismatch (-want +got):\n%s", diff) } }) diff --git a/database/repo/delete.go b/database/repo/delete.go index 6432e4ce1..c31dd43e3 100644 --- a/database/repo/delete.go +++ b/database/repo/delete.go @@ -5,23 +5,20 @@ package repo import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" - "github.com/go-vela/types/database" - "github.com/go-vela/types/library" "github.com/sirupsen/logrus" ) // DeleteRepo deletes an existing repo from the database. -func (e *engine) DeleteRepo(ctx context.Context, r *library.Repo) error { +func (e *engine) DeleteRepo(ctx context.Context, r *api.Repo) error { e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), }).Tracef("deleting repo %s from the database", r.GetFullName()) // cast the library type to database type - // - // https://pkg.go.dev/github.com/go-vela/types/database#RepoFromLibrary - repo := database.RepoFromLibrary(r) + repo := FromAPI(r) // send query to the database return e.client. diff --git a/database/repo/delete_test.go b/database/repo/delete_test.go index 30ff1d84c..34038d5fc 100644 --- a/database/repo/delete_test.go +++ b/database/repo/delete_test.go @@ -13,7 +13,7 @@ func TestRepo_Engine_DeleteRepo(t *testing.T) { // setup types _repo := testRepo() _repo.SetID(1) - _repo.SetUserID(1) + _repo.GetOwner().SetID(1) _repo.SetHash("baz") _repo.SetOrg("foo") _repo.SetName("bar") diff --git a/database/repo/get.go b/database/repo/get.go index af9db8e1c..ee603b09d 100644 --- a/database/repo/get.go +++ b/database/repo/get.go @@ -5,21 +5,21 @@ package repo import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" - "github.com/go-vela/types/database" - "github.com/go-vela/types/library" ) // GetRepo gets a repo by ID from the database. -func (e *engine) GetRepo(ctx context.Context, id int64) (*library.Repo, error) { +func (e *engine) GetRepo(ctx context.Context, id int64) (*api.Repo, error) { e.logger.Tracef("getting repo %d from the database", id) // variable to store query results - r := new(database.Repo) + r := new(Repo) // send query to the database and store result in variable err := e.client. Table(constants.TableRepo). + Preload("Owner"). Where("id = ?", id). Take(r). Error @@ -42,11 +42,20 @@ func (e *engine) GetRepo(ctx context.Context, id int64) (*library.Repo, error) { // return the unencrypted repo // // https://pkg.go.dev/github.com/go-vela/types/database#Repo.ToLibrary - return r.ToLibrary(), nil + return r.ToAPI(), nil + } + + // decrypt owner fields + err = r.Owner.Decrypt(e.config.EncryptionKey) + if err != nil { + e.logger.Errorf("unable to decrypt repo owner %d: %v", id, err) + + // return the unencrypted repo + return r.ToAPI(), nil } // return the decrypted repo // // https://pkg.go.dev/github.com/go-vela/types/database#Repo.ToLibrary - return r.ToLibrary(), nil + return r.ToAPI(), nil } diff --git a/database/repo/get_org.go b/database/repo/get_org.go index 2a1d053ea..186624276 100644 --- a/database/repo/get_org.go +++ b/database/repo/get_org.go @@ -5,25 +5,25 @@ package repo import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" - "github.com/go-vela/types/database" - "github.com/go-vela/types/library" "github.com/sirupsen/logrus" ) // GetRepoForOrg gets a repo by org and repo name from the database. -func (e *engine) GetRepoForOrg(ctx context.Context, org, name string) (*library.Repo, error) { +func (e *engine) GetRepoForOrg(ctx context.Context, org, name string) (*api.Repo, error) { e.logger.WithFields(logrus.Fields{ "org": org, "repo": name, }).Tracef("getting repo %s/%s from the database", org, name) // variable to store query results - r := new(database.Repo) + r := new(Repo) // send query to the database and store result in variable err := e.client. Table(constants.TableRepo). + Preload("Owner"). Where("org = ?", org). Where("name = ?", name). Take(r). @@ -33,8 +33,6 @@ func (e *engine) GetRepoForOrg(ctx context.Context, org, name string) (*library. } // decrypt the fields for the repo - // - // https://pkg.go.dev/github.com/go-vela/types/database#Repo.Decrypt err = r.Decrypt(e.config.EncryptionKey) if err != nil { // TODO: remove backwards compatibility before 1.x.x release @@ -45,13 +43,18 @@ func (e *engine) GetRepoForOrg(ctx context.Context, org, name string) (*library. e.logger.Errorf("unable to decrypt repo %s/%s: %v", org, name, err) // return the unencrypted repo - // - // https://pkg.go.dev/github.com/go-vela/types/database#Repo.ToLibrary - return r.ToLibrary(), nil + return r.ToAPI(), nil + } + + // decrypt owner fields + err = r.Owner.Decrypt(e.config.EncryptionKey) + if err != nil { + e.logger.Errorf("unable to decrypt repo owner %s/%s: %v", org, name, err) + + // return the unencrypted repo + return r.ToAPI(), nil } // return the decrypted repo - // - // https://pkg.go.dev/github.com/go-vela/types/database#Repo.ToLibrary - return r.ToLibrary(), nil + return r.ToAPI(), nil } diff --git a/database/repo/get_org_test.go b/database/repo/get_org_test.go index db5b36314..9df07c42d 100644 --- a/database/repo/get_org_test.go +++ b/database/repo/get_org_test.go @@ -4,18 +4,19 @@ package repo import ( "context" - "reflect" "testing" "github.com/DATA-DOG/go-sqlmock" - "github.com/go-vela/types/library" + api "github.com/go-vela/server/api/types" + "github.com/go-vela/types/constants" + "github.com/go-vela/types/database" + "github.com/google/go-cmp/cmp" ) func TestRepo_Engine_GetRepoForOrg(t *testing.T) { // setup types _repo := testRepo() _repo.SetID(1) - _repo.SetUserID(1) _repo.SetHash("baz") _repo.SetOrg("foo") _repo.SetName("bar") @@ -23,7 +24,15 @@ func TestRepo_Engine_GetRepoForOrg(t *testing.T) { _repo.SetVisibility("public") _repo.SetPipelineType("yaml") _repo.SetTopics([]string{}) - _repo.SetAllowEvents(library.NewEventsFromMask(1)) + _repo.SetAllowEvents(api.NewEventsFromMask(1)) + + _owner := testOwner() + _owner.SetID(1) + _owner.SetName("foo") + _owner.SetToken("bar") + _owner.SetHash("baz") + + _repo.SetOwner(_owner) _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() @@ -33,8 +42,13 @@ func TestRepo_Engine_GetRepoForOrg(t *testing.T) { []string{"id", "user_id", "hash", "org", "name", "full_name", "link", "clone", "branch", "topics", "build_limit", "timeout", "counter", "visibility", "private", "trusted", "active", "allow_events", "pipeline_type", "previous_name", "approve_build"}). AddRow(1, 1, "baz", "foo", "bar", "foo/bar", "", "", "", "{}", 0, 0, 0, "public", false, false, false, 1, "yaml", "", "") + _userRows := sqlmock.NewRows( + []string{"id", "name", "token", "hash", "active", "admin"}). + AddRow(1, "foo", "bar", "baz", false, false) + // ensure the mock expects the query _mock.ExpectQuery(`SELECT * FROM "repos" WHERE org = $1 AND name = $2 LIMIT $3`).WithArgs("foo", "bar", 1).WillReturnRows(_rows) + _mock.ExpectQuery(`SELECT * FROM "users" WHERE "users"."id" = $1`).WithArgs(1).WillReturnRows(_userRows) _sqlite := testSqlite(t) defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }() @@ -44,12 +58,22 @@ func TestRepo_Engine_GetRepoForOrg(t *testing.T) { t.Errorf("unable to create test repo for sqlite: %v", err) } + err = _sqlite.client.AutoMigrate(&database.User{}) + if err != nil { + t.Errorf("unable to create build table for sqlite: %v", err) + } + + err = _sqlite.client.Table(constants.TableUser).Create(database.UserFromLibrary(_owner)).Error + if err != nil { + t.Errorf("unable to create test user for sqlite: %v", err) + } + // setup tests tests := []struct { failure bool name string database *engine - want *library.Repo + want *api.Repo }{ { failure: false, @@ -82,8 +106,8 @@ func TestRepo_Engine_GetRepoForOrg(t *testing.T) { t.Errorf("GetRepoForOrg for %s returned err: %v", test.name, err) } - if !reflect.DeepEqual(got, test.want) { - t.Errorf("GetRepoForOrg for %s is %v, want %v", test.name, got, test.want) + if diff := cmp.Diff(_repo, got); diff != "" { + t.Errorf("GetRepoForOrg mismatch (-want +got):\n%s", diff) } }) } diff --git a/database/repo/get_test.go b/database/repo/get_test.go index 3957975d2..281d079b5 100644 --- a/database/repo/get_test.go +++ b/database/repo/get_test.go @@ -8,14 +8,15 @@ import ( "testing" "github.com/DATA-DOG/go-sqlmock" - "github.com/go-vela/types/library" + api "github.com/go-vela/server/api/types" + "github.com/go-vela/types/constants" + "github.com/go-vela/types/database" ) func TestRepo_Engine_GetRepo(t *testing.T) { // setup types _repo := testRepo() _repo.SetID(1) - _repo.SetUserID(1) _repo.SetHash("baz") _repo.SetOrg("foo") _repo.SetName("bar") @@ -23,7 +24,15 @@ func TestRepo_Engine_GetRepo(t *testing.T) { _repo.SetVisibility("public") _repo.SetPipelineType("yaml") _repo.SetTopics([]string{}) - _repo.SetAllowEvents(library.NewEventsFromMask(1)) + _repo.SetAllowEvents(api.NewEventsFromMask(1)) + + _owner := testOwner() + _owner.SetID(1) + _owner.SetName("foo") + _owner.SetToken("bar") + _owner.SetHash("baz") + + _repo.SetOwner(_owner) _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() @@ -33,8 +42,13 @@ func TestRepo_Engine_GetRepo(t *testing.T) { []string{"id", "user_id", "hash", "org", "name", "full_name", "link", "clone", "branch", "topics", "build_limit", "timeout", "counter", "visibility", "private", "trusted", "active", "allow_events", "pipeline_type", "previous_name", "approve_build"}). AddRow(1, 1, "baz", "foo", "bar", "foo/bar", "", "", "", "{}", 0, 0, 0, "public", false, false, false, 1, "yaml", "", "") + _userRows := sqlmock.NewRows( + []string{"id", "name", "token", "hash", "active", "admin"}). + AddRow(1, "foo", "bar", "baz", false, false) + // ensure the mock expects the query _mock.ExpectQuery(`SELECT * FROM "repos" WHERE id = $1 LIMIT $2`).WithArgs(1, 1).WillReturnRows(_rows) + _mock.ExpectQuery(`SELECT * FROM "users" WHERE "users"."id" = $1`).WithArgs(1).WillReturnRows(_userRows) _sqlite := testSqlite(t) defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }() @@ -44,12 +58,22 @@ func TestRepo_Engine_GetRepo(t *testing.T) { t.Errorf("unable to create test repo for sqlite: %v", err) } + err = _sqlite.client.AutoMigrate(&database.User{}) + if err != nil { + t.Errorf("unable to create build table for sqlite: %v", err) + } + + err = _sqlite.client.Table(constants.TableUser).Create(database.UserFromLibrary(_owner)).Error + if err != nil { + t.Errorf("unable to create test user for sqlite: %v", err) + } + // setup tests tests := []struct { failure bool name string database *engine - want *library.Repo + want *api.Repo }{ { failure: false, diff --git a/database/repo/interface.go b/database/repo/interface.go index a69b5fa31..f73a085b4 100644 --- a/database/repo/interface.go +++ b/database/repo/interface.go @@ -5,6 +5,7 @@ package repo import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/library" ) @@ -33,19 +34,19 @@ type RepoInterface interface { // CountReposForUser defines a function that gets the count of repos by user ID. CountReposForUser(context.Context, *library.User, map[string]interface{}) (int64, error) // CreateRepo defines a function that creates a new repo. - CreateRepo(context.Context, *library.Repo) (*library.Repo, error) + CreateRepo(context.Context, *api.Repo) (*api.Repo, error) // DeleteRepo defines a function that deletes an existing repo. - DeleteRepo(context.Context, *library.Repo) error + DeleteRepo(context.Context, *api.Repo) error // GetRepo defines a function that gets a repo by ID. - GetRepo(context.Context, int64) (*library.Repo, error) + GetRepo(context.Context, int64) (*api.Repo, error) // GetRepoForOrg defines a function that gets a repo by org and repo name. - GetRepoForOrg(context.Context, string, string) (*library.Repo, error) + GetRepoForOrg(context.Context, string, string) (*api.Repo, error) // ListRepos defines a function that gets a list of all repos. - ListRepos(context.Context) ([]*library.Repo, error) + ListRepos(context.Context) ([]*api.Repo, error) // ListReposForOrg defines a function that gets a list of repos by org name. - ListReposForOrg(context.Context, string, string, map[string]interface{}, int, int) ([]*library.Repo, int64, error) + ListReposForOrg(context.Context, string, string, map[string]interface{}, int, int) ([]*api.Repo, int64, error) // ListReposForUser defines a function that gets a list of repos by user ID. - ListReposForUser(context.Context, *library.User, string, map[string]interface{}, int, int) ([]*library.Repo, int64, error) + ListReposForUser(context.Context, *library.User, string, map[string]interface{}, int, int) ([]*api.Repo, int64, error) // UpdateRepo defines a function that updates an existing repo. - UpdateRepo(context.Context, *library.Repo) (*library.Repo, error) + UpdateRepo(context.Context, *api.Repo) (*api.Repo, error) } diff --git a/database/repo/list.go b/database/repo/list.go index 790227d63..2197acbb1 100644 --- a/database/repo/list.go +++ b/database/repo/list.go @@ -5,19 +5,18 @@ package repo import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" - "github.com/go-vela/types/database" - "github.com/go-vela/types/library" ) // ListRepos gets a list of all repos from the database. -func (e *engine) ListRepos(ctx context.Context) ([]*library.Repo, error) { +func (e *engine) ListRepos(ctx context.Context) ([]*api.Repo, error) { e.logger.Trace("listing all repos from the database") // variables to store query results and return value count := int64(0) - r := new([]database.Repo) - repos := []*library.Repo{} + r := new([]Repo) + repos := []*api.Repo{} // count the results count, err := e.CountRepos(ctx) @@ -33,6 +32,7 @@ func (e *engine) ListRepos(ctx context.Context) ([]*library.Repo, error) { // send query to the database and store result in variable err = e.client. Table(constants.TableRepo). + Preload("Owner"). Find(&r). Error if err != nil { @@ -45,8 +45,6 @@ func (e *engine) ListRepos(ctx context.Context) ([]*library.Repo, error) { tmp := repo // decrypt the fields for the repo - // - // https://pkg.go.dev/github.com/go-vela/types/database#Repo.Decrypt err = tmp.Decrypt(e.config.EncryptionKey) if err != nil { // TODO: remove backwards compatibility before 1.x.x release @@ -57,10 +55,14 @@ func (e *engine) ListRepos(ctx context.Context) ([]*library.Repo, error) { e.logger.Errorf("unable to decrypt repo %d: %v", tmp.ID.Int64, err) } + // decrypt owner fields + err = tmp.Owner.Decrypt(e.config.EncryptionKey) + if err != nil { + e.logger.Errorf("unable to decrypt repo owner %d: %v", tmp.ID.Int64, err) + } + // convert query result to library type - // - // https://pkg.go.dev/github.com/go-vela/types/database#Repo.ToLibrary - repos = append(repos, tmp.ToLibrary()) + repos = append(repos, tmp.ToAPI()) } return repos, nil diff --git a/database/repo/list_org.go b/database/repo/list_org.go index 95783bd95..646f9b184 100644 --- a/database/repo/list_org.go +++ b/database/repo/list_org.go @@ -5,24 +5,23 @@ package repo import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" - "github.com/go-vela/types/database" - "github.com/go-vela/types/library" "github.com/sirupsen/logrus" ) // ListReposForOrg gets a list of repos by org name from the database. // //nolint:lll // ignore long line length due to variable names -func (e *engine) ListReposForOrg(ctx context.Context, org, sortBy string, filters map[string]interface{}, page, perPage int) ([]*library.Repo, int64, error) { +func (e *engine) ListReposForOrg(ctx context.Context, org, sortBy string, filters map[string]interface{}, page, perPage int) ([]*api.Repo, int64, error) { e.logger.WithFields(logrus.Fields{ "org": org, }).Tracef("listing repos for org %s from the database", org) // variables to store query results and return values count := int64(0) - r := new([]database.Repo) - repos := []*library.Repo{} + r := new([]Repo) + repos := []*api.Repo{} // count the results count, err := e.CountReposForOrg(ctx, org, filters) @@ -49,6 +48,7 @@ func (e *engine) ListReposForOrg(ctx context.Context, org, sortBy string, filter err = e.client. Table(constants.TableRepo). + Preload("Owner"). Select("repos.*"). Joins("LEFT JOIN (?) t on repos.id = t.id", query). Order("latest_build DESC NULLS LAST"). @@ -64,6 +64,7 @@ func (e *engine) ListReposForOrg(ctx context.Context, org, sortBy string, filter default: err = e.client. Table(constants.TableRepo). + Preload("Owner"). Where("org = ?", org). Where(filters). Order("name"). @@ -82,8 +83,6 @@ func (e *engine) ListReposForOrg(ctx context.Context, org, sortBy string, filter tmp := repo // decrypt the fields for the repo - // - // https://pkg.go.dev/github.com/go-vela/types/database#Repo.Decrypt err = tmp.Decrypt(e.config.EncryptionKey) if err != nil { // TODO: remove backwards compatibility before 1.x.x release @@ -94,10 +93,14 @@ func (e *engine) ListReposForOrg(ctx context.Context, org, sortBy string, filter e.logger.Errorf("unable to decrypt repo %d: %v", tmp.ID.Int64, err) } - // convert query result to library type - // - // https://pkg.go.dev/github.com/go-vela/types/database#Repo.ToLibrary - repos = append(repos, tmp.ToLibrary()) + // decrypt owner fields + err = tmp.Owner.Decrypt(e.config.EncryptionKey) + if err != nil { + e.logger.Errorf("unable to decrypt repo owner %d: %v", tmp.ID.Int64, err) + } + + // convert query result to API type + repos = append(repos, tmp.ToAPI()) } return repos, count, nil diff --git a/database/repo/list_org_test.go b/database/repo/list_org_test.go index e59e29d49..8f790d72b 100644 --- a/database/repo/list_org_test.go +++ b/database/repo/list_org_test.go @@ -4,14 +4,15 @@ package repo import ( "context" - "reflect" "testing" "time" "github.com/DATA-DOG/go-sqlmock" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" + "github.com/google/go-cmp/cmp" ) func TestRepo_Engine_ListReposForOrg(t *testing.T) { @@ -30,7 +31,6 @@ func TestRepo_Engine_ListReposForOrg(t *testing.T) { _repoOne := testRepo() _repoOne.SetID(1) - _repoOne.SetUserID(1) _repoOne.SetHash("baz") _repoOne.SetOrg("foo") _repoOne.SetName("bar") @@ -38,11 +38,10 @@ func TestRepo_Engine_ListReposForOrg(t *testing.T) { _repoOne.SetVisibility("public") _repoOne.SetPipelineType("yaml") _repoOne.SetTopics([]string{}) - _repoOne.SetAllowEvents(library.NewEventsFromMask(1)) + _repoOne.SetAllowEvents(api.NewEventsFromMask(1)) _repoTwo := testRepo() _repoTwo.SetID(2) - _repoTwo.SetUserID(1) _repoTwo.SetHash("bar") _repoTwo.SetOrg("foo") _repoTwo.SetName("baz") @@ -50,7 +49,16 @@ func TestRepo_Engine_ListReposForOrg(t *testing.T) { _repoTwo.SetVisibility("public") _repoTwo.SetPipelineType("yaml") _repoTwo.SetTopics([]string{}) - _repoTwo.SetAllowEvents(library.NewEventsFromMask(1)) + _repoTwo.SetAllowEvents(api.NewEventsFromMask(1)) + + _owner := testOwner() + _owner.SetID(1) + _owner.SetName("foo") + _owner.SetToken("bar") + _owner.SetHash("baz") + + _repoOne.SetOwner(_owner) + _repoTwo.SetOwner(_owner) _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() @@ -67,8 +75,13 @@ func TestRepo_Engine_ListReposForOrg(t *testing.T) { AddRow(1, 1, "baz", "foo", "bar", "foo/bar", "", "", "", "{}", 0, 0, "public", false, false, false, 1, "yaml", nil, nil). AddRow(2, 1, "bar", "foo", "baz", "foo/baz", "", "", "", "{}", 0, 0, "public", false, false, false, 1, "yaml", nil, nil) + _userRows := sqlmock.NewRows( + []string{"id", "name", "token", "hash", "active", "admin"}). + AddRow(1, "foo", "bar", "baz", false, false) + // ensure the mock expects the name query _mock.ExpectQuery(`SELECT * FROM "repos" WHERE org = $1 ORDER BY name LIMIT $2`).WithArgs("foo", 10).WillReturnRows(_rows) + _mock.ExpectQuery(`SELECT * FROM "users" WHERE "users"."id" = $1`).WithArgs(1).WillReturnRows(_userRows) // create expected latest count query result in mock _rows = sqlmock.NewRows([]string{"count"}).AddRow(2) @@ -82,8 +95,13 @@ func TestRepo_Engine_ListReposForOrg(t *testing.T) { AddRow(1, 1, "baz", "foo", "bar", "foo/bar", "", "", "", "{}", 0, 0, "public", false, false, false, 1, "yaml", nil, nil). AddRow(2, 1, "bar", "foo", "baz", "foo/baz", "", "", "", "{}", 0, 0, "public", false, false, false, 1, "yaml", nil, nil) + _userRows = sqlmock.NewRows( + []string{"id", "name", "token", "hash", "active", "admin"}). + AddRow(1, "foo", "bar", "baz", false, false) + // ensure the mock expects the latest query _mock.ExpectQuery(`SELECT repos.* FROM "repos" LEFT JOIN (SELECT repos.id, MAX(builds.created) AS latest_build FROM "builds" INNER JOIN repos repos ON builds.repo_id = repos.id WHERE repos.org = $1 GROUP BY "repos"."id") t on repos.id = t.id ORDER BY latest_build DESC NULLS LAST LIMIT $2`).WithArgs("foo", 10).WillReturnRows(_rows) + _mock.ExpectQuery(`SELECT * FROM "users" WHERE "users"."id" = $1`).WithArgs(1).WillReturnRows(_userRows) _sqlite := testSqlite(t) defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }() @@ -113,41 +131,51 @@ func TestRepo_Engine_ListReposForOrg(t *testing.T) { t.Errorf("unable to create test build for sqlite: %v", err) } + err = _sqlite.client.AutoMigrate(&database.User{}) + if err != nil { + t.Errorf("unable to create build table for sqlite: %v", err) + } + + err = _sqlite.client.Table(constants.TableUser).Create(database.UserFromLibrary(_owner)).Error + if err != nil { + t.Errorf("unable to create test user for sqlite: %v", err) + } + // setup tests tests := []struct { failure bool name string sort string database *engine - want []*library.Repo + want []*api.Repo }{ { failure: false, name: "postgres with name", database: _postgres, sort: "name", - want: []*library.Repo{_repoOne, _repoTwo}, + want: []*api.Repo{_repoOne, _repoTwo}, }, { failure: false, name: "postgres with latest", database: _postgres, sort: "latest", - want: []*library.Repo{_repoOne, _repoTwo}, + want: []*api.Repo{_repoOne, _repoTwo}, }, { failure: false, name: "sqlite with name", database: _sqlite, sort: "name", - want: []*library.Repo{_repoOne, _repoTwo}, + want: []*api.Repo{_repoOne, _repoTwo}, }, { failure: false, name: "sqlite with latest", database: _sqlite, sort: "latest", - want: []*library.Repo{_repoOne, _repoTwo}, + want: []*api.Repo{_repoOne, _repoTwo}, }, } @@ -170,8 +198,8 @@ func TestRepo_Engine_ListReposForOrg(t *testing.T) { t.Errorf("ListReposForOrg for %s returned err: %v", test.name, err) } - if !reflect.DeepEqual(got, test.want) { - t.Errorf("ListReposForOrg for %s is %v, want %v", test.name, got, test.want) + if diff := cmp.Diff(test.want, got); diff != "" { + t.Errorf("ListReposForOrg for %s mismatch (-want +got):\n%s", test.name, diff) } }) } diff --git a/database/repo/list_test.go b/database/repo/list_test.go index 7b3a5ff5e..8d3dd2c76 100644 --- a/database/repo/list_test.go +++ b/database/repo/list_test.go @@ -8,14 +8,15 @@ import ( "testing" "github.com/DATA-DOG/go-sqlmock" - "github.com/go-vela/types/library" + api "github.com/go-vela/server/api/types" + "github.com/go-vela/types/constants" + "github.com/go-vela/types/database" ) func TestRepo_Engine_ListRepos(t *testing.T) { // setup types _repoOne := testRepo() _repoOne.SetID(1) - _repoOne.SetUserID(1) _repoOne.SetHash("baz") _repoOne.SetOrg("foo") _repoOne.SetName("bar") @@ -23,11 +24,10 @@ func TestRepo_Engine_ListRepos(t *testing.T) { _repoOne.SetVisibility("public") _repoOne.SetPipelineType("yaml") _repoOne.SetTopics([]string{}) - _repoOne.SetAllowEvents(library.NewEventsFromMask(1)) + _repoOne.SetAllowEvents(api.NewEventsFromMask(1)) _repoTwo := testRepo() _repoTwo.SetID(2) - _repoTwo.SetUserID(1) _repoTwo.SetHash("baz") _repoTwo.SetOrg("bar") _repoTwo.SetName("foo") @@ -35,7 +35,16 @@ func TestRepo_Engine_ListRepos(t *testing.T) { _repoTwo.SetVisibility("public") _repoTwo.SetPipelineType("yaml") _repoTwo.SetTopics([]string{}) - _repoTwo.SetAllowEvents(library.NewEventsFromMask(1)) + _repoTwo.SetAllowEvents(api.NewEventsFromMask(1)) + + _owner := testOwner() + _owner.SetID(1) + _owner.SetName("foo") + _owner.SetToken("bar") + _owner.SetHash("baz") + + _repoOne.SetOwner(_owner) + _repoTwo.SetOwner(_owner) _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() @@ -52,8 +61,13 @@ func TestRepo_Engine_ListRepos(t *testing.T) { AddRow(1, 1, "baz", "foo", "bar", "foo/bar", "", "", "", "{}", 0, 0, "public", false, false, false, 1, "yaml", nil, nil). AddRow(2, 1, "baz", "bar", "foo", "bar/foo", "", "", "", "{}", 0, 0, "public", false, false, false, 1, "yaml", nil, nil) + _userRows := sqlmock.NewRows( + []string{"id", "name", "token", "hash", "active", "admin"}). + AddRow(1, "foo", "bar", "baz", false, false) + // ensure the mock expects the query _mock.ExpectQuery(`SELECT * FROM "repos"`).WillReturnRows(_rows) + _mock.ExpectQuery(`SELECT * FROM "users" WHERE "users"."id" = $1`).WithArgs(1).WillReturnRows(_userRows) _sqlite := testSqlite(t) defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }() @@ -68,24 +82,34 @@ func TestRepo_Engine_ListRepos(t *testing.T) { t.Errorf("unable to create test repo for sqlite: %v", err) } + err = _sqlite.client.AutoMigrate(&database.User{}) + if err != nil { + t.Errorf("unable to create build table for sqlite: %v", err) + } + + err = _sqlite.client.Table(constants.TableUser).Create(database.UserFromLibrary(_owner)).Error + if err != nil { + t.Errorf("unable to create test user for sqlite: %v", err) + } + // setup tests tests := []struct { failure bool name string database *engine - want []*library.Repo + want []*api.Repo }{ { failure: false, name: "postgres", database: _postgres, - want: []*library.Repo{_repoOne, _repoTwo}, + want: []*api.Repo{_repoOne, _repoTwo}, }, { failure: false, name: "sqlite3", database: _sqlite, - want: []*library.Repo{_repoOne, _repoTwo}, + want: []*api.Repo{_repoOne, _repoTwo}, }, } diff --git a/database/repo/list_user.go b/database/repo/list_user.go index bc11fb1b2..76c5d8d7d 100644 --- a/database/repo/list_user.go +++ b/database/repo/list_user.go @@ -5,8 +5,8 @@ package repo import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" - "github.com/go-vela/types/database" "github.com/go-vela/types/library" "github.com/sirupsen/logrus" ) @@ -14,15 +14,15 @@ import ( // ListReposForUser gets a list of repos by user ID from the database. // //nolint:lll // ignore long line length due to variable names -func (e *engine) ListReposForUser(ctx context.Context, u *library.User, sortBy string, filters map[string]interface{}, page, perPage int) ([]*library.Repo, int64, error) { +func (e *engine) ListReposForUser(ctx context.Context, u *library.User, sortBy string, filters map[string]interface{}, page, perPage int) ([]*api.Repo, int64, error) { e.logger.WithFields(logrus.Fields{ "user": u.GetName(), }).Tracef("listing repos for user %s from the database", u.GetName()) // variables to store query results and return values count := int64(0) - r := new([]database.Repo) - repos := []*library.Repo{} + r := new([]Repo) + repos := []*api.Repo{} // count the results count, err := e.CountReposForUser(ctx, u, filters) @@ -49,6 +49,7 @@ func (e *engine) ListReposForUser(ctx context.Context, u *library.User, sortBy s err = e.client. Table(constants.TableRepo). + Preload("Owner"). Select("repos.*"). Joins("LEFT JOIN (?) t on repos.id = t.id", query). Order("latest_build DESC NULLS LAST"). @@ -64,6 +65,7 @@ func (e *engine) ListReposForUser(ctx context.Context, u *library.User, sortBy s default: err = e.client. Table(constants.TableRepo). + Preload("Owner"). Where("user_id = ?", u.GetID()). Where(filters). Order("name"). @@ -82,8 +84,6 @@ func (e *engine) ListReposForUser(ctx context.Context, u *library.User, sortBy s tmp := repo // decrypt the fields for the repo - // - // https://pkg.go.dev/github.com/go-vela/types/database#Repo.Decrypt err = tmp.Decrypt(e.config.EncryptionKey) if err != nil { // TODO: remove backwards compatibility before 1.x.x release @@ -94,10 +94,14 @@ func (e *engine) ListReposForUser(ctx context.Context, u *library.User, sortBy s e.logger.Errorf("unable to decrypt repo %d: %v", tmp.ID.Int64, err) } + // decrypt owner fields + err = tmp.Owner.Decrypt(e.config.EncryptionKey) + if err != nil { + e.logger.Errorf("unable to decrypt repo owner %d: %v", tmp.ID.Int64, err) + } + // convert query result to library type - // - // https://pkg.go.dev/github.com/go-vela/types/database#Repo.ToLibrary - repos = append(repos, tmp.ToLibrary()) + repos = append(repos, tmp.ToAPI()) } return repos, count, nil diff --git a/database/repo/list_user_test.go b/database/repo/list_user_test.go index 30f6bc3b1..4ac0112c0 100644 --- a/database/repo/list_user_test.go +++ b/database/repo/list_user_test.go @@ -9,6 +9,7 @@ import ( "time" "github.com/DATA-DOG/go-sqlmock" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" @@ -30,7 +31,7 @@ func TestRepo_Engine_ListReposForUser(t *testing.T) { _repoOne := testRepo() _repoOne.SetID(1) - _repoOne.SetUserID(1) + _repoOne.GetOwner().SetID(1) _repoOne.SetHash("baz") _repoOne.SetOrg("foo") _repoOne.SetName("bar") @@ -38,11 +39,11 @@ func TestRepo_Engine_ListReposForUser(t *testing.T) { _repoOne.SetVisibility("public") _repoOne.SetPipelineType("yaml") _repoOne.SetTopics([]string{}) - _repoOne.SetAllowEvents(library.NewEventsFromMask(1)) + _repoOne.SetAllowEvents(api.NewEventsFromMask(1)) _repoTwo := testRepo() _repoTwo.SetID(2) - _repoTwo.SetUserID(1) + _repoTwo.GetOwner().SetID(1) _repoTwo.SetHash("baz") _repoTwo.SetOrg("bar") _repoTwo.SetName("foo") @@ -50,12 +51,16 @@ func TestRepo_Engine_ListReposForUser(t *testing.T) { _repoTwo.SetVisibility("public") _repoTwo.SetPipelineType("yaml") _repoTwo.SetTopics([]string{}) - _repoTwo.SetAllowEvents(library.NewEventsFromMask(1)) + _repoTwo.SetAllowEvents(api.NewEventsFromMask(1)) - _user := new(library.User) - _user.SetID(1) - _user.SetName("foo") - _user.SetToken("bar") + _owner := testOwner() + _owner.SetID(1) + _owner.SetName("foo") + _owner.SetToken("bar") + _owner.SetHash("baz") + + _repoOne.SetOwner(_owner) + _repoTwo.SetOwner(_owner) _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() @@ -72,8 +77,13 @@ func TestRepo_Engine_ListReposForUser(t *testing.T) { AddRow(1, 1, "baz", "foo", "bar", "foo/bar", "", "", "", "{}", 0, 0, "public", false, false, false, 1, "yaml", nil, nil). AddRow(2, 1, "baz", "bar", "foo", "bar/foo", "", "", "", "{}", 0, 0, "public", false, false, false, 1, "yaml", nil, nil) + _userRows := sqlmock.NewRows( + []string{"id", "name", "token", "hash", "active", "admin"}). + AddRow(1, "foo", "bar", "baz", false, false) + // ensure the mock expects the name query _mock.ExpectQuery(`SELECT * FROM "repos" WHERE user_id = $1 ORDER BY name LIMIT $2`).WithArgs(1, 10).WillReturnRows(_rows) + _mock.ExpectQuery(`SELECT * FROM "users" WHERE "users"."id" = $1`).WithArgs(1).WillReturnRows(_userRows) // create expected latest count query result in mock _rows = sqlmock.NewRows([]string{"count"}).AddRow(2) @@ -87,8 +97,13 @@ func TestRepo_Engine_ListReposForUser(t *testing.T) { AddRow(1, 1, "baz", "foo", "bar", "foo/bar", "", "", "", "{}", 0, 0, "public", false, false, false, 1, "yaml", nil, nil). AddRow(2, 1, "baz", "bar", "foo", "bar/foo", "", "", "", "{}", 0, 0, "public", false, false, false, 1, "yaml", nil, nil) + _userRows = sqlmock.NewRows( + []string{"id", "name", "token", "hash", "active", "admin"}). + AddRow(1, "foo", "bar", "baz", false, false) + // ensure the mock expects the latest query _mock.ExpectQuery(`SELECT repos.* FROM "repos" LEFT JOIN (SELECT repos.id, MAX(builds.created) AS latest_build FROM "builds" INNER JOIN repos repos ON builds.repo_id = repos.id WHERE repos.user_id = $1 GROUP BY "repos"."id") t on repos.id = t.id ORDER BY latest_build DESC NULLS LAST LIMIT $2`).WithArgs(1, 10).WillReturnRows(_rows) + _mock.ExpectQuery(`SELECT * FROM "users" WHERE "users"."id" = $1`).WithArgs(1).WillReturnRows(_userRows) _sqlite := testSqlite(t) defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }() @@ -118,41 +133,51 @@ func TestRepo_Engine_ListReposForUser(t *testing.T) { t.Errorf("unable to create test build for sqlite: %v", err) } + err = _sqlite.client.AutoMigrate(&database.User{}) + if err != nil { + t.Errorf("unable to create build table for sqlite: %v", err) + } + + err = _sqlite.client.Table(constants.TableUser).Create(database.UserFromLibrary(_owner)).Error + if err != nil { + t.Errorf("unable to create test user for sqlite: %v", err) + } + // setup tests tests := []struct { failure bool name string sort string database *engine - want []*library.Repo + want []*api.Repo }{ { failure: false, name: "postgres with name", database: _postgres, sort: "name", - want: []*library.Repo{_repoOne, _repoTwo}, + want: []*api.Repo{_repoOne, _repoTwo}, }, { failure: false, name: "postgres with latest", database: _postgres, sort: "latest", - want: []*library.Repo{_repoOne, _repoTwo}, + want: []*api.Repo{_repoOne, _repoTwo}, }, { failure: false, name: "sqlite with name", database: _sqlite, sort: "name", - want: []*library.Repo{_repoOne, _repoTwo}, + want: []*api.Repo{_repoOne, _repoTwo}, }, { failure: false, name: "sqlite with latest", database: _sqlite, sort: "latest", - want: []*library.Repo{_repoOne, _repoTwo}, + want: []*api.Repo{_repoOne, _repoTwo}, }, } @@ -161,7 +186,7 @@ func TestRepo_Engine_ListReposForUser(t *testing.T) { // run tests for _, test := range tests { t.Run(test.name, func(t *testing.T) { - got, _, err := test.database.ListReposForUser(context.TODO(), _user, test.sort, filters, 1, 10) + got, _, err := test.database.ListReposForUser(context.TODO(), _owner, test.sort, filters, 1, 10) if test.failure { if err == nil { diff --git a/database/repo/repo.go b/database/repo/repo.go index 5e4300e19..40a755723 100644 --- a/database/repo/repo.go +++ b/database/repo/repo.go @@ -4,14 +4,51 @@ package repo import ( "context" + "database/sql" + "encoding/base64" + "errors" "fmt" + api "github.com/go-vela/server/api/types" + "github.com/go-vela/server/util" "github.com/go-vela/types/constants" + "github.com/go-vela/types/database" + "github.com/lib/pq" "github.com/sirupsen/logrus" "gorm.io/gorm" ) +var ( + // ErrEmptyRepoFullName defines the error type when a + // Repo type has an empty FullName field provided. + ErrEmptyRepoFullName = errors.New("empty repo full_name provided") + + // ErrEmptyRepoHash defines the error type when a + // Repo type has an empty Hash field provided. + ErrEmptyRepoHash = errors.New("empty repo hash provided") + + // ErrEmptyRepoName defines the error type when a + // Repo type has an empty Name field provided. + ErrEmptyRepoName = errors.New("empty repo name provided") + + // ErrEmptyRepoOrg defines the error type when a + // Repo type has an empty Org field provided. + ErrEmptyRepoOrg = errors.New("empty repo org provided") + + // ErrEmptyRepoUserID defines the error type when a + // Repo type has an empty UserID field provided. + ErrEmptyRepoUserID = errors.New("empty repo user_id provided") + + // ErrEmptyRepoVisibility defines the error type when a + // Repo type has an empty Visibility field provided. + ErrEmptyRepoVisibility = errors.New("empty repo visibility provided") + + // ErrExceededTopicsLimit defines the error type when a + // Repo type has Topics field provided that exceeds the database limit. + ErrExceededTopicsLimit = errors.New("exceeded topics limit") +) + type ( // config represents the settings required to create the engine that implements the RepoInterface interface. config struct { @@ -38,6 +75,33 @@ type ( // https://pkg.go.dev/github.com/sirupsen/logrus#Entry logger *logrus.Entry } + + // Repo is the database representation of a repo. + Repo struct { + ID sql.NullInt64 `sql:"id"` + UserID sql.NullInt64 `sql:"user_id"` + Hash sql.NullString `sql:"hash"` + Org sql.NullString `sql:"org"` + Name sql.NullString `sql:"name"` + FullName sql.NullString `sql:"full_name"` + Link sql.NullString `sql:"link"` + Clone sql.NullString `sql:"clone"` + Branch sql.NullString `sql:"branch"` + Topics pq.StringArray `sql:"topics" gorm:"type:varchar(1020)"` + BuildLimit sql.NullInt64 `sql:"build_limit"` + Timeout sql.NullInt64 `sql:"timeout"` + Counter sql.NullInt32 `sql:"counter"` + Visibility sql.NullString `sql:"visibility"` + Private sql.NullBool `sql:"private"` + Trusted sql.NullBool `sql:"trusted"` + Active sql.NullBool `sql:"active"` + AllowEvents sql.NullInt64 `sql:"allow_events"` + PipelineType sql.NullString `sql:"pipeline_type"` + PreviousName sql.NullString `sql:"previous_name"` + ApproveBuild sql.NullString `sql:"approve_build"` + + Owner database.User `gorm:"foreignKey:UserID"` + } ) // New creates and returns a Vela service for integrating with repos in the database. @@ -81,3 +145,267 @@ func New(opts ...EngineOpt) (*engine, error) { return e, nil } + +// Decrypt will manipulate the existing repo hash by +// base64 decoding that value. Then, a AES-256 cipher +// block is created from the encryption key in order to +// decrypt the base64 decoded secret value. +func (r *Repo) Decrypt(key string) error { + // base64 decode the encrypted repo hash + decoded, err := base64.StdEncoding.DecodeString(r.Hash.String) + if err != nil { + return err + } + + // decrypt the base64 decoded repo hash + decrypted, err := util.Decrypt(key, decoded) + if err != nil { + return err + } + + // set the decrypted repo hash + r.Hash = sql.NullString{ + String: string(decrypted), + Valid: true, + } + + return nil +} + +// Encrypt will manipulate the existing repo hash by +// creating a AES-256 cipher block from the encryption +// key in order to encrypt the repo hash. Then, the +// repo hash is base64 encoded for transport across +// network boundaries. +func (r *Repo) Encrypt(key string) error { + // encrypt the repo hash + encrypted, err := util.Encrypt(key, []byte(r.Hash.String)) + if err != nil { + return err + } + + // base64 encode the encrypted repo hash to make it network safe + r.Hash = sql.NullString{ + String: base64.StdEncoding.EncodeToString(encrypted), + Valid: true, + } + + return nil +} + +// Nullify ensures the valid flag for +// the sql.Null types are properly set. +// +// When a field within the Repo type is the zero +// value for the field, the valid flag is set to +// false causing it to be NULL in the database. +func (r *Repo) Nullify() *Repo { + if r == nil { + return nil + } + + // check if the ID field should be false + if r.ID.Int64 == 0 { + r.ID.Valid = false + } + + // check if the UserID field should be false + if r.UserID.Int64 == 0 { + r.UserID.Valid = false + } + + // check if the Hash field should be false + if len(r.Hash.String) == 0 { + r.Hash.Valid = false + } + + // check if the Org field should be false + if len(r.Org.String) == 0 { + r.Org.Valid = false + } + + // check if the Name field should be false + if len(r.Name.String) == 0 { + r.Name.Valid = false + } + + // check if the FullName field should be false + if len(r.FullName.String) == 0 { + r.FullName.Valid = false + } + + // check if the Link field should be false + if len(r.Link.String) == 0 { + r.Link.Valid = false + } + + // check if the Clone field should be false + if len(r.Clone.String) == 0 { + r.Clone.Valid = false + } + + // check if the Branch field should be false + if len(r.Branch.String) == 0 { + r.Branch.Valid = false + } + + // check if the BuildLimit field should be false + if r.BuildLimit.Int64 == 0 { + r.BuildLimit.Valid = false + } + + // check if the Timeout field should be false + if r.Timeout.Int64 == 0 { + r.Timeout.Valid = false + } + + // check if the AllowEvents field should be false + if r.AllowEvents.Int64 == 0 { + r.AllowEvents.Valid = false + } + + // check if the Visibility field should be false + if len(r.Visibility.String) == 0 { + r.Visibility.Valid = false + } + + // check if the PipelineType field should be false + if len(r.PipelineType.String) == 0 { + r.PipelineType.Valid = false + } + + // check if the PreviousName field should be false + if len(r.PreviousName.String) == 0 { + r.PreviousName.Valid = false + } + + // check if the ApproveForkBuild field should be false + if len(r.ApproveBuild.String) == 0 { + r.ApproveBuild.Valid = false + } + + return r +} + +// ToAPI converts the Repo type +// to an API Repo type. +func (r *Repo) ToAPI() *api.Repo { + repo := new(api.Repo) + + repo.SetID(r.ID.Int64) + repo.SetOwner(r.Owner.ToLibrary()) + repo.SetHash(r.Hash.String) + repo.SetOrg(r.Org.String) + repo.SetName(r.Name.String) + repo.SetFullName(r.FullName.String) + repo.SetLink(r.Link.String) + repo.SetClone(r.Clone.String) + repo.SetBranch(r.Branch.String) + repo.SetTopics(r.Topics) + repo.SetBuildLimit(r.BuildLimit.Int64) + repo.SetTimeout(r.Timeout.Int64) + repo.SetCounter(int(r.Counter.Int32)) + repo.SetVisibility(r.Visibility.String) + repo.SetPrivate(r.Private.Bool) + repo.SetTrusted(r.Trusted.Bool) + repo.SetActive(r.Active.Bool) + repo.SetAllowEvents(api.NewEventsFromMask(r.AllowEvents.Int64)) + repo.SetPipelineType(r.PipelineType.String) + repo.SetPreviousName(r.PreviousName.String) + repo.SetApproveBuild(r.ApproveBuild.String) + + return repo +} + +// Validate verifies the necessary fields for +// the Repo type are populated correctly. +func (r *Repo) Validate() error { + // verify the UserID field is populated + if r.UserID.Int64 <= 0 { + return ErrEmptyRepoUserID + } + + // verify the Hash field is populated + if len(r.Hash.String) == 0 { + return ErrEmptyRepoHash + } + + // verify the Org field is populated + if len(r.Org.String) == 0 { + return ErrEmptyRepoOrg + } + + // verify the Name field is populated + if len(r.Name.String) == 0 { + return ErrEmptyRepoName + } + + // verify the FullName field is populated + if len(r.FullName.String) == 0 { + return ErrEmptyRepoFullName + } + + // verify the Visibility field is populated + if len(r.Visibility.String) == 0 { + return ErrEmptyRepoVisibility + } + + // calculate total size of favorites while sanitizing entries + total := 0 + + for i, t := range r.Topics { + r.Topics[i] = util.Sanitize(t) + total += len(t) + } + + // verify the Favorites field is within the database constraints + // len is to factor in number of comma separators included in the database field, + // removing 1 due to the last item not having an appended comma + if (total + len(r.Topics) - 1) > constants.TopicsMaxSize { + return ErrExceededTopicsLimit + } + + // ensure that all Repo string fields + // that can be returned as JSON are sanitized + // to avoid unsafe HTML content + r.Org = sql.NullString{String: util.Sanitize(r.Org.String), Valid: r.Org.Valid} + r.Name = sql.NullString{String: util.Sanitize(r.Name.String), Valid: r.Name.Valid} + r.FullName = sql.NullString{String: util.Sanitize(r.FullName.String), Valid: r.FullName.Valid} + r.Link = sql.NullString{String: util.Sanitize(r.Link.String), Valid: r.Link.Valid} + r.Clone = sql.NullString{String: util.Sanitize(r.Clone.String), Valid: r.Clone.Valid} + r.Branch = sql.NullString{String: util.Sanitize(r.Branch.String), Valid: r.Branch.Valid} + r.Visibility = sql.NullString{String: util.Sanitize(r.Visibility.String), Valid: r.Visibility.Valid} + r.PipelineType = sql.NullString{String: util.Sanitize(r.PipelineType.String), Valid: r.PipelineType.Valid} + + return nil +} + +// FromAPI converts the API Repo type +// to a database repo type. +func FromAPI(r *api.Repo) *Repo { + repo := &Repo{ + ID: sql.NullInt64{Int64: r.GetID(), Valid: true}, + UserID: sql.NullInt64{Int64: r.GetOwner().GetID(), Valid: true}, + Hash: sql.NullString{String: r.GetHash(), Valid: true}, + Org: sql.NullString{String: r.GetOrg(), Valid: true}, + Name: sql.NullString{String: r.GetName(), Valid: true}, + FullName: sql.NullString{String: r.GetFullName(), Valid: true}, + Link: sql.NullString{String: r.GetLink(), Valid: true}, + Clone: sql.NullString{String: r.GetClone(), Valid: true}, + Branch: sql.NullString{String: r.GetBranch(), Valid: true}, + Topics: pq.StringArray(r.GetTopics()), + BuildLimit: sql.NullInt64{Int64: r.GetBuildLimit(), Valid: true}, + Timeout: sql.NullInt64{Int64: r.GetTimeout(), Valid: true}, + Counter: sql.NullInt32{Int32: int32(r.GetCounter()), Valid: true}, + Visibility: sql.NullString{String: r.GetVisibility(), Valid: true}, + Private: sql.NullBool{Bool: r.GetPrivate(), Valid: true}, + Trusted: sql.NullBool{Bool: r.GetTrusted(), Valid: true}, + Active: sql.NullBool{Bool: r.GetActive(), Valid: true}, + AllowEvents: sql.NullInt64{Int64: r.GetAllowEvents().ToDatabase(), Valid: true}, + PipelineType: sql.NullString{String: r.GetPipelineType(), Valid: true}, + PreviousName: sql.NullString{String: r.GetPreviousName(), Valid: true}, + ApproveBuild: sql.NullString{String: r.GetApproveBuild(), Valid: true}, + } + + return repo.Nullify() +} diff --git a/database/repo/repo_test.go b/database/repo/repo_test.go index 7abb255d6..e617e4feb 100644 --- a/database/repo/repo_test.go +++ b/database/repo/repo_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/DATA-DOG/go-sqlmock" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/library" "github.com/go-vela/types/library/actions" "github.com/sirupsen/logrus" @@ -171,12 +172,12 @@ func testSqlite(t *testing.T) *engine { return _engine } -// testRepo is a test helper function to create a library +// testRepo is a test helper function to create an API // Repo type with all fields set to their zero values. -func testRepo() *library.Repo { - return &library.Repo{ +func testRepo() *api.Repo { + return &api.Repo{ ID: new(int64), - UserID: new(int64), + Owner: testOwner(), BuildLimit: new(int64), Timeout: new(int64), Counter: new(int), @@ -198,8 +199,8 @@ func testRepo() *library.Repo { } } -func testEvents() *library.Events { - return &library.Events{ +func testEvents() *api.Events { + return &api.Events{ Push: &actions.Push{ Branch: new(bool), Tag: new(bool), @@ -227,6 +228,19 @@ func testEvents() *library.Events { } } +func testOwner() *library.User { + return &library.User{ + ID: new(int64), + Name: new(string), + RefreshToken: new(string), + Token: new(string), + Hash: new(string), + Favorites: new([]string), + Active: new(bool), + Admin: new(bool), + } +} + // This will be used with the github.com/DATA-DOG/go-sqlmock library to compare values // that are otherwise not easily compared. These typically would be values generated // before adding or updating them in the database. diff --git a/database/repo/update.go b/database/repo/update.go index 0c3273ce2..14ff985f7 100644 --- a/database/repo/update.go +++ b/database/repo/update.go @@ -7,35 +7,28 @@ import ( "context" "fmt" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" - "github.com/go-vela/types/database" - "github.com/go-vela/types/library" "github.com/sirupsen/logrus" ) // UpdateRepo updates an existing repo in the database. -func (e *engine) UpdateRepo(ctx context.Context, r *library.Repo) (*library.Repo, error) { +func (e *engine) UpdateRepo(ctx context.Context, r *api.Repo) (*api.Repo, error) { e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), }).Tracef("creating repo %s in the database", r.GetFullName()) // cast the library type to database type - // - // https://pkg.go.dev/github.com/go-vela/types/database#RepoFromLibrary - repo := database.RepoFromLibrary(r) + repo := FromAPI(r) // validate the necessary fields are populated - // - // https://pkg.go.dev/github.com/go-vela/types/database#Repo.Validate err := repo.Validate() if err != nil { return nil, err } // encrypt the fields for the repo - // - // https://pkg.go.dev/github.com/go-vela/types/database#Repo.Encrypt err = repo.Encrypt(e.config.EncryptionKey) if err != nil { return nil, fmt.Errorf("unable to encrypt repo %s: %w", r.GetFullName(), err) @@ -53,8 +46,12 @@ func (e *engine) UpdateRepo(ctx context.Context, r *library.Repo) (*library.Repo // only log to preserve backwards compatibility e.logger.Errorf("unable to decrypt repo %d: %v", r.GetID(), err) - return repo.ToLibrary(), nil + return repo.ToAPI(), nil } - return repo.ToLibrary(), nil + // set owner to provided owner if creation successful + result := repo.ToAPI() + result.SetOwner(r.GetOwner()) + + return result, nil } diff --git a/database/repo/update_test.go b/database/repo/update_test.go index c7bca1d15..e36a44ad2 100644 --- a/database/repo/update_test.go +++ b/database/repo/update_test.go @@ -8,15 +8,15 @@ import ( "testing" "github.com/DATA-DOG/go-sqlmock" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" - "github.com/go-vela/types/library" ) func TestRepo_Engine_UpdateRepo(t *testing.T) { // setup types _repo := testRepo() _repo.SetID(1) - _repo.SetUserID(1) + _repo.GetOwner().SetID(1) _repo.SetHash("baz") _repo.SetOrg("foo") _repo.SetName("bar") @@ -26,7 +26,7 @@ func TestRepo_Engine_UpdateRepo(t *testing.T) { _repo.SetPreviousName("oldName") _repo.SetApproveBuild(constants.ApproveForkAlways) _repo.SetTopics([]string{}) - _repo.SetAllowEvents(library.NewEventsFromMask(1)) + _repo.SetAllowEvents(api.NewEventsFromMask(1)) _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() diff --git a/database/schedule/count_repo.go b/database/schedule/count_repo.go index 471537d8f..e876dda57 100644 --- a/database/schedule/count_repo.go +++ b/database/schedule/count_repo.go @@ -4,13 +4,14 @@ package schedule import ( "context" + + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" - "github.com/go-vela/types/library" "github.com/sirupsen/logrus" ) // CountSchedulesForRepo gets the count of schedules by repo ID from the database. -func (e *engine) CountSchedulesForRepo(ctx context.Context, r *library.Repo) (int64, error) { +func (e *engine) CountSchedulesForRepo(ctx context.Context, r *api.Repo) (int64, error) { e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), diff --git a/database/schedule/get_repo.go b/database/schedule/get_repo.go index 6ee2e6bb7..3d8efbf0d 100644 --- a/database/schedule/get_repo.go +++ b/database/schedule/get_repo.go @@ -4,6 +4,8 @@ package schedule import ( "context" + + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" @@ -11,7 +13,7 @@ import ( ) // GetScheduleForRepo gets a schedule by repo ID and name from the database. -func (e *engine) GetScheduleForRepo(ctx context.Context, r *library.Repo, name string) (*library.Schedule, error) { +func (e *engine) GetScheduleForRepo(ctx context.Context, r *api.Repo, name string) (*library.Schedule, error) { e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), diff --git a/database/schedule/interface.go b/database/schedule/interface.go index 8d80a7c36..b25b3c363 100644 --- a/database/schedule/interface.go +++ b/database/schedule/interface.go @@ -5,6 +5,7 @@ package schedule import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/library" ) @@ -29,7 +30,7 @@ type ScheduleInterface interface { // CountSchedules defines a function that gets the count of all schedules. CountSchedules(context.Context) (int64, error) // CountSchedulesForRepo defines a function that gets the count of schedules by repo ID. - CountSchedulesForRepo(context.Context, *library.Repo) (int64, error) + CountSchedulesForRepo(context.Context, *api.Repo) (int64, error) // CreateSchedule defines a function that creates a new schedule. CreateSchedule(context.Context, *library.Schedule) (*library.Schedule, error) // DeleteSchedule defines a function that deletes an existing schedule. @@ -37,13 +38,13 @@ type ScheduleInterface interface { // GetSchedule defines a function that gets a schedule by ID. GetSchedule(context.Context, int64) (*library.Schedule, error) // GetScheduleForRepo defines a function that gets a schedule by repo ID and name. - GetScheduleForRepo(context.Context, *library.Repo, string) (*library.Schedule, error) + GetScheduleForRepo(context.Context, *api.Repo, string) (*library.Schedule, error) // ListActiveSchedules defines a function that gets a list of all active schedules. ListActiveSchedules(context.Context) ([]*library.Schedule, error) // ListSchedules defines a function that gets a list of all schedules. ListSchedules(context.Context) ([]*library.Schedule, error) // ListSchedulesForRepo defines a function that gets a list of schedules by repo ID. - ListSchedulesForRepo(context.Context, *library.Repo, int, int) ([]*library.Schedule, int64, error) + ListSchedulesForRepo(context.Context, *api.Repo, int, int) ([]*library.Schedule, int64, error) // UpdateSchedule defines a function that updates an existing schedule. UpdateSchedule(context.Context, *library.Schedule, bool) (*library.Schedule, error) } diff --git a/database/schedule/list_repo.go b/database/schedule/list_repo.go index c5d7866ca..6c4435a03 100644 --- a/database/schedule/list_repo.go +++ b/database/schedule/list_repo.go @@ -4,6 +4,8 @@ package schedule import ( "context" + + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" @@ -11,7 +13,7 @@ import ( ) // ListSchedulesForRepo gets a list of schedules by repo ID from the database. -func (e *engine) ListSchedulesForRepo(ctx context.Context, r *library.Repo, page, perPage int) ([]*library.Schedule, int64, error) { +func (e *engine) ListSchedulesForRepo(ctx context.Context, r *api.Repo, page, perPage int) ([]*library.Schedule, int64, error) { e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), diff --git a/database/schedule/schedule_test.go b/database/schedule/schedule_test.go index 723c40aaa..45663008b 100644 --- a/database/schedule/schedule_test.go +++ b/database/schedule/schedule_test.go @@ -10,6 +10,7 @@ import ( "time" "github.com/DATA-DOG/go-sqlmock" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/library" "github.com/sirupsen/logrus" @@ -190,10 +191,9 @@ func testSchedule() *library.Schedule { } // testRepo is a test helper function to create a library Repo type with all fields set to their zero values. -func testRepo() *library.Repo { - return &library.Repo{ +func testRepo() *api.Repo { + return &api.Repo{ ID: new(int64), - UserID: new(int64), BuildLimit: new(int64), Timeout: new(int64), Counter: new(int), diff --git a/database/secret/count_repo.go b/database/secret/count_repo.go index 8fd9b0090..c248e5ee5 100644 --- a/database/secret/count_repo.go +++ b/database/secret/count_repo.go @@ -5,13 +5,13 @@ package secret import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" - "github.com/go-vela/types/library" "github.com/sirupsen/logrus" ) // CountSecretsForRepo gets the count of secrets by org and repo name from the database. -func (e *engine) CountSecretsForRepo(ctx context.Context, r *library.Repo, filters map[string]interface{}) (int64, error) { +func (e *engine) CountSecretsForRepo(ctx context.Context, r *api.Repo, filters map[string]interface{}) (int64, error) { e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), diff --git a/database/secret/count_repo_test.go b/database/secret/count_repo_test.go index 3e688b0c2..fd5afacb4 100644 --- a/database/secret/count_repo_test.go +++ b/database/secret/count_repo_test.go @@ -16,7 +16,7 @@ func TestSecret_Engine_CountSecretsForRepo(t *testing.T) { // setup types _repo := testRepo() _repo.SetID(1) - _repo.SetUserID(1) + _repo.GetOwner().SetID(1) _repo.SetHash("baz") _repo.SetOrg("foo") _repo.SetName("bar") diff --git a/database/secret/count_team_test.go b/database/secret/count_team_test.go index 9800b34fd..07db848e9 100644 --- a/database/secret/count_team_test.go +++ b/database/secret/count_team_test.go @@ -112,7 +112,7 @@ func TestSecret_Engine_CountSecretsForTeams(t *testing.T) { // setup types _repo := testRepo() _repo.SetID(1) - _repo.SetUserID(1) + _repo.GetOwner().SetID(1) _repo.SetHash("baz") _repo.SetOrg("foo") _repo.SetName("bar") diff --git a/database/secret/get_repo.go b/database/secret/get_repo.go index b33b0d58d..d1ae48e7f 100644 --- a/database/secret/get_repo.go +++ b/database/secret/get_repo.go @@ -5,6 +5,7 @@ package secret import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" @@ -12,7 +13,7 @@ import ( ) // GetSecretForRepo gets a secret by org and repo name from the database. -func (e *engine) GetSecretForRepo(ctx context.Context, name string, r *library.Repo) (*library.Secret, error) { +func (e *engine) GetSecretForRepo(ctx context.Context, name string, r *api.Repo) (*library.Secret, error) { e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), diff --git a/database/secret/get_repo_test.go b/database/secret/get_repo_test.go index d32f81cc0..fb1c1ca9b 100644 --- a/database/secret/get_repo_test.go +++ b/database/secret/get_repo_test.go @@ -16,7 +16,7 @@ func TestSecret_Engine_GetSecretForRepo(t *testing.T) { // setup types _repo := testRepo() _repo.SetID(1) - _repo.SetUserID(1) + _repo.GetOwner().SetID(1) _repo.SetHash("baz") _repo.SetOrg("foo") _repo.SetName("bar") diff --git a/database/secret/interface.go b/database/secret/interface.go index e431fd458..b5aa4f73e 100644 --- a/database/secret/interface.go +++ b/database/secret/interface.go @@ -5,6 +5,7 @@ package secret import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/library" ) @@ -31,7 +32,7 @@ type SecretInterface interface { // CountSecretsForOrg defines a function that gets the count of secrets by org name. CountSecretsForOrg(context.Context, string, map[string]interface{}) (int64, error) // CountSecretsForRepo defines a function that gets the count of secrets by org and repo name. - CountSecretsForRepo(context.Context, *library.Repo, map[string]interface{}) (int64, error) + CountSecretsForRepo(context.Context, *api.Repo, map[string]interface{}) (int64, error) // CountSecretsForTeam defines a function that gets the count of secrets by org and team name. CountSecretsForTeam(context.Context, string, string, map[string]interface{}) (int64, error) // CountSecretsForTeams defines a function that gets the count of secrets by teams within an org. @@ -45,7 +46,7 @@ type SecretInterface interface { // GetSecretForOrg defines a function that gets a secret by org name. GetSecretForOrg(context.Context, string, string) (*library.Secret, error) // GetSecretForRepo defines a function that gets a secret by org and repo name. - GetSecretForRepo(context.Context, string, *library.Repo) (*library.Secret, error) + GetSecretForRepo(context.Context, string, *api.Repo) (*library.Secret, error) // GetSecretForTeam defines a function that gets a secret by org and team name. GetSecretForTeam(context.Context, string, string, string) (*library.Secret, error) // ListSecrets defines a function that gets a list of all secrets. @@ -53,7 +54,7 @@ type SecretInterface interface { // ListSecretsForOrg defines a function that gets a list of secrets by org name. ListSecretsForOrg(context.Context, string, map[string]interface{}, int, int) ([]*library.Secret, int64, error) // ListSecretsForRepo defines a function that gets a list of secrets by org and repo name. - ListSecretsForRepo(context.Context, *library.Repo, map[string]interface{}, int, int) ([]*library.Secret, int64, error) + ListSecretsForRepo(context.Context, *api.Repo, map[string]interface{}, int, int) ([]*library.Secret, int64, error) // ListSecretsForTeam defines a function that gets a list of secrets by org and team name. ListSecretsForTeam(context.Context, string, string, map[string]interface{}, int, int) ([]*library.Secret, int64, error) // ListSecretsForTeams defines a function that gets a list of secrets by teams within an org. diff --git a/database/secret/list_repo.go b/database/secret/list_repo.go index 213ee9353..a2731e321 100644 --- a/database/secret/list_repo.go +++ b/database/secret/list_repo.go @@ -5,6 +5,7 @@ package secret import ( "context" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/database" "github.com/go-vela/types/library" @@ -14,7 +15,7 @@ import ( // ListSecretsForRepo gets a list of secrets by org name from the database. // //nolint:lll // ignore long line length due to variable names -func (e *engine) ListSecretsForRepo(ctx context.Context, r *library.Repo, filters map[string]interface{}, page, perPage int) ([]*library.Secret, int64, error) { +func (e *engine) ListSecretsForRepo(ctx context.Context, r *api.Repo, filters map[string]interface{}, page, perPage int) ([]*library.Secret, int64, error) { e.logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), diff --git a/database/secret/list_repo_test.go b/database/secret/list_repo_test.go index 9fc76e7bf..304520c3e 100644 --- a/database/secret/list_repo_test.go +++ b/database/secret/list_repo_test.go @@ -17,7 +17,7 @@ func TestSecret_Engine_ListSecretsForRepo(t *testing.T) { // setup types _repo := testRepo() _repo.SetID(1) - _repo.SetUserID(1) + _repo.GetOwner().SetID(1) _repo.SetHash("baz") _repo.SetOrg("foo") _repo.SetName("bar") diff --git a/database/secret/secret_test.go b/database/secret/secret_test.go index 5382c054b..cbbefd9e2 100644 --- a/database/secret/secret_test.go +++ b/database/secret/secret_test.go @@ -9,6 +9,7 @@ import ( "time" "github.com/DATA-DOG/go-sqlmock" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/library" "github.com/go-vela/types/library/actions" "github.com/sirupsen/logrus" @@ -178,10 +179,9 @@ func testSqlite(t *testing.T) *engine { // testRepo is a test helper function to create a library // Repo type with all fields set to their zero values. -func testRepo() *library.Repo { - return &library.Repo{ +func testRepo() *api.Repo { + return &api.Repo{ ID: new(int64), - UserID: new(int64), BuildLimit: new(int64), Timeout: new(int64), Counter: new(int), diff --git a/go.mod b/go.mod index 2b2c83713..c0b21447e 100644 --- a/go.mod +++ b/go.mod @@ -12,9 +12,10 @@ require ( github.com/aws/aws-sdk-go v1.51.0 github.com/buildkite/yaml v0.0.0-20181016232759-0caa5f0796e3 github.com/drone/envsubst v1.0.3 + github.com/ghodss/yaml v1.0.0 github.com/gin-gonic/gin v1.9.1 github.com/go-playground/assert/v2 v2.2.0 - github.com/go-vela/types v0.23.4-0.20240402153726-f16c3e4cb5fb + github.com/go-vela/types v0.23.4-0.20240405205548-f24f795ac0b7 github.com/golang-jwt/jwt/v5 v5.2.1 github.com/google/go-cmp v0.6.0 github.com/google/go-github/v61 v61.0.0 @@ -58,7 +59,6 @@ require ( github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect - github.com/ghodss/yaml v1.0.0 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-jose/go-jose/v3 v3.0.3 // indirect github.com/go-logr/logr v1.3.0 // indirect diff --git a/go.sum b/go.sum index f3bc883ef..1f35c936b 100644 --- a/go.sum +++ b/go.sum @@ -85,8 +85,8 @@ github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= github.com/go-test/deep v1.0.2 h1:onZX1rnHT3Wv6cqNgYyFOOlgVKJrksuCMCRvJStbMYw= github.com/go-test/deep v1.0.2/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= -github.com/go-vela/types v0.23.4-0.20240402153726-f16c3e4cb5fb h1:jHao/NNRswInMLEb0m1OJ84d1m/LGWMCmaeRqSDnWQY= -github.com/go-vela/types v0.23.4-0.20240402153726-f16c3e4cb5fb/go.mod h1:mEF9dLkk00rUXf/t39n2WvXZgJbxnPEEWy+DHqIlRUo= +github.com/go-vela/types v0.23.4-0.20240405205548-f24f795ac0b7 h1:3mN7ej69dMH3Vis3G/tPLzLL0Rfp8nR5qd0gpj5ejRM= +github.com/go-vela/types v0.23.4-0.20240405205548-f24f795ac0b7/go.mod h1:mEF9dLkk00rUXf/t39n2WvXZgJbxnPEEWy+DHqIlRUo= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= diff --git a/internal/metadata.go b/internal/metadata.go new file mode 100644 index 000000000..00a94bde0 --- /dev/null +++ b/internal/metadata.go @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: Apache-2.0 + +package internal + +import "time" + +type ( + // Database is the extra set of database data passed to the compiler. + Database struct { + Driver string `json:"driver"` + Host string `json:"host"` + } + + // Queue is the extra set of queue data passed to the compiler. + Queue struct { + Channel string `json:"channel"` + Driver string `json:"driver"` + Host string `json:"host"` + } + + // Source is the extra set of source data passed to the compiler. + Source struct { + Driver string `json:"driver"` + Host string `json:"host"` + } + + // Vela is the extra set of Vela data passed to the compiler. + Vela struct { + Address string `json:"address"` + WebAddress string `json:"web_address"` + WebOauthCallbackPath string `json:"web_oauth_callback_path"` + AccessTokenDuration time.Duration `json:"access_token_duration"` + RefreshTokenDuration time.Duration `json:"refresh_token_duration"` + } + + // Metadata is the extra set of data passed to the compiler for + // converting a yaml configuration to an executable pipeline. + Metadata struct { + Database *Database `json:"database"` + Queue *Queue `json:"queue"` + Source *Source `json:"source"` + Vela *Vela `json:"vela"` + } +) diff --git a/internal/token/compose.go b/internal/token/compose.go index 6e48647e2..df3d9c652 100644 --- a/internal/token/compose.go +++ b/internal/token/compose.go @@ -7,7 +7,7 @@ import ( "net/url" "github.com/gin-gonic/gin" - "github.com/go-vela/types" + "github.com/go-vela/server/internal" "github.com/go-vela/types/constants" "github.com/go-vela/types/library" ) @@ -21,7 +21,7 @@ import ( func (tm *Manager) Compose(c *gin.Context, u *library.User) (string, string, error) { // grab the metadata from the context to pull in provided // cookie duration information - m := c.MustGet("metadata").(*types.Metadata) + m := c.MustGet("metadata").(*internal.Metadata) // mint token options for refresh token rmto := MintTokenOpts{ diff --git a/internal/token/compose_test.go b/internal/token/compose_test.go index a73bf912b..44e06f3b5 100644 --- a/internal/token/compose_test.go +++ b/internal/token/compose_test.go @@ -9,7 +9,7 @@ import ( "time" "github.com/gin-gonic/gin" - "github.com/go-vela/types" + "github.com/go-vela/server/internal" "github.com/go-vela/types/constants" "github.com/go-vela/types/library" @@ -53,8 +53,8 @@ func TestToken_Compose(t *testing.T) { t.Errorf("Unable to create test token: %v", err) } - m := &types.Metadata{ - Vela: &types.Vela{ + m := &internal.Metadata{ + Vela: &internal.Vela{ AccessTokenDuration: d, }, } diff --git a/internal/webhook.go b/internal/webhook.go new file mode 100644 index 000000000..ace36987e --- /dev/null +++ b/internal/webhook.go @@ -0,0 +1,70 @@ +// SPDX-License-Identifier: Apache-2.0 + +package internal + +import ( + "strings" + + api "github.com/go-vela/server/api/types" + "github.com/go-vela/types/constants" + "github.com/go-vela/types/library" +) + +var ( + skipDirectiveMsg = "skip ci directive found in commit title/message" +) + +// PullRequest defines the data pulled from PRs while +// processing a webhook. +type PullRequest struct { + Comment string + Number int + IsFromFork bool + Labels []string +} + +// Webhook defines a struct that is used to return +// the required data when processing webhook event +// a for a source provider event. +type Webhook struct { + Hook *library.Hook + Repo *api.Repo + Build *library.Build + PullRequest PullRequest + Deployment *library.Deployment +} + +// ShouldSkip uses the build information +// associated with the given hook to determine +// whether the hook should be skipped. +func (w *Webhook) ShouldSkip() (bool, string) { + // push or tag event + if strings.EqualFold(constants.EventPush, w.Build.GetEvent()) || strings.EqualFold(constants.EventTag, w.Build.GetEvent()) { + // check for skip ci directive in message or title + if hasSkipDirective(w.Build.GetMessage()) || + hasSkipDirective(w.Build.GetTitle()) { + return true, skipDirectiveMsg + } + } + + return false, "" +} + +// hasSkipDirective is a small helper function +// to check a string for a number of patterns +// that signal to vela that the hook should +// be skipped from processing. +func hasSkipDirective(s string) bool { + sl := strings.ToLower(s) + + switch { + case strings.Contains(sl, "[skip ci]"), + strings.Contains(sl, "[ci skip]"), + strings.Contains(sl, "[skip vela]"), + strings.Contains(sl, "[vela skip]"), + strings.Contains(sl, "***no_ci***"): + return true + default: + return false + } +} diff --git a/internal/webhook_test.go b/internal/webhook_test.go new file mode 100644 index 000000000..6b072090b --- /dev/null +++ b/internal/webhook_test.go @@ -0,0 +1,111 @@ +// SPDX-License-Identifier: Apache-2.0 + +package internal + +import ( + "testing" + + "github.com/go-vela/types/constants" + "github.com/go-vela/types/library" +) + +func TestWebhook_ShouldSkip(t *testing.T) { + // set up tests + tests := []struct { + hook *Webhook + wantBool bool + wantString string + }{ + { + &Webhook{Build: testPushBuild("testing [SKIP CI]", "", constants.EventPush)}, + true, + skipDirectiveMsg, + }, + { + &Webhook{Build: testPushBuild("testing", "wip [ci skip]", constants.EventPush)}, + true, + skipDirectiveMsg, + }, + { + &Webhook{Build: testPushBuild("testing [skip VELA]", "", constants.EventPush)}, + true, + skipDirectiveMsg, + }, + { + &Webhook{Build: testPushBuild("testing", "wip [vela skip]", constants.EventPush)}, + true, + skipDirectiveMsg, + }, + { + &Webhook{Build: testPushBuild("testing ***NO_CI*** ok", "nothing", constants.EventPush)}, + true, + skipDirectiveMsg, + }, + { + &Webhook{Build: testPushBuild("testing ok", "nothing", constants.EventPush)}, + false, + "", + }, + { + &Webhook{Build: testPushBuild("testing [SKIP CI]", "", constants.EventTag)}, + true, + skipDirectiveMsg, + }, + { + &Webhook{Build: testPushBuild("testing", "wip [ci skip]", constants.EventTag)}, + true, + skipDirectiveMsg, + }, + { + &Webhook{Build: testPushBuild("testing [skip VELA]", "", constants.EventTag)}, + true, + skipDirectiveMsg, + }, + { + &Webhook{Build: testPushBuild("testing", "wip [vela skip]", constants.EventTag)}, + true, + skipDirectiveMsg, + }, + { + &Webhook{Build: testPushBuild("testing ***NO_CI*** ok", "nothing", constants.EventTag)}, + true, + skipDirectiveMsg, + }, + { + &Webhook{Build: testPushBuild("testing ok", "nothing", constants.EventTag)}, + false, + "", + }, + } + + // run tests + for _, test := range tests { + gotBool, gotString := test.hook.ShouldSkip() + + if gotString != test.wantString { + t.Errorf("returned an error, wanted %s, but got %s", test.wantString, gotString) + } + + if gotBool != test.wantBool { + t.Errorf("returned an error, wanted %v, but got %v", test.wantBool, gotBool) + } + } +} + +func testPushBuild(message, title, event string) *library.Build { + b := new(library.Build) + + b.SetEvent(event) + + if len(message) > 0 { + b.SetMessage(message) + } + + if len(title) > 0 { + b.SetTitle(title) + } + + b.SetCommit("deadbeef") + + return b +} diff --git a/mock/server/repo.go b/mock/server/repo.go index 88b4d4715..79b82b7df 100644 --- a/mock/server/repo.go +++ b/mock/server/repo.go @@ -9,15 +9,21 @@ import ( "strings" "github.com/gin-gonic/gin" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types" - "github.com/go-vela/types/library" ) const ( // RepoResp represents a JSON return for a single repo. RepoResp = `{ "id": 1, - "user_id": 1, + "owner": { + "id": 1, + "name": "octocat", + "favorites": [], + "active": true, + "admin": false + }, "org": "github", "counter": 10, "name": "octocat", @@ -97,7 +103,7 @@ const ( func getRepos(c *gin.Context) { data := []byte(ReposResp) - var body []library.Repo + var body []api.Repo _ = json.Unmarshal(data, &body) c.JSON(http.StatusOK, body) @@ -119,7 +125,7 @@ func getRepo(c *gin.Context) { data := []byte(RepoResp) - var body library.Repo + var body api.Repo _ = json.Unmarshal(data, &body) c.JSON(http.StatusOK, body) @@ -129,7 +135,7 @@ func getRepo(c *gin.Context) { func addRepo(c *gin.Context) { data := []byte(RepoResp) - var body library.Repo + var body api.Repo _ = json.Unmarshal(data, &body) c.JSON(http.StatusCreated, body) @@ -153,7 +159,7 @@ func updateRepo(c *gin.Context) { data := []byte(RepoResp) - var body library.Repo + var body api.Repo _ = json.Unmarshal(data, &body) c.JSON(http.StatusOK, body) diff --git a/mock/server/repo_test.go b/mock/server/repo_test.go index 1e59ba022..b6b408a8f 100644 --- a/mock/server/repo_test.go +++ b/mock/server/repo_test.go @@ -7,11 +7,11 @@ import ( "reflect" "testing" - "github.com/go-vela/types/library" + api "github.com/go-vela/server/api/types" ) func TestRepo_ActiveRepoResp(t *testing.T) { - testRepo := library.Repo{} + testRepo := api.Repo{} err := json.Unmarshal([]byte(RepoResp), &testRepo) if err != nil { diff --git a/queue/models/item.go b/queue/models/item.go new file mode 100644 index 000000000..f0a5f4fa3 --- /dev/null +++ b/queue/models/item.go @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: Apache-2.0 + +package models + +import ( + api "github.com/go-vela/server/api/types" + "github.com/go-vela/types/library" +) + +// ItemVersion allows the worker to detect items that were queued before an Vela server +// upgrade or downgrade, so it can handle such stale data gracefully. +// For example, the worker could fail a stale build or ask the server to recompile it. +// This is not a public API and is unrelated to the version key in pipeline yaml. +const ItemVersion uint64 = 3 + +// Item is the queue representation of an item to publish to the queue. +type Item struct { + Build *library.Build `json:"build"` + Repo *api.Repo `json:"repo"` + // The 0-value is the implicit ItemVersion for queued Items that pre-date adding the field. + ItemVersion uint64 `json:"item_version"` +} + +// ToItem creates a queue item from a build, repo and user. +func ToItem(b *library.Build, r *api.Repo) *Item { + return &Item{ + Build: b, + Repo: r, + ItemVersion: ItemVersion, + } +} diff --git a/queue/models/item_test.go b/queue/models/item_test.go new file mode 100644 index 000000000..f6d34e19f --- /dev/null +++ b/queue/models/item_test.go @@ -0,0 +1,123 @@ +// SPDX-License-Identifier: Apache-2.0 + +package models + +import ( + "reflect" + "testing" + + api "github.com/go-vela/server/api/types" + "github.com/go-vela/types/library" +) + +func TestTypes_ToItem(t *testing.T) { + // setup types + booL := false + num := 1 + num64 := int64(num) + str := "foo" + e := new(api.Events) + + b := &library.Build{ + ID: &num64, + RepoID: &num64, + Number: &num, + Parent: &num, + Event: &str, + Status: &str, + Error: &str, + Enqueued: &num64, + Created: &num64, + Started: &num64, + Finished: &num64, + Deploy: &str, + Clone: &str, + Source: &str, + Title: &str, + Message: &str, + Commit: &str, + Sender: &str, + Author: &str, + Branch: &str, + Ref: &str, + BaseRef: &str, + } + r := &api.Repo{ + ID: &num64, + Owner: &library.User{ + ID: &num64, + Name: &str, + Token: &str, + Active: &booL, + Admin: &booL, + }, + Org: &str, + Name: &str, + FullName: &str, + Link: &str, + Clone: &str, + Branch: &str, + Timeout: &num64, + Visibility: &str, + Private: &booL, + Trusted: &booL, + Active: &booL, + AllowEvents: e, + } + want := &Item{ + Build: &library.Build{ + ID: &num64, + RepoID: &num64, + Number: &num, + Parent: &num, + Event: &str, + Status: &str, + Error: &str, + Enqueued: &num64, + Created: &num64, + Started: &num64, + Finished: &num64, + Deploy: &str, + Clone: &str, + Source: &str, + Title: &str, + Message: &str, + Commit: &str, + Sender: &str, + Author: &str, + Branch: &str, + Ref: &str, + BaseRef: &str, + }, + Repo: &api.Repo{ + ID: &num64, + Owner: &library.User{ + ID: &num64, + Name: &str, + Token: &str, + Active: &booL, + Admin: &booL, + }, + Org: &str, + Name: &str, + FullName: &str, + Link: &str, + Clone: &str, + Branch: &str, + Timeout: &num64, + Visibility: &str, + Private: &booL, + Trusted: &booL, + Active: &booL, + AllowEvents: e, + }, + ItemVersion: ItemVersion, + } + + // run test + got := ToItem(b, r) + + if !reflect.DeepEqual(got, want) { + t.Errorf("ToItem is %v, want %v", got, want) + } +} diff --git a/queue/redis/length_test.go b/queue/redis/length_test.go index 0049b3809..3f27ce60f 100644 --- a/queue/redis/length_test.go +++ b/queue/redis/length_test.go @@ -7,17 +7,16 @@ import ( "reflect" "testing" - "github.com/go-vela/types" + "github.com/go-vela/server/queue/models" "gopkg.in/square/go-jose.v2/json" ) func TestRedis_Length(t *testing.T) { // setup types // use global variables in redis_test.go - _item := &types.Item{ + _item := &models.Item{ Build: _build, Repo: _repo, - User: _user, } // setup queue item diff --git a/queue/redis/pop.go b/queue/redis/pop.go index 250d0c915..bd5872c9a 100644 --- a/queue/redis/pop.go +++ b/queue/redis/pop.go @@ -7,13 +7,13 @@ import ( "encoding/json" "errors" - "github.com/go-vela/types" + "github.com/go-vela/server/queue/models" "github.com/redis/go-redis/v9" "golang.org/x/crypto/nacl/sign" ) // Pop grabs an item from the specified channel off the queue. -func (c *client) Pop(ctx context.Context, routes []string) (*types.Item, error) { +func (c *client) Pop(ctx context.Context, routes []string) (*models.Item, error) { c.Logger.Tracef("popping item from queue %s", c.config.Channels) // define channels to pop from @@ -58,7 +58,7 @@ func (c *client) Pop(ctx context.Context, routes []string) (*types.Item, error) } // unmarshal result into queue item - item := new(types.Item) + item := new(models.Item) err = json.Unmarshal(opened, item) if err != nil { diff --git a/queue/redis/pop_test.go b/queue/redis/pop_test.go index eb05030bd..e95f828ab 100644 --- a/queue/redis/pop_test.go +++ b/queue/redis/pop_test.go @@ -4,11 +4,11 @@ package redis import ( "context" - "reflect" "testing" "time" - "github.com/go-vela/types" + "github.com/go-vela/server/queue/models" + "github.com/google/go-cmp/cmp" "golang.org/x/crypto/nacl/sign" "gopkg.in/square/go-jose.v2/json" ) @@ -16,10 +16,9 @@ import ( func TestRedis_Pop(t *testing.T) { // setup types // use global variables in redis_test.go - _item := &types.Item{ + _item := &models.Item{ Build: _build, Repo: _repo, - User: _user, } var signed []byte @@ -79,7 +78,7 @@ func TestRedis_Pop(t *testing.T) { tests := []struct { failure bool redis *client - want *types.Item + want *models.Item channels []string }{ { @@ -121,8 +120,8 @@ func TestRedis_Pop(t *testing.T) { t.Errorf("Pop returned err: %v", err) } - if !reflect.DeepEqual(got, test.want) { - t.Errorf("Pop is %v, want %v", got, test.want) + if diff := cmp.Diff(test.want, got); diff != "" { + t.Errorf("Pop() mismatch (-want +got):\n%s", diff) } } } diff --git a/queue/redis/push_test.go b/queue/redis/push_test.go index 5db7e1510..75acc317e 100644 --- a/queue/redis/push_test.go +++ b/queue/redis/push_test.go @@ -7,16 +7,15 @@ import ( "encoding/json" "testing" - "github.com/go-vela/types" + "github.com/go-vela/server/queue/models" ) func TestRedis_Push(t *testing.T) { // setup types // use global variables in redis_test.go - _item := &types.Item{ + _item := &models.Item{ Build: _build, Repo: _repo, - User: _user, } // setup queue item diff --git a/queue/redis/redis_test.go b/queue/redis/redis_test.go index 3f632b129..ee4a35a9b 100644 --- a/queue/redis/redis_test.go +++ b/queue/redis/redis_test.go @@ -8,8 +8,8 @@ import ( "time" "github.com/alicebob/miniredis/v2" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/library" - "github.com/go-vela/types/pipeline" ) // The following functions were taken from @@ -73,8 +73,16 @@ var ( Distribution: String("linux"), } - _repo = &library.Repo{ - ID: Int64(1), + _repo = &api.Repo{ + ID: Int64(1), + Owner: &library.User{ + ID: Int64(1), + Name: String("octocat"), + Token: nil, + Hash: nil, + Active: Bool(true), + Admin: Bool(false), + }, Org: String("github"), Name: String("octocat"), FullName: String("github/octocat"), @@ -87,62 +95,6 @@ var ( Trusted: Bool(false), Active: Bool(true), } - - _steps = &pipeline.Build{ - Version: "1", - ID: "github_octocat_1", - Services: pipeline.ContainerSlice{ - { - ID: "service_github_octocat_1_postgres", - Directory: "/home/github/octocat", - Environment: map[string]string{"FOO": "bar"}, - Image: "postgres:12-alpine", - Name: "postgres", - Number: 1, - Ports: []string{"5432:5432"}, - Pull: "not_present", - }, - }, - Steps: pipeline.ContainerSlice{ - { - ID: "step_github_octocat_1_init", - Directory: "/home/github/octocat", - Environment: map[string]string{"FOO": "bar"}, - Image: "#init", - Name: "init", - Number: 1, - Pull: "always", - }, - { - ID: "step_github_octocat_1_clone", - Directory: "/home/github/octocat", - Environment: map[string]string{"FOO": "bar"}, - Image: "target/vela-git:v0.5.1", - Name: "clone", - Number: 2, - Pull: "always", - }, - { - ID: "step_github_octocat_1_echo", - Commands: []string{"echo hello"}, - Directory: "/home/github/octocat", - Environment: map[string]string{"FOO": "bar"}, - Image: "alpine:latest", - Name: "echo", - Number: 3, - Pull: "always", - }, - }, - } - - _user = &library.User{ - ID: Int64(1), - Name: String("octocat"), - Token: nil, - Hash: nil, - Active: Bool(true), - Admin: Bool(false), - } ) func TestRedis_New(t *testing.T) { diff --git a/queue/service.go b/queue/service.go index fd36a64bc..3fdf27515 100644 --- a/queue/service.go +++ b/queue/service.go @@ -5,7 +5,7 @@ package queue import ( "context" - "github.com/go-vela/types" + "github.com/go-vela/server/queue/models" "github.com/go-vela/types/pipeline" ) @@ -24,7 +24,7 @@ type Service interface { // Pop defines a function that grabs an // item off the queue. - Pop(context.Context, []string) (*types.Item, error) + Pop(context.Context, []string) (*models.Item, error) // Push defines a function that publishes an // item to the specified route in the queue. diff --git a/router/middleware/build/build_test.go b/router/middleware/build/build_test.go index f86353d3a..b3be6b0c3 100644 --- a/router/middleware/build/build_test.go +++ b/router/middleware/build/build_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/gin-gonic/gin" + api "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" @@ -37,9 +38,12 @@ func TestBuild_Retrieve(t *testing.T) { func TestBuild_Establish(t *testing.T) { // setup types - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -158,9 +162,12 @@ func TestBuild_Establish_NoRepo(t *testing.T) { func TestBuild_Establish_NoBuildParameter(t *testing.T) { // setup types - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -206,9 +213,12 @@ func TestBuild_Establish_NoBuildParameter(t *testing.T) { func TestBuild_Establish_InvalidBuildParameter(t *testing.T) { // setup types - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -254,9 +264,9 @@ func TestBuild_Establish_InvalidBuildParameter(t *testing.T) { func TestBuild_Establish_NoBuild(t *testing.T) { // setup types - r := new(library.Repo) + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.GetOwner().SetID(1) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") diff --git a/router/middleware/executors/context.go b/router/middleware/executors/context.go index d040fa73a..8fcd5cfa0 100644 --- a/router/middleware/executors/context.go +++ b/router/middleware/executors/context.go @@ -5,7 +5,7 @@ package executors import ( "context" - "github.com/go-vela/types/library" + api "github.com/go-vela/server/api/types" ) const key = "executors" @@ -16,13 +16,13 @@ type Setter interface { } // FromContext returns the executors associated with this context. -func FromContext(c context.Context) []library.Executor { +func FromContext(c context.Context) []api.Executor { value := c.Value(key) if value == nil { return nil } - e, ok := value.([]library.Executor) + e, ok := value.([]api.Executor) if !ok { return nil } @@ -32,6 +32,6 @@ func FromContext(c context.Context) []library.Executor { // ToContext adds the executor to this context if it supports // the Setter interface. -func ToContext(c Setter, e []library.Executor) { +func ToContext(c Setter, e []api.Executor) { c.Set(key, e) } diff --git a/router/middleware/executors/context_test.go b/router/middleware/executors/context_test.go index 3cabdb687..f7e080d61 100644 --- a/router/middleware/executors/context_test.go +++ b/router/middleware/executors/context_test.go @@ -6,16 +6,15 @@ import ( "reflect" "testing" - "github.com/go-vela/types/library" - "github.com/gin-gonic/gin" + api "github.com/go-vela/server/api/types" ) func TestExecutors_FromContext(t *testing.T) { // setup types eID := int64(1) - e := library.Executor{ID: &eID} - want := []library.Executor{e} + e := api.Executor{ID: &eID} + want := []api.Executor{e} // setup context gin.SetMode(gin.TestMode) @@ -74,8 +73,8 @@ func TestExecutors_FromContext_Empty(t *testing.T) { func TestExecutors_ToContext(t *testing.T) { // setup types eID := int64(1) - e := library.Executor{ID: &eID} - want := []library.Executor{e} + e := api.Executor{ID: &eID} + want := []api.Executor{e} // setup context gin.SetMode(gin.TestMode) diff --git a/router/middleware/executors/executor_test.go b/router/middleware/executors/executor_test.go index 66006b6e2..423336a10 100644 --- a/router/middleware/executors/executor_test.go +++ b/router/middleware/executors/executor_test.go @@ -6,16 +6,15 @@ import ( "reflect" "testing" - "github.com/go-vela/types/library" - "github.com/gin-gonic/gin" + api "github.com/go-vela/server/api/types" ) func TestExecutors_Retrieve(t *testing.T) { // setup types eID := int64(1) - e := library.Executor{ID: &eID} - want := []library.Executor{e} + e := api.Executor{ID: &eID} + want := []api.Executor{e} // setup context gin.SetMode(gin.TestMode) diff --git a/router/middleware/executors/executors.go b/router/middleware/executors/executors.go index 688434f07..8ea9de2c1 100644 --- a/router/middleware/executors/executors.go +++ b/router/middleware/executors/executors.go @@ -12,23 +12,23 @@ import ( "time" "github.com/gin-gonic/gin" + api "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" "github.com/go-vela/server/internal/token" "github.com/go-vela/server/router/middleware/build" "github.com/go-vela/server/util" "github.com/go-vela/types/constants" - "github.com/go-vela/types/library" ) // Retrieve gets the executors in the given context. -func Retrieve(c *gin.Context) []library.Executor { +func Retrieve(c *gin.Context) []api.Executor { return FromContext(c) } // Establish sets the executors in the given context. func Establish() gin.HandlerFunc { return func(c *gin.Context) { - e := new([]library.Executor) + e := new([]api.Executor) b := build.Retrieve(c) ctx := c.Request.Context() diff --git a/router/middleware/header.go b/router/middleware/header.go index f04180bfd..980243f16 100644 --- a/router/middleware/header.go +++ b/router/middleware/header.go @@ -7,8 +7,8 @@ import ( "time" "github.com/gin-gonic/gin" + "github.com/go-vela/server/internal" "github.com/go-vela/server/version" - "github.com/go-vela/types" ) // NoCache is a middleware function that appends headers @@ -24,7 +24,7 @@ func NoCache(c *gin.Context) { // for OPTIONS preflight requests and aborts then exits // the middleware chain and ends the request. func Options(c *gin.Context) { - m := c.MustGet("metadata").(*types.Metadata) + m := c.MustGet("metadata").(*internal.Metadata) if c.Request.Method != "OPTIONS" { c.Next() @@ -58,7 +58,7 @@ func Secure(c *gin.Context) { // CORS related requests. These are attached to actual requests // unlike the OPTIONS preflight requests. func Cors(c *gin.Context) { - m := c.MustGet("metadata").(*types.Metadata) + m := c.MustGet("metadata").(*internal.Metadata) c.Header("Access-Control-Allow-Origin", "*") diff --git a/router/middleware/header_test.go b/router/middleware/header_test.go index ab13f992a..dfa18e063 100644 --- a/router/middleware/header_test.go +++ b/router/middleware/header_test.go @@ -11,7 +11,7 @@ import ( "time" "github.com/gin-gonic/gin" - "github.com/go-vela/types" + "github.com/go-vela/server/internal" ) func TestMiddleware_NoCache(t *testing.T) { @@ -64,8 +64,8 @@ func TestMiddleware_Options(t *testing.T) { wantHeaders := "authorization, origin, content-type, accept" wantAllow := "HEAD,GET,POST,PUT,PATCH,DELETE,OPTIONS" wantContentType := "application/json" - m := &types.Metadata{ - Vela: &types.Vela{ + m := &internal.Metadata{ + Vela: &internal.Vela{ Address: "http://localhost:8080", }, } @@ -120,8 +120,8 @@ func TestMiddleware_Options(t *testing.T) { func TestMiddleware_Options_InvalidMethod(t *testing.T) { // setup types - m := &types.Metadata{ - Vela: &types.Vela{ + m := &internal.Metadata{ + Vela: &internal.Vela{ Address: "http://localhost:8080", }, } @@ -178,8 +178,8 @@ func TestMiddleware_Cors(t *testing.T) { // setup types wantOrigin := "*" wantExposeHeaders := "link, x-total-count" - m := &types.Metadata{ - Vela: &types.Vela{ + m := &internal.Metadata{ + Vela: &internal.Vela{ Address: "http://localhost:8080", }, } diff --git a/router/middleware/logger_test.go b/router/middleware/logger_test.go index bfc084437..18677ae7c 100644 --- a/router/middleware/logger_test.go +++ b/router/middleware/logger_test.go @@ -33,9 +33,9 @@ func TestMiddleware_Logger(t *testing.T) { b.SetRepoID(1) b.SetNumber(1) - r := new(library.Repo) + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.GetOwner().SetID(1) r.SetOrg("foo") r.SetName("bar") r.SetFullName("foo/bar") @@ -162,9 +162,9 @@ func TestMiddleware_Logger_Error(t *testing.T) { func TestMiddleware_Logger_Sanitize(t *testing.T) { var logBody, logWant map[string]interface{} - r := new(library.Repo) + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.GetOwner().SetID(1) r.SetOrg("foo") r.SetName("bar") r.SetFullName("foo/bar") diff --git a/router/middleware/metadata.go b/router/middleware/metadata.go index 67ed62157..672491d1d 100644 --- a/router/middleware/metadata.go +++ b/router/middleware/metadata.go @@ -4,13 +4,12 @@ package middleware import ( "github.com/gin-gonic/gin" - - "github.com/go-vela/types" + "github.com/go-vela/server/internal" ) // Metadata is a middleware function that attaches the metadata // to the context of every http.Request. -func Metadata(m *types.Metadata) gin.HandlerFunc { +func Metadata(m *internal.Metadata) gin.HandlerFunc { return func(c *gin.Context) { c.Set("metadata", m) c.Next() diff --git a/router/middleware/metadata_test.go b/router/middleware/metadata_test.go index 0baaee36a..8e955805b 100644 --- a/router/middleware/metadata_test.go +++ b/router/middleware/metadata_test.go @@ -8,15 +8,14 @@ import ( "reflect" "testing" - "github.com/go-vela/types" - "github.com/gin-gonic/gin" + "github.com/go-vela/server/internal" ) func TestMiddleware_Metadata(t *testing.T) { // setup types - got := new(types.Metadata) - want := &types.Metadata{} + got := new(internal.Metadata) + want := &internal.Metadata{} // setup context gin.SetMode(gin.TestMode) @@ -28,7 +27,7 @@ func TestMiddleware_Metadata(t *testing.T) { // setup mock server engine.Use(Metadata(want)) engine.GET("/health", func(c *gin.Context) { - got = c.Value("metadata").(*types.Metadata) + got = c.Value("metadata").(*internal.Metadata) c.Status(http.StatusOK) }) diff --git a/router/middleware/org/org_test.go b/router/middleware/org/org_test.go index 8b217f5f8..a54d9ad33 100644 --- a/router/middleware/org/org_test.go +++ b/router/middleware/org/org_test.go @@ -10,8 +10,8 @@ import ( "testing" "github.com/gin-gonic/gin" + api "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" - "github.com/go-vela/types/library" ) func TestOrg_Retrieve(t *testing.T) { @@ -33,9 +33,9 @@ func TestOrg_Retrieve(t *testing.T) { func TestOrg_Establish(t *testing.T) { // setup types - r := new(library.Repo) + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.GetOwner().SetID(1) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") diff --git a/router/middleware/perm/perm.go b/router/middleware/perm/perm.go index a8656a261..3cac40571 100644 --- a/router/middleware/perm/perm.go +++ b/router/middleware/perm/perm.go @@ -8,7 +8,6 @@ import ( "strings" "github.com/gin-gonic/gin" - "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" "github.com/go-vela/server/router/middleware/claims" "github.com/go-vela/server/router/middleware/org" @@ -371,16 +370,7 @@ func MustAdmin() gin.HandlerFunc { // try again using the repo owner token // // https://docs.github.com/en/rest/reference/repos#get-repository-permissions-for-a-user - ro, err := database.FromContext(c).GetUser(ctx, r.GetUserID()) - if err != nil { - retErr := fmt.Errorf("unable to get owner for %s: %w", r.GetFullName(), err) - - util.HandleError(c, http.StatusBadRequest, retErr) - - return - } - - perm, err = scm.FromContext(c).RepoAccess(ctx, u.GetName(), ro.GetToken(), r.GetOrg(), r.GetName()) + perm, err = scm.FromContext(c).RepoAccess(ctx, u.GetName(), r.GetOwner().GetToken(), r.GetOrg(), r.GetName()) if err != nil { logger.Errorf("unable to get user %s access level for repo %s", u.GetName(), r.GetFullName()) } @@ -430,16 +420,7 @@ func MustWrite() gin.HandlerFunc { // try again using the repo owner token // // https://docs.github.com/en/rest/reference/repos#get-repository-permissions-for-a-user - ro, err := database.FromContext(c).GetUser(ctx, r.GetUserID()) - if err != nil { - retErr := fmt.Errorf("unable to get owner for %s: %w", r.GetFullName(), err) - - util.HandleError(c, http.StatusBadRequest, retErr) - - return - } - - perm, err = scm.FromContext(c).RepoAccess(ctx, u.GetName(), ro.GetToken(), r.GetOrg(), r.GetName()) + perm, err = scm.FromContext(c).RepoAccess(ctx, u.GetName(), r.GetOwner().GetToken(), r.GetOrg(), r.GetName()) if err != nil { logger.Errorf("unable to get user %s access level for repo %s", u.GetName(), r.GetFullName()) } @@ -513,16 +494,7 @@ func MustRead() gin.HandlerFunc { // try again using the repo owner token // // https://docs.github.com/en/rest/reference/repos#get-repository-permissions-for-a-user - ro, err := database.FromContext(c).GetUser(ctx, r.GetUserID()) - if err != nil { - retErr := fmt.Errorf("unable to get owner for %s: %w", r.GetFullName(), err) - - util.HandleError(c, http.StatusBadRequest, retErr) - - return - } - - perm, err = scm.FromContext(c).RepoAccess(ctx, u.GetName(), ro.GetToken(), r.GetOrg(), r.GetName()) + perm, err = scm.FromContext(c).RepoAccess(ctx, u.GetName(), r.GetOwner().GetToken(), r.GetOrg(), r.GetName()) if err != nil { logger.Errorf("unable to get user %s access level for repo %s", u.GetName(), r.GetFullName()) } diff --git a/router/middleware/perm/perm_test.go b/router/middleware/perm/perm_test.go index c2c9642e4..0628457e1 100644 --- a/router/middleware/perm/perm_test.go +++ b/router/middleware/perm/perm_test.go @@ -11,6 +11,7 @@ import ( "time" "github.com/gin-gonic/gin" + api "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" "github.com/go-vela/server/internal/token" "github.com/go-vela/server/router/middleware/build" @@ -397,9 +398,12 @@ func TestPerm_MustBuildAccess(t *testing.T) { // setup types secret := "superSecret" - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -483,9 +487,12 @@ func TestPerm_MustBuildAccess_PlatAdmin(t *testing.T) { // setup types secret := "superSecret" - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -576,9 +583,12 @@ func TestPerm_MustBuildToken_WrongBuild(t *testing.T) { // setup types secret := "superSecret" - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -662,9 +672,12 @@ func TestPerm_MustSecretAdmin_BuildToken_Repo(t *testing.T) { // setup types secret := "superSecret" - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -745,9 +758,12 @@ func TestPerm_MustSecretAdmin_BuildToken_Org(t *testing.T) { // setup types secret := "superSecret" - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -828,9 +844,12 @@ func TestPerm_MustSecretAdmin_BuildToken_Shared(t *testing.T) { // setup types secret := "superSecret" - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -918,9 +937,12 @@ func TestPerm_MustAdmin(t *testing.T) { UserRefreshTokenDuration: time.Minute * 30, } - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -1016,9 +1038,12 @@ func TestPerm_MustAdmin_PlatAdmin(t *testing.T) { UserRefreshTokenDuration: time.Minute * 30, } - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -1114,9 +1139,12 @@ func TestPerm_MustAdmin_NotAdmin(t *testing.T) { UserRefreshTokenDuration: time.Minute * 30, } - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -1212,9 +1240,12 @@ func TestPerm_MustWrite(t *testing.T) { UserRefreshTokenDuration: time.Minute * 30, } - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -1310,9 +1341,12 @@ func TestPerm_MustWrite_PlatAdmin(t *testing.T) { UserRefreshTokenDuration: time.Minute * 30, } - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -1408,9 +1442,12 @@ func TestPerm_MustWrite_RepoAdmin(t *testing.T) { UserRefreshTokenDuration: time.Minute * 30, } - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -1506,9 +1543,12 @@ func TestPerm_MustWrite_NotWrite(t *testing.T) { UserRefreshTokenDuration: time.Minute * 30, } - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -1604,9 +1644,12 @@ func TestPerm_MustRead(t *testing.T) { UserRefreshTokenDuration: time.Minute * 30, } - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -1702,9 +1745,12 @@ func TestPerm_MustRead_PlatAdmin(t *testing.T) { UserRefreshTokenDuration: time.Minute * 30, } - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -1800,9 +1846,12 @@ func TestPerm_MustRead_WorkerBuildToken(t *testing.T) { UserRefreshTokenDuration: time.Minute * 30, } - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -1886,9 +1935,12 @@ func TestPerm_MustRead_RepoAdmin(t *testing.T) { UserRefreshTokenDuration: time.Minute * 30, } - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -1984,9 +2036,12 @@ func TestPerm_MustRead_RepoWrite(t *testing.T) { UserRefreshTokenDuration: time.Minute * 30, } - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -2082,9 +2137,12 @@ func TestPerm_MustRead_RepoPublic(t *testing.T) { UserRefreshTokenDuration: time.Minute * 30, } - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -2180,9 +2238,12 @@ func TestPerm_MustRead_NotRead(t *testing.T) { UserRefreshTokenDuration: time.Minute * 30, } - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") diff --git a/router/middleware/pipeline/pipeline.go b/router/middleware/pipeline/pipeline.go index 61f308854..b8f3b819a 100644 --- a/router/middleware/pipeline/pipeline.go +++ b/router/middleware/pipeline/pipeline.go @@ -9,12 +9,12 @@ import ( "github.com/gin-gonic/gin" "github.com/go-vela/server/compiler" "github.com/go-vela/server/database" + "github.com/go-vela/server/internal" "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/scm" "github.com/go-vela/server/util" - "github.com/go-vela/types" "github.com/go-vela/types/library" "github.com/sirupsen/logrus" ) @@ -77,7 +77,7 @@ func Establish() gin.HandlerFunc { _, pipeline, err = compiler.FromContext(c). Duplicate(). WithCommit(p). - WithMetadata(c.MustGet("metadata").(*types.Metadata)). + WithMetadata(c.MustGet("metadata").(*internal.Metadata)). WithRepo(r). WithUser(u). Compile(config) diff --git a/router/middleware/pipeline/pipeline_test.go b/router/middleware/pipeline/pipeline_test.go index 545edb338..552a40989 100644 --- a/router/middleware/pipeline/pipeline_test.go +++ b/router/middleware/pipeline/pipeline_test.go @@ -13,9 +13,11 @@ import ( "time" "github.com/gin-gonic/gin" + api "github.com/go-vela/server/api/types" "github.com/go-vela/server/compiler" "github.com/go-vela/server/compiler/native" "github.com/go-vela/server/database" + "github.com/go-vela/server/internal" "github.com/go-vela/server/internal/token" "github.com/go-vela/server/router/middleware/claims" "github.com/go-vela/server/router/middleware/org" @@ -23,7 +25,6 @@ import ( "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/scm" "github.com/go-vela/server/scm/github" - "github.com/go-vela/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/library" "github.com/golang-jwt/jwt/v5" @@ -66,9 +67,12 @@ func TestPipeline_Retrieve(t *testing.T) { func TestPipeline_Establish(t *testing.T) { // setup types - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -168,9 +172,12 @@ func TestPipeline_Establish_NoRepo(t *testing.T) { func TestPipeline_Establish_NoPipelineParameter(t *testing.T) { // setup types - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -225,9 +232,12 @@ func TestPipeline_Establish_NoPipeline(t *testing.T) { UserRefreshTokenDuration: time.Minute * 30, } - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -241,21 +251,21 @@ func TestPipeline_Establish_NoPipeline(t *testing.T) { u.SetHash("baz") u.SetAdmin(true) - m := &types.Metadata{ - Database: &types.Database{ + m := &internal.Metadata{ + Database: &internal.Database{ Driver: "foo", Host: "foo", }, - Queue: &types.Queue{ + Queue: &internal.Queue{ Channel: "foo", Driver: "foo", Host: "foo", }, - Source: &types.Source{ + Source: &internal.Source{ Driver: "foo", Host: "foo", }, - Vela: &types.Vela{ + Vela: &internal.Vela{ Address: "foo", WebAddress: "foo", }, diff --git a/router/middleware/repo/context.go b/router/middleware/repo/context.go index 620aaf0c8..73c0db4f1 100644 --- a/router/middleware/repo/context.go +++ b/router/middleware/repo/context.go @@ -5,7 +5,7 @@ package repo import ( "context" - "github.com/go-vela/types/library" + api "github.com/go-vela/server/api/types" ) const key = "repo" @@ -16,13 +16,13 @@ type Setter interface { } // FromContext returns the Repo associated with this context. -func FromContext(c context.Context) *library.Repo { +func FromContext(c context.Context) *api.Repo { value := c.Value(key) if value == nil { return nil } - r, ok := value.(*library.Repo) + r, ok := value.(*api.Repo) if !ok { return nil } @@ -32,6 +32,6 @@ func FromContext(c context.Context) *library.Repo { // ToContext adds the Repo to this context if it supports // the Setter interface. -func ToContext(c Setter, r *library.Repo) { +func ToContext(c Setter, r *api.Repo) { c.Set(key, r) } diff --git a/router/middleware/repo/context_test.go b/router/middleware/repo/context_test.go index ebcadf01e..c02923ab8 100644 --- a/router/middleware/repo/context_test.go +++ b/router/middleware/repo/context_test.go @@ -5,15 +5,14 @@ package repo import ( "testing" - "github.com/go-vela/types/library" - "github.com/gin-gonic/gin" + api "github.com/go-vela/server/api/types" ) func TestRepo_FromContext(t *testing.T) { // setup types num := int64(1) - want := &library.Repo{ID: &num} + want := &api.Repo{ID: &num} // setup context gin.SetMode(gin.TestMode) @@ -72,7 +71,7 @@ func TestRepo_FromContext_Empty(t *testing.T) { func TestRepo_ToContext(t *testing.T) { // setup types num := int64(1) - want := &library.Repo{ID: &num} + want := &api.Repo{ID: &num} // setup context gin.SetMode(gin.TestMode) diff --git a/router/middleware/repo/repo.go b/router/middleware/repo/repo.go index 748a17bb7..f6f51cad7 100644 --- a/router/middleware/repo/repo.go +++ b/router/middleware/repo/repo.go @@ -7,16 +7,16 @@ import ( "net/http" "github.com/gin-gonic/gin" + api "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/org" "github.com/go-vela/server/router/middleware/user" "github.com/go-vela/server/util" - "github.com/go-vela/types/library" "github.com/sirupsen/logrus" ) // Retrieve gets the repo in the given context. -func Retrieve(c *gin.Context) *library.Repo { +func Retrieve(c *gin.Context) *api.Repo { return FromContext(c) } diff --git a/router/middleware/repo/repo_test.go b/router/middleware/repo/repo_test.go index 488bcac8d..64321b409 100644 --- a/router/middleware/repo/repo_test.go +++ b/router/middleware/repo/repo_test.go @@ -9,16 +9,17 @@ import ( "reflect" "testing" + api "github.com/go-vela/server/api/types" "github.com/go-vela/server/router/middleware/org" + "github.com/go-vela/types/library" "github.com/gin-gonic/gin" "github.com/go-vela/server/database" - "github.com/go-vela/types/library" ) func TestRepo_Retrieve(t *testing.T) { // setup types - want := new(library.Repo) + want := new(api.Repo) want.SetID(1) // setup context @@ -36,9 +37,19 @@ func TestRepo_Retrieve(t *testing.T) { func TestRepo_Establish(t *testing.T) { // setup types - want := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + owner.SetName("foo") + owner.SetActive(false) + owner.SetAdmin(false) + owner.SetFavorites([]string{}) + owner.SetHash("bar") + owner.SetToken("baz") + owner.SetRefreshToken("fresh") + + want := new(api.Repo) want.SetID(1) - want.SetUserID(1) + want.SetOwner(owner) want.SetHash("baz") want.SetOrg("foo") want.SetName("bar") @@ -54,12 +65,12 @@ func TestRepo_Establish(t *testing.T) { want.SetPrivate(false) want.SetTrusted(false) want.SetActive(false) - want.SetAllowEvents(library.NewEventsFromMask(1)) + want.SetAllowEvents(api.NewEventsFromMask(1)) want.SetPipelineType("yaml") want.SetPreviousName("") want.SetApproveBuild("") - got := new(library.Repo) + got := new(api.Repo) // setup database db, err := database.NewTest() @@ -69,9 +80,11 @@ func TestRepo_Establish(t *testing.T) { defer func() { _ = db.DeleteRepo(context.TODO(), want) + _ = db.DeleteUser(context.TODO(), owner) db.Close() }() + _, _ = db.CreateUser(context.TODO(), owner) _, _ = db.CreateRepo(context.TODO(), want) // setup context diff --git a/router/middleware/service/service_test.go b/router/middleware/service/service_test.go index fba880671..612f1e55f 100644 --- a/router/middleware/service/service_test.go +++ b/router/middleware/service/service_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/gin-gonic/gin" + api "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" "github.com/go-vela/server/router/middleware/org" @@ -37,9 +38,12 @@ func TestService_Retrieve(t *testing.T) { func TestService_Establish(t *testing.T) { // setup types - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -150,9 +154,9 @@ func TestService_Establish_NoRepo(t *testing.T) { func TestService_Establish_NoBuild(t *testing.T) { // setup types - r := new(library.Repo) + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.GetOwner().SetID(1) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -198,9 +202,12 @@ func TestService_Establish_NoBuild(t *testing.T) { func TestService_Establish_NoServiceParameter(t *testing.T) { // setup types - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -254,9 +261,12 @@ func TestService_Establish_NoServiceParameter(t *testing.T) { func TestService_Establish_InvalidServiceParameter(t *testing.T) { // setup types - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -310,9 +320,9 @@ func TestService_Establish_InvalidServiceParameter(t *testing.T) { func TestService_Establish_NoService(t *testing.T) { // setup types - r := new(library.Repo) + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.GetOwner().SetID(1) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") diff --git a/router/middleware/step/step_test.go b/router/middleware/step/step_test.go index e4432a14a..ee02d4a8a 100644 --- a/router/middleware/step/step_test.go +++ b/router/middleware/step/step_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/gin-gonic/gin" + api "github.com/go-vela/server/api/types" "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" "github.com/go-vela/server/router/middleware/org" @@ -38,9 +39,12 @@ func TestStep_Retrieve(t *testing.T) { func TestStep_Establish(t *testing.T) { // setup types - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -153,9 +157,12 @@ func TestStep_Establish_NoRepo(t *testing.T) { func TestStep_Establish_NoBuild(t *testing.T) { // setup types - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -201,9 +208,12 @@ func TestStep_Establish_NoBuild(t *testing.T) { func TestStep_Establish_NoStepParameter(t *testing.T) { // setup types - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -257,9 +267,12 @@ func TestStep_Establish_NoStepParameter(t *testing.T) { func TestStep_Establish_InvalidStepParameter(t *testing.T) { // setup types - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") @@ -313,9 +326,12 @@ func TestStep_Establish_InvalidStepParameter(t *testing.T) { func TestStep_Establish_NoStep(t *testing.T) { // setup types - r := new(library.Repo) + owner := new(library.User) + owner.SetID(1) + + r := new(api.Repo) r.SetID(1) - r.SetUserID(1) + r.SetOwner(owner) r.SetHash("baz") r.SetOrg("foo") r.SetName("bar") diff --git a/scm/github/changeset.go b/scm/github/changeset.go index de97c22d4..b99a00371 100644 --- a/scm/github/changeset.go +++ b/scm/github/changeset.go @@ -8,20 +8,20 @@ import ( "github.com/sirupsen/logrus" - "github.com/go-vela/types/library" + api "github.com/go-vela/server/api/types" "github.com/google/go-github/v61/github" ) // Changeset captures the list of files changed for a commit. -func (c *client) Changeset(ctx context.Context, u *library.User, r *library.Repo, sha string) ([]string, error) { +func (c *client) Changeset(ctx context.Context, r *api.Repo, sha string) ([]string, error) { c.Logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), - "user": u.GetName(), + "user": r.GetOwner().GetName(), }).Tracef("capturing commit changeset for %s/commit/%s", r.GetFullName(), sha) // create GitHub OAuth client with user's token - client := c.newClientToken(u.GetToken()) + client := c.newClientToken(r.GetOwner().GetToken()) s := []string{} // set the max per page for the options to capture the commit @@ -42,15 +42,15 @@ func (c *client) Changeset(ctx context.Context, u *library.User, r *library.Repo } // ChangesetPR captures the list of files changed for a pull request. -func (c *client) ChangesetPR(ctx context.Context, u *library.User, r *library.Repo, number int) ([]string, error) { +func (c *client) ChangesetPR(ctx context.Context, r *api.Repo, number int) ([]string, error) { c.Logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), - "user": u.GetName(), + "user": r.GetOwner().GetName(), }).Tracef("capturing pull request changeset for %s/pull/%d", r.GetFullName(), number) // create GitHub OAuth client with user's token - client := c.newClientToken(u.GetToken()) + client := c.newClientToken(r.GetOwner().GetToken()) s := []string{} f := []*github.CommitFile{} diff --git a/scm/github/changeset_test.go b/scm/github/changeset_test.go index 2fefbad93..713f08f9a 100644 --- a/scm/github/changeset_test.go +++ b/scm/github/changeset_test.go @@ -11,6 +11,7 @@ import ( "github.com/gin-gonic/gin" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/library" ) @@ -38,14 +39,15 @@ func TestGithub_Changeset(t *testing.T) { u.SetName("foo") u.SetToken("bar") - r := new(library.Repo) + r := new(api.Repo) r.SetOrg("repos") r.SetName("octocat") + r.SetOwner(u) client, _ := NewTest(s.URL) // run test - got, err := client.Changeset(context.TODO(), u, r, "6dcb09b5b57875f334f61aebed695e2e4193db5e") + got, err := client.Changeset(context.TODO(), r, "6dcb09b5b57875f334f61aebed695e2e4193db5e") if resp.Code != http.StatusOK { t.Errorf("Changeset returned %v, want %v", resp.Code, http.StatusOK) @@ -84,14 +86,15 @@ func TestGithub_ChangesetPR(t *testing.T) { u.SetName("foo") u.SetToken("bar") - r := new(library.Repo) + r := new(api.Repo) r.SetOrg("repos") r.SetName("octocat") + r.SetOwner(u) client, _ := NewTest(s.URL) // run test - got, err := client.ChangesetPR(context.TODO(), u, r, 1) + got, err := client.ChangesetPR(context.TODO(), r, 1) if resp.Code != http.StatusOK { t.Errorf("ChangesetPR returned %v, want %v", resp.Code, http.StatusOK) diff --git a/scm/github/deployment.go b/scm/github/deployment.go index 835ad851a..ce0db052d 100644 --- a/scm/github/deployment.go +++ b/scm/github/deployment.go @@ -8,13 +8,14 @@ import ( "github.com/sirupsen/logrus" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/library" "github.com/go-vela/types/raw" "github.com/google/go-github/v61/github" ) // GetDeployment gets a deployment from the GitHub repo. -func (c *client) GetDeployment(ctx context.Context, u *library.User, r *library.Repo, id int64) (*library.Deployment, error) { +func (c *client) GetDeployment(ctx context.Context, u *library.User, r *api.Repo, id int64) (*library.Deployment, error) { c.Logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), @@ -55,7 +56,7 @@ func (c *client) GetDeployment(ctx context.Context, u *library.User, r *library. } // GetDeploymentCount counts a list of deployments from the GitHub repo. -func (c *client) GetDeploymentCount(ctx context.Context, u *library.User, r *library.Repo) (int64, error) { +func (c *client) GetDeploymentCount(ctx context.Context, u *library.User, r *api.Repo) (int64, error) { c.Logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), @@ -97,7 +98,7 @@ func (c *client) GetDeploymentCount(ctx context.Context, u *library.User, r *lib } // GetDeploymentList gets a list of deployments from the GitHub repo. -func (c *client) GetDeploymentList(ctx context.Context, u *library.User, r *library.Repo, page, perPage int) ([]*library.Deployment, error) { +func (c *client) GetDeploymentList(ctx context.Context, u *library.User, r *api.Repo, page, perPage int) ([]*library.Deployment, error) { c.Logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), @@ -155,7 +156,7 @@ func (c *client) GetDeploymentList(ctx context.Context, u *library.User, r *libr } // CreateDeployment creates a new deployment for the GitHub repo. -func (c *client) CreateDeployment(ctx context.Context, u *library.User, r *library.Repo, d *library.Deployment) error { +func (c *client) CreateDeployment(ctx context.Context, u *library.User, r *api.Repo, d *library.Deployment) error { c.Logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), diff --git a/scm/github/deployment_test.go b/scm/github/deployment_test.go index 378a2417a..622dacfb4 100644 --- a/scm/github/deployment_test.go +++ b/scm/github/deployment_test.go @@ -10,6 +10,7 @@ import ( "github.com/gin-gonic/gin" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/library" ) @@ -35,7 +36,7 @@ func TestGithub_CreateDeployment(t *testing.T) { u.SetName("foo") u.SetToken("bar") - r := new(library.Repo) + r := new(api.Repo) r.SetID(1) r.SetOrg("foo") r.SetName("bar") diff --git a/scm/github/repo.go b/scm/github/repo.go index df90e5cb1..7a2d973f8 100644 --- a/scm/github/repo.go +++ b/scm/github/repo.go @@ -12,6 +12,7 @@ import ( "github.com/sirupsen/logrus" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/library" "github.com/google/go-github/v61/github" @@ -19,7 +20,7 @@ import ( // ConfigBackoff is a wrapper for Config that will retry five times if the function // fails to retrieve the yaml/yml file. -func (c *client) ConfigBackoff(ctx context.Context, u *library.User, r *library.Repo, ref string) (data []byte, err error) { +func (c *client) ConfigBackoff(ctx context.Context, u *library.User, r *api.Repo, ref string) (data []byte, err error) { // number of times to retry retryLimit := 5 @@ -47,7 +48,7 @@ func (c *client) ConfigBackoff(ctx context.Context, u *library.User, r *library. } // Config gets the pipeline configuration from the GitHub repo. -func (c *client) Config(ctx context.Context, u *library.User, r *library.Repo, ref string) ([]byte, error) { +func (c *client) Config(ctx context.Context, u *library.User, r *api.Repo, ref string) ([]byte, error) { c.Logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), @@ -149,7 +150,7 @@ func (c *client) Disable(ctx context.Context, u *library.User, org, name string) } // Enable activates a repo by creating the webhook. -func (c *client) Enable(ctx context.Context, u *library.User, r *library.Repo, h *library.Hook) (*library.Hook, string, error) { +func (c *client) Enable(ctx context.Context, u *library.User, r *api.Repo, h *library.Hook) (*library.Hook, string, error) { c.Logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), @@ -223,7 +224,7 @@ func (c *client) Enable(ctx context.Context, u *library.User, r *library.Repo, h } // Update edits a repo webhook. -func (c *client) Update(ctx context.Context, u *library.User, r *library.Repo, hookID int64) (bool, error) { +func (c *client) Update(ctx context.Context, u *library.User, r *api.Repo, hookID int64) (bool, error) { c.Logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), @@ -462,7 +463,7 @@ func (c *client) StepStatus(ctx context.Context, u *library.User, b *library.Bui } // GetRepo gets repo information from Github. -func (c *client) GetRepo(ctx context.Context, u *library.User, r *library.Repo) (*library.Repo, int, error) { +func (c *client) GetRepo(ctx context.Context, u *library.User, r *api.Repo) (*api.Repo, int, error) { c.Logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), @@ -502,7 +503,7 @@ func (c *client) GetOrgAndRepoName(ctx context.Context, u *library.User, o strin } // ListUserRepos returns a list of all repos the user has access to. -func (c *client) ListUserRepos(ctx context.Context, u *library.User) ([]*library.Repo, error) { +func (c *client) ListUserRepos(ctx context.Context, u *library.User) ([]*api.Repo, error) { c.Logger.WithFields(logrus.Fields{ "user": u.GetName(), }).Tracef("listing source repositories for %s", u.GetName()) @@ -511,7 +512,7 @@ func (c *client) ListUserRepos(ctx context.Context, u *library.User) ([]*library client := c.newClientToken(u.GetToken()) r := []*github.Repository{} - f := []*library.Repo{} + f := []*api.Repo{} // set the max per page for the options to capture the list of repos opts := &github.RepositoryListOptions{ @@ -556,7 +557,7 @@ func (c *client) ListUserRepos(ctx context.Context, u *library.User) ([]*library } // toLibraryRepo does a partial conversion of a github repo to a library repo. -func toLibraryRepo(gr github.Repository) *library.Repo { +func toLibraryRepo(gr github.Repository) *api.Repo { // setting the visbility to match the SCM visbility var visibility string if *gr.Private { @@ -565,7 +566,7 @@ func toLibraryRepo(gr github.Repository) *library.Repo { visibility = constants.VisibilityPublic } - return &library.Repo{ + return &api.Repo{ Org: gr.GetOwner().Login, Name: gr.Name, FullName: gr.FullName, @@ -580,15 +581,15 @@ func toLibraryRepo(gr github.Repository) *library.Repo { // GetPullRequest defines a function that retrieves // a pull request for a repo. -func (c *client) GetPullRequest(ctx context.Context, u *library.User, r *library.Repo, number int) (string, string, string, string, error) { +func (c *client) GetPullRequest(ctx context.Context, r *api.Repo, number int) (string, string, string, string, error) { c.Logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), - "user": u.GetName(), + "user": r.GetOwner().GetName(), }).Tracef("retrieving pull request %d for repo %s", number, r.GetFullName()) // create GitHub OAuth client with user's token - client := c.newClientToken(u.GetToken()) + client := c.newClientToken(r.GetOwner().GetToken()) pull, _, err := client.PullRequests.Get(ctx, r.GetOrg(), r.GetName(), number) if err != nil { @@ -640,15 +641,15 @@ func (c *client) GetHTMLURL(ctx context.Context, u *library.User, org, repo, nam } // GetBranch defines a function that retrieves a branch for a repo. -func (c *client) GetBranch(ctx context.Context, u *library.User, r *library.Repo, branch string) (string, string, error) { +func (c *client) GetBranch(ctx context.Context, r *api.Repo, branch string) (string, string, error) { c.Logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), - "user": u.GetName(), + "user": r.GetOwner().GetName(), }).Tracef("retrieving branch %s for repo %s", branch, r.GetFullName()) // create GitHub OAuth client with user's token - client := c.newClientToken(u.GetToken()) + client := c.newClientToken(r.GetOwner().GetToken()) maxRedirects := 3 diff --git a/scm/github/repo_test.go b/scm/github/repo_test.go index b34e704ac..64d86cb21 100644 --- a/scm/github/repo_test.go +++ b/scm/github/repo_test.go @@ -14,6 +14,7 @@ import ( "github.com/gin-gonic/gin" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/library" ) @@ -50,7 +51,7 @@ func TestGithub_Config_YML(t *testing.T) { u.SetName("foo") u.SetToken("bar") - r := new(library.Repo) + r := new(api.Repo) r.SetOrg("foo") r.SetName("bar") @@ -111,7 +112,7 @@ func TestGithub_ConfigBackoff_YML(t *testing.T) { u.SetName("foo") u.SetToken("bar") - r := new(library.Repo) + r := new(api.Repo) r.SetOrg("foo") r.SetName("bar") @@ -165,7 +166,7 @@ func TestGithub_Config_YAML(t *testing.T) { u.SetName("foo") u.SetToken("bar") - r := new(library.Repo) + r := new(api.Repo) r.SetOrg("foo") r.SetName("bar") @@ -219,7 +220,7 @@ func TestGithub_Config_Star(t *testing.T) { u.SetName("foo") u.SetToken("bar") - r := new(library.Repo) + r := new(api.Repo) r.SetOrg("foo") r.SetName("bar") r.SetPipelineType(constants.PipelineTypeStarlark) @@ -279,7 +280,7 @@ func TestGithub_Config_Star_Prefer(t *testing.T) { u.SetName("foo") u.SetToken("bar") - r := new(library.Repo) + r := new(api.Repo) r.SetOrg("foo") r.SetName("bar") r.SetPipelineType(constants.PipelineTypeStarlark) @@ -334,7 +335,7 @@ func TestGithub_Config_Py(t *testing.T) { u.SetName("foo") u.SetToken("bar") - r := new(library.Repo) + r := new(api.Repo) r.SetOrg("foo") r.SetName("bar") r.SetPipelineType(constants.PipelineTypeStarlark) @@ -384,7 +385,7 @@ func TestGithub_Config_YAML_BadRequest(t *testing.T) { u.SetName("foo") u.SetToken("bar") - r := new(library.Repo) + r := new(api.Repo) r.SetOrg("foo") r.SetName("bar") @@ -426,7 +427,7 @@ func TestGithub_Config_NotFound(t *testing.T) { u.SetName("foo") u.SetToken("bar") - r := new(library.Repo) + r := new(api.Repo) r.SetOrg("foo") r.SetName("bar") @@ -475,7 +476,7 @@ func TestGithub_Config_BadEncoding(t *testing.T) { u.SetName("foo") u.SetToken("bar") - r := new(library.Repo) + r := new(api.Repo) r.SetOrg("foo") r.SetName("bar") @@ -685,7 +686,7 @@ func TestGithub_Enable(t *testing.T) { wantHook.SetEvent("initialize") wantHook.SetStatus("success") - r := new(library.Repo) + r := new(api.Repo) r.SetID(1) r.SetName("bar") r.SetOrg("foo") @@ -731,7 +732,7 @@ func TestGithub_Update(t *testing.T) { u.SetName("foo") u.SetToken("bar") - r := new(library.Repo) + r := new(api.Repo) r.SetID(1) r.SetName("bar") r.SetOrg("foo") @@ -774,7 +775,7 @@ func TestGithub_Update_webhookExists_True(t *testing.T) { u.SetName("foo") u.SetToken("bar") - r := new(library.Repo) + r := new(api.Repo) client, _ := NewTest(s.URL) @@ -811,7 +812,7 @@ func TestGithub_Update_webhookExists_False(t *testing.T) { u.SetName("foo") u.SetToken("bar") - r := new(library.Repo) + r := new(api.Repo) client, _ := NewTest(s.URL) @@ -1260,11 +1261,11 @@ func TestGithub_GetRepo(t *testing.T) { u.SetName("foo") u.SetToken("bar") - r := new(library.Repo) + r := new(api.Repo) r.SetOrg("octocat") r.SetName("Hello-World") - want := new(library.Repo) + want := new(api.Repo) want.SetOrg("octocat") want.SetName("Hello-World") want.SetFullName("octocat/Hello-World") @@ -1314,7 +1315,7 @@ func TestGithub_GetRepo_Fail(t *testing.T) { u.SetName("foo") u.SetToken("bar") - r := new(library.Repo) + r := new(api.Repo) r.SetOrg("octocat") r.SetName("Hello-World") @@ -1432,7 +1433,7 @@ func TestGithub_ListUserRepos(t *testing.T) { u.SetName("foo") u.SetToken("bar") - r := new(library.Repo) + r := new(api.Repo) r.SetOrg("octocat") r.SetName("Hello-World") r.SetFullName("octocat/Hello-World") @@ -1443,7 +1444,7 @@ func TestGithub_ListUserRepos(t *testing.T) { r.SetTopics([]string{"octocat", "atom", "electron", "api"}) r.SetVisibility("public") - want := []*library.Repo{r} + want := []*api.Repo{r} client, _ := NewTest(s.URL) @@ -1480,7 +1481,7 @@ func TestGithub_ListUserRepos_Ineligible(t *testing.T) { u.SetName("foo") u.SetToken("bar") - want := []*library.Repo{} + want := []*api.Repo{} client, _ := NewTest(s.URL) @@ -1517,9 +1518,10 @@ func TestGithub_GetPullRequest(t *testing.T) { u.SetName("foo") u.SetToken("bar") - r := new(library.Repo) + r := new(api.Repo) r.SetOrg("octocat") r.SetName("Hello-World") + r.SetOwner(u) wantCommit := "6dcb09b5b57875f334f61aebed695e2e4193db5e" wantBranch := "main" @@ -1529,7 +1531,7 @@ func TestGithub_GetPullRequest(t *testing.T) { client, _ := NewTest(s.URL) // run test - gotCommit, gotBranch, gotBaseRef, gotHeadRef, err := client.GetPullRequest(context.TODO(), u, r, 1) + gotCommit, gotBranch, gotBaseRef, gotHeadRef, err := client.GetPullRequest(context.TODO(), r, 1) if err != nil { t.Errorf("Status returned err: %v", err) } @@ -1573,11 +1575,12 @@ func TestGithub_GetBranch(t *testing.T) { u.SetName("foo") u.SetToken("bar") - r := new(library.Repo) + r := new(api.Repo) r.SetOrg("octocat") r.SetName("Hello-World") r.SetFullName("octocat/Hello-World") r.SetBranch("main") + r.SetOwner(u) wantBranch := "main" wantCommit := "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d" @@ -1585,7 +1588,7 @@ func TestGithub_GetBranch(t *testing.T) { client, _ := NewTest(s.URL) // run test - gotBranch, gotCommit, err := client.GetBranch(context.TODO(), u, r, "main") + gotBranch, gotCommit, err := client.GetBranch(context.TODO(), r, "main") if err != nil { t.Errorf("Status returned err: %v", err) } diff --git a/scm/github/webhook.go b/scm/github/webhook.go index 3e2e5badd..9e9dccac5 100644 --- a/scm/github/webhook.go +++ b/scm/github/webhook.go @@ -15,7 +15,8 @@ import ( "github.com/sirupsen/logrus" - "github.com/go-vela/types" + api "github.com/go-vela/server/api/types" + "github.com/go-vela/server/internal" "github.com/go-vela/types/constants" "github.com/go-vela/types/library" "github.com/google/go-github/v61/github" @@ -24,7 +25,7 @@ import ( // ProcessWebhook parses the webhook from a repo. // //nolint:nilerr // ignore webhook returning nil -func (c *client) ProcessWebhook(ctx context.Context, request *http.Request) (*types.Webhook, error) { +func (c *client) ProcessWebhook(ctx context.Context, request *http.Request) (*internal.Webhook, error) { c.Logger.Tracef("processing GitHub webhook") // create our own record of the hook and populate its fields @@ -55,14 +56,14 @@ func (c *client) ProcessWebhook(ctx context.Context, request *http.Request) (*ty payload, err := github.ValidatePayloadFromBody(contentType, request.Body, "", nil) if err != nil { - return &types.Webhook{Hook: h}, nil + return &internal.Webhook{Hook: h}, nil } // parse the payload from the webhook event, err := github.ParseWebHook(github.WebHookType(request), payload) if err != nil { - return &types.Webhook{Hook: h}, nil + return &internal.Webhook{Hook: h}, nil } // process the event from the webhook @@ -79,11 +80,11 @@ func (c *client) ProcessWebhook(ctx context.Context, request *http.Request) (*ty return c.processRepositoryEvent(h, event) } - return &types.Webhook{Hook: h}, nil + return &internal.Webhook{Hook: h}, nil } // VerifyWebhook verifies the webhook from a repo. -func (c *client) VerifyWebhook(ctx context.Context, request *http.Request, r *library.Repo) error { +func (c *client) VerifyWebhook(ctx context.Context, request *http.Request, r *api.Repo) error { c.Logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), @@ -98,7 +99,7 @@ func (c *client) VerifyWebhook(ctx context.Context, request *http.Request, r *li } // RedeliverWebhook redelivers webhooks from GitHub. -func (c *client) RedeliverWebhook(ctx context.Context, u *library.User, r *library.Repo, h *library.Hook) error { +func (c *client) RedeliverWebhook(ctx context.Context, u *library.User, r *api.Repo, h *library.Hook) error { // create GitHub OAuth client with user's token //nolint:contextcheck // do not need to pass context in this instance client := c.newClientToken(*u.Token) @@ -127,7 +128,7 @@ func (c *client) RedeliverWebhook(ctx context.Context, u *library.User, r *libra } // processPushEvent is a helper function to process the push event. -func (c *client) processPushEvent(h *library.Hook, payload *github.PushEvent) (*types.Webhook, error) { +func (c *client) processPushEvent(h *library.Hook, payload *github.PushEvent) (*internal.Webhook, error) { c.Logger.WithFields(logrus.Fields{ "org": payload.GetRepo().GetOwner().GetLogin(), "repo": payload.GetRepo().GetName(), @@ -136,7 +137,7 @@ func (c *client) processPushEvent(h *library.Hook, payload *github.PushEvent) (* repo := payload.GetRepo() // convert payload to library repo - r := new(library.Repo) + r := new(api.Repo) r.SetOrg(repo.GetOwner().GetLogin()) r.SetName(repo.GetName()) r.SetFullName(repo.GetFullName()) @@ -224,7 +225,7 @@ func (c *client) processPushEvent(h *library.Hook, payload *github.PushEvent) (* } } - return &types.Webhook{ + return &internal.Webhook{ Hook: h, Repo: r, Build: b, @@ -232,7 +233,7 @@ func (c *client) processPushEvent(h *library.Hook, payload *github.PushEvent) (* } // processPREvent is a helper function to process the pull_request event. -func (c *client) processPREvent(h *library.Hook, payload *github.PullRequestEvent) (*types.Webhook, error) { +func (c *client) processPREvent(h *library.Hook, payload *github.PullRequestEvent) (*internal.Webhook, error) { c.Logger.WithFields(logrus.Fields{ "org": payload.GetRepo().GetOwner().GetLogin(), "repo": payload.GetRepo().GetName(), @@ -247,7 +248,7 @@ func (c *client) processPREvent(h *library.Hook, payload *github.PullRequestEven // if the pull request state isn't open we ignore it if payload.GetPullRequest().GetState() != "open" { - return &types.Webhook{Hook: h}, nil + return &internal.Webhook{Hook: h}, nil } // skip if the pull request action is not opened, synchronize, reopened, edited, labeled, or unlabeled @@ -257,14 +258,14 @@ func (c *client) processPREvent(h *library.Hook, payload *github.PullRequestEven !strings.EqualFold(payload.GetAction(), "edited") && !strings.EqualFold(payload.GetAction(), "labeled") && !strings.EqualFold(payload.GetAction(), "unlabeled") { - return &types.Webhook{Hook: h}, nil + return &internal.Webhook{Hook: h}, nil } // capture the repo from the payload repo := payload.GetRepo() // convert payload to library repo - r := new(library.Repo) + r := new(api.Repo) r.SetOrg(repo.GetOwner().GetLogin()) r.SetName(repo.GetName()) r.SetFullName(repo.GetFullName()) @@ -326,8 +327,8 @@ func (c *client) processPREvent(h *library.Hook, payload *github.PullRequestEven fromFork := payload.GetPullRequest().GetHead().GetRepo().GetFork() && !strings.EqualFold(payload.GetPullRequest().GetBase().GetRepo().GetFullName(), payload.GetPullRequest().GetHead().GetRepo().GetFullName()) - return &types.Webhook{ - PullRequest: types.PullRequest{ + return &internal.Webhook{ + PullRequest: internal.PullRequest{ Number: payload.GetNumber(), IsFromFork: fromFork, Labels: prLabels, @@ -339,7 +340,7 @@ func (c *client) processPREvent(h *library.Hook, payload *github.PullRequestEven } // processDeploymentEvent is a helper function to process the deployment event. -func (c *client) processDeploymentEvent(h *library.Hook, payload *github.DeploymentEvent) (*types.Webhook, error) { +func (c *client) processDeploymentEvent(h *library.Hook, payload *github.DeploymentEvent) (*internal.Webhook, error) { c.Logger.WithFields(logrus.Fields{ "org": payload.GetRepo().GetOwner().GetLogin(), "repo": payload.GetRepo().GetName(), @@ -349,7 +350,7 @@ func (c *client) processDeploymentEvent(h *library.Hook, payload *github.Deploym repo := payload.GetRepo() // convert payload to library repo - r := new(library.Repo) + r := new(api.Repo) r.SetOrg(repo.GetOwner().GetLogin()) r.SetName(repo.GetName()) r.SetFullName(repo.GetFullName()) @@ -399,7 +400,7 @@ func (c *client) processDeploymentEvent(h *library.Hook, payload *github.Deploym // unmarshal the payload into the expected map[string]string format err := json.Unmarshal(payload.GetDeployment().Payload, &deployPayload) if err != nil { - return &types.Webhook{}, err + return &internal.Webhook{}, err } // check if the map is empty @@ -430,7 +431,7 @@ func (c *client) processDeploymentEvent(h *library.Hook, payload *github.Deploym fmt.Sprintf("https://%s/%s/settings/hooks", h.GetHost(), r.GetFullName()), ) - return &types.Webhook{ + return &internal.Webhook{ Hook: h, Repo: r, Build: b, @@ -439,7 +440,7 @@ func (c *client) processDeploymentEvent(h *library.Hook, payload *github.Deploym } // processIssueCommentEvent is a helper function to process the issue comment event. -func (c *client) processIssueCommentEvent(h *library.Hook, payload *github.IssueCommentEvent) (*types.Webhook, error) { +func (c *client) processIssueCommentEvent(h *library.Hook, payload *github.IssueCommentEvent) (*internal.Webhook, error) { c.Logger.WithFields(logrus.Fields{ "org": payload.GetRepo().GetOwner().GetLogin(), "repo": payload.GetRepo().GetName(), @@ -453,8 +454,8 @@ func (c *client) processIssueCommentEvent(h *library.Hook, payload *github.Issue // skip if the comment action is deleted or not part of a pull request if strings.EqualFold(payload.GetAction(), "deleted") || !payload.GetIssue().IsPullRequest() { - // return &types.Webhook{Hook: h}, nil - return &types.Webhook{ + // return &internal.Webhook{Hook: h}, nil + return &internal.Webhook{ Hook: h, }, nil } @@ -463,7 +464,7 @@ func (c *client) processIssueCommentEvent(h *library.Hook, payload *github.Issue repo := payload.GetRepo() // convert payload to library repo - r := new(library.Repo) + r := new(api.Repo) r.SetOrg(repo.GetOwner().GetLogin()) r.SetName(repo.GetName()) r.SetFullName(repo.GetFullName()) @@ -486,8 +487,8 @@ func (c *client) processIssueCommentEvent(h *library.Hook, payload *github.Issue b.SetEmail(payload.GetIssue().GetUser().GetEmail()) b.SetRef(fmt.Sprintf("refs/pull/%d/head", payload.GetIssue().GetNumber())) - return &types.Webhook{ - PullRequest: types.PullRequest{ + return &internal.Webhook{ + PullRequest: internal.PullRequest{ Comment: payload.GetComment().GetBody(), Number: payload.GetIssue().GetNumber(), }, @@ -499,13 +500,13 @@ func (c *client) processIssueCommentEvent(h *library.Hook, payload *github.Issue // processRepositoryEvent is a helper function to process the repository event. -func (c *client) processRepositoryEvent(h *library.Hook, payload *github.RepositoryEvent) (*types.Webhook, error) { +func (c *client) processRepositoryEvent(h *library.Hook, payload *github.RepositoryEvent) (*internal.Webhook, error) { logrus.Tracef("processing repository event GitHub webhook for %s", payload.GetRepo().GetFullName()) repo := payload.GetRepo() // convert payload to library repo - r := new(library.Repo) + r := new(api.Repo) r.SetOrg(repo.GetOwner().GetLogin()) r.SetName(repo.GetName()) r.SetFullName(repo.GetFullName()) @@ -523,7 +524,7 @@ func (c *client) processRepositoryEvent(h *library.Hook, payload *github.Reposit fmt.Sprintf("https://%s/%s/settings/hooks", h.GetHost(), r.GetFullName()), ) - return &types.Webhook{ + return &internal.Webhook{ Hook: h, Repo: r, }, nil @@ -531,7 +532,7 @@ func (c *client) processRepositoryEvent(h *library.Hook, payload *github.Reposit // getDeliveryID gets the last 100 webhook deliveries for a repo and // finds the matching delivery id with the source id in the hook. -func (c *client) getDeliveryID(ctx context.Context, ghClient *github.Client, r *library.Repo, h *library.Hook) (int64, error) { +func (c *client) getDeliveryID(ctx context.Context, ghClient *github.Client, r *api.Repo, h *library.Hook) (int64, error) { c.Logger.WithFields(logrus.Fields{ "org": r.GetOrg(), "repo": r.GetName(), diff --git a/scm/github/webhook_test.go b/scm/github/webhook_test.go index 3bf0415f9..00454fdf7 100644 --- a/scm/github/webhook_test.go +++ b/scm/github/webhook_test.go @@ -13,10 +13,11 @@ import ( "time" "github.com/gin-gonic/gin" + api "github.com/go-vela/server/api/types" + "github.com/go-vela/server/internal" "github.com/go-vela/types/raw" "github.com/google/go-cmp/cmp" - "github.com/go-vela/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/library" ) @@ -56,7 +57,7 @@ func TestGithub_ProcessWebhook_Push(t *testing.T) { wantHook.SetStatus(constants.StatusSuccess) wantHook.SetLink("https://github.com/Codertocat/Hello-World/settings/hooks") - wantRepo := new(library.Repo) + wantRepo := new(api.Repo) wantRepo.SetOrg("Codertocat") wantRepo.SetName("Hello-World") wantRepo.SetFullName("Codertocat/Hello-World") @@ -80,7 +81,7 @@ func TestGithub_ProcessWebhook_Push(t *testing.T) { wantBuild.SetRef("refs/heads/main") wantBuild.SetBaseRef("") - want := &types.Webhook{ + want := &internal.Webhook{ Hook: wantHook, Repo: wantRepo, Build: wantBuild, @@ -134,7 +135,7 @@ func TestGithub_ProcessWebhook_Push_NoSender(t *testing.T) { wantHook.SetStatus(constants.StatusSuccess) wantHook.SetLink("https://github.com/Codertocat/Hello-World/settings/hooks") - wantRepo := new(library.Repo) + wantRepo := new(api.Repo) wantRepo.SetOrg("Codertocat") wantRepo.SetName("Hello-World") wantRepo.SetFullName("Codertocat/Hello-World") @@ -158,7 +159,7 @@ func TestGithub_ProcessWebhook_Push_NoSender(t *testing.T) { wantBuild.SetRef("refs/heads/main") wantBuild.SetBaseRef("") - want := &types.Webhook{ + want := &internal.Webhook{ Hook: wantHook, Repo: wantRepo, Build: wantBuild, @@ -210,7 +211,7 @@ func TestGithub_ProcessWebhook_Push_Branch_Delete(t *testing.T) { wantHook.SetStatus(constants.StatusSuccess) wantHook.SetLink("https://github.com/Codertocat/Hello-World/settings/hooks") - wantRepo := new(library.Repo) + wantRepo := new(api.Repo) wantRepo.SetOrg("Codertocat") wantRepo.SetName("Hello-World") wantRepo.SetFullName("Codertocat/Hello-World") @@ -235,7 +236,7 @@ func TestGithub_ProcessWebhook_Push_Branch_Delete(t *testing.T) { wantBuild.SetRef("d3d9188fc87a6977343e922c128f162a86018d76") wantBuild.SetBaseRef("") - want := &types.Webhook{ + want := &internal.Webhook{ Hook: wantHook, Repo: wantRepo, Build: wantBuild, @@ -287,7 +288,7 @@ func TestGithub_ProcessWebhook_Push_Tag_Delete(t *testing.T) { wantHook.SetStatus(constants.StatusSuccess) wantHook.SetLink("https://github.com/Codertocat/Hello-World/settings/hooks") - wantRepo := new(library.Repo) + wantRepo := new(api.Repo) wantRepo.SetOrg("Codertocat") wantRepo.SetName("Hello-World") wantRepo.SetFullName("Codertocat/Hello-World") @@ -312,7 +313,7 @@ func TestGithub_ProcessWebhook_Push_Tag_Delete(t *testing.T) { wantBuild.SetRef("d3d9188fc87a6977343e922c128f162a86018d76") wantBuild.SetBaseRef("") - want := &types.Webhook{ + want := &internal.Webhook{ Hook: wantHook, Repo: wantRepo, Build: wantBuild, @@ -346,7 +347,7 @@ func TestGithub_ProcessWebhook_PullRequest(t *testing.T) { wantHook.SetStatus(constants.StatusSuccess) wantHook.SetLink("https://github.com/Codertocat/Hello-World/settings/hooks") - wantRepo := new(library.Repo) + wantRepo := new(api.Repo) wantRepo.SetOrg("Codertocat") wantRepo.SetName("Hello-World") wantRepo.SetFullName("Codertocat/Hello-World") @@ -423,14 +424,14 @@ func TestGithub_ProcessWebhook_PullRequest(t *testing.T) { tests := []struct { name string testData string - want *types.Webhook + want *internal.Webhook wantErr bool }{ { name: "success", testData: "testdata/hooks/pull_request.json", - want: &types.Webhook{ - PullRequest: types.PullRequest{ + want: &internal.Webhook{ + PullRequest: internal.PullRequest{ Number: wantHook.GetNumber(), IsFromFork: false, }, @@ -442,8 +443,8 @@ func TestGithub_ProcessWebhook_PullRequest(t *testing.T) { { name: "fork", testData: "testdata/hooks/pull_request_fork.json", - want: &types.Webhook{ - PullRequest: types.PullRequest{ + want: &internal.Webhook{ + PullRequest: internal.PullRequest{ Number: wantHook.GetNumber(), IsFromFork: true, }, @@ -455,8 +456,8 @@ func TestGithub_ProcessWebhook_PullRequest(t *testing.T) { { name: "fork same repo", testData: "testdata/hooks/pull_request_fork_same-repo.json", - want: &types.Webhook{ - PullRequest: types.PullRequest{ + want: &internal.Webhook{ + PullRequest: internal.PullRequest{ Number: wantHook.GetNumber(), IsFromFork: false, }, @@ -468,7 +469,7 @@ func TestGithub_ProcessWebhook_PullRequest(t *testing.T) { { name: "closed action", testData: "testdata/hooks/pull_request_closed_action.json", - want: &types.Webhook{ + want: &internal.Webhook{ Hook: wantHook, Repo: nil, Build: nil, @@ -477,7 +478,7 @@ func TestGithub_ProcessWebhook_PullRequest(t *testing.T) { { name: "closed state", testData: "testdata/hooks/pull_request_closed_state.json", - want: &types.Webhook{ + want: &internal.Webhook{ Hook: wantHook, Repo: nil, Build: nil, @@ -486,8 +487,8 @@ func TestGithub_ProcessWebhook_PullRequest(t *testing.T) { { name: "labeled documentation", testData: "testdata/hooks/pull_request_labeled.json", - want: &types.Webhook{ - PullRequest: types.PullRequest{ + want: &internal.Webhook{ + PullRequest: internal.PullRequest{ Number: wantHook.GetNumber(), IsFromFork: false, Labels: []string{"documentation"}, @@ -500,8 +501,8 @@ func TestGithub_ProcessWebhook_PullRequest(t *testing.T) { { name: "unlabeled documentation", testData: "testdata/hooks/pull_request_unlabeled.json", - want: &types.Webhook{ - PullRequest: types.PullRequest{ + want: &internal.Webhook{ + PullRequest: internal.PullRequest{ Number: wantHook.GetNumber(), IsFromFork: false, Labels: []string{"documentation"}, @@ -514,8 +515,8 @@ func TestGithub_ProcessWebhook_PullRequest(t *testing.T) { { name: "edited while labeled documentation", testData: "testdata/hooks/pull_request_edited_while_labeled.json", - want: &types.Webhook{ - PullRequest: types.PullRequest{ + want: &internal.Webhook{ + PullRequest: internal.PullRequest{ Number: wantHook.GetNumber(), IsFromFork: false, Labels: []string{"documentation", "enhancement"}, @@ -576,7 +577,7 @@ func TestGithub_ProcessWebhook_Deployment(t *testing.T) { wantHook.SetEvent("deployment") wantHook.SetStatus(constants.StatusSuccess) - wantRepo := new(library.Repo) + wantRepo := new(api.Repo) wantRepo.SetOrg("Codertocat") wantRepo.SetName("Hello-World") wantRepo.SetFullName("Codertocat/Hello-World") @@ -615,7 +616,7 @@ func TestGithub_ProcessWebhook_Deployment(t *testing.T) { type args struct { file string hook *library.Hook - repo *library.Repo + repo *api.Repo build *library.Build deploymentPayload raw.StringSliceMap deployment *library.Deployment @@ -652,7 +653,7 @@ func TestGithub_ProcessWebhook_Deployment(t *testing.T) { client, _ := NewTest(s.URL) wantBuild.SetDeployPayload(tt.args.deploymentPayload) - want := &types.Webhook{ + want := &internal.Webhook{ Hook: tt.args.hook, Repo: tt.args.repo, Build: tt.args.build, @@ -709,7 +710,7 @@ func TestGithub_ProcessWebhook_Deployment_Commit(t *testing.T) { wantHook.SetEvent("deployment") wantHook.SetStatus(constants.StatusSuccess) - wantRepo := new(library.Repo) + wantRepo := new(api.Repo) wantRepo.SetOrg("Codertocat") wantRepo.SetName("Hello-World") wantRepo.SetFullName("Codertocat/Hello-World") @@ -746,7 +747,7 @@ func TestGithub_ProcessWebhook_Deployment_Commit(t *testing.T) { wantDeployment.SetCreatedAt(time.Now().UTC().Unix()) wantDeployment.SetCreatedBy("Codertocat") - want := &types.Webhook{ + want := &internal.Webhook{ Hook: wantHook, Repo: wantRepo, Build: wantBuild, @@ -799,7 +800,7 @@ func TestGithub_ProcessWebhook_BadGithubEvent(t *testing.T) { wantHook.SetEvent("foobar") wantHook.SetStatus(constants.StatusSuccess) - want := &types.Webhook{ + want := &internal.Webhook{ Hook: wantHook, Repo: nil, Build: nil, @@ -852,7 +853,7 @@ func TestGithub_ProcessWebhook_BadContentType(t *testing.T) { wantHook.SetEvent("pull_request") wantHook.SetStatus(constants.StatusSuccess) - want := &types.Webhook{ + want := &internal.Webhook{ Hook: wantHook, Repo: nil, Build: nil, @@ -896,7 +897,7 @@ func TestGithub_VerifyWebhook_EmptyRepo(t *testing.T) { client, _ := NewTest(s.URL) // run test - err = client.VerifyWebhook(context.TODO(), request, new(library.Repo)) + err = client.VerifyWebhook(context.TODO(), request, new(api.Repo)) if err != nil { t.Errorf("VerifyWebhook should have returned err") } @@ -907,7 +908,7 @@ func TestGithub_VerifyWebhook_NoSecret(t *testing.T) { s := httptest.NewServer(http.NotFoundHandler()) defer s.Close() - r := new(library.Repo) + r := new(api.Repo) r.SetOrg("Codertocat") r.SetName("Hello-World") r.SetFullName("Codertocat/Hello-World") @@ -979,7 +980,7 @@ func TestGithub_ProcessWebhook_IssueComment_PR(t *testing.T) { wantHook.SetStatus(constants.StatusSuccess) wantHook.SetLink("https://github.com/Codertocat/Hello-World/settings/hooks") - wantRepo := new(library.Repo) + wantRepo := new(api.Repo) wantRepo.SetOrg("Codertocat") wantRepo.SetName("Hello-World") wantRepo.SetFullName("Codertocat/Hello-World") @@ -1001,8 +1002,8 @@ func TestGithub_ProcessWebhook_IssueComment_PR(t *testing.T) { wantBuild.SetEmail("") wantBuild.SetRef("refs/pull/1/head") - want := &types.Webhook{ - PullRequest: types.PullRequest{ + want := &internal.Webhook{ + PullRequest: internal.PullRequest{ Comment: "ok to test", Number: wantHook.GetNumber(), }, @@ -1058,7 +1059,7 @@ func TestGithub_ProcessWebhook_IssueComment_Created(t *testing.T) { wantHook.SetStatus(constants.StatusSuccess) wantHook.SetLink("https://github.com/Codertocat/Hello-World/settings/hooks") - want := &types.Webhook{ + want := &internal.Webhook{ Hook: wantHook, } @@ -1109,7 +1110,7 @@ func TestGithub_ProcessWebhook_IssueComment_Deleted(t *testing.T) { wantHook.SetStatus(constants.StatusSuccess) wantHook.SetLink("https://github.com/Codertocat/Hello-World/settings/hooks") - want := &types.Webhook{ + want := &internal.Webhook{ Hook: wantHook, } @@ -1160,7 +1161,7 @@ func TestGitHub_ProcessWebhook_RepositoryRename(t *testing.T) { wantHook.SetStatus(constants.StatusSuccess) wantHook.SetLink("https://github.com/Codertocat/Hello-World/settings/hooks") - wantRepo := new(library.Repo) + wantRepo := new(api.Repo) wantRepo.SetActive(true) wantRepo.SetOrg("Codertocat") wantRepo.SetName("Hello-World") @@ -1171,7 +1172,7 @@ func TestGitHub_ProcessWebhook_RepositoryRename(t *testing.T) { wantRepo.SetPrivate(false) wantRepo.SetTopics(nil) - want := &types.Webhook{ + want := &internal.Webhook{ Hook: wantHook, Repo: wantRepo, } @@ -1223,7 +1224,7 @@ func TestGitHub_ProcessWebhook_RepositoryTransfer(t *testing.T) { wantHook.SetStatus(constants.StatusSuccess) wantHook.SetLink("https://github.com/Codertocat/Hello-World/settings/hooks") - wantRepo := new(library.Repo) + wantRepo := new(api.Repo) wantRepo.SetActive(true) wantRepo.SetOrg("Codertocat") wantRepo.SetName("Hello-World") @@ -1234,7 +1235,7 @@ func TestGitHub_ProcessWebhook_RepositoryTransfer(t *testing.T) { wantRepo.SetPrivate(false) wantRepo.SetTopics(nil) - want := &types.Webhook{ + want := &internal.Webhook{ Hook: wantHook, Repo: wantRepo, } @@ -1286,7 +1287,7 @@ func TestGitHub_ProcessWebhook_RepositoryArchived(t *testing.T) { wantHook.SetStatus(constants.StatusSuccess) wantHook.SetLink("https://github.com/Codertocat/Hello-World/settings/hooks") - wantRepo := new(library.Repo) + wantRepo := new(api.Repo) wantRepo.SetActive(false) wantRepo.SetOrg("Codertocat") wantRepo.SetName("Hello-World") @@ -1297,7 +1298,7 @@ func TestGitHub_ProcessWebhook_RepositoryArchived(t *testing.T) { wantRepo.SetPrivate(false) wantRepo.SetTopics(nil) - want := &types.Webhook{ + want := &internal.Webhook{ Hook: wantHook, Repo: wantRepo, } @@ -1349,7 +1350,7 @@ func TestGitHub_ProcessWebhook_RepositoryEdited(t *testing.T) { wantHook.SetStatus(constants.StatusSuccess) wantHook.SetLink("https://github.com/Codertocat/Hello-World/settings/hooks") - wantRepo := new(library.Repo) + wantRepo := new(api.Repo) wantRepo.SetActive(true) wantRepo.SetOrg("Codertocat") wantRepo.SetName("Hello-World") @@ -1360,7 +1361,7 @@ func TestGitHub_ProcessWebhook_RepositoryEdited(t *testing.T) { wantRepo.SetTopics([]string{"cloud", "security"}) wantRepo.SetPrivate(false) - want := &types.Webhook{ + want := &internal.Webhook{ Hook: wantHook, Repo: wantRepo, } @@ -1412,7 +1413,7 @@ func TestGitHub_ProcessWebhook_Repository(t *testing.T) { wantHook.SetStatus(constants.StatusSuccess) wantHook.SetLink("https://github.com/Codertocat/Hello-World/settings/hooks") - wantRepo := new(library.Repo) + wantRepo := new(api.Repo) wantRepo.SetActive(true) wantRepo.SetOrg("Codertocat") wantRepo.SetName("Hello-World") @@ -1423,7 +1424,7 @@ func TestGitHub_ProcessWebhook_Repository(t *testing.T) { wantRepo.SetPrivate(false) wantRepo.SetTopics(nil) - want := &types.Webhook{ + want := &internal.Webhook{ Hook: wantHook, Repo: wantRepo, } @@ -1474,7 +1475,7 @@ func TestGithub_Redeliver_Webhook(t *testing.T) { _hook.SetNumber(1) _hook.SetWebhookID(1234) - _repo := new(library.Repo) + _repo := new(api.Repo) _repo.SetID(1) _repo.SetName("bar") _repo.SetOrg("foo") @@ -1518,7 +1519,7 @@ func TestGithub_GetDeliveryID(t *testing.T) { _hook.SetNumber(1) _hook.SetWebhookID(1234) - _repo := new(library.Repo) + _repo := new(api.Repo) _repo.SetID(1) _repo.SetName("bar") _repo.SetOrg("foo") diff --git a/scm/service.go b/scm/service.go index bc594af03..b04a8cd5e 100644 --- a/scm/service.go +++ b/scm/service.go @@ -6,7 +6,8 @@ import ( "context" "net/http" - "github.com/go-vela/types" + api "github.com/go-vela/server/api/types" + "github.com/go-vela/server/internal" "github.com/go-vela/types/library" ) @@ -67,46 +68,46 @@ type Service interface { // of files changed for a commit. // // https://en.wikipedia.org/wiki/Changeset. - Changeset(context.Context, *library.User, *library.Repo, string) ([]string, error) + Changeset(context.Context, *api.Repo, string) ([]string, error) // ChangesetPR defines a function that captures the list // of files changed for a pull request. // // https://en.wikipedia.org/wiki/Changeset. - ChangesetPR(context.Context, *library.User, *library.Repo, int) ([]string, error) + ChangesetPR(context.Context, *api.Repo, int) ([]string, error) // Deployment SCM Interface Functions // GetDeployment defines a function that // gets a deployment by number and repo. - GetDeployment(context.Context, *library.User, *library.Repo, int64) (*library.Deployment, error) + GetDeployment(context.Context, *library.User, *api.Repo, int64) (*library.Deployment, error) // GetDeploymentCount defines a function that // counts a list of all deployment for a repo. - GetDeploymentCount(context.Context, *library.User, *library.Repo) (int64, error) + GetDeploymentCount(context.Context, *library.User, *api.Repo) (int64, error) // GetDeploymentList defines a function that gets // a list of all deployments for a repo. - GetDeploymentList(context.Context, *library.User, *library.Repo, int, int) ([]*library.Deployment, error) + GetDeploymentList(context.Context, *library.User, *api.Repo, int, int) ([]*library.Deployment, error) // CreateDeployment defines a function that // creates a new deployment. - CreateDeployment(context.Context, *library.User, *library.Repo, *library.Deployment) error + CreateDeployment(context.Context, *library.User, *api.Repo, *library.Deployment) error // Repo SCM Interface Functions // Config defines a function that captures // the pipeline configuration from a repo. - Config(context.Context, *library.User, *library.Repo, string) ([]byte, error) + Config(context.Context, *library.User, *api.Repo, string) ([]byte, error) // ConfigBackoff is a truncated constant backoff wrapper for Config. // Retry again in five seconds if Config fails to retrieve yaml/yml file. // Will return an error after five failed attempts. - ConfigBackoff(context.Context, *library.User, *library.Repo, string) ([]byte, error) + ConfigBackoff(context.Context, *library.User, *api.Repo, string) ([]byte, error) // Disable defines a function that deactivates // a repo by destroying the webhook. Disable(context.Context, *library.User, string, string) error // Enable defines a function that activates // a repo by creating the webhook. - Enable(context.Context, *library.User, *library.Repo, *library.Hook) (*library.Hook, string, error) + Enable(context.Context, *library.User, *api.Repo, *library.Hook) (*library.Hook, string, error) // Update defines a function that updates // a webhook for a specified repo. - Update(context.Context, *library.User, *library.Repo, int64) (bool, error) + Update(context.Context, *library.User, *api.Repo, int64) (bool, error) // Status defines a function that sends the // commit status for the given SHA from a repo. Status(context.Context, *library.User, *library.Build, string, string) error @@ -115,16 +116,16 @@ type Service interface { StepStatus(context.Context, *library.User, *library.Build, *library.Step, string, string) error // ListUserRepos defines a function that retrieves // all repos with admin rights for the user. - ListUserRepos(context.Context, *library.User) ([]*library.Repo, error) + ListUserRepos(context.Context, *library.User) ([]*api.Repo, error) // GetBranch defines a function that retrieves // a branch for a repo. - GetBranch(context.Context, *library.User, *library.Repo, string) (string, string, error) + GetBranch(context.Context, *api.Repo, string) (string, string, error) // GetPullRequest defines a function that retrieves // a pull request for a repo. - GetPullRequest(context.Context, *library.User, *library.Repo, int) (string, string, string, string, error) + GetPullRequest(context.Context, *api.Repo, int) (string, string, string, string, error) // GetRepo defines a function that retrieves // details for a repo. - GetRepo(context.Context, *library.User, *library.Repo) (*library.Repo, int, error) + GetRepo(context.Context, *library.User, *api.Repo) (*api.Repo, int, error) // GetOrgAndRepoName defines a function that retrieves // the name of the org and repo in the SCM. GetOrgAndRepoName(context.Context, *library.User, string, string) (string, string, error) @@ -139,13 +140,13 @@ type Service interface { // ProcessWebhook defines a function that // parses the webhook from a repo. - ProcessWebhook(context.Context, *http.Request) (*types.Webhook, error) + ProcessWebhook(context.Context, *http.Request) (*internal.Webhook, error) // VerifyWebhook defines a function that // verifies the webhook from a repo. - VerifyWebhook(context.Context, *http.Request, *library.Repo) error + VerifyWebhook(context.Context, *http.Request, *api.Repo) error // RedeliverWebhook defines a function that // redelivers the webhook from the SCM. - RedeliverWebhook(context.Context, *library.User, *library.Repo, *library.Hook) error + RedeliverWebhook(context.Context, *library.User, *api.Repo, *library.Hook) error // TODO: Add convert functions to interface? } diff --git a/secret/native/count.go b/secret/native/count.go index d76da83cc..379391d1b 100644 --- a/secret/native/count.go +++ b/secret/native/count.go @@ -6,8 +6,8 @@ import ( "context" "fmt" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" - "github.com/go-vela/types/library" "github.com/sirupsen/logrus" ) @@ -31,7 +31,7 @@ func (c *client) Count(ctx context.Context, sType, org, name string, teams []str }).Tracef("counting native %s secrets for %s/%s", sType, org, name) // create the repo with the information available - r := new(library.Repo) + r := new(api.Repo) r.SetOrg(org) r.SetName(name) r.SetFullName(fmt.Sprintf("%s/%s", org, name)) diff --git a/secret/native/get.go b/secret/native/get.go index ae80f1d09..059ee917e 100644 --- a/secret/native/get.go +++ b/secret/native/get.go @@ -6,6 +6,7 @@ import ( "context" "fmt" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/library" "github.com/sirupsen/logrus" @@ -33,7 +34,7 @@ func (c *client) Get(ctx context.Context, sType, org, name, path string) (*libra }).Tracef("getting native %s secret %s for %s/%s", sType, path, org, name) // create the repo with the information available - r := new(library.Repo) + r := new(api.Repo) r.SetOrg(org) r.SetName(name) r.SetFullName(fmt.Sprintf("%s/%s", org, name)) diff --git a/secret/native/list.go b/secret/native/list.go index 6e153c422..ab6f7d57b 100644 --- a/secret/native/list.go +++ b/secret/native/list.go @@ -6,6 +6,7 @@ import ( "context" "fmt" + api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" "github.com/go-vela/types/library" "github.com/sirupsen/logrus" @@ -36,7 +37,7 @@ func (c *client) List(ctx context.Context, sType, org, name string, page, perPag }).Tracef("listing native %s secrets for %s/%s", sType, org, name) // create the repo with the information available - r := new(library.Repo) + r := new(api.Repo) r.SetOrg(org) r.SetName(name) r.SetFullName(fmt.Sprintf("%s/%s", org, name)) diff --git a/util/encryption.go b/util/encryption.go new file mode 100644 index 000000000..06124d357 --- /dev/null +++ b/util/encryption.go @@ -0,0 +1,96 @@ +// SPDX-License-Identifier: Apache-2.0 + +package util + +import ( + "crypto/aes" + "crypto/cipher" + "crypto/rand" + "fmt" + "io" +) + +// Decrypt is a helper function to Decrypt values. First +// a AES-256 Galois Counter Mode cipher block is created +// from the encryption key to Decrypt the value. Then, we +// verify the value isn't smaller than the nonce which +// would indicate the value isn't encrypted. Finally the +// cipher block and nonce is used to Decrypt the value. +func Decrypt(key string, value []byte) ([]byte, error) { + // create a new cipher block from the encryption key + // + // the key should have a length of 64 bits to ensure + // we are using the AES-256 standard + // + // https://en.wikipedia.org/wiki/Advanced_Encryption_Standard + block, err := aes.NewCipher([]byte(key)) + if err != nil { + return value, err + } + + // creates a new Galois Counter Mode cipher block + gcm, err := cipher.NewGCM(block) + if err != nil { + return value, err + } + + // nonce is an arbitrary number used to to ensure that + // old communications cannot be reused in replay attacks. + // + // https://en.wikipedia.org/wiki/Cryptographic_nonce + nonceSize := gcm.NonceSize() + + // verify the value has a length greater than the nonce + // + // if the value is less than the nonce size, then we + // can assume the value hasn't been encrypted yet. + if len(value) < nonceSize { + return value, fmt.Errorf("invalid value length for decrypt provided: %d", len(value)) + } + + // capture nonce and ciphertext from the value + nonce, ciphertext := value[:nonceSize], value[nonceSize:] + + // decrypt the value from the ciphertext + return gcm.Open(nil, nonce, ciphertext, nil) +} + +// Encrypt is a helper function to Encrypt values. First +// a AES-256 Galois Counter Mode cipher block is created +// from the encryption key to Encrypt the value. Then, +// we create the nonce from a cryptographically secure +// random number generator. Finally, the cipher block +// and nonce is used to Encrypt the value. +func Encrypt(key string, value []byte) ([]byte, error) { + // create a new cipher block from the encryption key + // + // the key should have a length of 64 bits to ensure + // we are using the AES-256 standard + // + // https://en.wikipedia.org/wiki/Advanced_Encryption_Standard + block, err := aes.NewCipher([]byte(key)) + if err != nil { + return value, err + } + + // creates a new Galois Counter Mode cipher block + gcm, err := cipher.NewGCM(block) + if err != nil { + return value, err + } + + // nonce is an arbitrary number used to to ensure that + // old communications cannot be reused in replay attacks. + // + // https://en.wikipedia.org/wiki/Cryptographic_nonce + nonce := make([]byte, gcm.NonceSize()) + + // set nonce from a cryptographically secure random number generator + _, err = io.ReadFull(rand.Reader, nonce) + if err != nil { + return value, err + } + + // encrypt the value with the randomly generated nonce + return gcm.Seal(nonce, nonce, value, nil), nil +} diff --git a/util/encryption_test.go b/util/encryption_test.go new file mode 100644 index 000000000..efe2c8791 --- /dev/null +++ b/util/encryption_test.go @@ -0,0 +1,99 @@ +// SPDX-License-Identifier: Apache-2.0 + +package util + +import ( + "testing" +) + +func TestDatabase_decrypt(t *testing.T) { + // setup types + key := "C639A572E14D5075C526FDDD43E4ECF6" + value := []byte("abc") + + encrypted, err := Encrypt(key, value) + if err != nil { + t.Errorf("unable to encrypt value: %v", err) + } + + // setup tests + tests := []struct { + failure bool + key string + value []byte + }{ + { + failure: false, + key: key, + value: encrypted, + }, + { + failure: true, + key: "", + value: encrypted, + }, + { + failure: true, + key: key, + value: value, + }, + } + + // run tests + for _, test := range tests { + _, err := Decrypt(test.key, test.value) + + if test.failure { + if err == nil { + t.Errorf("decrypt should have returned err") + } + + continue + } + + if err != nil { + t.Errorf("decrypt returned err: %v", err) + } + } +} + +func TestDatabase_encrypt(t *testing.T) { + // setup types + key := "C639A572E14D5075C526FDDD43E4ECF6" + value := []byte("abc") + + // setup tests + tests := []struct { + failure bool + key string + value []byte + }{ + { + failure: false, + key: key, + value: value, + }, + { + failure: true, + key: "", + value: value, + }, + } + + // run tests + for _, test := range tests { + _, err := Encrypt(test.key, test.value) + + if test.failure { + if err == nil { + t.Errorf("encrypt should have returned err") + } + + continue + } + + if err != nil { + t.Errorf("encrypt returned err: %v", err) + } + } +} diff --git a/util/util.go b/util/util.go index 401c6a814..60d709765 100644 --- a/util/util.go +++ b/util/util.go @@ -8,7 +8,7 @@ import ( "net/url" "strings" - "github.com/go-vela/types/library" + api "github.com/go-vela/server/api/types" "github.com/microcosm-cc/bluemonday" "github.com/gin-gonic/gin" @@ -117,7 +117,7 @@ func Unique(stringSlice []string) []string { // allowlist are specified. // // a single entry of '*' allows any repo to be enabled. -func CheckAllowlist(r *library.Repo, allowlist []string) bool { +func CheckAllowlist(r *api.Repo, allowlist []string) bool { // check if all repos are allowed to be enabled if len(allowlist) == 1 && allowlist[0] == "*" { return true