diff --git a/.env.example b/.env.example index 6f209ab99..8ab90001b 100644 --- a/.env.example +++ b/.env.example @@ -12,7 +12,7 @@ # These are used by the ui service # defined in the docker compose stack -# customize the location where you want users to provide feedack +# customize the location where you want users to provide feedback # # default: https://github.com/go-vela/ui/issues/new # VELA_FEEDBACK_URL= @@ -44,10 +44,10 @@ VELA_API=http://localhost:8080 # github web url (only required if using GitHub Enterprise) # # default: https://github.com -# VELA_SOURCE_ADDR= +# VELA_SCM_ADDR= # github client id from oauth application -VELA_SOURCE_CLIENT= +VELA_SCM_CLIENT= # github client secret from oauth application -VELA_SOURCE_SECRET= +VELA_SCM_SECRET= diff --git a/api/authenticate.go b/api/authenticate.go index b5561b0c0..427a2415f 100644 --- a/api/authenticate.go +++ b/api/authenticate.go @@ -11,7 +11,7 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/token" - "github.com/go-vela/server/source" + "github.com/go-vela/server/scm" "github.com/go-vela/server/util" "github.com/go-vela/types" @@ -76,7 +76,7 @@ func Authenticate(c *gin.Context) { code := c.Request.FormValue("code") if len(code) == 0 { // start the initial OAuth workflow - oAuthState, err = source.FromContext(c).Login(c.Writer, c.Request) + oAuthState, err = scm.FromContext(c).Login(c.Writer, c.Request) if err != nil { retErr := fmt.Errorf("unable to login user: %w", err) @@ -87,7 +87,7 @@ func Authenticate(c *gin.Context) { } // complete the OAuth workflow and authenticates the user - newUser, err := source.FromContext(c).Authenticate(c.Writer, c.Request, oAuthState) + newUser, err := scm.FromContext(c).Authenticate(c.Writer, c.Request, oAuthState) if err != nil { retErr := fmt.Errorf("unable to authenticate user: %w", err) @@ -308,7 +308,7 @@ func AuthenticateType(c *gin.Context) { // the API. func AuthenticateToken(c *gin.Context) { // attempt to get user from source - u, err := source.FromContext(c).AuthenticateToken(c.Request) + u, err := scm.FromContext(c).AuthenticateToken(c.Request) if err != nil { retErr := fmt.Errorf("unable to authenticate user: %w", err) diff --git a/api/build.go b/api/build.go index 5b5c77254..561620923 100644 --- a/api/build.go +++ b/api/build.go @@ -20,7 +20,7 @@ import ( "github.com/go-vela/server/router/middleware/executors" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" - "github.com/go-vela/server/source" + "github.com/go-vela/server/scm" "github.com/go-vela/server/util" "github.com/go-vela/types" @@ -163,7 +163,7 @@ func CreateBuild(c *gin.Context) { // check if the build event is not pull_request if !strings.EqualFold(input.GetEvent(), constants.EventPull) { // send API call to capture list of files changed for the commit - files, err = source.FromContext(c).Changeset(u, r, input.GetCommit()) + files, err = scm.FromContext(c).Changeset(u, r, input.GetCommit()) if err != nil { // nolint: lll // ignore long line length due to error message retErr := fmt.Errorf("unable to process webhook: failed to get changeset for %s: %w", r.GetFullName(), err) @@ -188,7 +188,7 @@ func CreateBuild(c *gin.Context) { } // send API call to capture list of files changed for the pull request - files, err = source.FromContext(c).ChangesetPR(u, r, number) + files, err = scm.FromContext(c).ChangesetPR(u, r, number) if err != nil { // nolint: lll // ignore long line length due to error message retErr := fmt.Errorf("unable to process webhook: failed to get changeset for %s: %w", r.GetFullName(), err) @@ -200,7 +200,7 @@ func CreateBuild(c *gin.Context) { } // send API call to capture the pipeline configuration file - config, err := source.FromContext(c).ConfigBackoff(u, r, input.GetCommit()) + config, err := scm.FromContext(c).ConfigBackoff(u, r, input.GetCommit()) if err != nil { // nolint: lll // ignore long line length due to error message retErr := fmt.Errorf("unable to get pipeline configuration for %s/%d: %w", r.GetFullName(), input.GetNumber(), err) @@ -234,7 +234,7 @@ func CreateBuild(c *gin.Context) { input.SetStatus(constants.StatusSuccess) // send API call to set the status on the commit - err = source.FromContext(c).Status(u, input, r.GetOrg(), r.GetName()) + err = scm.FromContext(c).Status(u, input, r.GetOrg(), r.GetName()) if err != nil { // nolint: lll // ignore long line length due to error message logrus.Errorf("unable to set commit status for %s/%d: %v", r.GetFullName(), input.GetNumber(), err) @@ -258,7 +258,7 @@ func CreateBuild(c *gin.Context) { c.JSON(http.StatusCreated, input) // send API call to set the status on the commit - err = source.FromContext(c).Status(u, input, r.GetOrg(), r.GetName()) + err = scm.FromContext(c).Status(u, input, r.GetOrg(), r.GetName()) if err != nil { // nolint: lll // ignore long line length due to error message logrus.Errorf("unable to set commit status for build %s/%d: %v", r.GetFullName(), input.GetNumber(), err) @@ -613,7 +613,7 @@ func GetOrgBuilds(c *gin.Context) { perPage = util.MaxInt(1, util.MinInt(100, perPage)) // See if the user is an org admin to bypass individual permission checks - perm, err := source.FromContext(c).OrgAccess(u, o) + perm, err := scm.FromContext(c).OrgAccess(u, o) if err != nil { logrus.Errorf("unable to get user %s access level for org %s", u.GetName(), o) } @@ -801,7 +801,7 @@ func RestartBuild(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 = source.FromContext(c).Changeset(u, r, b.GetCommit()) + files, err = scm.FromContext(c).Changeset(u, r, b.GetCommit()) if err != nil { // nolint: lll // ignore long line length due to error message retErr := fmt.Errorf("unable to process webhook: failed to get changeset for %s: %w", r.GetFullName(), err) @@ -826,7 +826,7 @@ func RestartBuild(c *gin.Context) { } // send API call to capture list of files changed for the pull request - files, err = source.FromContext(c).ChangesetPR(u, r, number) + files, err = scm.FromContext(c).ChangesetPR(u, r, number) if err != nil { // nolint: lll // ignore long line length due to error message retErr := fmt.Errorf("unable to process webhook: failed to get changeset for %s: %w", r.GetFullName(), err) @@ -838,7 +838,7 @@ func RestartBuild(c *gin.Context) { } // send API call to capture the pipeline configuration file - config, err := source.FromContext(c).ConfigBackoff(u, r, b.GetCommit()) + config, err := scm.FromContext(c).ConfigBackoff(u, r, b.GetCommit()) if err != nil { // nolint: lll // ignore long line length due to error message retErr := fmt.Errorf("unable to get pipeline configuration for %s/%d: %w", r.GetFullName(), b.GetNumber(), err) @@ -872,7 +872,7 @@ func RestartBuild(c *gin.Context) { b.SetStatus(constants.StatusSkipped) // send API call to set the status on the commit - err = source.FromContext(c).Status(u, b, r.GetOrg(), r.GetName()) + err = scm.FromContext(c).Status(u, b, r.GetOrg(), r.GetName()) if err != nil { logrus.Errorf("unable to set commit status for %s/%d: %v", r.GetFullName(), b.GetNumber(), err) } @@ -904,7 +904,7 @@ func RestartBuild(c *gin.Context) { c.JSON(http.StatusCreated, b) // send API call to set the status on the commit - err = source.FromContext(c).Status(u, b, r.GetOrg(), r.GetName()) + err = scm.FromContext(c).Status(u, b, r.GetOrg(), r.GetName()) if err != nil { // nolint: lll // ignore long line length due to error message logrus.Errorf("unable to set commit status for build %s/%d: %v", r.GetFullName(), b.GetNumber(), err) @@ -1067,7 +1067,7 @@ func UpdateBuild(c *gin.Context) { } // send API call to set the status on the commit - err = source.FromContext(c).Status(u, b, r.GetOrg(), r.GetName()) + err = scm.FromContext(c).Status(u, b, r.GetOrg(), r.GetName()) if err != nil { // nolint: lll // ignore long line length due to error message logrus.Errorf("unable to set commit status for build %s/%d: %v", r.GetFullName(), b.GetNumber(), err) diff --git a/api/deployment.go b/api/deployment.go index b168ab0d9..6e8d285c4 100644 --- a/api/deployment.go +++ b/api/deployment.go @@ -12,7 +12,7 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" - "github.com/go-vela/server/source" + "github.com/go-vela/server/scm" "github.com/go-vela/server/util" "github.com/go-vela/types/library" @@ -89,7 +89,7 @@ func CreateDeployment(c *gin.Context) { } // send API call to create the deployment - err = source.FromContext(c).CreateDeployment(u, r, input) + err = scm.FromContext(c).CreateDeployment(u, r, input) if err != nil { retErr := fmt.Errorf("unable to create new deployment for %s: %w", r.GetFullName(), err) @@ -191,7 +191,7 @@ func GetDeployments(c *gin.Context) { perPage = util.MaxInt(1, util.MinInt(100, perPage)) // send API call to capture the total number of deployments for the repo - t, err := source.FromContext(c).GetDeploymentCount(u, r) + t, err := scm.FromContext(c).GetDeploymentCount(u, r) if err != nil { retErr := fmt.Errorf("unable to get deployment count for %s: %w", r.GetFullName(), err) @@ -201,7 +201,7 @@ func GetDeployments(c *gin.Context) { } // send API call to capture the list of steps for the build - d, err := source.FromContext(c).GetDeploymentList(u, r, page, perPage) + d, err := scm.FromContext(c).GetDeploymentList(u, r, page, perPage) if err != nil { retErr := fmt.Errorf("unable to get deployments for %s: %w", r.GetFullName(), err) @@ -302,7 +302,7 @@ func GetDeployment(c *gin.Context) { } // send API call to capture the deployment - d, err := source.FromContext(c).GetDeployment(u, r, int64(number)) + d, err := scm.FromContext(c).GetDeployment(u, r, int64(number)) if err != nil { retErr := fmt.Errorf("unable to get deployment %s/%d: %w", r.GetFullName(), number, err) diff --git a/api/pipeline.go b/api/pipeline.go index 8d8b9a5fd..d4ace4cd1 100644 --- a/api/pipeline.go +++ b/api/pipeline.go @@ -11,10 +11,10 @@ import ( "github.com/go-vela/server/compiler" "github.com/go-vela/server/compiler/registry/github" + "github.com/go-vela/server/scm" "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/repo" - "github.com/go-vela/server/source" "github.com/go-vela/server/util" "github.com/go-vela/types" @@ -94,7 +94,7 @@ func GetPipeline(c *gin.Context) { } // send API call to capture the pipeline configuration file - config, err := source.FromContext(c).ConfigBackoff(u, r, ref) + config, err := scm.FromContext(c).ConfigBackoff(u, r, ref) if err != nil { // nolint: lll // ignore long line length due to error message retErr := fmt.Errorf("unable to get pipeline configuration for %s@%s: %w", r.GetFullName(), ref, err) @@ -196,7 +196,7 @@ func GetTemplates(c *gin.Context) { } // send API call to capture the pipeline configuration file - config, err := source.FromContext(c).ConfigBackoff(u, r, ref) + config, err := scm.FromContext(c).ConfigBackoff(u, r, ref) if err != nil { // nolint: lll // ignore long line length due to error message retErr := fmt.Errorf("unable to get pipeline configuration for %s@%s: %w", r.GetFullName(), ref, err) @@ -309,7 +309,7 @@ func ExpandPipeline(c *gin.Context) { } // send API call to capture the pipeline configuration file - config, err := source.FromContext(c).ConfigBackoff(u, r, ref) + config, err := scm.FromContext(c).ConfigBackoff(u, r, ref) if err != nil { // nolint: lll // ignore long line length due to error message retErr := fmt.Errorf("unable to get pipeline configuration for %s@%s: %w", r.GetFullName(), ref, err) @@ -440,7 +440,7 @@ func ValidatePipeline(c *gin.Context) { } // send API call to capture the pipeline configuration file - config, err := source.FromContext(c).ConfigBackoff(u, r, ref) + config, err := scm.FromContext(c).ConfigBackoff(u, r, ref) if err != nil { // nolint: lll // ignore long line length due to error message retErr := fmt.Errorf("unable to get pipeline configuration for %s@%s: %w", r.GetFullName(), ref, err) @@ -579,7 +579,7 @@ func CompilePipeline(c *gin.Context) { } // send API call to capture the pipeline configuration file - config, err := source.FromContext(c).ConfigBackoff(u, r, ref) + config, err := scm.FromContext(c).ConfigBackoff(u, r, ref) if err != nil { // nolint: lll // ignore long line length due to error message retErr := fmt.Errorf("unable to get pipeline configuration for %s@%s: %w", r.GetFullName(), ref, err) @@ -705,7 +705,7 @@ func setTemplateLinks(c *gin.Context, u *library.User, templates yaml.TemplateSl } // retrieve link to template file from github - link, err := source.FromContext(c).GetHTMLURL(u, src.Org, src.Repo, src.Name, src.Ref) + link, err := scm.FromContext(c).GetHTMLURL(u, src.Org, src.Repo, src.Name, src.Ref) if err != nil { retErr := fmt.Errorf("unable to get html url for %s/%s/%s/@%s: %w", src.Org, src.Repo, src.Name, src.Ref, err) diff --git a/api/repo.go b/api/repo.go index 809df942d..0fe9ee9c2 100644 --- a/api/repo.go +++ b/api/repo.go @@ -14,7 +14,7 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" - "github.com/go-vela/server/source" + "github.com/go-vela/server/scm" "github.com/go-vela/server/util" "github.com/go-vela/types/constants" @@ -91,7 +91,7 @@ func CreateRepo(c *gin.Context) { } // get repo information from the source - r, err := source.FromContext(c).GetRepo(u, input) + r, err := scm.FromContext(c).GetRepo(u, input) if err != nil { retErr := fmt.Errorf("unable to retrieve repo info for %s from source: %w", r.GetFullName(), err) @@ -204,7 +204,7 @@ func CreateRepo(c *gin.Context) { // send API call to create the webhook if c.Value("webhookvalidation").(bool) { - _, err = source.FromContext(c).Enable(u, r.GetOrg(), r.GetName(), r.GetHash()) + _, err = scm.FromContext(c).Enable(u, r.GetOrg(), r.GetName(), r.GetHash()) if err != nil { retErr := fmt.Errorf("unable to create webhook for %s: %w", r.GetFullName(), err) @@ -457,7 +457,7 @@ func GetOrgRepos(c *gin.Context) { perPage = util.MaxInt(1, util.MinInt(100, perPage)) // See if the user is an org admin to bypass individual permission checks - perm, err := source.FromContext(c).OrgAccess(u, org) + perm, err := scm.FromContext(c).OrgAccess(u, org) if err != nil { logrus.Errorf("unable to get user %s access level for org %s", u.GetName(), org) } @@ -780,7 +780,7 @@ func DeleteRepo(c *gin.Context) { logrus.Infof("Deleting repo %s", r.GetFullName()) // send API call to remove the webhook - err := source.FromContext(c).Disable(u, r.GetOrg(), r.GetName()) + err := scm.FromContext(c).Disable(u, r.GetOrg(), r.GetName()) if err != nil { retErr := fmt.Errorf("unable to delete webhook for %s: %w", r.GetFullName(), err) @@ -854,7 +854,7 @@ func RepairRepo(c *gin.Context) { // capture middleware values r := repo.Retrieve(c) u := user.Retrieve(c) - s := source.FromContext(c) + s := scm.FromContext(c) logrus.Infof("Repairing repo %s", r.GetFullName()) @@ -1013,7 +1013,7 @@ func SyncRepos(c *gin.Context) { logrus.Infof("Reading repos for org %s", org) // See if the user is an org admin to bypass individual permission checks - perm, err := source.FromContext(c).OrgAccess(u, org) + perm, err := scm.FromContext(c).OrgAccess(u, org) if err != nil { logrus.Errorf("unable to get user %s access level for org %s", u.GetName(), org) } @@ -1053,7 +1053,7 @@ func SyncRepos(c *gin.Context) { // iterate through captured repos and check if they are in GitHub for _, repo := range repos { - _, err := source.FromContext(c).GetRepo(u, repo) + _, err := scm.FromContext(c).GetRepo(u, repo) // if repo cannot be captured from GitHub, set to inactive in database if err != nil { repo.SetActive(false) @@ -1115,7 +1115,7 @@ func SyncRepo(c *gin.Context) { r, _ := database.FromContext(c).GetRepo(org, repo) // retrieve repo from source code manager service - _, err := source.FromContext(c).GetRepo(u, r) + _, err := scm.FromContext(c).GetRepo(u, r) // if there is an error retrieving repo, we know it is deleted: sync time if err != nil { diff --git a/api/secret.go b/api/secret.go index 5fd85cb3a..ce3c0042f 100644 --- a/api/secret.go +++ b/api/secret.go @@ -11,8 +11,8 @@ import ( "strings" "github.com/go-vela/server/router/middleware/user" + "github.com/go-vela/server/scm" "github.com/go-vela/server/secret" - "github.com/go-vela/server/source" "github.com/go-vela/server/util" "github.com/go-vela/types/constants" @@ -232,7 +232,7 @@ func GetSecrets(c *gin.Context) { // get list of user's teams if type is shared secret and team is '*' if t == "shared" && n == "*" { var err error - teams, err = source.FromContext(c).ListUsersTeamsForOrg(u, o) + teams, err = scm.FromContext(c).ListUsersTeamsForOrg(u, o) if err != nil { retErr := fmt.Errorf("unable to get users %s teams for org %s: %v", u.GetName(), o, err) diff --git a/api/user.go b/api/user.go index c35c76654..2dd092c5f 100644 --- a/api/user.go +++ b/api/user.go @@ -13,7 +13,7 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/token" "github.com/go-vela/server/router/middleware/user" - "github.com/go-vela/server/source" + "github.com/go-vela/server/scm" "github.com/go-vela/server/util" "github.com/go-vela/types/library" @@ -379,7 +379,7 @@ func GetUserSourceRepos(c *gin.Context) { output := make(map[string][]library.Repo) // send API call to capture the list of repos for the user - srcRepos, err := source.FromContext(c).ListUserRepos(u) + srcRepos, err := scm.FromContext(c).ListUserRepos(u) if err != nil { retErr := fmt.Errorf("unable to get source repos for user %s: %w", u.GetName(), err) diff --git a/api/webhook.go b/api/webhook.go index 8fc00ba04..78489979f 100644 --- a/api/webhook.go +++ b/api/webhook.go @@ -17,7 +17,7 @@ import ( "github.com/go-vela/server/compiler" "github.com/go-vela/server/database" "github.com/go-vela/server/queue" - "github.com/go-vela/server/source" + "github.com/go-vela/server/scm" "github.com/go-vela/server/util" "github.com/go-vela/types" @@ -114,7 +114,7 @@ func PostWebhook(c *gin.Context) { // process the webhook from the source control provider // comment, number, h, r, b - webhook, err := source.FromContext(c).ProcessWebhook(c.Request) + webhook, err := scm.FromContext(c).ProcessWebhook(c.Request) if err != nil { retErr := fmt.Errorf("unable to parse webhook: %v", err) util.HandleError(c, http.StatusBadRequest, retErr) @@ -210,7 +210,7 @@ func PostWebhook(c *gin.Context) { // verify the webhook from the source control provider if c.Value("webhookvalidation").(bool) { - err = source.FromContext(c).VerifyWebhook(dupRequest, r) + err = scm.FromContext(c).VerifyWebhook(dupRequest, r) if err != nil { retErr := fmt.Errorf("unable to verify webhook: %v", err) util.HandleError(c, http.StatusUnauthorized, retErr) @@ -280,7 +280,7 @@ func PostWebhook(c *gin.Context) { // if this is a comment on a pull_request event if strings.EqualFold(b.GetEvent(), constants.EventComment) && webhook.PRNumber > 0 { // nolint: lll // ignore long line length due to variable names - commit, branch, baseref, headref, err := source.FromContext(c).GetPullRequest(u, r, webhook.PRNumber) + commit, branch, baseref, headref, err := scm.FromContext(c).GetPullRequest(u, r, webhook.PRNumber) if err != nil { // nolint: lll // ignore long line length due to error message retErr := fmt.Errorf("%s: failed to get pull request info for %s: %v", baseErr, r.GetFullName(), err) @@ -305,7 +305,7 @@ func PostWebhook(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 = source.FromContext(c).Changeset(u, r, b.GetCommit()) + files, err = scm.FromContext(c).Changeset(u, r, b.GetCommit()) if err != nil { retErr := fmt.Errorf("%s: failed to get changeset for %s: %v", baseErr, r.GetFullName(), err) util.HandleError(c, http.StatusInternalServerError, retErr) @@ -321,7 +321,7 @@ func PostWebhook(c *gin.Context) { // check if the build event is a pull_request if strings.EqualFold(b.GetEvent(), constants.EventPull) && webhook.PRNumber > 0 { // send API call to capture list of files changed for the pull request - files, err = source.FromContext(c).ChangesetPR(u, r, webhook.PRNumber) + files, err = scm.FromContext(c).ChangesetPR(u, r, webhook.PRNumber) if err != nil { retErr := fmt.Errorf("%s: failed to get changeset for %s: %v", baseErr, r.GetFullName(), err) util.HandleError(c, http.StatusInternalServerError, retErr) @@ -334,7 +334,7 @@ func PostWebhook(c *gin.Context) { } // send API call to capture the pipeline configuration file - config, err := source.FromContext(c).ConfigBackoff(u, r, b.GetCommit()) + config, err := scm.FromContext(c).ConfigBackoff(u, r, b.GetCommit()) if err != nil { // nolint: lll // ignore long line length due to error message retErr := fmt.Errorf("%s: failed to get pipeline configuration for %s: %v", baseErr, r.GetFullName(), err) @@ -426,7 +426,7 @@ func PostWebhook(c *gin.Context) { b.SetStatus(constants.StatusSkipped) // send API call to set the status on the commit - err = source.FromContext(c).Status(u, b, r.GetOrg(), r.GetName()) + err = scm.FromContext(c).Status(u, b, r.GetOrg(), r.GetName()) if err != nil { logrus.Errorf("unable to set commit status for %s/%d: %v", r.GetFullName(), b.GetNumber(), err) } @@ -494,7 +494,7 @@ func PostWebhook(c *gin.Context) { c.JSON(http.StatusOK, b) // send API call to set the status on the commit - err = source.FromContext(c).Status(u, b, r.GetOrg(), r.GetName()) + err = scm.FromContext(c).Status(u, 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/main.go b/cmd/vela-server/main.go index eb39ea802..d03d9bd2c 100644 --- a/cmd/vela-server/main.go +++ b/cmd/vela-server/main.go @@ -12,8 +12,8 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/queue" + "github.com/go-vela/server/scm" "github.com/go-vela/server/secret" - "github.com/go-vela/server/source" "github.com/go-vela/server/version" "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" @@ -180,7 +180,7 @@ func main() { // Source Flags - app.Flags = append(app.Flags, source.Flags...) + app.Flags = append(app.Flags, scm.Flags...) // set logrus to log in JSON format logrus.SetFormatter(&logrus.JSONFormatter{}) diff --git a/cmd/vela-server/metadata.go b/cmd/vela-server/metadata.go index ac8bcd859..272bf77df 100644 --- a/cmd/vela-server/metadata.go +++ b/cmd/vela-server/metadata.go @@ -85,13 +85,13 @@ func metadataQueue(c *cli.Context) (*types.Queue, error) { func metadataSource(c *cli.Context) (*types.Source, error) { logrus.Trace("Creating source metadata from CLI configuration") - u, err := url.Parse(c.String("source.addr")) + u, err := url.Parse(c.String("scm.addr")) if err != nil { return nil, err } return &types.Source{ - Driver: c.String("source.driver"), + Driver: c.String("scm.driver"), Host: u.Host, }, nil } diff --git a/cmd/vela-server/scm.go b/cmd/vela-server/scm.go new file mode 100644 index 000000000..e5ff39f0c --- /dev/null +++ b/cmd/vela-server/scm.go @@ -0,0 +1,35 @@ +// Copyright (c) 2021 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package main + +import ( + "github.com/go-vela/server/scm" + "github.com/sirupsen/logrus" + + "github.com/urfave/cli/v2" +) + +// helper function to setup the scm from the CLI arguments. +func setupSCM(c *cli.Context) (scm.Service, error) { + logrus.Debug("Creating scm client from CLI configuration") + + // scm configuration + _setup := &scm.Setup{ + Driver: c.String("scm.driver"), + Address: c.String("scm.addr"), + ClientID: c.String("scm.client"), + ClientSecret: c.String("scm.secret"), + ServerAddress: c.String("server-addr"), + ServerWebhookAddress: c.String("scm.webhook.addr"), + StatusContext: c.String("scm.context"), + WebUIAddress: c.String("webui-addr"), + Scopes: c.StringSlice("scm.scopes"), + } + + // setup the scm + // + // https://pkg.go.dev/github.com/go-vela/server/scm?tab=doc#New + return scm.New(_setup) +} diff --git a/cmd/vela-server/server.go b/cmd/vela-server/server.go index 90393dadb..d4e8578bd 100644 --- a/cmd/vela-server/server.go +++ b/cmd/vela-server/server.go @@ -74,7 +74,7 @@ func server(c *cli.Context) error { return err } - source, err := setupSource(c) + scm, err := setupSCM(c) if err != nil { return err } @@ -93,7 +93,7 @@ func server(c *cli.Context) error { middleware.RequestVersion, middleware.Secret(c.String("vela-secret")), middleware.Secrets(secrets), - middleware.Source(source), + middleware.Scm(scm), middleware.Allowlist(c.StringSlice("vela-repo-allowlist")), middleware.DefaultTimeout(c.Int64("default-build-timeout")), middleware.WebhookValidation(!c.Bool("vela-disable-webhook-validation")), diff --git a/cmd/vela-server/source.go b/cmd/vela-server/source.go deleted file mode 100644 index 3b3bb7a9f..000000000 --- a/cmd/vela-server/source.go +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) 2021 Target Brands, Inc. All rights reserved. -// -// Use of this source code is governed by the LICENSE file in this repository. - -package main - -import ( - "github.com/go-vela/server/source" - - "github.com/sirupsen/logrus" - - "github.com/urfave/cli/v2" -) - -// helper function to setup the source from the CLI arguments. -func setupSource(c *cli.Context) (source.Service, error) { - logrus.Debug("Creating source client from CLI configuration") - - // source configuration - _setup := &source.Setup{ - Driver: c.String("source.driver"), - Address: c.String("source.addr"), - ClientID: c.String("source.client"), - ClientSecret: c.String("source.secret"), - ServerAddress: c.String("server-addr"), - ServerWebhookAddress: c.String("source.webhook.addr"), - StatusContext: c.String("source.context"), - WebUIAddress: c.String("webui-addr"), - Scopes: c.StringSlice("source.scopes"), - } - - // setup the source - // - // https://pkg.go.dev/github.com/go-vela/server/source?tab=doc#New - return source.New(_setup) -} diff --git a/docker-compose.yml b/docker-compose.yml index da9fb0e76..6c04bff57 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -29,8 +29,8 @@ services: QUEUE_DRIVER: redis QUEUE_ADDR: 'redis://redis:6379' QUEUE_ROUTES: 'docker,local,docker:local' - SOURCE_DRIVER: github - SOURCE_CONTEXT: 'continuous-integration/vela' + SCM_DRIVER: github + SCM_CONTEXT: 'continuous-integration/vela' SECRET_VAULT: 'true' SECRET_VAULT_ADDR: 'http://vault:8200' SECRET_VAULT_TOKEN: vela diff --git a/router/middleware/perm/perm.go b/router/middleware/perm/perm.go index d951490b0..9b9636b16 100644 --- a/router/middleware/perm/perm.go +++ b/router/middleware/perm/perm.go @@ -12,7 +12,7 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/user" - "github.com/go-vela/server/source" + "github.com/go-vela/server/scm" "github.com/go-vela/server/util" "github.com/go-vela/types/constants" @@ -60,7 +60,7 @@ func MustSecretAdmin() gin.HandlerFunc { case constants.SecretOrg: logrus.Debugf("Verifying user %s has 'admin' permissions for org %s", u.GetName(), o) - perm, err := source.FromContext(c).OrgAccess(u, o) + perm, err := scm.FromContext(c).OrgAccess(u, o) if err != nil { logrus.Errorf("unable to get user %s access level for org %s: %v", u.GetName(), o, err) } @@ -75,7 +75,7 @@ func MustSecretAdmin() gin.HandlerFunc { case constants.SecretRepo: logrus.Debugf("Verifying user %s has 'admin' permissions for repo %s/%s", u.GetName(), o, n) - perm, err := source.FromContext(c).RepoAccess(u, u.GetToken(), o, n) + perm, err := scm.FromContext(c).RepoAccess(u, u.GetToken(), o, n) if err != nil { logrus.Errorf("unable to get user %s access level for repo %s/%s: %v", u.GetName(), o, n, err) } @@ -92,7 +92,7 @@ func MustSecretAdmin() gin.HandlerFunc { if n == "*" && m == "GET" { logrus.Debugf("Gathering teams user %s is a member of in the org %s", u.GetName(), o) - teams, err := source.FromContext(c).ListUsersTeamsForOrg(u, o) + teams, err := scm.FromContext(c).ListUsersTeamsForOrg(u, o) if err != nil { logrus.Errorf("unable to get users %s teams for org %s: %v", u.GetName(), o, err) } @@ -106,7 +106,7 @@ func MustSecretAdmin() gin.HandlerFunc { } } else { logrus.Debugf("Verifying user %s has 'admin' permissions for team %s/%s", u.GetName(), o, n) - perm, err := source.FromContext(c).TeamAccess(u, o, n) + perm, err := scm.FromContext(c).TeamAccess(u, o, n) if err != nil { logrus.Errorf("unable to get user %s access level for team %s/%s: %v", u.GetName(), o, n, err) } @@ -145,7 +145,7 @@ func MustAdmin() gin.HandlerFunc { } // query source to determine requesters permissions for the repo using the requester's token - perm, err := source.FromContext(c).RepoAccess(u, u.GetToken(), r.GetOrg(), r.GetName()) + perm, err := scm.FromContext(c).RepoAccess(u, u.GetToken(), r.GetOrg(), r.GetName()) if err != nil { // requester may not have permissions to use the Github API endpoint (requires read access) // try again using the repo owner token @@ -160,7 +160,7 @@ func MustAdmin() gin.HandlerFunc { return } - perm, err = source.FromContext(c).RepoAccess(u, ro.GetToken(), r.GetOrg(), r.GetName()) + perm, err = scm.FromContext(c).RepoAccess(u, ro.GetToken(), r.GetOrg(), r.GetName()) if err != nil { logrus.Errorf("unable to get user %s access level for repo %s", u.GetName(), r.GetFullName()) } @@ -196,7 +196,7 @@ func MustWrite() gin.HandlerFunc { } // query source to determine requesters permissions for the repo using the requester's token - perm, err := source.FromContext(c).RepoAccess(u, u.GetToken(), r.GetOrg(), r.GetName()) + perm, err := scm.FromContext(c).RepoAccess(u, u.GetToken(), r.GetOrg(), r.GetName()) if err != nil { // requester may not have permissions to use the Github API endpoint (requires read access) // try again using the repo owner token @@ -211,7 +211,7 @@ func MustWrite() gin.HandlerFunc { return } - perm, err = source.FromContext(c).RepoAccess(u, ro.GetToken(), r.GetOrg(), r.GetName()) + perm, err = scm.FromContext(c).RepoAccess(u, ro.GetToken(), r.GetOrg(), r.GetName()) if err != nil { logrus.Errorf("unable to get user %s access level for repo %s", u.GetName(), r.GetFullName()) } @@ -255,7 +255,7 @@ func MustRead() gin.HandlerFunc { } // query source to determine requesters permissions for the repo using the requester's token - perm, err := source.FromContext(c).RepoAccess(u, u.GetToken(), r.GetOrg(), r.GetName()) + perm, err := scm.FromContext(c).RepoAccess(u, u.GetToken(), r.GetOrg(), r.GetName()) if err != nil { // requester may not have permissions to use the Github API endpoint (requires read access) // try again using the repo owner token @@ -270,7 +270,7 @@ func MustRead() gin.HandlerFunc { return } - perm, err = source.FromContext(c).RepoAccess(u, ro.GetToken(), r.GetOrg(), r.GetName()) + perm, err = scm.FromContext(c).RepoAccess(u, ro.GetToken(), r.GetOrg(), r.GetName()) if err != nil { logrus.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 8a18af7b2..7a66019f1 100644 --- a/router/middleware/perm/perm_test.go +++ b/router/middleware/perm/perm_test.go @@ -17,8 +17,8 @@ import ( "github.com/go-vela/server/router/middleware/repo" "github.com/go-vela/server/router/middleware/token" "github.com/go-vela/server/router/middleware/user" - "github.com/go-vela/server/source" - "github.com/go-vela/server/source/github" + "github.com/go-vela/server/scm" + "github.com/go-vela/server/scm/github" "github.com/go-vela/types/library" ) @@ -71,7 +71,7 @@ func TestPerm_MustPlatformAdmin(t *testing.T) { // setup vela mock server engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) - engine.Use(func(c *gin.Context) { source.ToContext(c, client) }) + engine.Use(func(c *gin.Context) { scm.ToContext(c, client) }) engine.Use(user.Establish()) engine.Use(MustPlatformAdmin()) engine.GET("/admin/users", func(c *gin.Context) { @@ -136,7 +136,7 @@ func TestPerm_MustPlatformAdmin_NotAdmin(t *testing.T) { // setup vela mock server engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) - engine.Use(func(c *gin.Context) { source.ToContext(c, client) }) + engine.Use(func(c *gin.Context) { scm.ToContext(c, client) }) engine.Use(user.Establish()) engine.Use(MustPlatformAdmin()) engine.GET("/admin/users", func(c *gin.Context) { @@ -215,7 +215,7 @@ func TestPerm_MustAdmin(t *testing.T) { // setup vela mock server engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) - engine.Use(func(c *gin.Context) { source.ToContext(c, client) }) + engine.Use(func(c *gin.Context) { scm.ToContext(c, client) }) engine.Use(user.Establish()) engine.Use(repo.Establish()) engine.Use(MustAdmin()) @@ -295,7 +295,7 @@ func TestPerm_MustAdmin_PlatAdmin(t *testing.T) { // setup vela mock server engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) - engine.Use(func(c *gin.Context) { source.ToContext(c, client) }) + engine.Use(func(c *gin.Context) { scm.ToContext(c, client) }) engine.Use(user.Establish()) engine.Use(repo.Establish()) engine.Use(MustAdmin()) @@ -375,7 +375,7 @@ func TestPerm_MustAdmin_NotAdmin(t *testing.T) { // setup vela mock server engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) - engine.Use(func(c *gin.Context) { source.ToContext(c, client) }) + engine.Use(func(c *gin.Context) { scm.ToContext(c, client) }) engine.Use(user.Establish()) engine.Use(repo.Establish()) engine.Use(MustAdmin()) @@ -455,7 +455,7 @@ func TestPerm_MustWrite(t *testing.T) { // setup vela mock server engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) - engine.Use(func(c *gin.Context) { source.ToContext(c, client) }) + engine.Use(func(c *gin.Context) { scm.ToContext(c, client) }) engine.Use(user.Establish()) engine.Use(repo.Establish()) engine.Use(MustWrite()) @@ -535,7 +535,7 @@ func TestPerm_MustWrite_PlatAdmin(t *testing.T) { // setup vela mock server engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) - engine.Use(func(c *gin.Context) { source.ToContext(c, client) }) + engine.Use(func(c *gin.Context) { scm.ToContext(c, client) }) engine.Use(user.Establish()) engine.Use(repo.Establish()) engine.Use(MustWrite()) @@ -615,7 +615,7 @@ func TestPerm_MustWrite_RepoAdmin(t *testing.T) { // setup vela mock server engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) - engine.Use(func(c *gin.Context) { source.ToContext(c, client) }) + engine.Use(func(c *gin.Context) { scm.ToContext(c, client) }) engine.Use(user.Establish()) engine.Use(repo.Establish()) engine.Use(MustWrite()) @@ -695,7 +695,7 @@ func TestPerm_MustWrite_NotWrite(t *testing.T) { // setup vela mock server engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) - engine.Use(func(c *gin.Context) { source.ToContext(c, client) }) + engine.Use(func(c *gin.Context) { scm.ToContext(c, client) }) engine.Use(user.Establish()) engine.Use(repo.Establish()) engine.Use(MustWrite()) @@ -775,7 +775,7 @@ func TestPerm_MustRead(t *testing.T) { // setup vela mock server engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) - engine.Use(func(c *gin.Context) { source.ToContext(c, client) }) + engine.Use(func(c *gin.Context) { scm.ToContext(c, client) }) engine.Use(user.Establish()) engine.Use(repo.Establish()) engine.Use(MustRead()) @@ -855,7 +855,7 @@ func TestPerm_MustRead_PlatAdmin(t *testing.T) { // setup vela mock server engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) - engine.Use(func(c *gin.Context) { source.ToContext(c, client) }) + engine.Use(func(c *gin.Context) { scm.ToContext(c, client) }) engine.Use(user.Establish()) engine.Use(repo.Establish()) engine.Use(MustRead()) @@ -935,7 +935,7 @@ func TestPerm_MustRead_RepoAdmin(t *testing.T) { // setup vela mock server engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) - engine.Use(func(c *gin.Context) { source.ToContext(c, client) }) + engine.Use(func(c *gin.Context) { scm.ToContext(c, client) }) engine.Use(user.Establish()) engine.Use(repo.Establish()) engine.Use(MustRead()) @@ -1015,7 +1015,7 @@ func TestPerm_MustRead_RepoWrite(t *testing.T) { // setup vela mock server engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) - engine.Use(func(c *gin.Context) { source.ToContext(c, client) }) + engine.Use(func(c *gin.Context) { scm.ToContext(c, client) }) engine.Use(user.Establish()) engine.Use(repo.Establish()) engine.Use(MustRead()) @@ -1095,7 +1095,7 @@ func TestPerm_MustRead_RepoPublic(t *testing.T) { // setup vela mock server engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) - engine.Use(func(c *gin.Context) { source.ToContext(c, client) }) + engine.Use(func(c *gin.Context) { scm.ToContext(c, client) }) engine.Use(user.Establish()) engine.Use(repo.Establish()) engine.Use(MustRead()) @@ -1175,7 +1175,7 @@ func TestPerm_MustRead_NotRead(t *testing.T) { // setup vela mock server engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) - engine.Use(func(c *gin.Context) { source.ToContext(c, client) }) + engine.Use(func(c *gin.Context) { scm.ToContext(c, client) }) engine.Use(user.Establish()) engine.Use(repo.Establish()) engine.Use(MustRead()) diff --git a/router/middleware/source.go b/router/middleware/scm.go similarity index 63% rename from router/middleware/source.go rename to router/middleware/scm.go index 71238a1ff..025c3b47a 100644 --- a/router/middleware/source.go +++ b/router/middleware/scm.go @@ -6,14 +6,14 @@ package middleware import ( "github.com/gin-gonic/gin" - "github.com/go-vela/server/source" + "github.com/go-vela/server/scm" ) -// Source is a middleware function that initializes the source and +// Scm is a middleware function that initializes the scm and // attaches to the context of every http.Request. -func Source(s source.Service) gin.HandlerFunc { +func Scm(s scm.Service) gin.HandlerFunc { return func(c *gin.Context) { - source.ToContext(c, s) + scm.ToContext(c, s) c.Next() } } diff --git a/router/middleware/source_test.go b/router/middleware/scm_test.go similarity index 72% rename from router/middleware/source_test.go rename to router/middleware/scm_test.go index c4f3a49fd..0b6bc6ce6 100644 --- a/router/middleware/source_test.go +++ b/router/middleware/scm_test.go @@ -10,18 +10,18 @@ import ( "reflect" "testing" - "github.com/go-vela/server/source" - "github.com/go-vela/server/source/github" + "github.com/go-vela/server/scm" + "github.com/go-vela/server/scm/github" "github.com/gin-gonic/gin" ) -func TestMiddleware_Source(t *testing.T) { +func TestMiddleware_Scm(t *testing.T) { // setup types s := httptest.NewServer(http.NotFoundHandler()) defer s.Close() - var got source.Service + var got scm.Service want, _ := github.NewTest(s.URL) @@ -33,9 +33,9 @@ func TestMiddleware_Source(t *testing.T) { context.Request, _ = http.NewRequest(http.MethodGet, "/health", nil) // setup mock server - engine.Use(Source(want)) + engine.Use(Scm(want)) engine.GET("/health", func(c *gin.Context) { - got = source.FromContext(c) + got = scm.FromContext(c) c.Status(http.StatusOK) }) @@ -44,10 +44,10 @@ func TestMiddleware_Source(t *testing.T) { engine.ServeHTTP(context.Writer, context.Request) if resp.Code != http.StatusOK { - t.Errorf("Source returned %v, want %v", resp.Code, http.StatusOK) + t.Errorf("Scm returned %v, want %v", resp.Code, http.StatusOK) } if !reflect.DeepEqual(got, want) { - t.Errorf("Source is %v, want %v", got, want) + t.Errorf("Scm is %v, want %v", got, want) } } diff --git a/router/middleware/user/user_test.go b/router/middleware/user/user_test.go index 063dc68b9..1b84fa693 100644 --- a/router/middleware/user/user_test.go +++ b/router/middleware/user/user_test.go @@ -15,8 +15,8 @@ import ( "github.com/go-vela/server/database" "github.com/go-vela/server/database/sqlite" "github.com/go-vela/server/router/middleware/token" - "github.com/go-vela/server/source" - "github.com/go-vela/server/source/github" + "github.com/go-vela/server/scm" + "github.com/go-vela/server/scm/github" "github.com/go-vela/types/constants" "github.com/go-vela/types/library" @@ -101,7 +101,7 @@ func TestUser_Establish(t *testing.T) { // setup vela mock server engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) - engine.Use(func(c *gin.Context) { source.ToContext(c, client) }) + engine.Use(func(c *gin.Context) { scm.ToContext(c, client) }) engine.Use(Establish()) engine.GET("/users/:user", func(c *gin.Context) { got = Retrieve(c) @@ -212,7 +212,7 @@ func TestUser_Establish_NoAuthorizeUser(t *testing.T) { // setup vela mock server engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) - engine.Use(func(c *gin.Context) { source.ToContext(c, client) }) + engine.Use(func(c *gin.Context) { scm.ToContext(c, client) }) engine.Use(Establish()) // run test @@ -253,7 +253,7 @@ func TestUser_Establish_NoUser(t *testing.T) { // setup vela mock server engine.Use(func(c *gin.Context) { c.Set("secret", secret) }) engine.Use(func(c *gin.Context) { database.ToContext(c, db) }) - engine.Use(func(c *gin.Context) { source.ToContext(c, client) }) + engine.Use(func(c *gin.Context) { scm.ToContext(c, client) }) engine.Use(Establish()) engine.GET("/users/:user", func(c *gin.Context) { got = Retrieve(c) diff --git a/source/context.go b/scm/context.go similarity index 71% rename from source/context.go rename to scm/context.go index f1f209810..9295867d5 100644 --- a/source/context.go +++ b/scm/context.go @@ -2,31 +2,31 @@ // // Use of this source code is governed by the LICENSE file in this repository. -package source +package scm import ( "context" ) // key defines the key type for storing -// the source Service in the context. -const key = "source" +// the scm Service in the context. +const key = "scm" // Setter defines a context that enables setting values. type Setter interface { Set(string, interface{}) } -// FromContext returns the source Service +// FromContext returns the scm Service // associated with this context. func FromContext(c context.Context) Service { - // get source value from context + // get scm value from context v := c.Value(key) if v == nil { return nil } - // cast source value to expected Service type + // cast scm value to expected Service type s, ok := v.(Service) if !ok { return nil @@ -35,7 +35,7 @@ func FromContext(c context.Context) Service { return s } -// ToContext adds the source Service to this +// ToContext adds the scm Service to this // context if it supports the Setter interface. func ToContext(c Setter, s Service) { c.Set(key, s) diff --git a/source/context_test.go b/scm/context_test.go similarity index 85% rename from source/context_test.go rename to scm/context_test.go index c1407d12d..9845035b0 100644 --- a/source/context_test.go +++ b/scm/context_test.go @@ -2,19 +2,19 @@ // // Use of this source code is governed by the LICENSE file in this repository. -package source +package scm import ( "net/http" "net/http/httptest" "testing" - "github.com/go-vela/server/source/github" + "github.com/go-vela/server/scm/github" "github.com/gin-gonic/gin" ) -func TestSource_FromContext(t *testing.T) { +func TestSCM_FromContext(t *testing.T) { // setup context gin.SetMode(gin.TestMode) @@ -38,7 +38,7 @@ func TestSource_FromContext(t *testing.T) { } } -func TestSource_FromContext_Bad(t *testing.T) { +func TestSCM_FromContext_Bad(t *testing.T) { // setup context gin.SetMode(gin.TestMode) @@ -53,7 +53,7 @@ func TestSource_FromContext_Bad(t *testing.T) { } } -func TestSource_FromContext_WrongType(t *testing.T) { +func TestSCM_FromContext_WrongType(t *testing.T) { // setup context gin.SetMode(gin.TestMode) @@ -68,7 +68,7 @@ func TestSource_FromContext_WrongType(t *testing.T) { } } -func TestSource_FromContext_Empty(t *testing.T) { +func TestSCM_FromContext_Empty(t *testing.T) { // setup context gin.SetMode(gin.TestMode) @@ -82,7 +82,7 @@ func TestSource_FromContext_Empty(t *testing.T) { } } -func TestSource_ToContext(t *testing.T) { +func TestSCM_ToContext(t *testing.T) { // setup context gin.SetMode(gin.TestMode) diff --git a/scm/doc.go b/scm/doc.go new file mode 100644 index 000000000..2f9055416 --- /dev/null +++ b/scm/doc.go @@ -0,0 +1,11 @@ +// Copyright (c) 2021 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +// package scm provides the ability for Vela to integrate +// with different supported SCM providers. +// +// Usage: +// +// import "github.com/go-vela/server/scm" +package scm diff --git a/scm/flags.go b/scm/flags.go new file mode 100644 index 000000000..52e1b1732 --- /dev/null +++ b/scm/flags.go @@ -0,0 +1,90 @@ +// Copyright (c) 2021 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package scm + +import ( + "github.com/go-vela/types/constants" + "github.com/urfave/cli/v2" +) + +// Flags represents all supported command line +// interface (CLI) flags for the scm. +// +// https://pkg.go.dev/github.com/urfave/cli?tab=doc#Flag +// +// TODO: in a future release remove the "source" vars in favor of the "scm" ones. +// nolint:lll // these errors will go away when the TODO is completed +var Flags = []cli.Flag{ + + // Logger Flags + + &cli.StringFlag{ + EnvVars: []string{"VELA_SCM_LOG_FORMAT", "SCM_LOG_FORMAT", "VELA_LOG_FORMAT", "VELA_SOURCE_LOG_FORMAT", "SOURCE_LOG_FORMAT"}, + FilePath: "/vela/scm/log_format", + Name: "scm.log.format", + Usage: "format of logs to output", + Value: "json", + }, + &cli.StringFlag{ + EnvVars: []string{"VELA_SCM_LOG_LEVEL", "SCM_LOG_LEVEL", "VELA_LOG_LEVEL", "VELA_SOURCE_LOG_LEVEL", "SOURCE_LOG_LEVEL"}, + FilePath: "/vela/scm/log_level", + Name: "scm.log.level", + Usage: "level of logs to output", + Value: "info", + }, + + // scm Flags + + &cli.StringFlag{ + EnvVars: []string{"VELA_SCM_DRIVER", "SCM_DRIVER", "VELA_SOURCE_DRIVER", "SOURCE_DRIVER"}, + FilePath: "/vela/scm/driver", + Name: "scm.driver", + Usage: "driver to be used for the version control system", + Value: constants.DriverGithub, + }, + &cli.StringFlag{ + EnvVars: []string{"VELA_SCM_ADDR", "SCM_ADDR", "VELA_SOURCE_ADDR", "SOURCE_ADDR"}, + FilePath: "/vela/scm/addr", + Name: "scm.addr", + Usage: "fully qualified url (://) for the version control system", + Value: "https://github.com", + }, + &cli.StringFlag{ + EnvVars: []string{"VELA_SCM_CLIENT", "SCM_CLIENT", "VELA_SOURCE_CLIENT", "SOURCE_CLIENT"}, + FilePath: "/vela/scm/client", + Name: "scm.client", + Usage: "OAuth client id from version control system", + }, + &cli.StringFlag{ + EnvVars: []string{"VELA_SCM_SECRET", "SCM_SECRET", "VELA_SOURCE_SECRET", "SOURCE_SECRET"}, + FilePath: "/vela/scm/secret", + Name: "scm.secret", + Usage: "OAuth client secret from version control system", + }, + &cli.StringFlag{ + EnvVars: []string{"VELA_SCM_CONTEXT", "SCM_CONTEXT", "VELA_SOURCE_CONTEXT", "SOURCE_CONTEXT"}, + FilePath: "/vela/scm/context", + Name: "scm.context", + Usage: "context for commit status in version control system", + Value: "continuous-integration/vela", + }, + &cli.StringSliceFlag{ + EnvVars: []string{"VELA_SCM_SCOPES", "SCM_SCOPES", "VELA_SOURCE_SCOPES", "SOURCE_SCOPES"}, + FilePath: "/vela/scm/scopes", + Name: "scm.scopes", + Usage: "OAuth scopes to be used for the version control system", + Value: cli.NewStringSlice("repo", "repo:status", "user:email", "read:user", "read:org"), + }, + &cli.StringFlag{ + EnvVars: []string{"VELA_SCM_WEBHOOK_ADDR", "SCM_WEBHOOK_ADDR", "VELA_SOURCE_WEBHOOK_ADDR", "SOURCE_WEBHOOK_ADDR"}, + FilePath: "/vela/scm/webhook_addr", + Name: "scm.webhook.addr", + Usage: "Alternative or proxy server address as a fully qualified url (://). " + + "Use this when the Vela server address that the scm provider can send webhooks to " + + "differs from the server address the UI and oauth flows use, such as when the server " + + "is behind a Firewall or NAT, or when using something like ngrok to forward webhooks. " + + "(defaults to VELA_ADDR).", + }, +} diff --git a/source/github/access.go b/scm/github/access.go similarity index 100% rename from source/github/access.go rename to scm/github/access.go diff --git a/source/github/access_test.go b/scm/github/access_test.go similarity index 100% rename from source/github/access_test.go rename to scm/github/access_test.go diff --git a/source/github/authentication.go b/scm/github/authentication.go similarity index 100% rename from source/github/authentication.go rename to scm/github/authentication.go diff --git a/source/github/authentication_test.go b/scm/github/authentication_test.go similarity index 100% rename from source/github/authentication_test.go rename to scm/github/authentication_test.go diff --git a/source/github/changeset.go b/scm/github/changeset.go similarity index 100% rename from source/github/changeset.go rename to scm/github/changeset.go diff --git a/source/github/changeset_test.go b/scm/github/changeset_test.go similarity index 100% rename from source/github/changeset_test.go rename to scm/github/changeset_test.go diff --git a/source/github/deployment.go b/scm/github/deployment.go similarity index 100% rename from source/github/deployment.go rename to scm/github/deployment.go diff --git a/source/github/deployment_test.go b/scm/github/deployment_test.go similarity index 100% rename from source/github/deployment_test.go rename to scm/github/deployment_test.go diff --git a/source/github/doc.go b/scm/github/doc.go similarity index 65% rename from source/github/doc.go rename to scm/github/doc.go index db664b3fa..70aa9f7bb 100644 --- a/source/github/doc.go +++ b/scm/github/doc.go @@ -3,9 +3,9 @@ // Use of this source code is governed by the LICENSE file in this repository. // Package github provides the ability for Vela to -// integrate with GitHub or GitHub Enterprise as a source provider. +// integrate with GitHub or GitHub Enterprise as a scm provider. // // Usage: // -// import "github.com/go-vela/server/source/github" +// import "github.com/go-vela/server/scm/github" package github diff --git a/source/github/driver.go b/scm/github/driver.go similarity index 85% rename from source/github/driver.go rename to scm/github/driver.go index 1ca5635c3..8be02dab1 100644 --- a/source/github/driver.go +++ b/scm/github/driver.go @@ -6,7 +6,7 @@ package github import "github.com/go-vela/types/constants" -// Driver outputs the configured source driver. +// Driver outputs the configured scm driver. func (c *client) Driver() string { return constants.DriverGithub } diff --git a/source/github/driver_test.go b/scm/github/driver_test.go similarity index 93% rename from source/github/driver_test.go rename to scm/github/driver_test.go index 70828dfdc..2213aefbe 100644 --- a/source/github/driver_test.go +++ b/scm/github/driver_test.go @@ -24,7 +24,7 @@ func TestGitHub_Driver(t *testing.T) { WithWebUIAddress("https://vela.example.com"), ) if err != nil { - t.Errorf("unable to create source service: %v", err) + t.Errorf("unable to create scm service: %v", err) } // run test diff --git a/source/github/github.go b/scm/github/github.go similarity index 94% rename from source/github/github.go rename to scm/github/github.go index 782ca4dc7..fa3c43392 100644 --- a/source/github/github.go +++ b/scm/github/github.go @@ -37,7 +37,7 @@ type config struct { ClientSecret string // specifies the Vela server address to use for the GitHub client ServerAddress string - // specifies the Vela server address that the source provider should use to send Vela webhooks + // specifies the Vela server address that the scm provider should use to send Vela webhooks ServerWebhookAddress string // specifies the context for the commit status to use for the GitHub client StatusContext string @@ -53,7 +53,7 @@ type client struct { AuthReq *github.AuthorizationRequest } -// New returns a Source implementation that integrates with +// New returns a SCM implementation that integrates with // a GitHub or a GitHub Enterprise instance. // // nolint: revive // ignore returning unexported client @@ -100,7 +100,7 @@ func New(opts ...ClientOpt) (*client, error) { return c, nil } -// NewTest returns a Source implementation that integrates with the provided +// NewTest returns a SCM implementation that integrates with the provided // mock server. Only the url from the mock server is required. // // This function is intended for running tests only. diff --git a/source/github/github_test.go b/scm/github/github_test.go similarity index 100% rename from source/github/github_test.go rename to scm/github/github_test.go diff --git a/source/github/opts.go b/scm/github/opts.go similarity index 80% rename from source/github/opts.go rename to scm/github/opts.go index 45b300f32..5a4628501 100644 --- a/source/github/opts.go +++ b/scm/github/opts.go @@ -11,12 +11,12 @@ import ( "github.com/sirupsen/logrus" ) -// ClientOpt represents a configuration option to initialize the source client. +// ClientOpt represents a configuration option to initialize the scm client. type ClientOpt func(*client) error -// WithAddress sets the GitHub address in the source client. +// WithAddress sets the GitHub address in the scm client. func WithAddress(address string) ClientOpt { - logrus.Trace("configuring address in github source client") + logrus.Trace("configuring address in github scm client") return func(c *client) error { // set a default address for the client @@ -39,9 +39,9 @@ func WithAddress(address string) ClientOpt { } } -// WithClientID sets the GitHub OAuth client ID in the source client. +// WithClientID sets the GitHub OAuth client ID in the scm client. func WithClientID(id string) ClientOpt { - logrus.Trace("configuring OAuth client ID in github source client") + logrus.Trace("configuring OAuth client ID in github scm client") return func(c *client) error { // check if the OAuth client ID provided is empty @@ -56,9 +56,9 @@ func WithClientID(id string) ClientOpt { } } -// WithClientSecret sets the GitHub OAuth client secret in the source client. +// WithClientSecret sets the GitHub OAuth client secret in the scm client. func WithClientSecret(secret string) ClientOpt { - logrus.Trace("configuring OAuth client secret in github source client") + logrus.Trace("configuring OAuth client secret in github scm client") return func(c *client) error { // check if the OAuth client secret provided is empty @@ -73,9 +73,9 @@ func WithClientSecret(secret string) ClientOpt { } } -// WithServerAddress sets the Vela server address in the source client. +// WithServerAddress sets the Vela server address in the scm client. func WithServerAddress(address string) ClientOpt { - logrus.Trace("configuring Vela server address in github source client") + logrus.Trace("configuring Vela server address in github scm client") return func(c *client) error { // check if the Vela server address provided is empty @@ -90,9 +90,9 @@ func WithServerAddress(address string) ClientOpt { } } -// WithServerWebhookAddress sets the Vela server webhook address in the source client. +// WithServerWebhookAddress sets the Vela server webhook address in the scm client. func WithServerWebhookAddress(address string) ClientOpt { - logrus.Trace("configuring Vela server webhook address in github source client") + logrus.Trace("configuring Vela server webhook address in github scm client") return func(c *client) error { // fallback to Vela server address if the provided Vela server webhook address is empty @@ -108,9 +108,9 @@ func WithServerWebhookAddress(address string) ClientOpt { } } -// WithStatusContext sets the GitHub context for commit statuses in the source client. +// WithStatusContext sets the GitHub context for commit statuses in the scm client. func WithStatusContext(context string) ClientOpt { - logrus.Trace("configuring context for commit statuses in github source client") + logrus.Trace("configuring context for commit statuses in github scm client") return func(c *client) error { // check if the context for the commit statuses provided is empty @@ -125,9 +125,9 @@ func WithStatusContext(context string) ClientOpt { } } -// WithWebUIAddress sets the Vela web UI address in the source client. +// WithWebUIAddress sets the Vela web UI address in the scm client. func WithWebUIAddress(address string) ClientOpt { - logrus.Trace("configuring Vela web UI address in github source client") + logrus.Trace("configuring Vela web UI address in github scm client") return func(c *client) error { // set the Vela web UI address in the github client @@ -137,9 +137,9 @@ func WithWebUIAddress(address string) ClientOpt { } } -// WithScopes sets the GitHub OAuth scopes in the source client. +// WithScopes sets the GitHub OAuth scopes in the scm client. func WithScopes(scopes []string) ClientOpt { - logrus.Trace("configuring oauth scopes in github source client") + logrus.Trace("configuring oauth scopes in github scm client") return func(c *client) error { // check if the scopes provided is empty diff --git a/source/github/opts_test.go b/scm/github/opts_test.go similarity index 100% rename from source/github/opts_test.go rename to scm/github/opts_test.go diff --git a/source/github/repo.go b/scm/github/repo.go similarity index 100% rename from source/github/repo.go rename to scm/github/repo.go diff --git a/source/github/repo_test.go b/scm/github/repo_test.go similarity index 100% rename from source/github/repo_test.go rename to scm/github/repo_test.go diff --git a/source/github/testdata/deployment.json b/scm/github/testdata/deployment.json similarity index 100% rename from source/github/testdata/deployment.json rename to scm/github/testdata/deployment.json diff --git a/source/github/testdata/deployments.json b/scm/github/testdata/deployments.json similarity index 100% rename from source/github/testdata/deployments.json rename to scm/github/testdata/deployments.json diff --git a/source/github/testdata/get_pull_request.json b/scm/github/testdata/get_pull_request.json similarity index 100% rename from source/github/testdata/get_pull_request.json rename to scm/github/testdata/get_pull_request.json diff --git a/source/github/testdata/get_repo.json b/scm/github/testdata/get_repo.json similarity index 100% rename from source/github/testdata/get_repo.json rename to scm/github/testdata/get_repo.json diff --git a/source/github/testdata/hook.json b/scm/github/testdata/hook.json similarity index 100% rename from source/github/testdata/hook.json rename to scm/github/testdata/hook.json diff --git a/source/github/testdata/hooks.json b/scm/github/testdata/hooks.json similarity index 100% rename from source/github/testdata/hooks.json rename to scm/github/testdata/hooks.json diff --git a/source/github/testdata/hooks/deployment.json b/scm/github/testdata/hooks/deployment.json similarity index 100% rename from source/github/testdata/hooks/deployment.json rename to scm/github/testdata/hooks/deployment.json diff --git a/source/github/testdata/hooks/deployment_commit.json b/scm/github/testdata/hooks/deployment_commit.json similarity index 100% rename from source/github/testdata/hooks/deployment_commit.json rename to scm/github/testdata/hooks/deployment_commit.json diff --git a/source/github/testdata/hooks/deployment_unexpected_json_payload.json b/scm/github/testdata/hooks/deployment_unexpected_json_payload.json similarity index 100% rename from source/github/testdata/hooks/deployment_unexpected_json_payload.json rename to scm/github/testdata/hooks/deployment_unexpected_json_payload.json diff --git a/source/github/testdata/hooks/deployment_unexpected_text_payload.json b/scm/github/testdata/hooks/deployment_unexpected_text_payload.json similarity index 100% rename from source/github/testdata/hooks/deployment_unexpected_text_payload.json rename to scm/github/testdata/hooks/deployment_unexpected_text_payload.json diff --git a/source/github/testdata/hooks/issue_comment_created.json b/scm/github/testdata/hooks/issue_comment_created.json similarity index 100% rename from source/github/testdata/hooks/issue_comment_created.json rename to scm/github/testdata/hooks/issue_comment_created.json diff --git a/source/github/testdata/hooks/issue_comment_deleted.json b/scm/github/testdata/hooks/issue_comment_deleted.json similarity index 100% rename from source/github/testdata/hooks/issue_comment_deleted.json rename to scm/github/testdata/hooks/issue_comment_deleted.json diff --git a/source/github/testdata/hooks/issue_comment_pr.json b/scm/github/testdata/hooks/issue_comment_pr.json similarity index 100% rename from source/github/testdata/hooks/issue_comment_pr.json rename to scm/github/testdata/hooks/issue_comment_pr.json diff --git a/source/github/testdata/hooks/pull_request.json b/scm/github/testdata/hooks/pull_request.json similarity index 100% rename from source/github/testdata/hooks/pull_request.json rename to scm/github/testdata/hooks/pull_request.json diff --git a/source/github/testdata/hooks/pull_request_closed_action.json b/scm/github/testdata/hooks/pull_request_closed_action.json similarity index 100% rename from source/github/testdata/hooks/pull_request_closed_action.json rename to scm/github/testdata/hooks/pull_request_closed_action.json diff --git a/source/github/testdata/hooks/pull_request_closed_state.json b/scm/github/testdata/hooks/pull_request_closed_state.json similarity index 100% rename from source/github/testdata/hooks/pull_request_closed_state.json rename to scm/github/testdata/hooks/pull_request_closed_state.json diff --git a/source/github/testdata/hooks/push.json b/scm/github/testdata/hooks/push.json similarity index 100% rename from source/github/testdata/hooks/push.json rename to scm/github/testdata/hooks/push.json diff --git a/source/github/testdata/hooks/push_no_sender.json b/scm/github/testdata/hooks/push_no_sender.json similarity index 100% rename from source/github/testdata/hooks/push_no_sender.json rename to scm/github/testdata/hooks/push_no_sender.json diff --git a/source/github/testdata/hooks_multi.json b/scm/github/testdata/hooks_multi.json similarity index 100% rename from source/github/testdata/hooks_multi.json rename to scm/github/testdata/hooks_multi.json diff --git a/source/github/testdata/listchanges.json b/scm/github/testdata/listchanges.json similarity index 100% rename from source/github/testdata/listchanges.json rename to scm/github/testdata/listchanges.json diff --git a/source/github/testdata/listchangespr.json b/scm/github/testdata/listchangespr.json similarity index 100% rename from source/github/testdata/listchangespr.json rename to scm/github/testdata/listchangespr.json diff --git a/source/github/testdata/listuserrepos.json b/scm/github/testdata/listuserrepos.json similarity index 100% rename from source/github/testdata/listuserrepos.json rename to scm/github/testdata/listuserrepos.json diff --git a/source/github/testdata/listuserrepos_ineligible.json b/scm/github/testdata/listuserrepos_ineligible.json similarity index 100% rename from source/github/testdata/listuserrepos_ineligible.json rename to scm/github/testdata/listuserrepos_ineligible.json diff --git a/source/github/testdata/login.json b/scm/github/testdata/login.json similarity index 100% rename from source/github/testdata/login.json rename to scm/github/testdata/login.json diff --git a/source/github/testdata/org_admin.json b/scm/github/testdata/org_admin.json similarity index 100% rename from source/github/testdata/org_admin.json rename to scm/github/testdata/org_admin.json diff --git a/source/github/testdata/org_member.json b/scm/github/testdata/org_member.json similarity index 100% rename from source/github/testdata/org_member.json rename to scm/github/testdata/org_member.json diff --git a/source/github/testdata/org_pending.json b/scm/github/testdata/org_pending.json similarity index 100% rename from source/github/testdata/org_pending.json rename to scm/github/testdata/org_pending.json diff --git a/source/github/testdata/pipeline.yml b/scm/github/testdata/pipeline.yml similarity index 100% rename from source/github/testdata/pipeline.yml rename to scm/github/testdata/pipeline.yml diff --git a/source/github/testdata/py.json b/scm/github/testdata/py.json similarity index 100% rename from source/github/testdata/py.json rename to scm/github/testdata/py.json diff --git a/source/github/testdata/repo_admin.json b/scm/github/testdata/repo_admin.json similarity index 100% rename from source/github/testdata/repo_admin.json rename to scm/github/testdata/repo_admin.json diff --git a/source/github/testdata/star.json b/scm/github/testdata/star.json similarity index 100% rename from source/github/testdata/star.json rename to scm/github/testdata/star.json diff --git a/source/github/testdata/status.json b/scm/github/testdata/status.json similarity index 100% rename from source/github/testdata/status.json rename to scm/github/testdata/status.json diff --git a/source/github/testdata/team_admin.json b/scm/github/testdata/team_admin.json similarity index 100% rename from source/github/testdata/team_admin.json rename to scm/github/testdata/team_admin.json diff --git a/source/github/testdata/token.json b/scm/github/testdata/token.json similarity index 100% rename from source/github/testdata/token.json rename to scm/github/testdata/token.json diff --git a/source/github/testdata/user.json b/scm/github/testdata/user.json similarity index 100% rename from source/github/testdata/user.json rename to scm/github/testdata/user.json diff --git a/source/github/testdata/yaml.json b/scm/github/testdata/yaml.json similarity index 100% rename from source/github/testdata/yaml.json rename to scm/github/testdata/yaml.json diff --git a/source/github/testdata/yml.json b/scm/github/testdata/yml.json similarity index 100% rename from source/github/testdata/yml.json rename to scm/github/testdata/yml.json diff --git a/source/github/webhook.go b/scm/github/webhook.go similarity index 100% rename from source/github/webhook.go rename to scm/github/webhook.go diff --git a/source/github/webhook_test.go b/scm/github/webhook_test.go similarity index 100% rename from source/github/webhook_test.go rename to scm/github/webhook_test.go diff --git a/scm/scm.go b/scm/scm.go new file mode 100644 index 000000000..1f7a94280 --- /dev/null +++ b/scm/scm.go @@ -0,0 +1,49 @@ +// Copyright (c) 2021 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package scm + +import ( + "fmt" + + "github.com/go-vela/types/constants" + + "github.com/sirupsen/logrus" +) + +// nolint: godot // top level comment ends in a list +// +// New creates and returns a Vela service capable of +// integrating with the configured scm provider. +// +// Currently the following scm providers are supported: +// +// * Github +func New(s *Setup) (Service, error) { + // validate the setup being provided + // + // https://pkg.go.dev/github.com/go-vela/server/scm?tab=doc#Setup.Validate + err := s.Validate() + if err != nil { + return nil, err + } + + logrus.Debug("creating scm service from setup") + // process the scm driver being provided + switch s.Driver { + case constants.DriverGithub: + // handle the Github scm driver being provided + // + // https://pkg.go.dev/github.com/go-vela/server/scm?tab=doc#Setup.Github + return s.Github() + case constants.DriverGitlab: + // handle the Gitlab scm driver being provided + // + // https://pkg.go.dev/github.com/go-vela/server/scm?tab=doc#Setup.Gitlab + return s.Gitlab() + default: + // handle an invalid scm driver being provided + return nil, fmt.Errorf("invalid scm driver provided: %s", s.Driver) + } +} diff --git a/source/source_test.go b/scm/scm_test.go similarity index 97% rename from source/source_test.go rename to scm/scm_test.go index 6dadddda1..55f66420c 100644 --- a/source/source_test.go +++ b/scm/scm_test.go @@ -2,13 +2,13 @@ // // Use of this source code is governed by the LICENSE file in this repository. -package source +package scm import ( "testing" ) -func TestSource_New(t *testing.T) { +func TestSCM_New(t *testing.T) { // setup tests tests := []struct { failure bool diff --git a/source/service.go b/scm/service.go similarity index 92% rename from source/service.go rename to scm/service.go index cb0900cf0..eb7f1c7ad 100644 --- a/source/service.go +++ b/scm/service.go @@ -2,7 +2,7 @@ // // Use of this source code is governed by the LICENSE file in this repository. -package source +package scm import ( "net/http" @@ -12,15 +12,15 @@ import ( ) // Service represents the interface for Vela integrating -// with the different supported source providers. +// with the different supported scm providers. type Service interface { // Service Interface Functions // Driver defines a function that outputs - // the configured source driver. + // the configured scm driver. Driver() string - // Authentication Source Interface Functions + // Authentication SCM Interface Functions // Authorize defines a function that uses the // given access token to authorize the user. @@ -37,7 +37,7 @@ type Service interface { // the OAuth workflow for the session. Login(http.ResponseWriter, *http.Request) (string, error) - // Access Source Interface Functions + // Access SCM Interface Functions // OrgAccess defines a function that captures // the user's access level for an org. @@ -49,13 +49,13 @@ type Service interface { // the user's access level for a team. TeamAccess(*library.User, string, string) (string, error) - // Teams Source Interface Functions + // Teams SCM Interface Functions // ListUsersTeamsForOrg defines a function that captures // the user's teams for an org ListUsersTeamsForOrg(*library.User, string) ([]string, error) - // Changeset Source Interface Functions + // Changeset SCM Interface Functions // Changeset defines a function that captures the list // of files changed for a commit. @@ -68,7 +68,7 @@ type Service interface { // https://en.wikipedia.org/wiki/Changeset. ChangesetPR(*library.User, *library.Repo, int) ([]string, error) - // Deployment Source Interface Functions + // Deployment SCM Interface Functions // GetDeployment defines a function that // gets a deployment by number and repo. @@ -83,7 +83,7 @@ type Service interface { // creates a new deployment. CreateDeployment(*library.User, *library.Repo, *library.Deployment) error - // Repo Source Interface Functions + // Repo SCM Interface Functions // Config defines a function that captures // the pipeline configuration from a repo. @@ -114,7 +114,7 @@ type Service interface { // a repository file's html_url. GetHTMLURL(*library.User, string, string, string, string) (string, error) - // Webhook Source Interface Functions + // Webhook SCM Interface Functions // ProcessWebhook defines a function that // parses the webhook from a repo. diff --git a/scm/setup.go b/scm/setup.go new file mode 100644 index 000000000..c1783cd1f --- /dev/null +++ b/scm/setup.go @@ -0,0 +1,117 @@ +// Copyright (c) 2021 Target Brands, Inc. All rights reserved. +// +// Use of this source code is governed by the LICENSE file in this repository. + +package scm + +import ( + "fmt" + "strings" + + "github.com/go-vela/server/scm/github" + "github.com/go-vela/types/constants" + + "github.com/sirupsen/logrus" +) + +// Setup represents the configuration necessary for +// creating a Vela service capable of integrating +// with a configured scm system. +type Setup struct { + // scm Configuration + + // specifies the driver to use for the scm client + Driver string + // specifies the address to use for the scm client + Address string + // specifies the OAuth client ID from the scm system to use for the scm client + ClientID string + // specifies the OAuth client secret from the scm system to use for the scm client + ClientSecret string + // specifies the Vela server address to use for the scm client + ServerAddress string + // specifies the Vela server address that the scm provider should use to send Vela webhooks + ServerWebhookAddress string + // specifies the context for the commit status to use for the scm client + StatusContext string + // specifies the Vela web UI address to use for the scm client + WebUIAddress string + // specifies the OAuth scopes to use for the scm client + Scopes []string +} + +// Github creates and returns a Vela service capable of +// integrating with a Github scm system. +func (s *Setup) Github() (Service, error) { + logrus.Trace("creating github scm client from setup") + + // create new Github scm service + // + // https://pkg.go.dev/github.com/go-vela/server/scm/github?tab=doc#New + return github.New( + github.WithAddress(s.Address), + github.WithClientID(s.ClientID), + github.WithClientSecret(s.ClientSecret), + github.WithServerAddress(s.ServerAddress), + github.WithServerWebhookAddress(s.ServerWebhookAddress), + github.WithStatusContext(s.StatusContext), + github.WithWebUIAddress(s.WebUIAddress), + github.WithScopes(s.Scopes), + ) +} + +// Gitlab creates and returns a Vela service capable of +// integrating with a Gitlab scm system. +func (s *Setup) Gitlab() (Service, error) { + logrus.Trace("creating gitlab scm client from setup") + + return nil, fmt.Errorf("unsupported scm driver: %s", constants.DriverGitlab) +} + +// Validate verifies the necessary fields for the +// provided configuration are populated correctly. +func (s *Setup) Validate() error { + logrus.Trace("validating scm setup for client") + + // verify a scm driver was provided + if len(s.Driver) == 0 { + return fmt.Errorf("no scm driver provided") + } + + // verify a scm address was provided + if len(s.Address) == 0 { + return fmt.Errorf("no scm address provided") + } + + // check if the scm address has a scheme + if !strings.Contains(s.Address, "://") { + return fmt.Errorf("scm address must be fully qualified (://)") + } + + // check if the scm address has a trailing slash + if strings.HasSuffix(s.Address, "/") { + return fmt.Errorf("scm address must not have trailing slash") + } + + // verify a scm OAuth client ID was provided + if len(s.ClientID) == 0 { + return fmt.Errorf("no scm client id provided") + } + + // verify a scm OAuth client secret was provided + if len(s.ClientSecret) == 0 { + return fmt.Errorf("no scm client secret provided") + } + + // verify a scm status context secret was provided + if len(s.StatusContext) == 0 { + return fmt.Errorf("no scm status context provided") + } + + if len(s.Scopes) == 0 { + return fmt.Errorf("no scm scopes provided") + } + + // setup is valid + return nil +} diff --git a/source/setup_test.go b/scm/setup_test.go similarity index 97% rename from source/setup_test.go rename to scm/setup_test.go index d5ed63ddc..3341a8615 100644 --- a/source/setup_test.go +++ b/scm/setup_test.go @@ -2,14 +2,14 @@ // // Use of this source code is governed by the LICENSE file in this repository. -package source +package scm import ( "reflect" "testing" ) -func TestSource_Setup_Github(t *testing.T) { +func TestSCM_Setup_Github(t *testing.T) { // setup types _setup := &Setup{ Driver: "github", @@ -25,7 +25,7 @@ func TestSource_Setup_Github(t *testing.T) { _github, err := _setup.Github() if err != nil { - t.Errorf("unable to setup source: %v", err) + t.Errorf("unable to setup scm: %v", err) } // setup tests @@ -68,7 +68,7 @@ func TestSource_Setup_Github(t *testing.T) { } } -func TestSource_Setup_Gitlab(t *testing.T) { +func TestSCM_Setup_Gitlab(t *testing.T) { // setup types _setup := &Setup{ Driver: "gitlab", @@ -92,7 +92,7 @@ func TestSource_Setup_Gitlab(t *testing.T) { } } -func TestSource_Setup_Validate(t *testing.T) { +func TestSCM_Setup_Validate(t *testing.T) { // setup tests tests := []struct { failure bool diff --git a/source/doc.go b/source/doc.go deleted file mode 100644 index bc8e62f38..000000000 --- a/source/doc.go +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright (c) 2021 Target Brands, Inc. All rights reserved. -// -// Use of this source code is governed by the LICENSE file in this repository. - -// Package source provides the ability for Vela to integrate -// with different supported Source control providers. -// -// Usage: -// -// import "github.com/go-vela/server/source" -package source diff --git a/source/flags.go b/source/flags.go deleted file mode 100644 index b2188beef..000000000 --- a/source/flags.go +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright (c) 2021 Target Brands, Inc. All rights reserved. -// -// Use of this source code is governed by the LICENSE file in this repository. - -package source - -import ( - "github.com/go-vela/types/constants" - "github.com/urfave/cli/v2" -) - -// Flags represents all supported command line -// interface (CLI) flags for the source. -// -// https://pkg.go.dev/github.com/urfave/cli?tab=doc#Flag -var Flags = []cli.Flag{ - - // Logger Flags - - &cli.StringFlag{ - EnvVars: []string{"VELA_SOURCE_LOG_FORMAT", "SOURCE_LOG_FORMAT", "VELA_LOG_FORMAT"}, - FilePath: "/vela/source/log_format", - Name: "source.log.format", - Usage: "format of logs to output", - Value: "json", - }, - &cli.StringFlag{ - EnvVars: []string{"VELA_SOURCE_LOG_LEVEL", "SOURCE_LOG_LEVEL", "VELA_LOG_LEVEL"}, - FilePath: "/vela/source/log_level", - Name: "source.log.level", - Usage: "level of logs to output", - Value: "info", - }, - - // Source Flags - - &cli.StringFlag{ - EnvVars: []string{"VELA_SOURCE_DRIVER", "SOURCE_DRIVER"}, - FilePath: "/vela/source/driver", - Name: "source.driver", - Usage: "driver to be used for the version control system", - Value: constants.DriverGithub, - }, - &cli.StringFlag{ - EnvVars: []string{"VELA_SOURCE_ADDR", "SOURCE_ADDR"}, - FilePath: "/vela/source/addr", - Name: "source.addr", - Usage: "fully qualified url (://) for the version control system", - Value: "https://github.com", - }, - &cli.StringFlag{ - EnvVars: []string{"VELA_SOURCE_CLIENT", "SOURCE_CLIENT"}, - FilePath: "/vela/source/client", - Name: "source.client", - Usage: "OAuth client id from version control system", - }, - &cli.StringFlag{ - EnvVars: []string{"VELA_SOURCE_SECRET", "SOURCE_SECRET"}, - FilePath: "/vela/source/secret", - Name: "source.secret", - Usage: "OAuth client secret from version control system", - }, - &cli.StringFlag{ - EnvVars: []string{"VELA_SOURCE_CONTEXT", "SOURCE_CONTEXT"}, - FilePath: "/vela/source/context", - Name: "source.context", - Usage: "context for commit status in version control system", - Value: "continuous-integration/vela", - }, - &cli.StringSliceFlag{ - EnvVars: []string{"VELA_SOURCE_SCOPES", "SOURCE_SCOPES"}, - FilePath: "/vela/source/scopes", - Name: "source.scopes", - Usage: "OAuth scopes to be used for the version control system", - Value: cli.NewStringSlice("repo", "repo:status", "user:email", "read:user", "read:org"), - }, - &cli.StringFlag{ - EnvVars: []string{"VELA_SOURCE_WEBHOOK_ADDR", "SOURCE_WEBHOOK_ADDR"}, - FilePath: "/vela/source/webhook_addr", - Name: "source.webhook.addr", - Usage: "Alternative or proxy server address as a fully qualified url (://). " + - "Use this when the Vela server address that the source provider can send webhooks to " + - "differs from the server address the UI and oauth flows use, such as when the server " + - "is behind a Firewall or NAT, or when using something like ngrok to forward webhooks. " + - "(defaults to VELA_ADDR).", - }, -} diff --git a/source/setup.go b/source/setup.go deleted file mode 100644 index 21bca5ce5..000000000 --- a/source/setup.go +++ /dev/null @@ -1,117 +0,0 @@ -// Copyright (c) 2021 Target Brands, Inc. All rights reserved. -// -// Use of this source code is governed by the LICENSE file in this repository. - -package source - -import ( - "fmt" - "strings" - - "github.com/go-vela/server/source/github" - "github.com/go-vela/types/constants" - - "github.com/sirupsen/logrus" -) - -// Setup represents the configuration necessary for -// creating a Vela service capable of integrating -// with a configured source system. -type Setup struct { - // Source Configuration - - // specifies the driver to use for the source client - Driver string - // specifies the address to use for the source client - Address string - // specifies the OAuth client ID from the source system to use for the source client - ClientID string - // specifies the OAuth client secret from the source system to use for the source client - ClientSecret string - // specifies the Vela server address to use for the source client - ServerAddress string - // specifies the Vela server address that the source provider should use to send Vela webhooks - ServerWebhookAddress string - // specifies the context for the commit status to use for the source client - StatusContext string - // specifies the Vela web UI address to use for the source client - WebUIAddress string - // specifies the OAuth scopes to use for the source client - Scopes []string -} - -// Github creates and returns a Vela service capable of -// integrating with a Github source system. -func (s *Setup) Github() (Service, error) { - logrus.Trace("creating github source client from setup") - - // create new Github source service - // - // https://pkg.go.dev/github.com/go-vela/server/source/github?tab=doc#New - return github.New( - github.WithAddress(s.Address), - github.WithClientID(s.ClientID), - github.WithClientSecret(s.ClientSecret), - github.WithServerAddress(s.ServerAddress), - github.WithServerWebhookAddress(s.ServerWebhookAddress), - github.WithStatusContext(s.StatusContext), - github.WithWebUIAddress(s.WebUIAddress), - github.WithScopes(s.Scopes), - ) -} - -// Gitlab creates and returns a Vela service capable of -// integrating with a Gitlab source system. -func (s *Setup) Gitlab() (Service, error) { - logrus.Trace("creating gitlab source client from setup") - - return nil, fmt.Errorf("unsupported source driver: %s", constants.DriverGitlab) -} - -// Validate verifies the necessary fields for the -// provided configuration are populated correctly. -func (s *Setup) Validate() error { - logrus.Trace("validating source setup for client") - - // verify a source driver was provided - if len(s.Driver) == 0 { - return fmt.Errorf("no source driver provided") - } - - // verify a source address was provided - if len(s.Address) == 0 { - return fmt.Errorf("no source address provided") - } - - // check if the source address has a scheme - if !strings.Contains(s.Address, "://") { - return fmt.Errorf("source address must be fully qualified (://)") - } - - // check if the source address has a trailing slash - if strings.HasSuffix(s.Address, "/") { - return fmt.Errorf("source address must not have trailing slash") - } - - // verify a source OAuth client ID was provided - if len(s.ClientID) == 0 { - return fmt.Errorf("no source client id provided") - } - - // verify a source OAuth client secret was provided - if len(s.ClientSecret) == 0 { - return fmt.Errorf("no source client secret provided") - } - - // verify a source status context secret was provided - if len(s.StatusContext) == 0 { - return fmt.Errorf("no source status context provided") - } - - if len(s.Scopes) == 0 { - return fmt.Errorf("no source scopes provided") - } - - // setup is valid - return nil -} diff --git a/source/source.go b/source/source.go deleted file mode 100644 index 8fc3be427..000000000 --- a/source/source.go +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright (c) 2021 Target Brands, Inc. All rights reserved. -// -// Use of this source code is governed by the LICENSE file in this repository. - -package source - -import ( - "fmt" - - "github.com/go-vela/types/constants" - - "github.com/sirupsen/logrus" -) - -// nolint: godot // top level comment ends in a list -// -// New creates and returns a Vela service capable of -// integrating with the configured source provider. -// -// Currently the following source providers are supported: -// -// * Github -func New(s *Setup) (Service, error) { - // validate the setup being provided - // - // https://pkg.go.dev/github.com/go-vela/server/source?tab=doc#Setup.Validate - err := s.Validate() - if err != nil { - return nil, err - } - - logrus.Debug("creating source service from setup") - // process the source driver being provided - switch s.Driver { - case constants.DriverGithub: - // handle the Github source driver being provided - // - // https://pkg.go.dev/github.com/go-vela/server/source?tab=doc#Setup.Github - return s.Github() - case constants.DriverGitlab: - // handle the Gitlab source driver being provided - // - // https://pkg.go.dev/github.com/go-vela/server/source?tab=doc#Setup.Gitlab - return s.Gitlab() - default: - // handle an invalid source driver being provided - return nil, fmt.Errorf("invalid source driver provided: %s", s.Driver) - } -}