1
0

initial commit

This commit is contained in:
Konstantin Demin 2024-02-23 01:23:34 +03:00
commit 51e14d687f
Signed by: krd
GPG Key ID: 1F33CB0BA4731BC6
14 changed files with 1241 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/plugin-sonatype-nexus

175
LICENSE Normal file
View File

@ -0,0 +1,175 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.

56
Makefile Normal file
View File

@ -0,0 +1,56 @@
#!/usr/bin/make -f
# SPDX-License-Identifier: Apache-2.0
# (c) 2024, Konstantin Demin
SHELL :=/bin/sh
.SHELLFLAGS :=-ec
.NOTPARALLEL:
BIN := plugin-sonatype-nexus
OUTDIR ?= .
OUTBIN := $(OUTDIR)/$(BIN)
export GO ?= go
export CGO_ENABLED ?= 0
TAGS ?=
LDFLAGS ?=
GO_BUILDFLAGS ?=
GO_LDFLAGS := -w $(LDFLAGS)
comma :=,
ifeq ($(RELMODE),1)
## not ready yet
# TAGS := nodebug$(if $(strip $(TAGS)),$(comma)$(strip $(TAGS)))
GO_LDFLAGS += -s
endif
.PHONY: all
all: build
.PHONY: clean build dev-build
clean:
$(if $(wildcard $(OUTBIN)),rm -fv $(OUTBIN),:)
build: $(OUTBIN)
test_git = git -c log.showsignature=false show -s --format=%H:%ct
$(OUTBIN):
GO_BUILDFLAGS='$(strip $(GO_BUILDFLAGS))' ; \
if ! $(test_git) >/dev/null 2>&1 ; then \
echo "!!! git information is asbent !!!" >&2 ; \
GO_BUILDFLAGS="-buildvcs=false $${GO_BUILDFLAGS}" ; \
fi ; \
$(GO) build -o $@ \
$${GO_BUILDFLAGS} \
$(if $(strip $(TAGS)),-tags '$(strip $(TAGS))') \
$(if $(strip $(GO_LDFLAGS)),-ldflags '$(strip $(GO_LDFLAGS))') \
$(if $(VERBOSE),-v)
dev-build: GO_BUILDFLAGS := -race $(GO_BUILDFLAGS)
dev-build: CGO_ENABLED := 1
dev-build: RELMODE := 0
dev-build: build

72
flags.go Normal file
View File

@ -0,0 +1,72 @@
// SPDX-License-Identifier: Apache-2.0
// (c) 2024, Konstantin Demin
package main
import (
"github.com/urfave/cli/v2"
)
type Settings struct {
RootUrl string
AuthPlain string
AuthBase64 string
RawUploads string
// semi-interactive mode
Repository string
Paths cli.StringSlice
Properties cli.StringSlice
}
func (p *Plugin) Flags() []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: "nexus.url",
Usage: "Sonatype Nexus URL (e.g. \"https://nexus.domain.com\")",
EnvVars: []string{"PLUGIN_NEXUS_URL", "NEXUS_URL"},
Destination: &p.Settings.RootUrl,
Required: true,
},
&cli.StringFlag{
Name: "nexus.auth.base64",
Usage: "Sonatype Nexus authorization (base64-encoded, preferred)",
EnvVars: []string{"PLUGIN_NEXUS_AUTH_BASE64", "PLUGIN_AUTH_BASE64", "NEXUS_AUTH_BASE64"},
Destination: &p.Settings.AuthBase64,
},
// https://help.sonatype.com/en/user-tokens.html#use-user-token-for-repository-authentication
&cli.StringFlag{
Name: "nexus.auth.plain",
Usage: "Sonatype Nexus authorization (either {username}:{password} or {token name}:{token pass})",
EnvVars: []string{"PLUGIN_NEXUS_AUTH_PLAIN", "PLUGIN_AUTH_PLAIN", "NEXUS_AUTH_PLAIN"},
Destination: &p.Settings.AuthPlain,
},
&cli.StringFlag{
Name: "nexus.upload",
Usage: "List of upload rules (JSON array)",
EnvVars: []string{"PLUGIN_NEXUS_UPLOAD", "PLUGIN_UPLOAD", "NEXUS_UPLOAD"},
Destination: &p.Settings.RawUploads,
Value: "[]",
},
// semi-interactive mode
&cli.StringFlag{
Name: "nexus.repository",
Usage: "Repository name",
EnvVars: []string{"NEXUS_REPOSITORY"},
Destination: &p.Settings.Repository,
},
&cli.StringSliceFlag{
Name: "nexus.paths",
Usage: "Comma-separated list of paths/globs",
EnvVars: []string{"NEXUS_PATHS"},
Destination: &p.Settings.Paths,
},
&cli.StringSliceFlag{
Name: "nexus.properties",
Usage: "Comma-separated list of properties (in form {key}={value})",
EnvVars: []string{"NEXUS_PROPERTIES"},
Destination: &p.Settings.Properties,
},
}
}

23
go.mod Normal file
View File

@ -0,0 +1,23 @@
// SPDX-License-Identifier: Apache-2.0
// (c) 2024, Konstantin Demin
module git.krd.sh/krd/woodpecker-sonatype-nexus
go 1.21
require (
codeberg.org/woodpecker-plugins/go-plugin v0.3.1
github.com/rs/zerolog v1.32.0
github.com/urfave/cli/v2 v2.27.1
)
require (
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
)

39
go.sum Normal file
View File

@ -0,0 +1,39 @@
codeberg.org/woodpecker-plugins/go-plugin v0.3.1 h1:DVnKS4NXiDCp6boFYF5pzHvL98fQMN48Bd05Q1r9J/4=
codeberg.org/woodpecker-plugins/go-plugin v0.3.1/go.mod h1:7qBAmeCVaDxRzYA9LYG0aTzpOxv48eF4NWsOW58fmro=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e h1:+SOyEddqYF09QP7vr7CgJ1eti3pY9Fn3LHO1M1r/0sI=
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

306
impl.go Normal file
View File

@ -0,0 +1,306 @@
// SPDX-License-Identifier: Apache-2.0
// (c) 2024, Konstantin Demin
package main
import (
"bytes"
"context"
"fmt"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
"github.com/rs/zerolog/log"
)
func (p *Plugin) Execute(ctx context.Context) error {
var err error
err = p.parseSettings()
if err != nil {
return err
}
if len(p.Uploads) == 0 {
log.Info().Msg("nothing to upload")
return nil
}
err = p.GetNexusStatus(ctx)
if err != nil {
return err
}
err = p.getUploadSpecs(ctx)
if err != nil {
return err
}
repos := make(map[string]NexusRepo)
var seen bool
var repo NexusRepo
var spec UploadSpec
// validation
for i := range p.Uploads {
repo, seen = repos[p.Uploads[i].Repository]
if !seen {
repo, err = p.GetNexusRepo(ctx, p.Uploads[i].Repository)
if err != nil {
return fmt.Errorf("upload[%d] encountered error while getting repository information: %v", i, err)
}
repos[p.Uploads[i].Repository] = repo
}
spec, seen = p.UploadSpecs[repo.Format]
if !seen {
return fmt.Errorf("upload[%d] has format which is not known by upload-specs (this shouldn't happen!)", i)
}
for k := range p.Uploads[i].Properties {
_, seen = spec.AllFieldNames[k]
if seen {
continue
}
log.Info().Msgf("upload[%d]: %q is not used in %q spec", i, k, repo.Format)
delete(p.Uploads[i].Properties, k)
}
for _, cf := range spec.ComponentFields {
err = p.verifyUploadField(ctx, i, cf)
if err != nil {
return err
}
}
for _, af := range spec.AssetFields {
err = p.verifyUploadField(ctx, i, af)
if err != nil {
return err
}
}
}
var assets []string
for i := range p.Uploads {
repo = repos[p.Uploads[i].Repository]
spec = p.UploadSpecs[repo.Format]
// naive capacity assumption
assets = make([]string, 0, len(p.Uploads[i].Paths))
for k, patt := range p.Uploads[i].Paths {
g, err := filepath.Glob(patt)
if err != nil {
// this shouldn't happen
return err
}
if len(g) == 0 {
log.Info().Msgf("upload[%d].paths[%d]: empty match for %q", i, k, patt)
continue
}
assets = append(assets, g...)
}
if len(assets) == 0 {
log.Warn().Msgf("upload[%d]: none of paths have returned matches", i)
continue
}
if spec.MultipleUpload {
s_end := 0
for s_start := 0; s_start < len(assets); s_start += MaxAssetsPerUpload {
s_end += MaxAssetsPerUpload
if s_end > len(assets) {
s_end = len(assets)
}
err = p.uploadToNexus(ctx, &p.Uploads[i], &repo, &spec, assets[s_start:s_end]...)
if err != nil {
return err
}
}
} else {
for _, a := range assets {
err = p.uploadToNexus(ctx, &p.Uploads[i], &repo, &spec, a)
if err != nil {
return err
}
}
}
}
log.Debug().Msg("executed")
return nil
}
func (p *Plugin) verifyUploadField(ctx context.Context, uploadNum int, field UploadField) error {
prop, seen := p.Uploads[uploadNum].Properties[field.Name]
if !seen {
if field.Optional {
return nil
}
switch strings.ToLower(field.Name) {
case "asset", "filename":
// generated on per-artifact basis
return nil
}
return fmt.Errorf("upload[%d]: %q is required but missing", uploadNum, field.Name)
}
rkind1 := reflect.TypeOf(prop).Kind()
rkind2 := field.Type.ToReflectKind()
if rkind1 != rkind2 {
return fmt.Errorf("upload[%d]: %q has wrong type: %v != %v", uploadNum, field.Name, rkind1, rkind2)
}
switch field.Type {
case String, File:
s := prop.(string)
if s == "" {
if !field.Optional {
return fmt.Errorf("upload[%d]: %q is required but empty", uploadNum, field.Name)
}
log.Info().Msgf("upload[%d]: %q is set but empty", uploadNum, field.Name)
delete(p.Uploads[uploadNum].Properties, field.Name)
return nil
}
if field.Type == String {
// done with String
return nil
}
fpath, err := filepath.EvalSymlinks(s)
if err != nil {
return fmt.Errorf("upload[%d]: file %q is required but missing: %q %v", uploadNum, field.Name, s, err)
}
finfo, err := os.Stat(fpath)
if err != nil {
return fmt.Errorf("upload[%d]: file %q is required but missing: %q %v", uploadNum, field.Name, s, err)
}
if !finfo.Mode().IsRegular() {
return fmt.Errorf("upload[%d]: file %q is required but not a regular file: %q %v", uploadNum, field.Name, s, err)
}
}
return nil
}
func (p *Plugin) uploadToNexus(ctx context.Context, upload *UploadRule, repo *NexusRepo, spec *UploadSpec, assets ...string) error {
var err error
buf := new(bytes.Buffer)
w := multipart.NewWriter(buf)
var postField string
for _, cf := range spec.ComponentFields {
postField = fmt.Sprintf("%s.%s", repo.Format, cf.Name)
prop, seen := upload.Properties[cf.Name]
if !seen {
continue
}
err = writeFormFieldType(w, postField, cf.Type, prop)
if err != nil {
return err
}
}
var assetField string
for i, a := range assets {
if spec.MultipleUpload {
assetField = fmt.Sprintf("%s.asset%d", repo.Format, i+1)
} else {
assetField = fmt.Sprintf("%s.asset", repo.Format)
}
err = writeFormFile(w, assetField, a)
if err != nil {
return err
}
for _, af := range spec.AssetFields {
switch strings.ToLower(af.Name) {
case "asset":
//ignored
continue
}
postField = fmt.Sprintf("%s.%s", assetField, af.Name)
switch strings.ToLower(af.Name) {
case "filename":
err = w.WriteField(postField, filepath.Base(a))
if err != nil {
return err
}
continue
}
prop, seen := upload.Properties[af.Name]
if !seen {
continue
}
err = writeFormFieldType(w, postField, af.Type, prop)
if err != nil {
return err
}
}
}
err = w.Close()
if err != nil {
return err
}
res, err := p.NexusRequestEx(ctx, http.MethodPost, "v1/components?repository="+upload.Repository, buf, func(r *http.Request) {
r.Header.Set("Content-Type", w.FormDataContentType())
})
if err != nil {
return err
}
return GenericResponseHandler(res)
}
func writeFormFile(w *multipart.Writer, fieldName string, fileName string) error {
data, err := os.ReadFile(fileName)
if err != nil {
return err
}
part, err := w.CreateFormFile(fieldName, filepath.Base(fileName))
if err != nil {
return err
}
_, err = part.Write(data)
return err
}
func writeFormFieldType(w *multipart.Writer, fieldName string, fieldType UploadFieldType, fieldValue any) error {
switch fieldType {
case File:
return writeFormFile(w, fieldName, fieldValue.(string))
case String:
return w.WriteField(fieldName, fieldValue.(string))
case Boolean:
return w.WriteField(fieldName, strconv.FormatBool(fieldValue.(bool)))
}
return fmt.Errorf("unhandled field type: %v", fieldType)
}

