From 9cfb4b925b208ba8a8e1213a5a2ed4882d86081b Mon Sep 17 00:00:00 2001 From: Don Date: Wed, 16 Jun 2021 13:40:07 -0700 Subject: [PATCH] Use plugin library Migrates this plugin to the drone-plugin-lib. Follows the latest boilr template. --- .drone.jsonnet | 14 -- .drone.star | 342 ++++++++++++++++++++++++++++++ .drone.windows.jsonnet | 9 - .drone.windows.yml | 273 ------------------------ .drone.yml | 332 ----------------------------- .github/settings.yml | 8 +- .gitignore | 31 +-- cmd/drone-gitea-release/config.go | 69 ++++++ cmd/drone-gitea-release/main.go | 70 ++++++ docker/Dockerfile.linux.amd64 | 2 +- docker/Dockerfile.windows.1809 | 2 +- docker/Dockerfile.windows.1903 | 10 + docker/Dockerfile.windows.1909 | 10 + docker/Dockerfile.windows.2004 | 10 + docker/manifest.tmpl | 29 ++- go.mod | 12 +- go.sum | 43 ++-- main.go | 139 ------------ pipeline.libsonnet | 205 ------------------ plugin.go | 172 --------------- plugin/impl.go | 132 ++++++++++++ plugin/impl_test.go | 18 ++ plugin/plugin.go | 26 +++ plugin/plugin_test.go | 14 ++ release.go => plugin/release.go | 35 +-- utils.go => plugin/utils.go | 33 +-- renovate.json | 26 +++ 27 files changed, 827 insertions(+), 1239 deletions(-) delete mode 100644 .drone.jsonnet create mode 100644 .drone.star delete mode 100644 .drone.windows.jsonnet delete mode 100644 .drone.windows.yml delete mode 100644 .drone.yml create mode 100644 cmd/drone-gitea-release/config.go create mode 100644 cmd/drone-gitea-release/main.go create mode 100644 docker/Dockerfile.windows.1903 create mode 100644 docker/Dockerfile.windows.1909 create mode 100644 docker/Dockerfile.windows.2004 delete mode 100644 main.go delete mode 100644 pipeline.libsonnet delete mode 100644 plugin.go create mode 100644 plugin/impl.go create mode 100644 plugin/impl_test.go create mode 100644 plugin/plugin.go create mode 100644 plugin/plugin_test.go rename release.go => plugin/release.go (68%) rename utils.go => plugin/utils.go (69%) create mode 100644 renovate.json diff --git a/.drone.jsonnet b/.drone.jsonnet deleted file mode 100644 index b2cbfe4..0000000 --- a/.drone.jsonnet +++ /dev/null @@ -1,14 +0,0 @@ -local pipeline = import 'pipeline.libsonnet'; -local name = 'drone-gitea-release'; - -[ - pipeline.test('linux', 'amd64'), - pipeline.build(name, 'linux', 'amd64'), - pipeline.build(name, 'linux', 'arm64'), - pipeline.build(name, 'linux', 'arm'), - pipeline.notifications(depends_on=[ - 'linux-amd64', - 'linux-arm64', - 'linux-arm', - ]), -] diff --git a/.drone.star b/.drone.star new file mode 100644 index 0000000..d8f5723 --- /dev/null +++ b/.drone.star @@ -0,0 +1,342 @@ +def main(ctx): + before = testing(ctx) + + stages = [ + linux(ctx, "amd64"), + linux(ctx, "arm64"), + linux(ctx, "arm"), + windows(ctx, "1909"), + windows(ctx, "1903"), + windows(ctx, "1809"), + ] + + after = manifest(ctx) + gitter(ctx) + + for b in before: + for s in stages: + s["depends_on"].append(b["name"]) + + for s in stages: + for a in after: + a["depends_on"].append(s["name"]) + + return before + stages + after + +def testing(ctx): + return [{ + "kind": "pipeline", + "type": "docker", + "name": "testing", + "platform": { + "os": "linux", + "arch": "amd64", + }, + "steps": [ + { + "name": "staticcheck", + "image": "golang:1.15", + "pull": "always", + "commands": [ + "go run honnef.co/go/tools/cmd/staticcheck ./...", + ], + "volumes": [ + { + "name": "gopath", + "path": "/go", + }, + ], + }, + { + "name": "lint", + "image": "golang:1.15", + "commands": [ + "go run golang.org/x/lint/golint -set_exit_status ./...", + ], + "volumes": [ + { + "name": "gopath", + "path": "/go", + }, + ], + }, + { + "name": "vet", + "image": "golang:1.15", + "commands": [ + "go vet ./...", + ], + "volumes": [ + { + "name": "gopath", + "path": "/go", + }, + ], + }, + { + "name": "test", + "image": "golang:1.15", + "commands": [ + "go test -cover ./...", + ], + "volumes": [ + { + "name": "gopath", + "path": "/go", + }, + ], + }, + ], + "volumes": [ + { + "name": "gopath", + "temp": {}, + }, + ], + "trigger": { + "ref": [ + "refs/heads/master", + "refs/tags/**", + "refs/pull/**", + ], + }, + }] + +def linux(ctx, arch): + if ctx.build.event == "tag": + build = [ + 'go build -v -ldflags "-X main.version=%s" -a -tags netgo -o release/linux/%s/drone-gitea-release ./cmd/drone-gitea-release' % (ctx.build.ref.replace("refs/tags/v", ""), arch), + ] + else: + build = [ + 'go build -v -ldflags "-X main.version=%s" -a -tags netgo -o release/linux/%s/drone-gitea-release ./cmd/drone-gitea-release' % (ctx.build.commit[0:8], arch), + ] + + steps = [ + { + "name": "environment", + "image": "golang:1.15", + "pull": "always", + "environment": { + "CGO_ENABLED": "0", + }, + "commands": [ + "go version", + "go env", + ], + }, + { + "name": "build", + "image": "golang:1.15", + "environment": { + "CGO_ENABLED": "0", + }, + "commands": build, + }, + { + "name": "executable", + "image": "golang:1.15", + "commands": [ + "./release/linux/%s/drone-gitea-release --help" % (arch), + ], + }, + ] + + if ctx.build.event != "pull_request": + steps.append({ + "name": "docker", + "image": "plugins/docker", + "settings": { + "dockerfile": "docker/Dockerfile.linux.%s" % (arch), + "repo": "plugins/gitea-release", + "username": { + "from_secret": "docker_username", + }, + "password": { + "from_secret": "docker_password", + }, + "auto_tag": True, + "auto_tag_suffix": "linux-%s" % (arch), + }, + }) + + return { + "kind": "pipeline", + "type": "docker", + "name": "linux-%s" % (arch), + "platform": { + "os": "linux", + "arch": arch, + }, + "steps": steps, + "depends_on": [], + "trigger": { + "ref": [ + "refs/heads/master", + "refs/tags/**", + "refs/pull/**", + ], + }, + } + +def windows(ctx, version): + docker = [ + "echo $env:PASSWORD | docker login --username $env:USERNAME --password-stdin", + ] + + if ctx.build.event == "tag": + build = [ + 'go build -v -ldflags "-X main.version=%s" -a -tags netgo -o release/windows/amd64/drone-gitea-release.exe ./cmd/drone-gitea-release' % (ctx.build.ref.replace("refs/tags/v", "")), + ] + + docker = docker + [ + "docker build --pull -f docker/Dockerfile.windows.%s -t plugins/gitea-release:%s-windows-%s-amd64 ." % (version, ctx.build.ref.replace("refs/tags/v", ""), version), + "docker run --rm plugins/gitea-release:%s-windows-%s-amd64 --help" % (ctx.build.ref.replace("refs/tags/v", ""), version), + "docker push plugins/gitea-release:%s-windows-%s-amd64" % (ctx.build.ref.replace("refs/tags/v", ""), version), + ] + else: + build = [ + 'go build -v -ldflags "-X main.version=%s" -a -tags netgo -o release/windows/amd64/drone-gitea-release.exe ./cmd/drone-gitea-release' % (ctx.build.commit[0:8]), + ] + + docker = docker + [ + "docker build --pull -f docker/Dockerfile.windows.%s -t plugins/gitea-release:windows-%s-amd64 ." % (version, version), + "docker run --rm plugins/gitea-release:windows-%s-amd64 --help" % (version), + "docker push plugins/gitea-release:windows-%s-amd64" % (version), + ] + + return { + "kind": "pipeline", + "type": "ssh", + "name": "windows-%s" % (version), + "platform": { + "os": "windows", + }, + "server": { + "host": { + "from_secret": "windows_server_%s" % (version), + }, + "user": { + "from_secret": "windows_username", + }, + "password": { + "from_secret": "windows_password", + }, + }, + "steps": [ + { + "name": "environment", + "environment": { + "CGO_ENABLED": "0", + }, + "commands": [ + "go version", + "go env", + ], + }, + { + "name": "build", + "environment": { + "CGO_ENABLED": "0", + }, + "commands": build, + }, + { + "name": "executable", + "commands": [ + "./release/windows/amd64/drone-gitea-release.exe --help", + ], + }, + { + "name": "docker", + "environment": { + "USERNAME": { + "from_secret": "docker_username", + }, + "PASSWORD": { + "from_secret": "docker_password", + }, + }, + "commands": docker, + }, + ], + "depends_on": [], + "trigger": { + "ref": [ + "refs/heads/master", + "refs/tags/**", + ], + }, + } + +def manifest(ctx): + return [{ + "kind": "pipeline", + "type": "docker", + "name": "manifest", + "steps": [ + { + "name": "manifest", + "image": "plugins/manifest", + "settings": { + "auto_tag": "true", + "username": { + "from_secret": "docker_username", + }, + "password": { + "from_secret": "docker_password", + }, + "spec": "docker/manifest.tmpl", + "ignore_missing": "true", + }, + }, + { + "name": "microbadger", + "image": "plugins/webhook", + "settings": { + "urls": { + "from_secret": "microbadger_url", + }, + }, + }, + ], + "depends_on": [], + "trigger": { + "ref": [ + "refs/heads/master", + "refs/tags/**", + ], + }, + }] + +def gitter(ctx): + return [{ + "kind": "pipeline", + "type": "docker", + "name": "gitter", + "clone": { + "disable": True, + }, + "steps": [ + { + "name": "gitter", + "image": "plugins/gitter", + "settings": { + "webhook": { + "from_secret": "gitter_webhook", + }, + }, + }, + ], + "depends_on": [ + "manifest", + ], + "trigger": { + "ref": [ + "refs/heads/master", + "refs/tags/**", + ], + "status": [ + "failure", + ], + }, + }] diff --git a/.drone.windows.jsonnet b/.drone.windows.jsonnet deleted file mode 100644 index 7e2bdfe..0000000 --- a/.drone.windows.jsonnet +++ /dev/null @@ -1,9 +0,0 @@ -local pipeline = import 'pipeline.libsonnet'; -local name = 'drone-gitea-release'; - -[ - pipeline.test('windows', 'amd64', '1803'), - pipeline.build(name, 'windows', 'amd64', '1803'), - pipeline.build(name, 'windows', 'amd64', '1809'), - pipeline.notifications('windows', 'amd64', '1809', ['windows-1803', 'windows-1809']), -] diff --git a/.drone.windows.yml b/.drone.windows.yml deleted file mode 100644 index 1443eb2..0000000 --- a/.drone.windows.yml +++ /dev/null @@ -1,273 +0,0 @@ ---- -kind: pipeline -name: testing - -platform: - os: windows - arch: amd64 - version: 1803 - -steps: -- name: vet - pull: always - image: golang:1.13-windowsservercore-1803 - commands: - - go vet ./... - environment: - GO111MODULE: on - volumes: - - name: gopath - path: C:\\gopath - -- name: test - pull: always - image: golang:1.13-windowsservercore-1803 - commands: - - go test -cover ./... - environment: - GO111MODULE: on - volumes: - - name: gopath - path: C:\\gopath - -volumes: -- name: gopath - temp: {} - -trigger: - ref: - - refs/heads/master - - "refs/tags/**" - - "refs/pull/**" - ---- -kind: pipeline -name: windows-1803 - -platform: - os: windows - arch: amd64 - version: 1803 - -steps: -- name: build-push - pull: always - image: golang:1.13-windowsservercore-1803 - commands: - - "go build -v -ldflags \"-X main.version=${DRONE_COMMIT_SHA:0:8}\" -a -tags netgo -o release/windows/amd64/drone-gitea-release.exe" - environment: - CGO_ENABLED: 0 - GO111MODULE: on - when: - event: - exclude: - - tag - -- name: build-tag - pull: always - image: golang:1.13-windowsservercore-1803 - commands: - - "go build -v -ldflags \"-X main.version=${DRONE_TAG##v}\" -a -tags netgo -o release/windows/amd64/drone-gitea-release.exe" - environment: - CGO_ENABLED: 0 - GO111MODULE: on - when: - event: - - tag - -- name: executable - pull: always - image: golang:1.13-windowsservercore-1803 - commands: - - ./release/windows/amd64/drone-gitea-release.exe --help - -- name: dryrun - pull: always - image: plugins/docker:windows-1803 - settings: - daemon_off: true - dockerfile: docker/Dockerfile.windows.1803 - dry_run: true - password: - from_secret: docker_password - repo: plugins/gitea-release - tags: windows-1803 - username: - from_secret: docker_username - volumes: - - name: docker_pipe - path: \\\\.\\pipe\\docker_engine - when: - event: - - pull_request - -- name: publish - pull: always - image: plugins/docker:windows-1803 - settings: - auto_tag: true - auto_tag_suffix: windows-1803 - daemon_off: true - dockerfile: docker/Dockerfile.windows.1803 - password: - from_secret: docker_password - repo: plugins/gitea-release - username: - from_secret: docker_username - volumes: - - name: docker_pipe - path: \\\\.\\pipe\\docker_engine - when: - event: - exclude: - - pull_request - -volumes: -- name: docker_pipe - host: - path: \\\\.\\pipe\\docker_engine - -trigger: - ref: - - refs/heads/master - - "refs/tags/**" - - "refs/pull/**" - -depends_on: -- testing - ---- -kind: pipeline -name: windows-1809 - -platform: - os: windows - arch: amd64 - version: 1809 - -steps: -- name: build-push - pull: always - image: golang:1.13-windowsservercore-1809 - commands: - - "go build -v -ldflags \"-X main.version=${DRONE_COMMIT_SHA:0:8}\" -a -tags netgo -o release/windows/amd64/drone-gitea-release.exe" - environment: - CGO_ENABLED: 0 - GO111MODULE: on - when: - event: - exclude: - - tag - -- name: build-tag - pull: always - image: golang:1.13-windowsservercore-1809 - commands: - - "go build -v -ldflags \"-X main.version=${DRONE_TAG##v}\" -a -tags netgo -o release/windows/amd64/drone-gitea-release.exe" - environment: - CGO_ENABLED: 0 - GO111MODULE: on - when: - event: - - tag - -- name: executable - pull: always - image: golang:1.13-windowsservercore-1809 - commands: - - ./release/windows/amd64/drone-gitea-release.exe --help - -- name: dryrun - pull: always - image: plugins/docker:windows-1809 - settings: - daemon_off: true - dockerfile: docker/Dockerfile.windows.1809 - dry_run: true - password: - from_secret: docker_password - repo: plugins/gitea-release - tags: windows-1809 - username: - from_secret: docker_username - volumes: - - name: docker_pipe - path: \\\\.\\pipe\\docker_engine - when: - event: - - pull_request - -- name: publish - pull: always - image: plugins/docker:windows-1809 - settings: - auto_tag: true - auto_tag_suffix: windows-1809 - daemon_off: true - dockerfile: docker/Dockerfile.windows.1809 - password: - from_secret: docker_password - repo: plugins/gitea-release - username: - from_secret: docker_username - volumes: - - name: docker_pipe - path: \\\\.\\pipe\\docker_engine - when: - event: - exclude: - - pull_request - -volumes: -- name: docker_pipe - host: - path: \\\\.\\pipe\\docker_engine - -trigger: - ref: - - refs/heads/master - - "refs/tags/**" - - "refs/pull/**" - -depends_on: -- testing - ---- -kind: pipeline -name: notifications - -platform: - os: windows - arch: amd64 - version: 1809 - -steps: -- name: manifest - pull: always - image: plugins/manifest - settings: - auto_tag: true - ignore_missing: true - password: - from_secret: docker_password - spec: docker/manifest.tmpl - username: - from_secret: docker_username - -- name: microbadger - pull: always - image: plugins/webhook - settings: - urls: - from_secret: microbadger_url - -trigger: - ref: - - refs/heads/master - - "refs/tags/**" - -depends_on: -- windows-1803 -- windows-1809 - -... diff --git a/.drone.yml b/.drone.yml deleted file mode 100644 index f23a5db..0000000 --- a/.drone.yml +++ /dev/null @@ -1,332 +0,0 @@ ---- -kind: pipeline -name: testing - -platform: - os: linux - arch: amd64 - -steps: -- name: vet - pull: always - image: golang:1.13 - commands: - - go vet ./... - environment: - GO111MODULE: on - volumes: - - name: gopath - path: /go - -- name: test - pull: always - image: golang:1.13 - commands: - - go test -cover ./... - environment: - GO111MODULE: on - volumes: - - name: gopath - path: /go - -volumes: -- name: gopath - temp: {} - -trigger: - ref: - - refs/heads/master - - "refs/tags/**" - - "refs/pull/**" - ---- -kind: pipeline -name: linux-amd64 - -platform: - os: linux - arch: amd64 - -steps: -- name: build-push - pull: always - image: golang:1.13 - commands: - - "go build -v -ldflags \"-X main.version=${DRONE_COMMIT_SHA:0:8}\" -a -tags netgo -o release/linux/amd64/drone-gitea-release" - environment: - CGO_ENABLED: 0 - GO111MODULE: on - when: - event: - exclude: - - tag - -- name: build-tag - pull: always - image: golang:1.13 - commands: - - "go build -v -ldflags \"-X main.version=${DRONE_TAG##v}\" -a -tags netgo -o release/linux/amd64/drone-gitea-release" - environment: - CGO_ENABLED: 0 - GO111MODULE: on - when: - event: - - tag - -- name: executable - pull: always - image: golang:1.13 - commands: - - ./release/linux/amd64/drone-gitea-release --help - -- name: dryrun - pull: always - image: plugins/docker:linux-amd64 - settings: - daemon_off: false - dockerfile: docker/Dockerfile.linux.amd64 - dry_run: true - password: - from_secret: docker_password - repo: plugins/gitea-release - tags: linux-amd64 - username: - from_secret: docker_username - when: - event: - - pull_request - -- name: publish - pull: always - image: plugins/docker:linux-amd64 - settings: - auto_tag: true - auto_tag_suffix: linux-amd64 - daemon_off: false - dockerfile: docker/Dockerfile.linux.amd64 - password: - from_secret: docker_password - repo: plugins/gitea-release - username: - from_secret: docker_username - when: - event: - exclude: - - pull_request - -trigger: - ref: - - refs/heads/master - - "refs/tags/**" - - "refs/pull/**" - -depends_on: -- testing - ---- -kind: pipeline -name: linux-arm64 - -platform: - os: linux - arch: arm64 - -steps: -- name: build-push - pull: always - image: golang:1.13 - commands: - - "go build -v -ldflags \"-X main.version=${DRONE_COMMIT_SHA:0:8}\" -a -tags netgo -o release/linux/arm64/drone-gitea-release" - environment: - CGO_ENABLED: 0 - GO111MODULE: on - when: - event: - exclude: - - tag - -- name: build-tag - pull: always - image: golang:1.13 - commands: - - "go build -v -ldflags \"-X main.version=${DRONE_TAG##v}\" -a -tags netgo -o release/linux/arm64/drone-gitea-release" - environment: - CGO_ENABLED: 0 - GO111MODULE: on - when: - event: - - tag - -- name: executable - pull: always - image: golang:1.13 - commands: - - ./release/linux/arm64/drone-gitea-release --help - -- name: dryrun - pull: always - image: plugins/docker:linux-arm64 - settings: - daemon_off: false - dockerfile: docker/Dockerfile.linux.arm64 - dry_run: true - password: - from_secret: docker_password - repo: plugins/gitea-release - tags: linux-arm64 - username: - from_secret: docker_username - when: - event: - - pull_request - -- name: publish - pull: always - image: plugins/docker:linux-arm64 - settings: - auto_tag: true - auto_tag_suffix: linux-arm64 - daemon_off: false - dockerfile: docker/Dockerfile.linux.arm64 - password: - from_secret: docker_password - repo: plugins/gitea-release - username: - from_secret: docker_username - when: - event: - exclude: - - pull_request - -trigger: - ref: - - refs/heads/master - - "refs/tags/**" - - "refs/pull/**" - -depends_on: -- testing - ---- -kind: pipeline -name: linux-arm - -platform: - os: linux - arch: arm - -steps: -- name: build-push - pull: always - image: golang:1.13 - commands: - - "go build -v -ldflags \"-X main.version=${DRONE_COMMIT_SHA:0:8}\" -a -tags netgo -o release/linux/arm/drone-gitea-release" - environment: - CGO_ENABLED: 0 - GO111MODULE: on - when: - event: - exclude: - - tag - -- name: build-tag - pull: always - image: golang:1.13 - commands: - - "go build -v -ldflags \"-X main.version=${DRONE_TAG##v}\" -a -tags netgo -o release/linux/arm/drone-gitea-release" - environment: - CGO_ENABLED: 0 - GO111MODULE: on - when: - event: - - tag - -- name: executable - pull: always - image: golang:1.13 - commands: - - ./release/linux/arm/drone-gitea-release --help - -- name: dryrun - pull: always - image: plugins/docker:linux-arm - settings: - daemon_off: false - dockerfile: docker/Dockerfile.linux.arm - dry_run: true - password: - from_secret: docker_password - repo: plugins/gitea-release - tags: linux-arm - username: - from_secret: docker_username - when: - event: - - pull_request - -- name: publish - pull: always - image: plugins/docker:linux-arm - settings: - auto_tag: true - auto_tag_suffix: linux-arm - daemon_off: false - dockerfile: docker/Dockerfile.linux.arm - password: - from_secret: docker_password - repo: plugins/gitea-release - username: - from_secret: docker_username - when: - event: - exclude: - - pull_request - -trigger: - ref: - - refs/heads/master - - "refs/tags/**" - - "refs/pull/**" - -depends_on: -- testing - ---- -kind: pipeline -name: notifications - -platform: - os: linux - arch: amd64 - -steps: -- name: manifest - pull: always - image: plugins/manifest - settings: - auto_tag: true - ignore_missing: true - password: - from_secret: docker_password - spec: docker/manifest.tmpl - username: - from_secret: docker_username - -- name: microbadger - pull: always - image: plugins/webhook - settings: - urls: - from_secret: microbadger_url - -trigger: - ref: - - refs/heads/master - - "refs/tags/**" - -depends_on: -- linux-amd64 -- linux-arm64 -- linux-arm - -... diff --git a/.github/settings.yml b/.github/settings.yml index d01b75f..db517fd 100644 --- a/.github/settings.yml +++ b/.github/settings.yml @@ -1,6 +1,6 @@ repository: name: drone-gitea-release - description: Drone plugin for creating and tagging Gitea releases + description: Drone plugin for creating and tagging GitHub releases homepage: http://plugins.drone.io/drone-plugins/drone-gitea-release topics: drone, drone-plugin @@ -69,5 +69,9 @@ branches: - continuous-integration/drone/pr enforce_admins: false restrictions: + apps: + - renovate users: [] - teams: [] + teams: + - Admins + - Maintainers diff --git a/.gitignore b/.gitignore index aaa10cf..cac2aa4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,30 +1,5 @@ -# Compiled Object files, Static and Dynamic libs (Shared Objects) -*.o -*.a -*.so - -# Folders -_obj -_test - -# Architecture specific extensions/prefixes -*.[568vq] -[568vq].out - -*.cgo1.go -*.cgo2.c -_cgo_defun.c -_cgo_gotypes.go -_cgo_export.* - -_testmain.go - -*.exe -*.test -*.prof - -release/ -vendor/ +/release/ +/drone-gitea-release* coverage.out -drone-gitea-release +.drone.yml diff --git a/cmd/drone-gitea-release/config.go b/cmd/drone-gitea-release/config.go new file mode 100644 index 0000000..d52853d --- /dev/null +++ b/cmd/drone-gitea-release/config.go @@ -0,0 +1,69 @@ +// Copyright (c) 2021, the Drone Plugins project authors. +// Please see the AUTHORS file for details. All rights reserved. +// Use of this source code is governed by an Apache 2.0 license that can be +// found in the LICENSE file. + +package main + +import ( + "github.com/drone-plugins/drone-gitea-release/plugin" + "github.com/urfave/cli/v2" +) + +// settingsFlags has the cli.Flags for the plugin.Settings. +func settingsFlags(settings *plugin.Settings) []cli.Flag { + return []cli.Flag{ + + &cli.StringFlag{ + Name: "api-key", + Usage: "api key to access gitea api", + EnvVars: []string{"PLUGIN_API_KEY", "GITEA_RELEASE_API_KEY", "GITEA_TOKEN"}, + Destination: &settings.APIKey, + }, + &cli.StringSliceFlag{ + Name: "files", + Usage: "list of files to upload", + EnvVars: []string{"PLUGIN_FILES", "GITEA_RELEASE_FILES"}, + Destination: &settings.Files, + }, + &cli.StringFlag{ + Name: "file-exists", + Value: "overwrite", + Usage: "what to do if file already exist", + EnvVars: []string{"PLUGIN_FILE_EXISTS", "GITEA_RELEASE_FILE_EXISTS"}, + Destination: &settings.FileExists, + }, + &cli.StringSliceFlag{ + Name: "checksum", + Usage: "generate specific checksums", + EnvVars: []string{"PLUGIN_CHECKSUM", "GITEA_RELEASE_CHECKSUM"}, + Destination: &settings.Checksum}, + &cli.BoolFlag{ + Name: "draft", + Usage: "create a draft release", + EnvVars: []string{"PLUGIN_DRAFT", "GITEA_RELEASE_DRAFT"}, + Destination: &settings.Draft}, + &cli.BoolFlag{ + Name: "prerelease", + Usage: "set the release as prerelease", + EnvVars: []string{"PLUGIN_PRERELEASE", "GITEA_RELEASE_PRERELEASE"}, + Destination: &settings.PreRelease}, + &cli.StringFlag{ + Name: "base-url", + Usage: "url of the gitea instance", + EnvVars: []string{"PLUGIN_BASE_URL", "GITEA_RELEASE_BASE_URL"}, + Destination: &settings.BaseURL}, + &cli.StringFlag{ + Name: "title", + Value: "", + Usage: "file or string for the title shown in the gitea release", + EnvVars: []string{"PLUGIN_TITLE", "GITEA_RELEASE_TITLE"}, + Destination: &settings.Title}, + &cli.StringFlag{ + Name: "note", + Value: "", + Usage: "file or string with notes for the release (example: changelog)", + EnvVars: []string{"PLUGIN_NOTE", "GITEA_RELEASE_NOTE"}, + Destination: &settings.Note}, + } +} diff --git a/cmd/drone-gitea-release/main.go b/cmd/drone-gitea-release/main.go new file mode 100644 index 0000000..a368ea2 --- /dev/null +++ b/cmd/drone-gitea-release/main.go @@ -0,0 +1,70 @@ +// Copyright (c) 2021, the Drone Plugins project authors. +// Please see the AUTHORS file for details. All rights reserved. +// Use of this source code is governed by an Apache 2.0 license that can be +// found in the LICENSE file. + +// DO NOT MODIFY THIS FILE DIRECTLY + +package main + +import ( + "os" + + "github.com/drone-plugins/drone-gitea-release/plugin" + "github.com/drone-plugins/drone-plugin-lib/errors" + "github.com/drone-plugins/drone-plugin-lib/urfave" + "github.com/joho/godotenv" + "github.com/urfave/cli/v2" +) + +var version = "unknown" + +func main() { + settings := &plugin.Settings{} + + if _, err := os.Stat("/run/drone/env"); err == nil { + godotenv.Overload("/run/drone/env") + } + + app := &cli.App{ + Name: "drone-gitea-release", + Usage: "creates a gitea release", + Version: version, + Flags: append(settingsFlags(settings), urfave.Flags()...), + Action: run(settings), + } + + if err := app.Run(os.Args); err != nil { + errors.HandleExit(err) + } +} + +func run(settings *plugin.Settings) cli.ActionFunc { + return func(ctx *cli.Context) error { + urfave.LoggingFromContext(ctx) + + plugin := plugin.New( + *settings, + urfave.PipelineFromContext(ctx), + urfave.NetworkFromContext(ctx), + ) + + if err := plugin.Validate(); err != nil { + if e, ok := err.(errors.ExitCoder); ok { + return e + } + + return errors.ExitMessagef("validation failed: %w", err) + } + + if err := plugin.Execute(); err != nil { + if e, ok := err.(errors.ExitCoder); ok { + return e + } + + return errors.ExitMessagef("execution failed: %w", err) + } + + return nil + } +} diff --git a/docker/Dockerfile.linux.amd64 b/docker/Dockerfile.linux.amd64 index decf980..5ef2b63 100644 --- a/docker/Dockerfile.linux.amd64 +++ b/docker/Dockerfile.linux.amd64 @@ -6,4 +6,4 @@ LABEL maintainer="Drone.IO Community " \ org.label-schema.schema-version="1.0" ADD release/linux/amd64/drone-gitea-release /bin/ -ENTRYPOINT ["/bin/drone-gitea-release"] +ENTRYPOINT [ "/bin/drone-gitea-release" ] diff --git a/docker/Dockerfile.windows.1809 b/docker/Dockerfile.windows.1809 index 7afe6fb..130760d 100644 --- a/docker/Dockerfile.windows.1809 +++ b/docker/Dockerfile.windows.1809 @@ -1,5 +1,5 @@ # escape=` -FROM plugins/base:windows-1809 +FROM plugins/base:windows-1809-amd64 LABEL maintainer="Drone.IO Community " ` org.label-schema.name="Drone Gitea Release" ` diff --git a/docker/Dockerfile.windows.1903 b/docker/Dockerfile.windows.1903 new file mode 100644 index 0000000..ba604cb --- /dev/null +++ b/docker/Dockerfile.windows.1903 @@ -0,0 +1,10 @@ +# escape=` +FROM plugins/base:windows-1903-amd64 + +LABEL maintainer="Drone.IO Community " ` + org.label-schema.name="Drone Gitea Release" ` + org.label-schema.vendor="Drone.IO Community" ` + org.label-schema.schema-version="1.0" + +ADD release/windows/amd64/drone-gitea-release.exe C:/bin/drone-gitea-release.exe +ENTRYPOINT [ "C:\\bin\\drone-gitea-release.exe" ] diff --git a/docker/Dockerfile.windows.1909 b/docker/Dockerfile.windows.1909 new file mode 100644 index 0000000..b5badf5 --- /dev/null +++ b/docker/Dockerfile.windows.1909 @@ -0,0 +1,10 @@ +# escape=` +FROM plugins/base:windows-1909-amd64 + +LABEL maintainer="Drone.IO Community " ` + org.label-schema.name="Drone Gitea Release" ` + org.label-schema.vendor="Drone.IO Community" ` + org.label-schema.schema-version="1.0" + +ADD release/windows/amd64/drone-gitea-release.exe C:/bin/drone-gitea-release.exe +ENTRYPOINT [ "C:\\bin\\drone-gitea-release.exe" ] diff --git a/docker/Dockerfile.windows.2004 b/docker/Dockerfile.windows.2004 new file mode 100644 index 0000000..02cab10 --- /dev/null +++ b/docker/Dockerfile.windows.2004 @@ -0,0 +1,10 @@ +# escape=` +FROM plugins/base:windows-2004-amd64 + +LABEL maintainer="Drone.IO Community " ` + org.label-schema.name="Drone Gitea Release" ` + org.label-schema.vendor="Drone.IO Community" ` + org.label-schema.schema-version="1.0" + +ADD release/windows/amd64/drone-gitea-release.exe C:/bin/drone-gitea-release.exe +ENTRYPOINT [ "C:\\bin\\drone-gitea-release.exe" ] diff --git a/docker/manifest.tmpl b/docker/manifest.tmpl index 2ae94ea..19c305d 100644 --- a/docker/manifest.tmpl +++ b/docker/manifest.tmpl @@ -1,36 +1,43 @@ image: plugins/gitea-release:{{#if build.tag}}{{trimPrefix "v" build.tag}}{{else}}latest{{/if}} + {{#if build.tags}} tags: {{#each build.tags}} - {{this}} {{/each}} {{/if}} + manifests: - - - image: plugins/gitea-release:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-amd64 + - image: plugins/gitea-release:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-amd64 platform: architecture: amd64 os: linux - - - image: plugins/gitea-release:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-arm64 + - image: plugins/gitea-release:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-arm64 platform: architecture: arm64 os: linux variant: v8 - - - image: plugins/gitea-release:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-arm + - image: plugins/gitea-release:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}linux-arm platform: architecture: arm os: linux variant: v7 - - - image: plugins/gitea-release:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}windows-1803 + - image: plugins/gitea-release:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}windows-2004-amd64 platform: architecture: amd64 os: windows - version: 1803 - - - image: plugins/gitea-release:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}windows-1809 + version: 2004 + - image: plugins/gitea-release:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}windows-1909-amd64 + platform: + architecture: amd64 + os: windows + version: 1909 + - image: plugins/gitea-release:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}windows-1903-amd64 + platform: + architecture: amd64 + os: windows + version: 1903 + - image: plugins/gitea-release:{{#if build.tag}}{{trimPrefix "v" build.tag}}-{{/if}}windows-1809-amd64 platform: architecture: amd64 os: windows diff --git a/go.mod b/go.mod index cdf8275..609152f 100644 --- a/go.mod +++ b/go.mod @@ -1,14 +1,10 @@ module github.com/drone-plugins/drone-gitea-release -go 1.14 +go 1.15 require ( - code.gitea.io/sdk/gitea v0.13.2 - github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect + code.gitea.io/sdk/gitea v0.14.0 + github.com/drone-plugins/drone-plugin-lib v0.4.0 github.com/joho/godotenv v1.3.0 - github.com/russross/blackfriday/v2 v2.1.0 // indirect - github.com/sirupsen/logrus v1.7.0 - github.com/urfave/cli v1.22.5 - golang.org/x/sys v0.0.0-20201211090839-8ad439b19e0f // indirect - gopkg.in/yaml.v2 v2.3.0 // indirect + github.com/urfave/cli/v2 v2.3.0 ) diff --git a/go.sum b/go.sum index 2448962..5f48941 100644 --- a/go.sum +++ b/go.sum @@ -1,39 +1,48 @@ -code.gitea.io/sdk/gitea v0.13.2 h1:wAnT/J7Z62q3fJXbgnecoaOBh8CM1Qq0/DakWxiv4yA= -code.gitea.io/sdk/gitea v0.13.2/go.mod h1:lee2y8LeV3kQb2iK+hHlMqoadL4bp27QOkOV/hawLKg= +code.gitea.io/sdk/gitea v0.14.0 h1:m4J352I3p9+bmJUfS+g0odeQzBY/5OXP91Gv6D4fnJ0= +code.gitea.io/sdk/gitea v0.14.0/go.mod h1:89WiyOX1KEcvjP66sRHdu0RafojGo60bT9UqW17VbWs= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= -github.com/cpuguy83/go-md2man/v2 v2.0.0 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng4PGlyM= -github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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/drone-plugins/drone-plugin-lib v0.4.0 h1:qywEYGhquUuid6zNLmKia8CWY1TUa8jPQQ/G9ozfAmc= +github.com/drone-plugins/drone-plugin-lib v0.4.0/go.mod h1:EgqogX38GoJFtckeSQyhBJYX8P+KWBPhdprAVvyRxF8= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI= github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= +github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= +github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8= +github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/pkg/errors v0.8.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/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -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/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= -github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM= -github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I= +github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= +github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/urfave/cli v1.22.5 h1:lNq9sAHXK2qfdI8W+GRItjCEkI+2oR4d+MEHy1CKXoU= -github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201211090839-8ad439b19e0f h1:QdHQnPce6K4XQewki9WNbG5KOROuDzqO3NaYjI1cXJ0= -golang.org/x/sys v0.0.0-20201211090839-8ad439b19e0f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= +github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M= +github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= -gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.3 h1:fvjTMHxHEw/mxHbtzPi3JCcKXQRAnQTBRo6YCJSVHKI= +gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk= diff --git a/main.go b/main.go deleted file mode 100644 index 5c50307..0000000 --- a/main.go +++ /dev/null @@ -1,139 +0,0 @@ -package main - -import ( - "os" - - "github.com/sirupsen/logrus" - "github.com/joho/godotenv" - "github.com/urfave/cli" -) - -var ( - version = "unknown" -) - -func main() { - app := cli.NewApp() - app.Name = "gitea-release plugin" - app.Usage = "gitea-release plugin" - app.Action = run - app.Version = version - app.Flags = []cli.Flag{ - cli.StringFlag{ - Name: "api-key", - Usage: "api key to access gitea api", - EnvVar: "PLUGIN_API_KEY,GITEA_RELEASE_API_KEY,GITEA_TOKEN", - }, - cli.StringSliceFlag{ - Name: "files", - Usage: "list of files to upload", - EnvVar: "PLUGIN_FILES,GITEA_RELEASE_FILES", - }, - cli.StringFlag{ - Name: "file-exists", - Value: "overwrite", - Usage: "what to do if file already exist", - EnvVar: "PLUGIN_FILE_EXISTS,GITEA_RELEASE_FILE_EXISTS", - }, - cli.StringSliceFlag{ - Name: "checksum", - Usage: "generate specific checksums", - EnvVar: "PLUGIN_CHECKSUM,GITEA_RELEASE_CHECKSUM", - }, - cli.BoolFlag{ - Name: "draft", - Usage: "create a draft release", - EnvVar: "PLUGIN_DRAFT,GITEA_RELEASE_DRAFT", - }, - cli.BoolFlag{ - Name: "insecure", - Usage: "visit base-url via insecure https protocol", - EnvVar: "PLUGIN_INSECURE,GITEA_RELEASE_INSECURE", - }, - cli.BoolFlag{ - Name: "prerelease", - Usage: "set the release as prerelease", - EnvVar: "PLUGIN_PRERELEASE,GITEA_RELEASE_PRERELEASE", - }, - cli.StringFlag{ - Name: "base-url", - Usage: "url of the gitea instance", - EnvVar: "PLUGIN_BASE_URL,GITEA_RELEASE_BASE_URL", - }, - cli.StringFlag{ - Name: "note", - Value: "", - Usage: "file or string with notes for the release (example: changelog)", - EnvVar: "PLUGIN_NOTE,GITEA_RELEASE_NOTE", - }, - cli.StringFlag{ - Name: "title", - Value: "", - Usage: "file or string for the title shown in the gitea release", - EnvVar: "PLUGIN_TITLE,GITEA_RELEASE_TITLE", - }, - cli.StringFlag{ - Name: "repo.owner", - Usage: "repository owner", - EnvVar: "DRONE_REPO_OWNER", - }, - cli.StringFlag{ - Name: "repo.name", - Usage: "repository name", - EnvVar: "DRONE_REPO_NAME", - }, - cli.StringFlag{ - Name: "build.event", - Value: "push", - Usage: "build event", - EnvVar: "DRONE_BUILD_EVENT", - }, - cli.StringFlag{ - Name: "commit.ref", - Value: "refs/heads/master", - Usage: "git commit ref", - EnvVar: "DRONE_COMMIT_REF", - }, - cli.StringFlag{ - Name: "env-file", - Usage: "source env file", - }, - } - - if err := app.Run(os.Args); err != nil { - logrus.Fatal(err) - } -} - -func run(c *cli.Context) error { - if c.String("env-file") != "" { - _ = godotenv.Load(c.String("env-file")) - } - - plugin := Plugin{ - Repo: Repo{ - Owner: c.String("repo.owner"), - Name: c.String("repo.name"), - }, - Build: Build{ - Event: c.String("build.event"), - }, - Commit: Commit{ - Ref: c.String("commit.ref"), - }, - Config: Config{ - APIKey: c.String("api-key"), - Files: c.StringSlice("files"), - FileExists: c.String("file-exists"), - Checksum: c.StringSlice("checksum"), - Draft: c.Bool("draft"), - PreRelease: c.Bool("prerelease"), - BaseURL: c.String("base-url"), - Insecure: c.Bool("insecure"), - Title: c.String("title"), - Note: c.String("note"), - }, - } - - return plugin.Exec() -} diff --git a/pipeline.libsonnet b/pipeline.libsonnet deleted file mode 100644 index 54a07b2..0000000 --- a/pipeline.libsonnet +++ /dev/null @@ -1,205 +0,0 @@ -local windows_pipe = '\\\\\\\\.\\\\pipe\\\\docker_engine'; -local windows_pipe_volume = 'docker_pipe'; -local test_pipeline_name = 'testing'; - -local windows(os) = os == 'windows'; - -local golang_image(os, version) = - 'golang:' + '1.11' + if windows(os) then '-windowsservercore-' + version else ''; - -{ - test(os='linux', arch='amd64', version=''):: - local is_windows = windows(os); - local golang = golang_image(os, version); - local volumes = if is_windows then [{name: 'gopath', path: 'C:\\\\gopath'}] else [{name: 'gopath', path: '/go',}]; - { - kind: 'pipeline', - name: test_pipeline_name, - platform: { - os: os, - arch: arch, - version: if std.length(version) > 0 then version, - }, - steps: [ - { - name: 'vet', - image: golang, - pull: 'always', - environment: { - GO111MODULE: 'on', - }, - commands: [ - 'go vet ./...', - ], - volumes: volumes, - }, - { - name: 'test', - image: golang, - pull: 'always', - environment: { - GO111MODULE: 'on', - }, - commands: [ - 'go test -cover ./...', - ], - volumes: volumes, - }, - ], - trigger: { - ref: [ - 'refs/heads/master', - 'refs/tags/**', - 'refs/pull/**', - ], - }, - volumes: [{name: 'gopath', temp: {}}] - }, - - build(name, os='linux', arch='amd64', version=''):: - local is_windows = windows(os); - local tag = if is_windows then os + '-' + version else os + '-' + arch; - local file_suffix = std.strReplace(tag, '-', '.'); - local volumes = if is_windows then [{ name: windows_pipe_volume, path: windows_pipe }] else []; - local golang = golang_image(os, version); - local plugin_repo = 'plugins/' + std.splitLimit(name, '-', 1)[1]; - local extension = if is_windows then '.exe' else ''; - { - kind: 'pipeline', - name: tag, - platform: { - os: os, - arch: arch, - version: if std.length(version) > 0 then version, - }, - steps: [ - { - name: 'build-push', - image: golang, - pull: 'always', - environment: { - CGO_ENABLED: '0', - GO111MODULE: 'on', - }, - commands: [ - 'go build -v -ldflags "-X main.version=${DRONE_COMMIT_SHA:0:8}" -a -tags netgo -o release/' + os + '/' + arch + '/' + name + extension, - ], - when: { - event: { - exclude: ['tag'], - }, - }, - }, - { - name: 'build-tag', - image: golang, - pull: 'always', - environment: { - CGO_ENABLED: '0', - GO111MODULE: 'on', - }, - commands: [ - 'go build -v -ldflags "-X main.version=${DRONE_TAG##v}" -a -tags netgo -o release/' + os + '/' + arch + '/' + name + extension, - ], - when: { - event: ['tag'], - }, - }, - { - name: 'executable', - image: golang, - pull: 'always', - commands: [ - './release/' + os + '/' + arch + '/' + name + extension + ' --help', - ], - }, - { - name: 'dryrun', - image: 'plugins/docker:' + tag, - pull: 'always', - settings: { - dry_run: true, - tags: tag, - dockerfile: 'docker/Dockerfile.' + file_suffix, - daemon_off: if is_windows then 'true' else 'false', - repo: plugin_repo, - username: { from_secret: 'docker_username' }, - password: { from_secret: 'docker_password' }, - }, - volumes: if std.length(volumes) > 0 then volumes, - when: { - event: ['pull_request'], - }, - }, - { - name: 'publish', - image: 'plugins/docker:' + tag, - pull: 'always', - settings: { - auto_tag: true, - auto_tag_suffix: tag, - daemon_off: if is_windows then 'true' else 'false', - dockerfile: 'docker/Dockerfile.' + file_suffix, - repo: plugin_repo, - username: { from_secret: 'docker_username' }, - password: { from_secret: 'docker_password' }, - }, - volumes: if std.length(volumes) > 0 then volumes, - when: { - event: { - exclude: ['pull_request'], - }, - }, - }, - ], - trigger: { - ref: [ - 'refs/heads/master', - 'refs/tags/**', - 'refs/pull/**', - ], - }, - depends_on: [test_pipeline_name], - volumes: if is_windows then [{ name: windows_pipe_volume, host: { path: windows_pipe } }], - }, - - notifications(os='linux', arch='amd64', version='', depends_on=[]):: - { - kind: 'pipeline', - name: 'notifications', - platform: { - os: os, - arch: arch, - version: if std.length(version) > 0 then version, - }, - steps: [ - { - name: 'manifest', - image: 'plugins/manifest', - pull: 'always', - settings: { - username: { from_secret: 'docker_username' }, - password: { from_secret: 'docker_password' }, - spec: 'docker/manifest.tmpl', - ignore_missing: true, - auto_tag: true, - }, - }, - { - name: 'microbadger', - image: 'plugins/webhook', - pull: 'always', - settings: { - urls: { from_secret: 'microbadger_url' }, - }, - }, - ], - trigger: { - ref: [ - 'refs/heads/master', - 'refs/tags/**', - ], - }, - depends_on: depends_on, - }, -} diff --git a/plugin.go b/plugin.go deleted file mode 100644 index 205cd26..0000000 --- a/plugin.go +++ /dev/null @@ -1,172 +0,0 @@ -package main - -import ( - "crypto/tls" - "fmt" - "io/ioutil" - "net/http" - "net/http/cookiejar" - "os" - "path/filepath" - "strings" - - "code.gitea.io/sdk/gitea" -) - -type ( - Repo struct { - Owner string - Name string - } - - Build struct { - Event string - } - - Commit struct { - Ref string - } - - Config struct { - APIKey string - Files []string - FileExists string - Checksum []string - Draft bool - PreRelease bool - Insecure bool - BaseURL string - Title string - Note string - } - - Plugin struct { - Repo Repo - Build Build - Commit Commit - Config Config - } -) - -func (p Plugin) Exec() error { - var ( - files []string - ) - - if p.Build.Event != "tag" { - return fmt.Errorf("The Gitea Release plugin is only available for tags") - } - - if p.Config.APIKey == "" { - return fmt.Errorf("You must provide an API key") - } - - if !fileExistsValues[p.Config.FileExists] { - return fmt.Errorf("Invalid value for file_exists") - } - - if p.Config.BaseURL == "" { - return fmt.Errorf("You must provide a base url.") - } - - if !strings.HasSuffix(p.Config.BaseURL, "/") { - p.Config.BaseURL = p.Config.BaseURL + "/" - } - - var err error - if p.Config.Note != "" { - if p.Config.Note, err = readStringOrFile(p.Config.Note); err != nil { - return fmt.Errorf("error while reading %s: %v", p.Config.Note, err) - } - } - - if p.Config.Title != "" { - if p.Config.Title, err = readStringOrFile(p.Config.Title); err != nil { - return fmt.Errorf("error while reading %s: %v", p.Config.Note, err) - } - } - - for _, glob := range p.Config.Files { - globed, err := filepath.Glob(glob) - - if err != nil { - return fmt.Errorf("Failed to glob %s. %s", glob, err) - } - - if globed != nil { - files = append(files, globed...) - } - } - - if len(p.Config.Checksum) > 0 { - var ( - err error - ) - - files, err = writeChecksums(files, p.Config.Checksum) - - if err != nil { - return fmt.Errorf("Failed to write checksums. %s", err) - } - } - - httpClient := &http.Client{} - if p.Config.Insecure { - cookieJar, _ := cookiejar.New(nil) - httpClient = &http.Client{ - Jar: cookieJar, - Transport: &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - }, - } - } - client, err := gitea.NewClient(p.Config.BaseURL, gitea.SetToken(p.Config.APIKey), gitea.SetHTTPClient(httpClient)) - if err != nil { - return err - } - - rc := releaseClient{ - Client: client, - Owner: p.Repo.Owner, - Repo: p.Repo.Name, - Tag: strings.TrimPrefix(p.Commit.Ref, "refs/tags/"), - Draft: p.Config.Draft, - Prerelease: p.Config.PreRelease, - FileExists: p.Config.FileExists, - Title: p.Config.Title, - Note: p.Config.Note, - } - - // if the title was not provided via .drone.yml we use the tag instead - // fixes https://github.com/drone-plugins/drone-gitea-release/issues/26 - if rc.Title == "" { - rc.Title = rc.Tag - } - - release, err := rc.buildRelease() - - if err != nil { - return fmt.Errorf("Failed to create the release. %s", err) - } - - if err := rc.uploadFiles(release.ID, files); err != nil { - return fmt.Errorf("Failed to upload the files. %s", err) - } - - return nil -} - -func readStringOrFile(input string) (string, error) { - // Check if input is a file path - if _, err := os.Stat(input); err != nil && os.IsNotExist(err) { - // No file found => use input as result - return input, nil - } else if err != nil { - return "", err - } - result, err := ioutil.ReadFile(input) - if err != nil { - return "", err - } - return string(result), nil -} diff --git a/plugin/impl.go b/plugin/impl.go new file mode 100644 index 0000000..1311988 --- /dev/null +++ b/plugin/impl.go @@ -0,0 +1,132 @@ +// Copyright (c) 2021, the Drone Plugins project authors. +// Please see the AUTHORS file for details. All rights reserved. +// Use of this source code is governed by an Apache 2.0 license that can be +// found in the LICENSE file. + +package plugin + +import ( + "fmt" + "path/filepath" + "strings" + + "code.gitea.io/sdk/gitea" + "github.com/urfave/cli/v2" +) + +// Settings for the plugin. +type Settings struct { + APIKey string + Files cli.StringSlice + FileExists string + Checksum cli.StringSlice + Draft bool + PreRelease bool + BaseURL string + Title string + Note string + + uploads []string +} + +// Validate handles the settings validation of the plugin. +func (p *Plugin) Validate() error { + var err error + + if p.pipeline.Build.Event != "tag" { + return fmt.Errorf("gitea release plugin is only available for tags") + } + + if p.settings.APIKey == "" { + return fmt.Errorf("no api key provided") + } + + if !fileExistsValues[p.settings.FileExists] { + return fmt.Errorf("invalid value for file_exists") + } + + if p.settings.BaseURL == "" { + return fmt.Errorf("no base url provided") + } + + if !strings.HasSuffix(p.settings.BaseURL, "/") { + p.settings.BaseURL = p.settings.BaseURL + "/" + } + + if p.settings.Note != "" { + if p.settings.Note, err = readStringOrFile(p.settings.Note); err != nil { + return fmt.Errorf("error while reading %s: %w", p.settings.Note, err) + } + } + + if p.settings.Title != "" { + if p.settings.Title, err = readStringOrFile(p.settings.Title); err != nil { + return fmt.Errorf("error while reading %s: %w", p.settings.Note, err) + } + } + + files := p.settings.Files.Value() + for _, glob := range files { + globed, err := filepath.Glob(glob) + + if err != nil { + return fmt.Errorf("failed to glob %s: %w", glob, err) + } + + if globed != nil { + p.settings.uploads = append(p.settings.uploads, globed...) + } + } + + if len(files) > 0 && len(p.settings.uploads) < 1 { + return fmt.Errorf("failed to find any file to release") + } + + checksum := p.settings.Checksum.Value() + if len(checksum) > 0 { + p.settings.uploads, err = writeChecksums(files, checksum) + + if err != nil { + return fmt.Errorf("failed to write checksums: %w", err) + } + } + + return nil +} + +// Execute provides the implementation of the plugin. +func (p *Plugin) Execute() error { + client, err := gitea.NewClient(p.settings.BaseURL, gitea.SetToken(p.settings.APIKey), gitea.SetHTTPClient(p.network.Client)) + if err != nil { + return err + } + + rc := releaseClient{ + Client: client, + Owner: p.pipeline.Repo.Owner, + Repo: p.pipeline.Repo.Name, + Tag: strings.TrimPrefix(p.pipeline.Commit.Ref, "refs/tags/"), + Draft: p.settings.Draft, + Prerelease: p.settings.PreRelease, + FileExists: p.settings.FileExists, + Title: p.settings.Title, + Note: p.settings.Note, + } + + // When the title was not provided in the config use the tag instead + if rc.Title == "" { + rc.Title = rc.Tag + } + + release, err := rc.buildRelease() + + if err != nil { + return fmt.Errorf("failed to create the release. %s", err) + } + + if err := rc.uploadFiles(release.ID, p.settings.uploads); err != nil { + return fmt.Errorf("failed to upload the files. %s", err) + } + + return nil +} diff --git a/plugin/impl_test.go b/plugin/impl_test.go new file mode 100644 index 0000000..d101388 --- /dev/null +++ b/plugin/impl_test.go @@ -0,0 +1,18 @@ +// Copyright (c) 2021, the Drone Plugins project authors. +// Please see the AUTHORS file for details. All rights reserved. +// Use of this source code is governed by an Apache 2.0 license that can be +// found in the LICENSE file. + +package plugin + +import ( + "testing" +) + +func TestValidate(t *testing.T) { + t.Skip() +} + +func TestExecute(t *testing.T) { + t.Skip() +} diff --git a/plugin/plugin.go b/plugin/plugin.go new file mode 100644 index 0000000..970e755 --- /dev/null +++ b/plugin/plugin.go @@ -0,0 +1,26 @@ +// Copyright (c) 2021, the Drone Plugins project authors. +// Please see the AUTHORS file for details. All rights reserved. +// Use of this source code is governed by an Apache 2.0 license that can be +// found in the LICENSE file. + +package plugin + +import ( + "github.com/drone-plugins/drone-plugin-lib/drone" +) + +// Plugin implements drone.Plugin to provide the plugin implementation. +type Plugin struct { + settings Settings + pipeline drone.Pipeline + network drone.Network +} + +// New initializes a plugin from the given Settings, Pipeline, and Network. +func New(settings Settings, pipeline drone.Pipeline, network drone.Network) drone.Plugin { + return &Plugin{ + settings: settings, + pipeline: pipeline, + network: network, + } +} diff --git a/plugin/plugin_test.go b/plugin/plugin_test.go new file mode 100644 index 0000000..b4d9167 --- /dev/null +++ b/plugin/plugin_test.go @@ -0,0 +1,14 @@ +// Copyright (c) 2021, the Drone Plugins project authors. +// Please see the AUTHORS file for details. All rights reserved. +// Use of this source code is governed by an Apache 2.0 license that can be +// found in the LICENSE file. + +package plugin + +import ( + "testing" +) + +func TestPlugin(t *testing.T) { + t.Skip() +} diff --git a/release.go b/plugin/release.go similarity index 68% rename from release.go rename to plugin/release.go index e80d84b..9843348 100644 --- a/release.go +++ b/plugin/release.go @@ -1,4 +1,9 @@ -package main +// Copyright (c) 2021, the Drone Plugins project authors. +// Please see the AUTHORS file for details. All rights reserved. +// Use of this source code is governed by an Apache 2.0 license that can be +// found in the LICENSE file. + +package plugin import ( "fmt" @@ -35,7 +40,7 @@ func (rc *releaseClient) buildRelease() (*gitea.Release, error) { release, err = rc.newRelease() if err != nil { - return nil, fmt.Errorf("Failed to retrieve or create a release: %s", err) + return nil, fmt.Errorf("failed to retrieve or create a release: %s", err) } return release, nil @@ -49,11 +54,11 @@ func (rc *releaseClient) getRelease() (*gitea.Release, error) { for _, release := range releases { if release.TagName == rc.Tag { - fmt.Printf("Successfully retrieved %s release\n", rc.Tag) + fmt.Printf("successfully retrieved %s release\n", rc.Tag) return release, nil } } - return nil, fmt.Errorf("Release %s not found", rc.Tag) + return nil, fmt.Errorf("release %s not found", rc.Tag) } func (rc *releaseClient) newRelease() (*gitea.Release, error) { @@ -67,10 +72,10 @@ func (rc *releaseClient) newRelease() (*gitea.Release, error) { release, _, err := rc.Client.CreateRelease(rc.Owner, rc.Repo, r) if err != nil { - return nil, fmt.Errorf("Failed to create release: %s", err) + return nil, fmt.Errorf("failed to create release: %s", err) } - fmt.Printf("Successfully created %s release\n", rc.Tag) + fmt.Printf("successfully created %s release\n", rc.Tag) return release, nil } @@ -78,7 +83,7 @@ func (rc *releaseClient) uploadFiles(releaseID int64, files []string) error { attachments, _, err := rc.Client.ListReleaseAttachments(rc.Owner, rc.Repo, releaseID, gitea.ListReleaseAttachmentsOptions{}) if err != nil { - return fmt.Errorf("Failed to fetch existing assets: %s", err) + return fmt.Errorf("failed to fetch existing assets: %s", err) } var uploadFiles []string @@ -91,12 +96,12 @@ files: case "overwrite": // do nothing case "fail": - return fmt.Errorf("Asset file %s already exists", path.Base(file)) + return fmt.Errorf("asset file %s already exists", path.Base(file)) case "skip": - fmt.Printf("Skipping pre-existing %s artifact\n", attachment.Name) + fmt.Printf("skipping pre-existing %s artifact\n", attachment.Name) continue files default: - return fmt.Errorf("Internal error, unkown file_exist value %s", rc.FileExists) + return fmt.Errorf("internal error, unkown file_exist value %s", rc.FileExists) } } } @@ -108,24 +113,24 @@ files: handle, err := os.Open(file) if err != nil { - return fmt.Errorf("Failed to read %s artifact: %s", file, err) + return fmt.Errorf("failed to read %s artifact: %s", file, err) } for _, attachment := range attachments { if attachment.Name == path.Base(file) { if _, err := rc.Client.DeleteReleaseAttachment(rc.Owner, rc.Repo, releaseID, attachment.ID); err != nil { - return fmt.Errorf("Failed to delete %s artifact: %s", file, err) + return fmt.Errorf("failed to delete %s artifact: %s", file, err) } - fmt.Printf("Successfully deleted old %s artifact\n", attachment.Name) + fmt.Printf("successfully deleted old %s artifact\n", attachment.Name) } } if _, _, err = rc.Client.CreateReleaseAttachment(rc.Owner, rc.Repo, releaseID, handle, path.Base(file)); err != nil { - return fmt.Errorf("Failed to upload %s artifact: %s", file, err) + return fmt.Errorf("failed to upload %s artifact: %s", file, err) } - fmt.Printf("Successfully uploaded %s artifact\n", file) + fmt.Printf("successfully uploaded %s artifact\n", file) } return nil diff --git a/utils.go b/plugin/utils.go similarity index 69% rename from utils.go rename to plugin/utils.go index 794b0a5..53a41d7 100644 --- a/utils.go +++ b/plugin/utils.go @@ -1,4 +1,9 @@ -package main +// Copyright (c) 2021, the Drone Plugins project authors. +// Please see the AUTHORS file for details. All rights reserved. +// Use of this source code is governed by an Apache 2.0 license that can be +// found in the LICENSE file. + +package plugin import ( "crypto/md5" @@ -11,9 +16,7 @@ import ( "io" "io/ioutil" "os" - "os/exec" "strconv" - "strings" ) var ( @@ -24,13 +27,19 @@ var ( } ) -func execute(cmd *exec.Cmd) error { - fmt.Println("+", strings.Join(cmd.Args, " ")) - - cmd.Stderr = os.Stderr - cmd.Stdin = os.Stdin - - return cmd.Run() +func readStringOrFile(input string) (string, error) { + // Check if input is a file path + if _, err := os.Stat(input); err != nil && os.IsNotExist(err) { + // No file found => use input as result + return input, nil + } else if err != nil { + return "", err + } + result, err := ioutil.ReadFile(input) + if err != nil { + return "", err + } + return string(result), nil } func checksum(r io.Reader, method string) (string, error) { @@ -55,7 +64,7 @@ func checksum(r io.Reader, method string) (string, error) { return strconv.FormatUint(uint64(crc32.ChecksumIEEE(b)), 10), nil } - return "", fmt.Errorf("Hashing method %s is not supported", method) + return "", fmt.Errorf("hashing method %s is not supported", method) } func writeChecksums(files, methods []string) ([]string, error) { @@ -66,7 +75,7 @@ func writeChecksums(files, methods []string) ([]string, error) { handle, err := os.Open(file) if err != nil { - return nil, fmt.Errorf("Failed to read %s artifact: %s", file, err) + return nil, fmt.Errorf("failed to read %s artifact: %s", file, err) } hash, err := checksum(handle, method) diff --git a/renovate.json b/renovate.json new file mode 100644 index 0000000..a88a8a2 --- /dev/null +++ b/renovate.json @@ -0,0 +1,26 @@ +{ + "extends": [ + "config:base", + ":automergeMinor", + ":automergeDigest" + ], + "enabledManagers": [ + "dockerfile", + "gomod" + ], + "dockerfile": { + "fileMatch": [ + "docker/Dockerfile\\.linux\\.(arm|arm64|amd64|multiarch)", + "docker/Dockerfile\\.windows\\.(1809|1903|1909|2004)" + ], + "pinDigests": true + }, + "gomod": { + "postUpdateOptions": [ + "gomodTidy" + ] + }, + "labels": [ + "renovate" + ] +}