diff --git a/go.mod b/go.mod index ae5f97f..1a4d2e7 100644 --- a/go.mod +++ b/go.mod @@ -6,9 +6,9 @@ require ( code.gitea.io/sdk/gitea v0.18.0 github.com/rs/zerolog v1.32.0 github.com/stretchr/testify v1.9.0 - github.com/thegeeklab/wp-plugin-go/v2 v2.3.0 + github.com/thegeeklab/wp-plugin-go/v2 v2.3.1 github.com/urfave/cli/v2 v2.27.2 - golang.org/x/crypto v0.22.0 + golang.org/x/crypto v0.23.0 ) require ( @@ -33,7 +33,7 @@ require ( github.com/shopspring/decimal v1.2.0 // indirect github.com/spf13/cast v1.3.1 // indirect github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect - golang.org/x/net v0.24.0 // indirect + golang.org/x/net v0.25.0 // indirect golang.org/x/sys v0.20.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 9794680..b5b7d64 100644 --- a/go.sum +++ b/go.sum @@ -54,8 +54,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -github.com/thegeeklab/wp-plugin-go/v2 v2.3.0 h1:9LOdITzjxEEbgcH9yU0tDZL0dcDZzNYzbk2+hZ5QsBA= -github.com/thegeeklab/wp-plugin-go/v2 v2.3.0/go.mod h1:I/3M/4OPvr4FFS+s0aaImpX1llA/lS2KC6Bnp+qzsCs= +github.com/thegeeklab/wp-plugin-go/v2 v2.3.1 h1:ARwYgTPZ5iPsmOenmqcCf8TjiEe8wBOHKO7H/Xshe48= +github.com/thegeeklab/wp-plugin-go/v2 v2.3.1/go.mod h1:0t8M8txtEFiaB6RqLX8vLrxkqAo5FT5Hx7dztN592D4= github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI= github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM= github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw= @@ -66,16 +66,16 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= -golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= -golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= -golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= -golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= +golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= +golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -93,8 +93,8 @@ golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= -golang.org/x/term v0.19.0 h1:+ThwsDv+tYfnJFhF4L8jITxu1tdTWRTZpdsWgEgjL6Q= -golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk= +golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw= +golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= diff --git a/plugin/impl.go b/plugin/impl.go index 7da2f8e..384f997 100644 --- a/plugin/impl.go +++ b/plugin/impl.go @@ -136,7 +136,7 @@ func (p *Plugin) FlagsFromContext() error { if len(p.Settings.Checksum.Value()) > 0 { var err error - files, err = WriteChecksums(files, p.Settings.Checksum.Value()) + files, err = WriteChecksums(files, p.Settings.Checksum.Value(), "") if err != nil { return fmt.Errorf("failed to write checksums: %w", err) } diff --git a/plugin/release.go b/plugin/release.go index 834dd59..149c50e 100644 --- a/plugin/release.go +++ b/plugin/release.go @@ -15,8 +15,7 @@ var ( ErrFileExists = errors.New("asset file already exist") ) -// release is a struct that holds the necessary information to interact with the Gitea API -// to manage releases for a given repository. +// Release represents a release for a Gitea repository. type Release struct { *gitea.Client Owner string @@ -29,7 +28,7 @@ type Release struct { Note string } -// buildRelease attempts to retrieve an existing release by the specified tag, and if not found, creates a new release. +// buildRelease attempts to retrieve an existing release by the specified tag name. func (rc *Release) buildRelease() (*gitea.Release, error) { // first attempt to get a release by that tag release, err := rc.getRelease() @@ -49,7 +48,7 @@ func (rc *Release) buildRelease() (*gitea.Release, error) { return release, nil } -// getRelease retrieves the release with the specified tag name from the Gitea repository. +// getRelease retrieves the release with the specified tag name from the repository. func (rc *Release) getRelease() (*gitea.Release, error) { releases, _, err := rc.Client.ListReleases(rc.Owner, rc.Repo, gitea.ListReleasesOptions{}) if err != nil { @@ -67,6 +66,7 @@ func (rc *Release) getRelease() (*gitea.Release, error) { return nil, fmt.Errorf("%w: %s", ErrReleaseNotFound, rc.Tag) } +// newRelease creates a new release on the repository with the specified options. func (rc *Release) newRelease() (*gitea.Release, error) { r := gitea.CreateReleaseOption{ TagName: rc.Tag, @@ -87,14 +87,14 @@ func (rc *Release) newRelease() (*gitea.Release, error) { } // uploadFiles uploads the specified files as attachments to the release with the given ID. -// It first checks for existing attachments with the same names, -// and handles them according to the FileExists configuration: +// It first checks for any existing attachments with the same names, +// and handles them according to the FileExists option: // // - "overwrite": overwrites the existing attachment // - "fail": returns an error if the file already exists // - "skip": skips uploading the file and logs a warning // -// If the file does not exist, it is uploaded as a new attachment. +// If there are no conflicts, it uploads the new files as attachments to the release. func (rc *Release) uploadFiles(releaseID int64, files []string) error { attachments, _, err := rc.Client.ListReleaseAttachments( rc.Owner, diff --git a/plugin/util.go b/plugin/util.go index e7e0f83..aaa33f7 100644 --- a/plugin/util.go +++ b/plugin/util.go @@ -11,6 +11,7 @@ import ( "hash/crc32" "io" "os" + "path/filepath" "golang.org/x/crypto/blake2b" "golang.org/x/crypto/blake2s" @@ -52,44 +53,42 @@ func Checksum(r io.Reader, method string) (string, error) { // WriteChecksums calculates the checksums for the given files using the specified hash methods, // and writes the checksums to files named after the hash methods (e.g. "md5sum.txt", "sha256sum.txt"). -func WriteChecksums(files, methods []string) ([]string, error) { - checksums := make(map[string][]string) +func WriteChecksums(files, methods []string, outDir string) ([]string, error) { + if len(files) == 0 { + return files, nil + } + + checksumFiles := make([]string, 0) for _, method := range methods { + checksumFile := filepath.Join(outDir, method+"sum.txt") + + f, err := os.Create(checksumFile) + if err != nil { + return nil, err + } + defer f.Close() + for _, file := range files { handle, err := os.Open(file) if err != nil { return nil, fmt.Errorf("failed to read %q artifact: %w", file, err) } + defer handle.Close() hash, err := Checksum(handle, method) if err != nil { return nil, fmt.Errorf("could not checksum %q file: %w", file, err) } - checksums[method] = append(checksums[method], hash, file) - } - } - - for method, results := range checksums { - filename := method + "sum.txt" - - f, err := os.Create(filename) - if err != nil { - return nil, err - } - - for i := 0; i < len(results); i += 2 { - hash := results[i] - file := results[i+1] - - if _, err := f.WriteString(fmt.Sprintf("%s %s\n", hash, file)); err != nil { + _, err = f.WriteString(fmt.Sprintf("%s %s\n", hash, file)) + if err != nil { return nil, err } } - files = append(files, filename) + checksumFiles = append(checksumFiles, checksumFile) } - return files, nil + return append(files, checksumFiles...), nil } diff --git a/plugin/util_test.go b/plugin/util_test.go index 841065a..6973002 100644 --- a/plugin/util_test.go +++ b/plugin/util_test.go @@ -14,74 +14,74 @@ import ( //nolint:lll func TestChecksum(t *testing.T) { tests := []struct { - name string - method string - input []byte - expected string - wantErr bool + name string + method string + input []byte + want string + wantErr bool }{ { - name: "md5", - method: "md5", - input: []byte("hello"), - expected: "5d41402abc4b2a76b9719d911017c592", - wantErr: false, + name: "md5", + method: "md5", + input: []byte("hello"), + want: "5d41402abc4b2a76b9719d911017c592", + wantErr: false, }, { - name: "sha1", - method: "sha1", - input: []byte("hello"), - expected: "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d", - wantErr: false, + name: "sha1", + method: "sha1", + input: []byte("hello"), + want: "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d", + wantErr: false, }, { - name: "sha256", - method: "sha256", - input: []byte("hello"), - expected: "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824", - wantErr: false, + name: "sha256", + method: "sha256", + input: []byte("hello"), + want: "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824", + wantErr: false, }, { - name: "sha512", - method: "sha512", - input: []byte("hello"), - expected: "9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043", - wantErr: false, + name: "sha512", + method: "sha512", + input: []byte("hello"), + want: "9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043", + wantErr: false, }, { - name: "adler32", - method: "adler32", - input: []byte("hello"), - expected: "062c0215", - wantErr: false, + name: "adler32", + method: "adler32", + input: []byte("hello"), + want: "062c0215", + wantErr: false, }, { - name: "crc32", - method: "crc32", - input: []byte("hello"), - expected: "3610a686", - wantErr: false, + name: "crc32", + method: "crc32", + input: []byte("hello"), + want: "3610a686", + wantErr: false, }, { - name: "blake2b", - method: "blake2b", - input: []byte("hello"), - expected: "324dcf027dd4a30a932c441f365a25e86b173defa4b8e58948253471b81b72cf", - wantErr: false, + name: "blake2b", + method: "blake2b", + input: []byte("hello"), + want: "324dcf027dd4a30a932c441f365a25e86b173defa4b8e58948253471b81b72cf", + wantErr: false, }, { - name: "blake2s", - method: "blake2s", - input: []byte("hello"), - expected: "19213bacc58dee6dbde3ceb9a47cbb330b3d86f8cca8997eb00be456f140ca25", - wantErr: false, + name: "blake2s", + method: "blake2s", + input: []byte("hello"), + want: "19213bacc58dee6dbde3ceb9a47cbb330b3d86f8cca8997eb00be456f140ca25", + wantErr: false, }, { - name: "unsupported_method", - method: "unsupported", - input: []byte("hello"), - expected: "", - wantErr: true, + name: "unsupported_method", + method: "unsupported", + input: []byte("hello"), + want: "", + wantErr: true, }, } @@ -97,7 +97,7 @@ func TestChecksum(t *testing.T) { } assert.NoError(t, err) - assert.Equal(t, tt.expected, sum) + assert.Equal(t, tt.want, sum) }) } } @@ -137,17 +137,19 @@ func TestWriteChecksums(t *testing.T) { methods := []string{"md5", "sha256"} tests := []struct { - name string - files []string - methods []string - expected []string - wantErr bool + name string + tempDir string + files []string + methods []string + want []string + wantErr bool }{ { - name: "valid_input", + name: "valid input", + tempDir: tempDir, files: files, methods: methods, - expected: []string{ + want: []string{ files[0], files[1], filepath.Join(tempDir, "md5sum.txt"), @@ -156,37 +158,37 @@ func TestWriteChecksums(t *testing.T) { wantErr: false, }, { - name: "empty_files", + name: "empty files", + tempDir: tempDir, files: []string{}, methods: methods, - expected: []string{ - filepath.Join(tempDir, "md5sum.txt"), - filepath.Join(tempDir, "sha256sum.txt"), - }, + want: []string{}, wantErr: false, }, { - name: "empty_methods", + name: "empty methods", + tempDir: tempDir, files: files, methods: []string{}, - expected: []string{ + want: []string{ files[0], files[1], }, wantErr: false, }, { - name: "non_existent_file", - files: append(files, filepath.Join(tempDir, "non_existent.txt")), - methods: methods, - expected: nil, - wantErr: true, + name: "non existent file", + tempDir: tempDir, + files: append(files, filepath.Join(tempDir, "non_existent.txt")), + methods: methods, + want: nil, + wantErr: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - result, err := WriteChecksums(tt.files, tt.methods) + result, err := WriteChecksums(tt.files, tt.methods, tt.tempDir) if tt.wantErr { assert.Error(t, err) @@ -195,8 +197,8 @@ func TestWriteChecksums(t *testing.T) { assert.NoError(t, err) sort.Strings(result) - sort.Strings(tt.expected) - assert.Equal(t, tt.expected, result) + sort.Strings(tt.want) + assert.Equal(t, tt.want, result) }) } }