35
main.go Normal file
View File

@ -0,0 +1,35 @@
// SPDX-License-Identifier: Apache-2.0
// (c) 2024, Konstantin Demin
package main
import (
"codeberg.org/woodpecker-plugins/go-plugin"
)
const (
MaxAssetsPerUpload = 32
)
type Plugin struct {
*plugin.Plugin
Settings *Settings
RestUrl string
UploadSpecs map[string]UploadSpec
Uploads []UploadRule
}
func main() {
p := &Plugin{
Settings: &Settings{},
}
p.Plugin = plugin.New(plugin.Options{
Name: "woodpecker-sonatype-nexus",
Description: "Woodpecker CI plugin to publish artifacts to Sonatype Nexus",
Version: "0.0.1",
Flags: p.Flags(),
Execute: p.Execute,
})
p.Run()
}

50
nexus-repo.go Normal file
View File

@ -0,0 +1,50 @@
// SPDX-License-Identifier: Apache-2.0
// (c) 2024, Konstantin Demin
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
)
type NexusRepo struct {
Name string `json:"name"`
Format string `json:"format"`
Type string `json:"type"`
Attributes map[string]string `json:"attributes,omitempty"`
}
func (p *Plugin) GetNexusRepo(ctx context.Context, repoName string) (NexusRepo, error) {
var empty, repo NexusRepo
if repoName == "" {
return empty, errors.New("\"repoName\" parameter is empty")
}
res, err := p.NexusRequest(ctx, "v1/repositories/"+repoName)
if err != nil {
return empty, err
}
defer res.Body.Close()
err = GenericResponseHandler(res)
if err != nil {
return empty, err
}
dec := json.NewDecoder(res.Body)
err = dec.Decode(&repo)
if err != nil {
return empty, err
}
// basic sanity check
if repo.Type == "proxy" {
return empty, fmt.Errorf("repository %q is type of %q", repoName, repo.Type)
}
// TODO: is "group" upload allowed?
return repo, nil
}

