diff --git a/cli/exec/exec.go b/cli/exec/exec.go index 34a4571f1..91df8181f 100644 --- a/cli/exec/exec.go +++ b/cli/exec/exec.go @@ -319,7 +319,7 @@ func execWithAxis(c *cli.Context, axis matrix.Axis) error { metadata := metadataFromContext(c, axis) environ := metadata.Environ() - secrets := []compiler.Secret{} + var secrets []compiler.Secret for k, v := range metadata.EnvironDrone() { environ[k] = v } diff --git a/cmd/server/server.go b/cmd/server/server.go index 38cee5a7c..deaebd42c 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -279,7 +279,7 @@ func (a *authorizer) authorize(ctx context.Context) error { } func redirect(w http.ResponseWriter, req *http.Request) { - var serverHost string = server.Config.Server.Host + serverHost := server.Config.Server.Host serverHost = strings.TrimPrefix(serverHost, "http://") serverHost = strings.TrimPrefix(serverHost, "https://") req.URL.Scheme = "https" diff --git a/go.mod b/go.mod index 5d7690d9b..0f4125d4f 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( docker.io/go-docker v1.0.0 github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect github.com/Microsoft/go-winio v0.4.15-0.20190919025122-fc70bd9a86b5 // indirect - github.com/bmatcuk/doublestar v1.3.4 + github.com/bmatcuk/doublestar v1.3.4 // indirect github.com/bmatcuk/doublestar/v4 v4.0.2 github.com/bradrydzewski/togo v0.0.0-20180401185031-50a0e4726e74 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect diff --git a/model/queue.go b/model/queue.go index 3ea6a3bf7..ea81333b8 100644 --- a/model/queue.go +++ b/model/queue.go @@ -41,7 +41,7 @@ type TaskStore interface { // ensures the task Queue can be restored when the system starts. func WithTaskStore(q queue.Queue, s TaskStore) queue.Queue { tasks, _ := s.TaskList() - toEnqueue := []*queue.Task{} + var toEnqueue []*queue.Task for _, task := range tasks { toEnqueue = append(toEnqueue, &queue.Task{ ID: task.ID, diff --git a/pipeline/backend/docker/convert.go b/pipeline/backend/docker/convert.go index 9bf0075e7..5323db136 100644 --- a/pipeline/backend/docker/convert.go +++ b/pipeline/backend/docker/convert.go @@ -165,7 +165,7 @@ func splitVolumeParts(volumeParts string) ([]string, error) { } if r.MatchString(volumeParts) { results := r.FindStringSubmatch(volumeParts)[1:] - cleanResults := []string{} + var cleanResults []string for _, item := range results { if item != "" { cleanResults = append(cleanResults, item) diff --git a/pipeline/backend/docker/docker.go b/pipeline/backend/docker/docker.go index 2e06defaa..330b154d4 100644 --- a/pipeline/backend/docker/docker.go +++ b/pipeline/backend/docker/docker.go @@ -2,9 +2,6 @@ package docker import ( "context" - "github.com/docker/docker/pkg/jsonmessage" - "github.com/docker/docker/pkg/stdcopy" - "github.com/docker/docker/pkg/term" "io" "os" @@ -14,6 +11,9 @@ import ( "docker.io/go-docker/api/types" "docker.io/go-docker/api/types/network" "docker.io/go-docker/api/types/volume" + "github.com/docker/docker/pkg/jsonmessage" + "github.com/docker/docker/pkg/stdcopy" + "github.com/docker/docker/pkg/term" ) type engine struct { @@ -49,10 +49,10 @@ func (e *engine) Setup(_ context.Context, conf *backend.Config) error { return err } } - for _, network := range conf.Networks { - _, err := e.client.NetworkCreate(noContext, network.Name, types.NetworkCreate{ - Driver: network.Driver, - Options: network.DriverOpts, + for _, n := range conf.Networks { + _, err := e.client.NetworkCreate(noContext, n.Name, types.NetworkCreate{ + Driver: n.Driver, + Options: n.DriverOpts, // Labels: defaultLabels, }) if err != nil { @@ -89,7 +89,7 @@ func (e *engine) Exec(ctx context.Context, proc *backend.Step) error { } _, err := e.client.ContainerCreate(ctx, config, hostConfig, nil, proc.Name) - if docker.IsErrImageNotFound(err) { + if docker.IsErrNotFound(err) { // automatically pull and try to re-create the image if the // failure is caused because the image does not exist. responseBody, perr := e.client.ImagePull(ctx, config.Image, pullopts) @@ -178,11 +178,11 @@ func (e *engine) Destroy(_ context.Context, conf *backend.Config) error { e.client.ContainerRemove(noContext, step.Name, removeOpts) } } - for _, volume := range conf.Volumes { - e.client.VolumeRemove(noContext, volume.Name, true) + for _, v := range conf.Volumes { + e.client.VolumeRemove(noContext, v.Name, true) } - for _, network := range conf.Networks { - e.client.NetworkRemove(noContext, network.Name) + for _, n := range conf.Networks { + e.client.NetworkRemove(noContext, n.Name) } return nil } diff --git a/pipeline/doc.go b/pipeline/doc.go deleted file mode 100644 index fb2071c7f..000000000 --- a/pipeline/doc.go +++ /dev/null @@ -1 +0,0 @@ -package pipeline diff --git a/pipeline/frontend/yaml/compiler/params.go b/pipeline/frontend/yaml/compiler/params.go index f823a68b0..1daa8b09f 100644 --- a/pipeline/frontend/yaml/compiler/params.go +++ b/pipeline/frontend/yaml/compiler/params.go @@ -47,7 +47,7 @@ func paramsToEnv(from map[string]interface{}, to map[string]string) error { return err } - in := []string{} + var in []string err = yaml.Unmarshal(out, &in) if err == nil { to[k] = strings.Join(in, ",") diff --git a/pipeline/frontend/yaml/constraint.go b/pipeline/frontend/yaml/constraint.go index a79ccd217..41f04b207 100644 --- a/pipeline/frontend/yaml/constraint.go +++ b/pipeline/frontend/yaml/constraint.go @@ -5,7 +5,7 @@ import ( "path/filepath" "strings" - doublestar "github.com/bmatcuk/doublestar/v4" + "github.com/bmatcuk/doublestar/v4" libcompose "github.com/docker/libcompose/yaml" "github.com/woodpecker-ci/woodpecker/pipeline/frontend" "github.com/woodpecker-ci/woodpecker/pipeline/frontend/yaml/types" diff --git a/pipeline/frontend/yaml/matrix/matrix.go b/pipeline/frontend/yaml/matrix/matrix.go index bc5fb50c7..8ca8569b8 100644 --- a/pipeline/frontend/yaml/matrix/matrix.go +++ b/pipeline/frontend/yaml/matrix/matrix.go @@ -66,7 +66,7 @@ func calc(matrix Matrix) []Axis { } // structure to hold the transformed result set - axisList := []Axis{} + var axisList []Axis // for each axis calculate the uniqe set of values that should be used. for p := 0; p < perm; p++ { diff --git a/pipeline/rpc/client_grpc.go b/pipeline/rpc/client_grpc.go index 5260e0aa2..716fbf232 100644 --- a/pipeline/rpc/client_grpc.go +++ b/pipeline/rpc/client_grpc.go @@ -11,6 +11,7 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" ) // generate protobuffs @@ -48,9 +49,9 @@ func (c *client) Next(ctx context.Context, f Filter) (*Pipeline, error) { if err == nil { break } else { - log.Printf("grpc error: done(): code: %v: %s", grpc.Code(err), err) + log.Printf("grpc error: done(): code: %v: %s", status.Code(err), err) } - switch grpc.Code(err) { + switch status.Code(err) { case codes.Aborted, codes.DataLoss, @@ -88,9 +89,9 @@ func (c *client) Wait(ctx context.Context, id string) (err error) { if err == nil { break } else { - log.Printf("grpc error: wait(): code: %v: %s", grpc.Code(err), err) + log.Printf("grpc error: wait(): code: %v: %s", status.Code(err), err) } - switch grpc.Code(err) { + switch status.Code(err) { case codes.Aborted, codes.DataLoss, @@ -122,9 +123,9 @@ func (c *client) Init(ctx context.Context, id string, state State) (err error) { if err == nil { break } else { - log.Printf("grpc error: init(): code: %v: %s", grpc.Code(err), err) + log.Printf("grpc error: init(): code: %v: %s", status.Code(err), err) } - switch grpc.Code(err) { + switch status.Code(err) { case codes.Aborted, codes.DataLoss, @@ -156,9 +157,9 @@ func (c *client) Done(ctx context.Context, id string, state State) (err error) { if err == nil { break } else { - log.Printf("grpc error: done(): code: %v: %s", grpc.Code(err), err) + log.Printf("grpc error: done(): code: %v: %s", status.Code(err), err) } - switch grpc.Code(err) { + switch status.Code(err) { case codes.Aborted, codes.DataLoss, @@ -183,9 +184,9 @@ func (c *client) Extend(ctx context.Context, id string) (err error) { if err == nil { break } else { - log.Printf("grpc error: extend(): code: %v: %s", grpc.Code(err), err) + log.Printf("grpc error: extend(): code: %v: %s", status.Code(err), err) } - switch grpc.Code(err) { + switch status.Code(err) { case codes.Aborted, codes.DataLoss, @@ -217,9 +218,9 @@ func (c *client) Update(ctx context.Context, id string, state State) (err error) if err == nil { break } else { - log.Printf("grpc error: update(): code: %v: %s", grpc.Code(err), err) + log.Printf("grpc error: update(): code: %v: %s", status.Code(err), err) } - switch grpc.Code(err) { + switch status.Code(err) { case codes.Aborted, codes.DataLoss, @@ -252,9 +253,9 @@ func (c *client) Upload(ctx context.Context, id string, file *File) (err error) if err == nil { break } else { - log.Printf("grpc error: upload(): code: %v: %s", grpc.Code(err), err) + log.Printf("grpc error: upload(): code: %v: %s", status.Code(err), err) } - switch grpc.Code(err) { + switch status.Code(err) { case codes.Aborted, codes.DataLoss, @@ -284,9 +285,9 @@ func (c *client) Log(ctx context.Context, id string, line *Line) (err error) { if err == nil { break } else { - log.Printf("grpc error: log(): code: %v: %s", grpc.Code(err), err) + log.Printf("grpc error: log(): code: %v: %s", status.Code(err), err) } - switch grpc.Code(err) { + switch status.Code(err) { case codes.Aborted, codes.DataLoss, diff --git a/pipeline/rpc/client_grpc_health.go b/pipeline/rpc/client_grpc_health.go index ff8e241cc..7bf2f6855 100644 --- a/pipeline/rpc/client_grpc_health.go +++ b/pipeline/rpc/client_grpc_health.go @@ -10,6 +10,7 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" ) // generate protobuffs @@ -45,7 +46,7 @@ func (c *healthClient) Check(ctx context.Context) (bool, error) { } return false, nil } - switch grpc.Code(err) { + switch status.Code(err) { case codes.Aborted, codes.DataLoss, diff --git a/pipeline/version/version.go b/pipeline/version/version.go deleted file mode 100644 index df3f1d9de..000000000 --- a/pipeline/version/version.go +++ /dev/null @@ -1,17 +0,0 @@ -package version - -import "fmt" - -var ( - // major is for an API incompatible changes - major = 1 - // minor is for functionality in a backwards-compatible manner - minor = 0 - // patch is for backwards-compatible bug fixes - patch = 0 -) - -// String returns the supporeted specification versions in string format. -func String() string { - return fmt.Sprintf("%d.%d.%d", major, minor, patch) -} diff --git a/server/api/build.go b/server/api/build.go index 2bfcb111a..658e63704 100644 --- a/server/api/build.go +++ b/server/api/build.go @@ -167,13 +167,13 @@ func DeleteBuild(c *gin.Context) { build, err := store.GetBuildNumber(c, repo, num) if err != nil { - c.AbortWithError(404, err) + _ = c.AbortWithError(404, err) return } procs, err := store.FromContext(c).ProcList(build) if err != nil { - c.AbortWithError(404, err) + _ = c.AbortWithError(404, err) return } @@ -183,8 +183,10 @@ func DeleteBuild(c *gin.Context) { } // First cancel/evict procs in the queue in one go - procToCancel := []string{} - procToEvict := []string{} + var ( + procToCancel []string + procToEvict []string + ) for _, proc := range procs { if proc.PPID != 0 { continue diff --git a/server/api/hook.go b/server/api/hook.go index 82ee65d21..b47f4ff56 100644 --- a/server/api/hook.go +++ b/server/api/hook.go @@ -404,8 +404,7 @@ func queueBuild(build *model.Build, repo *model.Repo, buildItems []*shared.Build server.Config.Services.Queue.PushAtOnce(context.Background(), tasks) } -func taskIds(dependsOn []string, buildItems []*shared.BuildItem) []string { - taskIds := []string{} +func taskIds(dependsOn []string, buildItems []*shared.BuildItem) (taskIds []string) { for _, dep := range dependsOn { for _, buildItem := range buildItems { if buildItem.Proc.Name == dep { @@ -413,7 +412,7 @@ func taskIds(dependsOn []string, buildItems []*shared.BuildItem) []string { } } } - return taskIds + return } func shasum(raw []byte) string { diff --git a/server/api/login.go b/server/api/login.go index 842e21a49..9f4e3b0e1 100644 --- a/server/api/login.go +++ b/server/api/login.go @@ -133,15 +133,14 @@ func HandleAuth(c *gin.Context) { } exp := time.Now().Add(server.Config.Server.SessionExpires).Unix() - token := token.New(token.SessToken, u.Login) - tokenstr, err := token.SignExpires(u.Hash, exp) + tokenString, err := token.New(token.SessToken, u.Login).SignExpires(u.Hash, exp) if err != nil { logrus.Errorf("cannot create token for %s. %s", u.Login, err) c.Redirect(303, "/login?error=internal_error") return } - httputil.SetCookie(c.Writer, c.Request, "user_sess", tokenstr) + httputil.SetCookie(c.Writer, c.Request, "user_sess", tokenString) intendedURL := c.Request.URL.Query()["url"] if len(intendedURL) > 0 { diff --git a/server/api/repo.go b/server/api/repo.go index b89ebb023..028195bed 100644 --- a/server/api/repo.go +++ b/server/api/repo.go @@ -32,7 +32,7 @@ import ( ) func PostRepo(c *gin.Context) { - remote := remote.FromContext(c) + r := remote.FromContext(c) user := session.User(c) repo := session.Repo(c) @@ -75,13 +75,13 @@ func PostRepo(c *gin.Context) { sig, ) - err = remote.Activate(user, repo, link) + err = r.Activate(user, repo, link) if err != nil { c.String(500, err.Error()) return } - from, err := remote.Repo(user, repo.Owner, repo.Name) + from, err := r.Repo(user, repo.Owner, repo.Name) if err == nil { repo.Update(from) } @@ -166,7 +166,7 @@ func GetRepo(c *gin.Context) { func DeleteRepo(c *gin.Context) { remove, _ := strconv.ParseBool(c.Query("remove")) - remote := remote.FromContext(c) + r := remote.FromContext(c) repo := session.Repo(c) user := session.User(c) @@ -187,12 +187,12 @@ func DeleteRepo(c *gin.Context) { } } - remote.Deactivate(user, repo, server.Config.Server.Host) + r.Deactivate(user, repo, server.Config.Server.Host) c.JSON(200, repo) } func RepairRepo(c *gin.Context) { - remote := remote.FromContext(c) + r := remote.FromContext(c) repo := session.Repo(c) user := session.User(c) @@ -212,14 +212,14 @@ func RepairRepo(c *gin.Context) { sig, ) - remote.Deactivate(user, repo, host) - err = remote.Activate(user, repo, link) + r.Deactivate(user, repo, host) + err = r.Activate(user, repo, link) if err != nil { c.String(500, err.Error()) return } - from, err := remote.Repo(user, repo.Owner, repo.Name) + from, err := r.Repo(user, repo.Owner, repo.Name) if err == nil { repo.Name = from.Name repo.Owner = from.Owner @@ -238,7 +238,7 @@ func RepairRepo(c *gin.Context) { } func MoveRepo(c *gin.Context) { - remote := remote.FromContext(c) + r := remote.FromContext(c) repo := session.Repo(c) user := session.User(c) @@ -255,7 +255,7 @@ func MoveRepo(c *gin.Context) { return } - from, err := remote.Repo(user, owner, name) + from, err := r.Repo(user, owner, name) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return @@ -298,8 +298,8 @@ func MoveRepo(c *gin.Context) { sig, ) - remote.Deactivate(user, repo, host) - err = remote.Activate(user, repo, link) + r.Deactivate(user, repo, host) + err = r.Activate(user, repo, link) if err != nil { c.String(500, err.Error()) return diff --git a/server/api/user.go b/server/api/user.go index 8dcab69a5..574e3a549 100644 --- a/server/api/user.go +++ b/server/api/user.go @@ -118,7 +118,7 @@ func GetRepos(c *gin.Context) { return } - active := []*model.Repo{} + var active []*model.Repo for _, repo := range repos { if repo.IsActive { active = append(active, repo) @@ -129,14 +129,12 @@ func GetRepos(c *gin.Context) { func PostToken(c *gin.Context) { user := session.User(c) - - token := token.New(token.UserToken, user.Login) - tokenstr, err := token.Sign(user.Hash) + tokenString, err := token.New(token.UserToken, user.Login).Sign(user.Hash) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } - c.String(http.StatusOK, tokenstr) + c.String(http.StatusOK, tokenString) } func DeleteToken(c *gin.Context) { @@ -149,11 +147,10 @@ func DeleteToken(c *gin.Context) { return } - token := token.New(token.UserToken, user.Login) - tokenstr, err := token.Sign(user.Hash) + tokenString, err := token.New(token.UserToken, user.Login).Sign(user.Hash) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) return } - c.String(http.StatusOK, tokenstr) + c.String(http.StatusOK, tokenString) } diff --git a/server/plugins/registry/filesystem.go b/server/plugins/registry/filesystem.go index 8efca3efc..44fa4a138 100644 --- a/server/plugins/registry/filesystem.go +++ b/server/plugins/registry/filesystem.go @@ -50,8 +50,7 @@ func parseDockerConfig(path string) ([]*model.Registry, error) { } } - auths := []*model.Registry{} - + var auths []*model.Registry for key, auth := range configFile.AuthConfigs { auths = append(auths, &model.Registry{ Address: key, diff --git a/server/plugins/sender/plugin.go b/server/plugins/sender/plugin.go index 46ff128b0..6ff32e059 100644 --- a/server/plugins/sender/plugin.go +++ b/server/plugins/sender/plugin.go @@ -44,9 +44,8 @@ func (p *plugin) SenderDelete(repo *model.Repo, login string) error { return internal.Send("DELETE", path, nil, nil) } -func (p *plugin) SenderList(repo *model.Repo) ([]*model.Sender, error) { +func (p *plugin) SenderList(repo *model.Repo) (out []*model.Sender, err error) { path := fmt.Sprintf("%s/senders/%s/%s", p.endpoint, repo.Owner, repo.Name) - out := []*model.Sender{} - err := internal.Send("GET", path, nil, out) + err = internal.Send("GET", path, nil, out) return out, err } diff --git a/server/queue/fifo.go b/server/queue/fifo.go index 9576cf791..5bf82f172 100644 --- a/server/queue/fifo.go +++ b/server/queue/fifo.go @@ -267,7 +267,7 @@ func (q *fifo) filterWaiting() { // rebuild waitingDeps q.waitingOnDeps = list.New() - filtered := []*list.Element{} + var filtered []*list.Element var nextPending *list.Element for e := q.pending.Front(); e != nil; e = nextPending { nextPending = e.Next() diff --git a/server/remote/bitbucketserver/bitbucketserver.go b/server/remote/bitbucketserver/bitbucketserver.go index a3146595e..97a54dc04 100644 --- a/server/remote/bitbucketserver/bitbucketserver.go +++ b/server/remote/bitbucketserver/bitbucketserver.go @@ -104,13 +104,13 @@ func New(opts Opts) (remote.Remote, error) { } func (c *Config) Login(res http.ResponseWriter, req *http.Request) (*model.User, error) { - requestToken, url, err := c.Consumer.GetRequestTokenAndUrl("oob") + requestToken, u, err := c.Consumer.GetRequestTokenAndUrl("oob") if err != nil { return nil, err } var code = req.FormValue("oauth_verifier") if len(code) == 0 { - http.Redirect(res, req, url, http.StatusSeeOther) + http.Redirect(res, req, u, http.StatusSeeOther) return nil, nil } requestToken.Token = req.FormValue("oauth_token") diff --git a/server/remote/coding/coding.go b/server/remote/coding/coding.go index a1e08fae8..5d6a555dc 100644 --- a/server/remote/coding/coding.go +++ b/server/remote/coding/coding.go @@ -48,7 +48,7 @@ type Opts struct { // New returns a Remote implementation that integrates with a Coding Platform or // Coding Enterprise version control hosting provider. func New(opts Opts) (remote.Remote, error) { - remote := &Coding{ + r := &Coding{ URL: defaultURL, Client: opts.Client, Secret: opts.Secret, @@ -59,10 +59,10 @@ func New(opts Opts) (remote.Remote, error) { SkipVerify: opts.SkipVerify, } if opts.URL != defaultURL { - remote.URL = strings.TrimSuffix(opts.URL, "/") + r.URL = strings.TrimSuffix(opts.URL, "/") } - return remote, nil + return r, nil } type Coding struct { diff --git a/server/remote/gerrit/gerrit.go b/server/remote/gerrit/gerrit.go index 1109d8e45..ec6387282 100644 --- a/server/remote/gerrit/gerrit.go +++ b/server/remote/gerrit/gerrit.go @@ -48,17 +48,17 @@ type client struct { // New returns a Remote implementation that integrates with Getter, an open // source Git hosting service and code review system. func New(opts Opts) (remote.Remote, error) { - url, err := url.Parse(opts.URL) + u, err := url.Parse(opts.URL) if err != nil { return nil, err } - host, _, err := net.SplitHostPort(url.Host) + host, _, err := net.SplitHostPort(u.Host) if err == nil { - url.Host = host + u.Host = host } return &client{ URL: opts.URL, - Machine: url.Host, + Machine: u.Host, Username: opts.Username, Password: opts.Password, PrivateMode: opts.PrivateMode, diff --git a/server/remote/gitea/gitea.go b/server/remote/gitea/gitea.go index 6a6bb24f6..1054a8dd5 100644 --- a/server/remote/gitea/gitea.go +++ b/server/remote/gitea/gitea.go @@ -110,18 +110,18 @@ func getDesc(status string) string { // New returns a Remote implementation that integrates with Gitea, an open // source Git service written in Go. See https://gitea.io/ func New(opts Opts) (remote.Remote, error) { - url, err := url.Parse(opts.URL) + u, err := url.Parse(opts.URL) if err != nil { return nil, err } - host, _, err := net.SplitHostPort(url.Host) + host, _, err := net.SplitHostPort(u.Host) if err == nil { - url.Host = host + u.Host = host } return &client{ URL: opts.URL, Context: opts.Context, - Machine: url.Host, + Machine: u.Host, Username: opts.Username, Password: opts.Password, PrivateMode: opts.PrivateMode, @@ -129,6 +129,8 @@ func New(opts Opts) (remote.Remote, error) { }, nil } +// TODO: dont create a new client for each func + // Login authenticates an account with Gitea using basic authentication. The // Gitea account details are returned when the user is successfully authenticated. func (c *client) Login(res http.ResponseWriter, req *http.Request) (*model.User, error) { @@ -228,16 +230,14 @@ func (c *client) Repo(u *model.User, owner, name string) (*model.Repo, error) { // Repos returns a list of all repositories for the Gitea account, including // organization repositories. -func (c *client) Repos(u *model.User) ([]*model.Repo, error) { - repos := []*model.Repo{} - +func (c *client) Repos(u *model.User) (repos []*model.Repo, err error) { client, err := c.newClientToken(u.Token) if err != nil { return nil, err } // Gitea SDK forces us to read repo list paginated. - var page int = 1 + var page = 1 for { all, _, err := client.ListMyRepos( gitea.ListReposOptions{ diff --git a/server/remote/gitea/gitea_oauth.go b/server/remote/gitea/gitea_oauth.go index c0a887ad5..f8ccf6c1d 100644 --- a/server/remote/gitea/gitea_oauth.go +++ b/server/remote/gitea/gitea_oauth.go @@ -51,21 +51,23 @@ type oauthclient struct { SkipVerify bool } -// New returns a Remote implementation that integrates with Gitea, an open +// TODO: merge with gitea.go (or drop basic auth) + +// NewOauth returns a Remote implementation that integrates with Gitea, an open // source Git service written in Go. See https://gitea.io/ func NewOauth(opts Opts) (remote.Remote, error) { - url, err := url.Parse(opts.URL) + u, err := url.Parse(opts.URL) if err != nil { return nil, err } - host, _, err := net.SplitHostPort(url.Host) + host, _, err := net.SplitHostPort(u.Host) if err == nil { - url.Host = host + u.Host = host } return &oauthclient{ URL: opts.URL, Context: opts.Context, - Machine: url.Host, + Machine: u.Host, Client: opts.Client, Secret: opts.Secret, Username: opts.Username, @@ -219,7 +221,7 @@ func (c *oauthclient) Repos(u *model.User) ([]*model.Repo, error) { } // Gitea SDK forces us to read repo list paginated. - var page int = 1 + var page = 1 for { all, _, err := client.ListMyRepos( gitea.ListReposOptions{ diff --git a/server/remote/github/github.go b/server/remote/github/github.go index 9fed82c33..1634a4a95 100644 --- a/server/remote/github/github.go +++ b/server/remote/github/github.go @@ -55,15 +55,15 @@ type Opts struct { // New returns a Remote implementation that integrates with a GitHub Cloud or // GitHub Enterprise version control hosting provider. func New(opts Opts) (remote.Remote, error) { - url, err := url.Parse(opts.URL) + u, err := url.Parse(opts.URL) if err != nil { return nil, err } - host, _, err := net.SplitHostPort(url.Host) + host, _, err := net.SplitHostPort(u.Host) if err == nil { - url.Host = host + u.Host = host } - remote := &client{ + r := &client{ API: defaultAPI, URL: defaultURL, Context: opts.Context, @@ -73,18 +73,19 @@ func New(opts Opts) (remote.Remote, error) { PrivateMode: opts.PrivateMode, SkipVerify: opts.SkipVerify, MergeRef: opts.MergeRef, - Machine: url.Host, + Machine: u.Host, Username: opts.Username, Password: opts.Password, } if opts.URL != defaultURL { - remote.URL = strings.TrimSuffix(opts.URL, "/") - remote.API = remote.URL + "/api/v3/" + r.URL = strings.TrimSuffix(opts.URL, "/") + r.API = r.URL + "/api/v3/" } // Hack to enable oauth2 access in older GHE - oauth2.RegisterBrokenAuthHeaderProvider(remote.URL) - return remote, nil + // TODO: dont use deprecated func + oauth2.RegisterBrokenAuthHeaderProvider(r.URL) + return r, nil } type client struct { @@ -371,9 +372,9 @@ func (c *client) newClientToken(token string) *github.Client { }, } } - github := github.NewClient(tc) - github.BaseURL, _ = url.Parse(c.API) - return github + client := github.NewClient(tc) + client.BaseURL, _ = url.Parse(c.API) + return client } // helper function to return matching user email. @@ -437,13 +438,12 @@ func (c *client) Status(u *model.User, r *model.Repo, b *model.Build, link strin } func repoStatus(client *github.Client, r *model.Repo, b *model.Build, link, ctx string, proc *model.Proc) error { - context := ctx switch b.Event { case model.EventPull: - context += "/pr" + ctx += "/pr" default: if len(b.Event) > 0 { - context += "/" + b.Event + ctx += "/" + b.Event } } @@ -451,13 +451,13 @@ func repoStatus(client *github.Client, r *model.Repo, b *model.Build, link, ctx desc := github.String(convertDesc(b.Status)) if proc != nil { - context += "/" + proc.Name + ctx += "/" + proc.Name status = github.String(convertStatus(proc.State)) desc = github.String(convertDesc(proc.State)) } data := github.RepoStatus{ - Context: github.String(context), + Context: github.String(ctx), State: status, Description: desc, TargetURL: github.String(link), diff --git a/server/remote/gitlab/gitlab.go b/server/remote/gitlab/gitlab.go index 6d6c12592..968492121 100644 --- a/server/remote/gitlab/gitlab.go +++ b/server/remote/gitlab/gitlab.go @@ -48,19 +48,19 @@ type Opts struct { // New returns a Remote implementation that integrates with Gitlab, an open // source Git service. See https://gitlab.com func New(opts Opts) (remote.Remote, error) { - url, err := url.Parse(opts.URL) + u, err := url.Parse(opts.URL) if err != nil { return nil, err } - host, _, err := net.SplitHostPort(url.Host) + host, _, err := net.SplitHostPort(u.Host) if err == nil { - url.Host = host + u.Host = host } return &Gitlab{ URL: opts.URL, Client: opts.Client, Secret: opts.Secret, - Machine: url.Host, + Machine: u.Host, Username: opts.Username, Password: opts.Password, PrivateMode: opts.PrivateMode, diff --git a/server/remote/gitlab3/gitlab.go b/server/remote/gitlab3/gitlab.go index c2ec7f049..19b4579a2 100644 --- a/server/remote/gitlab3/gitlab.go +++ b/server/remote/gitlab3/gitlab.go @@ -48,19 +48,19 @@ type Opts struct { // New returns a Remote implementation that integrates with Gitlab, an open // source Git service. See https://gitlab.com func New(opts Opts) (remote.Remote, error) { - url, err := url.Parse(opts.URL) + u, err := url.Parse(opts.URL) if err != nil { return nil, err } - host, _, err := net.SplitHostPort(url.Host) + host, _, err := net.SplitHostPort(u.Host) if err == nil { - url.Host = host + u.Host = host } return &Gitlab{ URL: opts.URL, Client: opts.Client, Secret: opts.Secret, - Machine: url.Host, + Machine: u.Host, Username: opts.Username, Password: opts.Password, PrivateMode: opts.PrivateMode, diff --git a/server/remote/gogs/gogs.go b/server/remote/gogs/gogs.go index 17d2acf07..deeee7b7d 100644 --- a/server/remote/gogs/gogs.go +++ b/server/remote/gogs/gogs.go @@ -48,17 +48,17 @@ type client struct { // New returns a Remote implementation that integrates with Gogs, an open // source Git service written in Go. See https://gogs.io/ func New(opts Opts) (remote.Remote, error) { - url, err := url.Parse(opts.URL) + u, err := url.Parse(opts.URL) if err != nil { return nil, err } - host, _, err := net.SplitHostPort(url.Host) + host, _, err := net.SplitHostPort(u.Host) if err == nil { - url.Host = host + u.Host = host } return &client{ URL: opts.URL, - Machine: url.Host, + Machine: u.Host, Username: opts.Username, Password: opts.Password, PrivateMode: opts.PrivateMode, diff --git a/server/shared/configFetcher_test.go b/server/shared/configFetcher_test.go index b8b83b4f8..eb605e583 100644 --- a/server/shared/configFetcher_test.go +++ b/server/shared/configFetcher_test.go @@ -202,7 +202,7 @@ func TestFetch(t *testing.T) { } if matchingFiles != len(tt.expectedFileNames) { - receivedFileNames := []string{} + var receivedFileNames []string for _, file := range files { receivedFileNames = append(receivedFileNames, file.Name) } diff --git a/server/shared/procStatus_test.go b/server/shared/procStatus_test.go index f4c139876..6d238a17d 100644 --- a/server/shared/procStatus_test.go +++ b/server/shared/procStatus_test.go @@ -37,7 +37,7 @@ func TestUpdateProcStatusNotExited(t *testing.T) { Exited: false, // Dummy data Finished: int64(1), - ExitCode: int(137), + ExitCode: 137, Error: "not an error", } proc, _ := UpdateProcStatus(&mockUpdateProcStore{}, model.Proc{}, state, int64(1)) @@ -48,7 +48,7 @@ func TestUpdateProcStatusNotExited(t *testing.T) { t.Errorf("Proc started not equals 42 != %d", proc.Started) } else if proc.Stopped != int64(0) { t.Errorf("Proc stopped not equals 0 != %d", proc.Stopped) - } else if proc.ExitCode != int(0) { + } else if proc.ExitCode != 0 { t.Errorf("Proc exit code not equals 0 != %d", proc.ExitCode) } else if proc.Error != "" { t.Errorf("Proc error not equals '' != '%s'", proc.Error) @@ -64,7 +64,7 @@ func TestUpdateProcStatusNotExitedButStopped(t *testing.T) { Exited: false, // Dummy data Finished: int64(1), - ExitCode: int(137), + ExitCode: 137, Error: "not an error", } proc, _ = UpdateProcStatus(&mockUpdateProcStore{}, *proc, state, int64(42)) @@ -75,7 +75,7 @@ func TestUpdateProcStatusNotExitedButStopped(t *testing.T) { t.Errorf("Proc started not equals 42 != %d", proc.Started) } else if proc.Stopped != int64(64) { t.Errorf("Proc stopped not equals 64 != %d", proc.Stopped) - } else if proc.ExitCode != int(0) { + } else if proc.ExitCode != 0 { t.Errorf("Proc exit code not equals 0 != %d", proc.ExitCode) } else if proc.Error != "" { t.Errorf("Proc error not equals '' != '%s'", proc.Error) @@ -89,7 +89,7 @@ func TestUpdateProcStatusExited(t *testing.T) { Started: int64(42), Exited: true, Finished: int64(34), - ExitCode: int(137), + ExitCode: 137, Error: "an error", } proc, _ := UpdateProcStatus(&mockUpdateProcStore{}, model.Proc{}, state, int64(42)) @@ -100,7 +100,7 @@ func TestUpdateProcStatusExited(t *testing.T) { t.Errorf("Proc started not equals 42 != %d", proc.Started) } else if proc.Stopped != int64(34) { t.Errorf("Proc stopped not equals 34 != %d", proc.Stopped) - } else if proc.ExitCode != int(137) { + } else if proc.ExitCode != 137 { t.Errorf("Proc exit code not equals 137 != %d", proc.ExitCode) } else if proc.Error != "an error" { t.Errorf("Proc error not equals 'an error' != '%s'", proc.Error) @@ -124,7 +124,7 @@ func TestUpdateProcStatusExitedButNot137(t *testing.T) { t.Errorf("Proc started not equals 42 != %d", proc.Started) } else if proc.Stopped != int64(34) { t.Errorf("Proc stopped not equals 34 != %d", proc.Stopped) - } else if proc.ExitCode != int(0) { + } else if proc.ExitCode != 0 { t.Errorf("Proc exit code not equals 0 != %d", proc.ExitCode) } else if proc.Error != "an error" { t.Errorf("Proc error not equals 'an error' != '%s'", proc.Error) @@ -138,14 +138,14 @@ func TestUpdateProcStatusExitedWithCode(t *testing.T) { Started: int64(42), Exited: true, Finished: int64(34), - ExitCode: int(1), + ExitCode: 1, Error: "an error", } proc, _ := UpdateProcStatus(&mockUpdateProcStore{}, model.Proc{}, state, int64(42)) if proc.State != model.StatusFailure { t.Errorf("Proc status not equals '%s' != '%s'", model.StatusFailure, proc.State) - } else if proc.ExitCode != int(1) { + } else if proc.ExitCode != 1 { t.Errorf("Proc exit code not equals 1 != %d", proc.ExitCode) } } @@ -206,7 +206,7 @@ func TestUpdateProcStatusToDoneSkipped(t *testing.T) { t.Errorf("Proc stopped not equals 34 != %d", proc.Stopped) } else if proc.Error != "" { t.Errorf("Proc error not equals '' != '%s'", proc.Error) - } else if proc.ExitCode != int(0) { + } else if proc.ExitCode != 0 { t.Errorf("Proc exit code not equals 0 != %d", proc.ExitCode) } } @@ -227,7 +227,7 @@ func TestUpdateProcStatusToDoneSuccess(t *testing.T) { t.Errorf("Proc stopped not equals 34 != %d", proc.Stopped) } else if proc.Error != "" { t.Errorf("Proc error not equals '' != '%s'", proc.Error) - } else if proc.ExitCode != int(0) { + } else if proc.ExitCode != 0 { t.Errorf("Proc exit code not equals 0 != %d", proc.ExitCode) } } @@ -247,7 +247,7 @@ func TestUpdateProcStatusToDoneFailureWithError(t *testing.T) { func TestUpdateProcStatusToDoneFailureWithExitCode(t *testing.T) { t.Parallel() - state := rpc.State{ExitCode: int(43)} + state := rpc.State{ExitCode: 43} proc, _ := UpdateProcStatusToDone(&mockUpdateProcStore{}, model.Proc{}, state) @@ -269,7 +269,7 @@ func TestUpdateProcToStatusKilledStarted(t *testing.T) { t.Errorf("Proc stopped not equals %d < %d", now, proc.Stopped) } else if proc.Started != proc.Stopped { t.Errorf("Proc started not equals %d != %d", proc.Stopped, proc.Started) - } else if proc.ExitCode != int(137) { + } else if proc.ExitCode != 137 { t.Errorf("Proc exit code not equals 137 != %d", proc.ExitCode) } } diff --git a/server/shared/userSyncer.go b/server/shared/userSyncer.go index cdd3e8e34..ec4e91f56 100644 --- a/server/shared/userSyncer.go +++ b/server/shared/userSyncer.go @@ -69,12 +69,12 @@ func (s *Syncer) Sync(user *model.User) error { return err } - var remote []*model.Repo + var remoteRepos []*model.Repo var perms []*model.Perm for _, repo := range repos { if s.Match(repo) { - remote = append(remote, repo) + remoteRepos = append(remoteRepos, repo) perm := model.Perm{ UserID: user.ID, Repo: repo.FullName, @@ -89,7 +89,7 @@ func (s *Syncer) Sync(user *model.User) error { } } - err = s.Store.RepoBatch(remote) + err = s.Store.RepoBatch(remoteRepos) if err != nil { return err } diff --git a/server/store/datastore/init.go b/server/store/datastore/init.go index 12febc36f..3728245f0 100644 --- a/server/store/datastore/init.go +++ b/server/store/datastore/init.go @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +//go:build !cgo // +build !cgo package datastore diff --git a/server/store/datastore/init_cgo.go b/server/store/datastore/init_cgo.go index 8a4e20839..3d71d08a3 100644 --- a/server/store/datastore/init_cgo.go +++ b/server/store/datastore/init_cgo.go @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +//go:build cgo // +build cgo package datastore diff --git a/shared/oauth2/oauth2.go b/shared/oauth2/oauth2.go index d69d74848..78e40ff89 100644 --- a/shared/oauth2/oauth2.go +++ b/shared/oauth2/oauth2.go @@ -380,29 +380,6 @@ func (t *Transport) AuthenticateClient() error { return t.updateToken(t.Token, url.Values{"grant_type": {"client_credentials"}}) } -// providerAuthHeaderWorks reports whether the OAuth2 server identified by the tokenURL -// implements the OAuth2 spec correctly -// See https://code.google.com/p/goauth2/issues/detail?id=31 for background. -// In summary: -// - Reddit only accepts client secret in the Authorization header -// - Dropbox accepts either it in URL param or Auth header, but not both. -// - Google only accepts URL param (not spec compliant?), not Auth header -func providerAuthHeaderWorks(tokenURL string) bool { - if strings.HasPrefix(tokenURL, "https://accounts.google.com/") || - strings.HasPrefix(tokenURL, "https://github.com/") || - strings.HasPrefix(tokenURL, "https://api.instagram.com/") || - strings.HasPrefix(tokenURL, "https://www.douban.com/") { - // Some sites fail to implement the OAuth2 spec fully. - return false - } - - // Assume the provider implements the spec properly - // otherwise. We can add more exceptions as they're - // discovered. We will _not_ be adding configurable hooks - // to this package to let users select server bugs. - return true -} - // updateToken mutates both tok and v. func (t *Transport) updateToken(tok *Token, v url.Values) error { v.Set("client_id", t.ClientId) diff --git a/tools.go b/tools.go index d26da3563..adb85cf43 100644 --- a/tools.go +++ b/tools.go @@ -5,6 +5,5 @@ package tools import ( - _ "github.com/bmatcuk/doublestar" _ "github.com/woodpecker-ci/togo" ) diff --git a/vendor/modules.txt b/vendor/modules.txt index 30d3e5561..d58715bf4 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -174,8 +174,6 @@ github.com/mattn/go-isatty # github.com/mattn/go-sqlite3 v2.0.3+incompatible ## explicit github.com/mattn/go-sqlite3 -# github.com/mattn/go-zglob v0.0.3 -## explicit # github.com/matttproud/golang_protobuf_extensions v1.0.1 github.com/matttproud/golang_protobuf_extensions/pbutil # github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd