From 93da4f40e19f0afc6051c0a4383ab7fdae73ae15 Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Sun, 10 Dec 2023 20:27:57 +0000 Subject: [PATCH] Add Forge metadata (#20) Adds support for loading Forge metadata from env variables Reviewed-on: https://codeberg.org/woodpecker-plugins/go-plugin/pulls/20 Reviewed-by: qwerty287 Co-authored-by: Lauris BH Co-committed-by: Lauris BH --- flags.go | 1 + forge.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ metadata.go | 2 ++ metadata_test.go | 11 +++++++-- pipeline.go | 9 ++++++++ 5 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 forge.go diff --git a/flags.go b/flags.go index e84db8d..2107685 100644 --- a/flags.go +++ b/flags.go @@ -29,6 +29,7 @@ func Flags() []cli.Flag { flags = append(flags, prevFlags()...) flags = append(flags, stepFlags()...) flags = append(flags, systemFlags()...) + flags = append(flags, forgeFlags()...) // Plugin flags flags = append(flags, loggingFlags()...) diff --git a/forge.go b/forge.go new file mode 100644 index 0000000..9e14ae3 --- /dev/null +++ b/forge.go @@ -0,0 +1,59 @@ +// Copyright 2023 Woodpecker Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package plugin + +import ( + "github.com/urfave/cli/v2" +) + +const ( + ForgeTypeGitea = "gitea" + ForgeTypeGitHub = "github" + ForgeTypeGitLab = "gitlab" + ForgeTypeBitbucket = "bitbucket" +) + +// Forge defines metadata for integration with a forge. +type Forge struct { + Type string `json:"type,omitempty"` + Link string `json:"link,omitempty"` +} + +func forgeFlags() []cli.Flag { + return []cli.Flag{ + &cli.StringFlag{ + Name: "forge.type", + Usage: "forge type (gitea, github, gitlab, bitbucker)", + EnvVars: []string{ + "CI_FORGE_TYPE", + }, + }, + &cli.StringFlag{ + Name: "forge.link", + Usage: "forge link", + EnvVars: []string{ + "CI_FORGE_URL", + "CI_FORGE_LINK", + }, + }, + } +} + +func forgeFromContext(ctx *cli.Context) Forge { + return Forge{ + Type: ctx.String("forge.type"), + Link: ctx.String("forge.link"), + } +} diff --git a/metadata.go b/metadata.go index 5c7a818..4b6ce24 100644 --- a/metadata.go +++ b/metadata.go @@ -26,6 +26,7 @@ type Metadata struct { Prev Commit `json:"prev,omitempty"` Step Step `json:"step,omitempty"` System System `json:"sys,omitempty"` + Forge Forge `json:"forge,omitempty"` } // MetadataFromContext creates a Metadata from the cli.Context. @@ -37,5 +38,6 @@ func MetadataFromContext(ctx *cli.Context) Metadata { Prev: prevFromContext(ctx), Step: stepFromContext(ctx), System: systemFromContext(ctx), + Forge: forgeFromContext(ctx), } } diff --git a/metadata_test.go b/metadata_test.go index 5b2d248..5f8c7bc 100644 --- a/metadata_test.go +++ b/metadata_test.go @@ -48,7 +48,7 @@ func testMetadata() map[string]string { "CI_COMMIT_URL": "https://codeberg.org/woodpecker-plugins/go-plugin/commit/a1b2c3d4", // Build "CI_PIPELINE_NUMBER": "1", - "CI_PIPELINE_EVENT": "push", + "CI_PIPELINE_EVENT": EventTypePush, "CI_PIPELINE_URL": "https://ci.woodpecker-ci.org/woodpecker-ci/woodpecker/1", "CI_PIPELINE_STATUS": "running", "CI_PIPELINE_CREATED": "1611234567", @@ -63,6 +63,9 @@ func testMetadata() map[string]string { "CI_SYSTEM_URL": "https://ci.woodpecker-ci.org", "CI_SYSTEM_VERSION": "1.0.0", "CI_SYSTEM_HOST": "woodpecker-ci.org", + // Forge + "CI_FORGE_TYPE": ForgeTypeGitea, + "CI_FORGE_URL": "https://codeberg.org", } } @@ -99,7 +102,7 @@ func TestMetadata(t *testing.T) { // Pipeline assert.Equal(t, int64(1), plugin.Metadata.Pipeline.Number) - assert.Equal(t, "push", plugin.Metadata.Pipeline.Event) + assert.Equal(t, EventTypePush, plugin.Metadata.Pipeline.Event) assert.Equal(t, "https://ci.woodpecker-ci.org/woodpecker-ci/woodpecker/1", plugin.Metadata.Pipeline.Link) assert.Equal(t, "running", plugin.Metadata.Pipeline.Status) assert.Equal(t, time.Unix(1611234567, 0), plugin.Metadata.Pipeline.Created) @@ -114,4 +117,8 @@ func TestMetadata(t *testing.T) { assert.Equal(t, "https://ci.woodpecker-ci.org", plugin.Metadata.System.Link) assert.Equal(t, "1.0.0", plugin.Metadata.System.Version) assert.Equal(t, "woodpecker-ci.org", plugin.Metadata.System.Host) + + // Forge + assert.Equal(t, ForgeTypeGitea, plugin.Metadata.Forge.Type) + assert.Equal(t, "https://codeberg.org", plugin.Metadata.Forge.Link) } diff --git a/pipeline.go b/pipeline.go index a6a7478..218534a 100644 --- a/pipeline.go +++ b/pipeline.go @@ -20,6 +20,15 @@ import ( "github.com/urfave/cli/v2" ) +const ( + EventTypePush = "push" + EventTypePullRequest = "pull_request" + EventTypeTag = "tag" + EventTypeDeployment = "deployment" + EventTypeCron = "cron" + EventTypeManual = "manual" +) + // Pipeline defines runtime metadata for a pipeline. type Pipeline struct { Number int64 `json:"number,omitempty"`