67
nexus-request.go Normal file
View File

@ -0,0 +1,67 @@
// SPDX-License-Identifier: Apache-2.0
// (c) 2024, Konstantin Demin
package main
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"strings"
)
func (p *Plugin) NexusRequest(ctx context.Context, requestUrl string) (*http.Response, error) {
return p.NexusRequestEx(ctx, http.MethodGet, requestUrl, nil, nil)
}
func (p *Plugin) NexusRequestEx(ctx context.Context, requestMethod string, requestUrl string, body io.Reader, requestSetup func(*http.Request)) (*http.Response, error) {
c := p.HTTPClient()
req, err := http.NewRequestWithContext(ctx, requestMethod, p.RestUrl+requestUrl, body)
if err != nil {
return nil, err
}
if req == nil {
return nil, errors.ErrUnsupported
}
if requestSetup != nil {
requestSetup(req)
}
// TODO: support more authz schemes
req.Header.Set("Authorization", "Basic "+p.Settings.AuthBase64)
res, err := c.Do(req)
if err != nil {
return nil, err
}
if res == nil {
return nil, errors.ErrUnsupported
}
return res, err
}
func GenericResponseHandler(response *http.Response) error {
if response == nil {
return errors.ErrUnsupported
}
if response.StatusCode >= 100 && response.StatusCode < 300 {
return nil
}
if strings.Contains(response.Status, " ") {
return errors.New("HTTP " + response.Status)
}
switch response.StatusCode {
case http.StatusUnauthorized:
return fmt.Errorf("HTTP %d Unauthorized", response.StatusCode)
case http.StatusForbidden:
return fmt.Errorf("HTTP %d Forbidden", response.StatusCode)
case http.StatusNotFound:
return fmt.Errorf("HTTP %d Not found", response.StatusCode)
}
return fmt.Errorf("HTTP %d", response.StatusCode)
}

