1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-24 10:07:21 +02:00

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -20,7 +20,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"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)
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

View File

@ -16,7 +16,7 @@ package bitbucket
import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"github.com/woodpecker-ci/woodpecker/server/model"
@ -34,7 +34,7 @@ const (
// 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.
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) {
case hookPush:

View File

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

View File

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

View File

@ -17,7 +17,7 @@ package coding
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"regexp"
"strings"
@ -94,7 +94,7 @@ type MergeRequestHook struct {
}
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()
if err != nil {
return nil, nil, err

View File

@ -15,7 +15,7 @@
package coding
import (
"io/ioutil"
"io"
"net/http"
"strings"
"testing"
@ -30,7 +30,7 @@ func Test_hook(t *testing.T) {
g := goblin.Goblin(t)
g.Describe("Coding 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{
Header: map[string][]string{
hookEvent: {hookPush},

View File

@ -18,7 +18,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"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)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
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"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
@ -46,7 +45,7 @@ func parseHook(r *http.Request, merge bool) (*github.PullRequest, *model.Repo, *
reader = bytes.NewBufferString(payload)
}
raw, err := ioutil.ReadAll(reader)
raw, err := io.ReadAll(reader)
if err != nil {
return nil, nil, nil, err
}

View File

@ -14,7 +14,8 @@
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 (
"context"

View File

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

View File

@ -322,53 +322,8 @@ func metadataFromStruct(repo *model.Repo, build, last *model.Build, proc *model.
Private: repo.IsSCMPrivate,
Branch: repo.Branch,
},
Curr: frontend.Build{
Number: build.Number,
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,
},
},
Curr: metadataBuildFromModelBuild(build, true),
Prev: metadataBuildFromModelBuild(last, false),
Job: frontend.Job{
Number: proc.PID,
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 {
path = filepath.Base(path)
path = strings.TrimSuffix(path, ".yml")

View File

@ -17,7 +17,6 @@ package datastore
import (
"bytes"
"io"
"io/ioutil"
"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
}
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 {
data, err := ioutil.ReadAll(reader)
data, err := io.ReadAll(reader)
if err != nil {
return err
}

View File

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

View File

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

View File

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

View File

@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
@ -522,7 +521,7 @@ func (c *client) open(rawurl, method string, in, out interface{}) (io.ReadCloser
return nil, derr
}
buf := bytes.NewBuffer(decoded)
req.Body = ioutil.NopCloser(buf)
req.Body = io.NopCloser(buf)
req.ContentLength = int64(len(decoded))
req.Header.Set("Content-Length", strconv.Itoa(len(decoded)))
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 {
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 resp.Body, nil

View File

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