From 26cc02d9d979e5114b207e6c396d48fe6cd1bd2f Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 13 Nov 2024 01:01:15 +0000 Subject: [PATCH] Add support for CI_PIPELINE_FILES (#33) Reviewed-on: https://codeberg.org/woodpecker-plugins/go-plugin/pulls/33 --- metadata_test.go | 2 ++ pipeline.go | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/metadata_test.go b/metadata_test.go index 0103f4f..ad9877b 100644 --- a/metadata_test.go +++ b/metadata_test.go @@ -53,6 +53,7 @@ func testMetadata() map[string]string { "CI_PIPELINE_STATUS": "running", "CI_PIPELINE_CREATED": "1611234567", "CI_PIPELINE_STARTED": "1611234567", + "CI_PIPELINE_FILES": `["README.md", "main.go"]`, // Step "CI_STEP_NUMBER": "1", "CI_STEP_NAME": "test", @@ -107,6 +108,7 @@ func TestMetadata(t *testing.T) { assert.Equal(t, "running", plugin.Metadata.Pipeline.Status) assert.Equal(t, time.Unix(1611234567, 0), plugin.Metadata.Pipeline.Created) assert.Equal(t, time.Unix(1611234567, 0), plugin.Metadata.Pipeline.Started) + assert.Equal(t, []string{"README.md", "main.go"}, plugin.Metadata.Pipeline.ChangedFiles) // Step assert.Equal(t, 1, plugin.Metadata.Step.Number) diff --git a/pipeline.go b/pipeline.go index 4b1cda0..4166c9f 100644 --- a/pipeline.go +++ b/pipeline.go @@ -15,8 +15,11 @@ package plugin import ( + "encoding/json" + "os" "time" + "github.com/rs/zerolog/log" "github.com/urfave/cli/v3" ) @@ -40,6 +43,7 @@ type Pipeline struct { Started time.Time `json:"started,omitempty"` Finished time.Time `json:"finished,omitempty"` Parent int64 `json:"parent,omitempty"` + ChangedFiles []string `json:"files,omitempty"` // Deprecated: Please use URL instead. Link string `json:"link,omitempty"` @@ -121,10 +125,26 @@ func pipelineFlags() []cli.Flag { "DRONE_BUILD_PARENT", ), }, + &cli.StringFlag{ + Name: "pipeline.files", + Usage: "pipeline changed files (string list as json)", + Sources: cli.EnvVars( + "CI_PIPELINE_FILES", + ), + }, } } func pipelineFromContext(c *cli.Command) Pipeline { + var changedFiles []string + + if files := c.String("pipeline.files"); files != "" { + if err := json.Unmarshal([]byte(files), &changedFiles); err != nil { + log.Error().Err(err).Msg("parse \"CI_PIPELINE_FILES\" failed") + os.Exit(1) + } + } + return Pipeline{ Number: c.Int("pipeline.number"), Status: c.String("pipeline.status"), @@ -136,5 +156,6 @@ func pipelineFromContext(c *cli.Command) Pipeline { Started: time.Unix(c.Int("pipeline.started"), 0), Finished: time.Unix(c.Int("pipeline.finished"), 0), Parent: c.Int("pipeline.parent"), + ChangedFiles: changedFiles, } }