49
nexus-status.go Normal file
View File

@ -0,0 +1,49 @@
// SPDX-License-Identifier: Apache-2.0
// (c) 2024, Konstantin Demin
package main
import (
"context"
"encoding/json"
"fmt"
)
type ReadOnlyStatus struct {
Frozen bool `json:"frozen"`
SystemInitiated bool `json:"systemInitiated"`
Reason string `json:"summaryReason"`
}
func (p *Plugin) GetNexusStatus(ctx context.Context) error {
res, err := p.NexusRequest(ctx, "v1/status/writable")
if err != nil {
return err
}
if res.StatusCode != 200 {
return fmt.Errorf("Nexus is not writable now (HTTP status %d)", res.StatusCode)
}
res, err = p.NexusRequest(ctx, "v1/read-only")
if err != nil {
return err
}
defer res.Body.Close()
var roStatus ReadOnlyStatus
dec := json.NewDecoder(res.Body)
err = dec.Decode(&roStatus)
if err != nil {
return err
}
if roStatus.Frozen {
if roStatus.Reason == "" {
return fmt.Errorf("Nexus is read-only (system-initiated: %v)", roStatus.SystemInitiated)
}
return fmt.Errorf("Nexus is read-only (system-initiated: %v), reason: %q", roStatus.SystemInitiated, roStatus.Reason)
}
//TODO: determine early whether supplied credentials allows one to proceed with Sonatype Nexus
return nil
}

88
settings.go Normal file
View File

