Add support for CI_PIPELINE_FILES (#33)

Reviewed-on: https://codeberg.org/woodpecker-plugins/go-plugin/pulls/33
This commit is contained in:
6543 2024-11-13 01:01:15 +00:00
parent b43eefd089
commit 26cc02d9d9
2 changed files with 23 additions and 0 deletions

View File

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

View File

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