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 <qwerty287@noreply.codeberg.org>
Co-authored-by: Lauris BH <lauris@nix.lv>
Co-committed-by: Lauris BH <lauris@nix.lv>
This commit is contained in:
Lauris BH 2023-12-10 20:27:57 +00:00 committed by Lauris BH
parent 30c031631d
commit 93da4f40e1
5 changed files with 80 additions and 2 deletions

View File

@ -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()...)

59
forge.go Normal file
View File

@ -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"),
}
}

View File

@ -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),
}
}

View File

@ -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)
}

View File

@ -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"`