@ -0,0 +1,88 @@
// SPDX-License-Identifier: Apache-2.0
// (c) 2024, Konstantin Demin
package main
import (
"encoding/base64"
"errors"
"net/url"
"os"
"strings"
"github.com/rs/zerolog/log"
)
func (p *Plugin) parseSettings() error {
var err error
p.Settings.RootUrl = strings.TrimSpace(p.Settings.RootUrl)
if p.Settings.RootUrl == "" {
return errors.New("empty nexus.url")
}
restUrl := strings.TrimSuffix(p.Settings.RootUrl, "/") + "/service/rest/"
_, err = url.Parse(restUrl)
if err != nil {
return err
}
p.RestUrl = restUrl
p.Settings.AuthBase64 = strings.TrimSpace(p.Settings.AuthBase64)
if (p.Settings.AuthPlain == "") && (p.Settings.AuthBase64 == "") {
return errors.New("empty nexus.auth.plain/nexus.auth.base64")
}
if p.Settings.AuthBase64 == "" {
if !strings.Contains(p.Settings.AuthPlain, ":") {
return errors.New("nexus.auth.plain does not contain ':'")
}
p.Settings.AuthBase64 = base64.StdEncoding.EncodeToString([]byte(p.Settings.AuthPlain))
}
// paranoia area
os.Unsetenv("PLUGIN_NEXUS_AUTH_PLAIN")
os.Unsetenv("PLUGIN_AUTH_PLAIN")
os.Unsetenv("PLUGIN_NEXUS_AUTH_BASE64")
os.Unsetenv("PLUGIN_AUTH_BASE64")
p.Settings.AuthPlain = ""
err = p.processRawUploads()
if err != nil {
return err
}
if len(p.Uploads) != 0 {
return nil
}
// semi-interactive mode
var ur UploadRule
ur.Repository = p.Settings.Repository
ur.Paths = p.Settings.Paths.Value()
if ur.Repository == "" {
return errors.New("empty nexus.repository")
}
if len(ur.Paths) == 0 {
return errors.New("empty nexus.paths")
}
ur.Properties = make(map[string]any)
for i, s := range p.Settings.Properties.Value() {
if !strings.Contains(s, "=") {
log.Warn().Msgf("cli.property[%d]: value does not contain '='", i)
continue
}
parts := strings.SplitN(s, "=", 2)
_, seen := ur.Properties[parts[0]]
if seen {
log.Warn().Msgf("cli.property[%d]: overriding previous value of %q", i, parts[0])
}
ur.Properties[parts[0]] = parts[1]
}
p.Uploads = append(p.Uploads, ur)
return nil
}

162
upload-spec.go Normal file
View File

@ -0,0 +1,162 @@
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
)
type UploadSpec struct {
Format string `json:"format"`
MultipleUpload bool `json:"multipleUpload"`
ComponentFields []UploadField `json:"componentFields,omitempty"`
AssetFields []UploadField `json:"assetFields,omitempty"`
AllFieldNames map[string]bool
}
type UploadField struct {
Name string `json:"name"`
Type UploadFieldType `json:"type,string"`
Optional bool `json:"optional"`
// optional fields
// Group string `json:"group,omitempty"`
// Description string `json:"description,omitempty"`
}
type UploadFieldType uint8
const (
Unspecified UploadFieldType = iota
Invalid
File
String
Boolean
)
func (p *Plugin) getUploadSpecs(ctx context.Context) error {
var rawspecs []UploadSpec
res, err := p.NexusRequest(ctx, "v1/formats/upload-specs")
if err != nil {
return err
}
defer res.Body.Close()
err = GenericResponseHandler(res)
if err != nil {
return err
}
dec := json.NewDecoder(res.Body)
err = dec.Decode(&rawspecs)
if err != nil {
return err
}
if len(rawspecs) == 0 {
return errors.New("no upload specs were acquired from Sonatype Nexus")
}
p.UploadSpecs = make(map[string]UploadSpec)
for _, s := range rawspecs {
s.AllFieldNames = make(map[string]bool)
var seen bool
for _, f := range s.ComponentFields {
_, seen = s.AllFieldNames[f.Name]
if seen {
continue
}
s.AllFieldNames[f.Name] = true
}
for _, f := range s.AssetFields {
_, seen = s.AllFieldNames[f.Name]
if seen {
continue
}
s.AllFieldNames[f.Name] = true
}
p.UploadSpecs[s.Format] = s
}
return nil
}
var (
UploadFieldType_to_str map[UploadFieldType]string = map[UploadFieldType]string{
File: "file",
String: "string",
Boolean: "boolean",
}
UploadFieldType_to_reflect map[UploadFieldType]reflect.Kind = map[UploadFieldType]reflect.Kind{
File: reflect.String,
String: reflect.String,
Boolean: reflect.Bool,
}
UploadFieldType_from_str map[string]UploadFieldType = map[string]UploadFieldType{
"file": File,
"string": String,
"boolean": Boolean,
}
)
func (x UploadFieldType) IsUnspecified() bool {
return x == Unspecified
}
func (x UploadFieldType) IsValid() bool {
switch x {
case File, String, Boolean:
return true
}
return false
}
func (x UploadFieldType) String() string {
s, ok := UploadFieldType_to_str[x]
if ok {
return s
}
return "UNSPECIFIED"
}
func (x UploadFieldType) ToReflectKind() reflect.Kind {
t, ok := UploadFieldType_to_reflect[x]
if ok {
return t
}
return reflect.Invalid
}
func StringToUploadFieldType(s string) UploadFieldType {
// if s == "" {
// return Unspecified
// }
x, ok := UploadFieldType_from_str[strings.ToLower(s)]
if ok {
return x
}
return Invalid
}
func (x *UploadFieldType) UnmarshalJSON(b []byte) error {
s := string(b)
t := StringToUploadFieldType(s)
if !t.IsUnspecified() {
if t.IsValid() {
*x = t
return nil
}
return fmt.Errorf("incorrect type: %q", s)
}
return nil
}

