Dedup code and migrate away from deprecated funcs (#1141)

Co-authored-by: Anbraten <anton@ju60.de>
This commit is contained in:
6543 2022-08-30 01:14:07 +02:00 committed by GitHub
parent ca84f703e3
commit 08a99152d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 86 additions and 113 deletions

View file

@ -65,3 +65,10 @@ func FormatFlag(tmpl string, hidden ...bool) *cli.StringFlag {
Hidden: len(hidden) != 0, Hidden: len(hidden) != 0,
} }
} }
// specify repository
var RepoFlag = &cli.StringFlag{
Name: "repository",
Aliases: []string{"repo"},
Usage: "repository name (e.g. octocat/hello-world)",
}

View file

@ -1,7 +1,7 @@
package registry package registry
import ( import (
"io/ioutil" "os"
"strings" "strings"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -17,10 +17,7 @@ var registryCreateCmd = &cli.Command{
ArgsUsage: "[repo/name]", ArgsUsage: "[repo/name]",
Action: registryCreate, Action: registryCreate,
Flags: append(common.GlobalFlags, Flags: append(common.GlobalFlags,
&cli.StringFlag{ common.RepoFlag,
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
&cli.StringFlag{ &cli.StringFlag{
Name: "hostname", Name: "hostname",
Usage: "registry hostname", Usage: "registry hostname",
@ -62,7 +59,7 @@ func registryCreate(c *cli.Context) error {
} }
if strings.HasPrefix(registry.Password, "@") { if strings.HasPrefix(registry.Password, "@") {
path := strings.TrimPrefix(registry.Password, "@") path := strings.TrimPrefix(registry.Password, "@")
out, ferr := ioutil.ReadFile(path) out, ferr := os.ReadFile(path)
if ferr != nil { if ferr != nil {
return ferr return ferr
} }

View file

@ -16,10 +16,7 @@ var registryInfoCmd = &cli.Command{
ArgsUsage: "[repo/name]", ArgsUsage: "[repo/name]",
Action: registryInfo, Action: registryInfo,
Flags: append(common.GlobalFlags, Flags: append(common.GlobalFlags,
&cli.StringFlag{ common.RepoFlag,
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
&cli.StringFlag{ &cli.StringFlag{
Name: "hostname", Name: "hostname",
Usage: "registry hostname", Usage: "registry hostname",

View file

@ -16,10 +16,7 @@ var registryListCmd = &cli.Command{
ArgsUsage: "[repo/name]", ArgsUsage: "[repo/name]",
Action: registryList, Action: registryList,
Flags: append(common.GlobalFlags, Flags: append(common.GlobalFlags,
&cli.StringFlag{ common.RepoFlag,
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
common.FormatFlag(tmplRegistryList, true), common.FormatFlag(tmplRegistryList, true),
), ),
} }

View file

@ -13,10 +13,7 @@ var registryDeleteCmd = &cli.Command{
ArgsUsage: "[repo/name]", ArgsUsage: "[repo/name]",
Action: registryDelete, Action: registryDelete,
Flags: append(common.GlobalFlags, Flags: append(common.GlobalFlags,
&cli.StringFlag{ common.RepoFlag,
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
&cli.StringFlag{ &cli.StringFlag{
Name: "hostname", Name: "hostname",
Usage: "registry hostname", Usage: "registry hostname",

View file

@ -1,7 +1,7 @@
package registry package registry
import ( import (
"io/ioutil" "os"
"strings" "strings"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -17,10 +17,7 @@ var registryUpdateCmd = &cli.Command{
ArgsUsage: "[repo/name]", ArgsUsage: "[repo/name]",
Action: registryUpdate, Action: registryUpdate,
Flags: append(common.GlobalFlags, Flags: append(common.GlobalFlags,
&cli.StringFlag{ common.RepoFlag,
Name: "repository",
Usage: "repository name (e.g. octocat/hello-world)",
},
&cli.StringFlag{ &cli.StringFlag{
Name: "hostname", Name: "hostname",
Usage: "registry hostname", Usage: "registry hostname",
@ -62,7 +59,7 @@ func registryUpdate(c *cli.Context) error {
} }
if strings.HasPrefix(registry.Password, "@") { if strings.HasPrefix(registry.Password, "@") {
path := strings.TrimPrefix(registry.Password, "@") path := strings.TrimPrefix(registry.Password, "@")
out, ferr := ioutil.ReadFile(path) out, ferr := os.ReadFile(path)
if ferr != nil { if ferr != nil {
return ferr return ferr
} }

View file

@ -2,7 +2,7 @@ package multipart
import ( import (
"bytes" "bytes"
"io/ioutil" "io"
"testing" "testing"
) )
@ -20,7 +20,7 @@ func TestReader(t *testing.T) {
if got, want := header.Get("Content-Type"), "text/plain"; got != want { if got, want := header.Get("Content-Type"), "text/plain"; got != want {
t.Errorf("Want Content-Type %q, got %q", want, got) t.Errorf("Want Content-Type %q, got %q", want, got)
} }
body, err := ioutil.ReadAll(part) body, err := io.ReadAll(part)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return

View file

@ -7,7 +7,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
@ -53,7 +52,7 @@ func Send(ctx context.Context, method, path string, privateKey crypto.PrivateKey
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return resp.StatusCode, err return resp.StatusCode, err
} }

View file

@ -20,7 +20,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
@ -252,7 +251,7 @@ func (c *Client) do(rawurl, method string, in, out interface{}) (*string, error)
return nil, json.NewDecoder(resp.Body).Decode(out) return nil, json.NewDecoder(resp.Body).Decode(out)
} }
bodyBytes, err := ioutil.ReadAll(resp.Body) bodyBytes, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -16,7 +16,7 @@ package bitbucket
import ( import (
"encoding/json" "encoding/json"
"io/ioutil" "io"
"net/http" "net/http"
"github.com/woodpecker-ci/woodpecker/server/model" "github.com/woodpecker-ci/woodpecker/server/model"
@ -34,7 +34,7 @@ const (
// parseHook parses a Bitbucket hook from an http.Request request and returns // parseHook parses a Bitbucket hook from an http.Request request and returns
// Repo and Build detail. If a hook type is unsupported nil values are returned. // Repo and Build detail. If a hook type is unsupported nil values are returned.
func parseHook(r *http.Request) (*model.Repo, *model.Build, error) { func parseHook(r *http.Request) (*model.Repo, *model.Build, error) {
payload, _ := ioutil.ReadAll(r.Body) payload, _ := io.ReadAll(r.Body)
switch r.Header.Get(hookEvent) { switch r.Header.Get(hookEvent) {
case hookPush: case hookPush:

View file

@ -24,8 +24,8 @@ import (
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"os"
"github.com/mrjones/oauth" "github.com/mrjones/oauth"
@ -86,7 +86,7 @@ func New(opts Opts) (remote.Remote, error) {
var keyFileBytes []byte var keyFileBytes []byte
if opts.ConsumerRSA != "" { if opts.ConsumerRSA != "" {
var err error var err error
keyFileBytes, err = ioutil.ReadFile(opts.ConsumerRSA) keyFileBytes, err = os.ReadFile(opts.ConsumerRSA)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -20,7 +20,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
@ -78,7 +77,7 @@ func (c *Client) FindCurrentUser() (*User, error) {
return nil, err return nil, err
} }
bits, err := ioutil.ReadAll(CurrentUserIDResponse.Body) bits, err := io.ReadAll(CurrentUserIDResponse.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -92,7 +91,7 @@ func (c *Client) FindCurrentUser() (*User, error) {
return nil, err return nil, err
} }
contents, err := ioutil.ReadAll(CurrentUserResponse.Body) contents, err := io.ReadAll(CurrentUserResponse.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -115,7 +114,7 @@ func (c *Client) FindRepo(owner, name string) (*Repo, error) {
if err != nil { if err != nil {
log.Err(err).Msg("") log.Err(err).Msg("")
} }
contents, err := ioutil.ReadAll(response.Body) contents, err := io.ReadAll(response.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -162,7 +161,7 @@ func (c *Client) FindFileForRepo(owner, repo, fileName, ref string) ([]byte, err
if response.StatusCode == 404 { if response.StatusCode == 404 {
return nil, nil return nil, nil
} }
responseBytes, err := ioutil.ReadAll(response.Body) responseBytes, err := io.ReadAll(response.Body)
if err != nil { if err != nil {
log.Err(err).Msg("") log.Err(err).Msg("")
} }

View file

@ -17,7 +17,7 @@ package coding
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"regexp" "regexp"
"strings" "strings"
@ -94,7 +94,7 @@ type MergeRequestHook struct {
} }
func parseHook(r *http.Request) (*model.Repo, *model.Build, error) { func parseHook(r *http.Request) (*model.Repo, *model.Build, error) {
raw, err := ioutil.ReadAll(r.Body) raw, err := io.ReadAll(r.Body)
defer r.Body.Close() defer r.Body.Close()
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err

View file

@ -15,7 +15,7 @@
package coding package coding
import ( import (
"io/ioutil" "io"
"net/http" "net/http"
"strings" "strings"
"testing" "testing"
@ -30,7 +30,7 @@ func Test_hook(t *testing.T) {
g := goblin.Goblin(t) g := goblin.Goblin(t)
g.Describe("Coding hook", func() { g.Describe("Coding hook", func() {
g.It("Should parse hook", func() { g.It("Should parse hook", func() {
reader := ioutil.NopCloser(strings.NewReader(fixtures.PushHook)) reader := io.NopCloser(strings.NewReader(fixtures.PushHook))
r := &http.Request{ r := &http.Request{
Header: map[string][]string{ Header: map[string][]string{
hookEvent: {hookPush}, hookEvent: {hookPush},

View file

@ -18,7 +18,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
@ -84,7 +84,7 @@ func (c *Client) Do(method, u string, params url.Values) ([]byte, error) {
return nil, fmt.Errorf("%s %s respond %d", req.Method, req.URL, resp.StatusCode) return nil, fmt.Errorf("%s %s respond %d", req.Method, req.URL, resp.StatusCode)
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, fmt.Errorf("fail to read response from %s %s: %v", req.Method, req.URL.String(), err) return nil, fmt.Errorf("fail to read response from %s %s: %v", req.Method, req.URL.String(), err)
} }

View file

@ -18,7 +18,6 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"strings" "strings"
@ -46,7 +45,7 @@ func parseHook(r *http.Request, merge bool) (*github.PullRequest, *model.Repo, *
reader = bytes.NewBufferString(payload) reader = bytes.NewBufferString(payload)
} }
raw, err := ioutil.ReadAll(reader) raw, err := io.ReadAll(reader)
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, err
} }

View file

@ -14,7 +14,8 @@
package remote package remote
//go:generate mockery -name Remote -output mocks -case=underscore //go:generate go install github.com/vektra/mockery/v2@latest
//go:generate mockery --name Remote --output mocks --case underscore
import ( import (
"context" "context"

View file

@ -6,7 +6,7 @@ import (
"crypto/rand" "crypto/rand"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"path/filepath" "path/filepath"
@ -382,7 +382,7 @@ func TestFetchFromConfigService(t *testing.T) {
} }
var req incoming var req incoming
body, err := ioutil.ReadAll(r.Body) body, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
http.Error(w, "can't read body", http.StatusBadRequest) http.Error(w, "can't read body", http.StatusBadRequest)
return return

View file

@ -322,53 +322,8 @@ func metadataFromStruct(repo *model.Repo, build, last *model.Build, proc *model.
Private: repo.IsSCMPrivate, Private: repo.IsSCMPrivate,
Branch: repo.Branch, Branch: repo.Branch,
}, },
Curr: frontend.Build{ Curr: metadataBuildFromModelBuild(build, true),
Number: build.Number, Prev: metadataBuildFromModelBuild(last, false),
Parent: build.Parent,
Created: build.Created,
Started: build.Started,
Finished: build.Finished,
Status: string(build.Status),
Event: string(build.Event),
Link: build.Link,
Target: build.Deploy,
Commit: frontend.Commit{
Sha: build.Commit,
Ref: build.Ref,
Refspec: build.Refspec,
Branch: build.Branch,
Message: build.Message,
Author: frontend.Author{
Name: build.Author,
Email: build.Email,
Avatar: build.Avatar,
},
ChangedFiles: build.ChangedFiles,
},
},
Prev: frontend.Build{
Number: last.Number,
Created: last.Created,
Started: last.Started,
Finished: last.Finished,
Status: string(last.Status),
Event: string(last.Event),
Link: last.Link,
Target: last.Deploy,
Commit: frontend.Commit{
Sha: last.Commit,
Ref: last.Ref,
Refspec: last.Refspec,
Branch: last.Branch,
Message: last.Message,
Author: frontend.Author{
Name: last.Author,
Email: last.Email,
Avatar: last.Avatar,
},
ChangedFiles: last.ChangedFiles,
},
},
Job: frontend.Job{ Job: frontend.Job{
Number: proc.PID, Number: proc.PID,
Matrix: proc.Environ, Matrix: proc.Environ,
@ -382,6 +337,38 @@ func metadataFromStruct(repo *model.Repo, build, last *model.Build, proc *model.
} }
} }
func metadataBuildFromModelBuild(build *model.Build, includeParent bool) frontend.Build {
parent := int64(0)
if includeParent {
parent = build.Parent
}
return frontend.Build{
Number: build.Number,
Parent: parent,
Created: build.Created,
Started: build.Started,
Finished: build.Finished,
Status: string(build.Status),
Event: string(build.Event),
Link: build.Link,
Target: build.Deploy,
Commit: frontend.Commit{
Sha: build.Commit,
Ref: build.Ref,
Refspec: build.Refspec,
Branch: build.Branch,
Message: build.Message,
Author: frontend.Author{
Name: build.Author,
Email: build.Email,
Avatar: build.Avatar,
},
ChangedFiles: build.ChangedFiles,
},
}
}
func SanitizePath(path string) string { func SanitizePath(path string) string {
path = filepath.Base(path) path = filepath.Base(path)
path = strings.TrimSuffix(path, ".yml") path = strings.TrimSuffix(path, ".yml")

View file

@ -17,7 +17,6 @@ package datastore
import ( import (
"bytes" "bytes"
"io" "io"
"io/ioutil"
"github.com/woodpecker-ci/woodpecker/server/model" "github.com/woodpecker-ci/woodpecker/server/model"
) )
@ -41,11 +40,11 @@ func (s storage) FileRead(proc *model.Proc, name string) (io.ReadCloser, error)
return nil, err return nil, err
} }
buf := bytes.NewBuffer(file.Data) buf := bytes.NewBuffer(file.Data)
return ioutil.NopCloser(buf), err return io.NopCloser(buf), err
} }
func (s storage) FileCreate(file *model.File, reader io.Reader) error { func (s storage) FileCreate(file *model.File, reader io.Reader) error {
data, err := ioutil.ReadAll(reader) data, err := io.ReadAll(reader)
if err != nil { if err != nil {
return err return err
} }

View file

@ -16,7 +16,7 @@ package datastore
import ( import (
"bytes" "bytes"
"io/ioutil" "io"
"testing" "testing"
"github.com/woodpecker-ci/woodpecker/server/model" "github.com/woodpecker-ci/woodpecker/server/model"
@ -41,7 +41,7 @@ func TestLogCreateFind(t *testing.T) {
} }
defer rc.Close() defer rc.Close()
out, _ := ioutil.ReadAll(rc) out, _ := io.ReadAll(rc)
if got, want := string(out), "echo hi"; got != want { if got, want := string(out), "echo hi"; got != want {
t.Errorf("Want log data %s, got %s", want, got) t.Errorf("Want log data %s, got %s", want, got)
} }
@ -71,7 +71,7 @@ func TestLogUpdate(t *testing.T) {
} }
defer rc.Close() defer rc.Close()
out, _ := ioutil.ReadAll(rc) out, _ := io.ReadAll(rc)
if got, want := string(out), "echo allo?"; got != want { if got, want := string(out), "echo allo?"; got != want {
t.Errorf("Want log data %s, got %s", want, got) t.Errorf("Want log data %s, got %s", want, got)
} }

View file

@ -1,7 +1,6 @@
package migration package migration
import ( import (
"io/ioutil"
"os" "os"
"testing" "testing"
"time" "time"
@ -29,16 +28,16 @@ func testDriver() string {
} }
func createSQLiteDB(t *testing.T) string { func createSQLiteDB(t *testing.T) string {
tmpF, err := ioutil.TempFile("./testfiles", "tmp_") tmpF, err := os.CreateTemp("./testfiles", "tmp_")
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
t.FailNow() t.FailNow()
} }
dbF, err := ioutil.ReadFile(sqliteDB) dbF, err := os.ReadFile(sqliteDB)
if !assert.NoError(t, err) { if !assert.NoError(t, err) {
t.FailNow() t.FailNow()
} }
if !assert.NoError(t, ioutil.WriteFile(tmpF.Name(), dbF, 0o644)) { if !assert.NoError(t, os.WriteFile(tmpF.Name(), dbF, 0o644)) {
t.FailNow() t.FailNow()
} }
return tmpF.Name() return tmpF.Name()

View file

@ -2,8 +2,8 @@ package web
import ( import (
"embed" "embed"
"io"
"io/fs" "io/fs"
"io/ioutil"
"net/http" "net/http"
) )
@ -26,7 +26,7 @@ func Lookup(path string) (buf []byte, err error) {
} }
defer file.Close() defer file.Close()
buf, err = ioutil.ReadAll(file) buf, err = io.ReadAll(file)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -5,7 +5,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"strconv" "strconv"
@ -522,7 +521,7 @@ func (c *client) open(rawurl, method string, in, out interface{}) (io.ReadCloser
return nil, derr return nil, derr
} }
buf := bytes.NewBuffer(decoded) buf := bytes.NewBuffer(decoded)
req.Body = ioutil.NopCloser(buf) req.Body = io.NopCloser(buf)
req.ContentLength = int64(len(decoded)) req.ContentLength = int64(len(decoded))
req.Header.Set("Content-Length", strconv.Itoa(len(decoded))) req.Header.Set("Content-Length", strconv.Itoa(len(decoded)))
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
@ -533,7 +532,7 @@ func (c *client) open(rawurl, method string, in, out interface{}) (io.ReadCloser
} }
if resp.StatusCode > http.StatusPartialContent { if resp.StatusCode > http.StatusPartialContent {
defer resp.Body.Close() defer resp.Body.Close()
out, _ := ioutil.ReadAll(resp.Body) out, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("client error %d: %s", resp.StatusCode, string(out)) return nil, fmt.Errorf("client error %d: %s", resp.StatusCode, string(out))
} }
return resp.Body, nil return resp.Body, nil

View file

@ -4,7 +4,7 @@ import (
"net/http" "net/http"
) )
// Client is used to communicate with a Drone server. // Client is used to communicate with a Woodpecker server.
type Client interface { type Client interface {
// SetClient sets the http.Client. // SetClient sets the http.Client.
SetClient(*http.Client) SetClient(*http.Client)