118
upload.go Normal file
View File

@ -0,0 +1,118 @@
// SPDX-License-Identifier: Apache-2.0
// (c) 2024, Konstantin Demin
package main
import (
"encoding/json"
"fmt"
"path/filepath"
"reflect"
"strings"
)
type UploadRuleBase struct {
Repository string `json:"repository"`
Paths []string `json:"paths"`
}
type UploadRule struct {
UploadRuleBase
Properties map[string]any
}
func (p *Plugin) processRawUploads() error {
var err error
var result []UploadRule
b := []byte(p.Settings.RawUploads)
var base []UploadRuleBase
err = json.Unmarshal(b, &base)
if err != nil {
return err
}
if len(base) == 0 {
return nil
}
var raw []any
err = json.Unmarshal(b, &raw)
if err != nil {
return err
}
if len(raw) == 0 {
return nil
}
if len(raw) != len(base) {
// just in case
return fmt.Errorf("upload[] deserialization error: array length mismatch: %d != %d", len(base), len(raw))
}
result = make([]UploadRule, 0, len(raw))
for i := range raw {
if base[i].Repository == "" {
return fmt.Errorf("upload[%d]: empty \"repository\"", i)
}
if len(base[i].Paths) == 0 {
return fmt.Errorf("upload[%d]: empty \"paths\"", i)
}
for k, patt := range base[i].Paths {
_, err = filepath.Glob(patt)
if err != nil {
return fmt.Errorf("upload[%d].paths[%d]: bad pattern %q: %v", i, k, patt, err)
}
}
rtype := reflect.TypeOf(raw[i])
if rtype.Kind() != reflect.Map {
return fmt.Errorf("upload[%d]: not a map[string]any", i)
}
if rtype.Key().Kind() != reflect.String {
return fmt.Errorf("upload[%d]: not a map[string]any", i)
}
m := raw[i].(map[string]any)
ur := UploadRule{}
ur.Repository = base[i].Repository
ur.Paths = make([]string, len(base[i].Paths))
copy(ur.Paths, base[i].Paths)
for k := range m {
switch strings.ToLower(k) {
case "repository", "paths":
// already handled by UploadRuleBase
continue
case "asset", "filename":
// generated on per-artifact basis
continue
}
rtype = reflect.TypeOf(m[k])
switch rtype.Kind() {
case reflect.Array,
reflect.Chan,
reflect.Func,
reflect.Interface,
reflect.Map,
reflect.Pointer,
reflect.Slice,
reflect.Struct,
reflect.UnsafePointer:
//
return fmt.Errorf("upload[%d]: %q is type of %q", i, k, rtype.String())
}
ur.Properties[k] = m[k]
}
result = append(result, ur)
}
p.Uploads = result
return nil
}