From 5bf317199534084055f65360aee05ab79f46951f Mon Sep 17 00:00:00 2001 From: Moritz Wiesinger Date: Tue, 29 Jun 2021 10:06:20 +0200 Subject: [PATCH] chore: Auto update shkeptnspecversion on release of new keptn/spec version (#4404) --- .github/workflows/auto-pr.yml | 4 +- .github/workflows/auto-update-keptn-spec.yml | 45 ++++++++++++++++++++ config/config.go | 30 +++++++++++++ config/config.yaml | 1 + config/config_test.go | 11 +++++ pkg/lib/v0_2_0/events.go | 6 +-- pkg/lib/v0_2_0/events_test.go | 13 +++--- pkg/lib/v0_2_0/keptn.go | 3 +- version | 1 - 9 files changed, 101 insertions(+), 13 deletions(-) create mode 100644 .github/workflows/auto-update-keptn-spec.yml create mode 100644 config/config.go create mode 100644 config/config.yaml create mode 100644 config/config_test.go delete mode 100644 version diff --git a/.github/workflows/auto-pr.yml b/.github/workflows/auto-pr.yml index 30da01b9..f1037bc8 100644 --- a/.github/workflows/auto-pr.yml +++ b/.github/workflows/auto-pr.yml @@ -28,7 +28,7 @@ jobs: with: path: 'keptn' repository: 'keptn/keptn' - token: ${{ secrets.KEPTN_KEPTN_GITHUB_TOKEN }} + token: ${{ secrets.KEPTN_BOT_TOKEN }} ref: 'master' - name: Determine Target CommitIsh id: target_commit @@ -68,7 +68,7 @@ jobs: - name: Create PR working-directory: 'keptn' env: - GITHUB_TOKEN: ${{ secrets.KEPTN_KEPTN_GITHUB_TOKEN }} + GITHUB_TOKEN: ${{ secrets.KEPTN_BOT_TOKEN }} TARGET_BRANCH: ${{ steps.target_commit.outputs.TARGET_BRANCH }} GO_UTILS_TARGET: ${{ steps.target_commit.outputs.GO_UTILS_TARGET }} run: | diff --git a/.github/workflows/auto-update-keptn-spec.yml b/.github/workflows/auto-update-keptn-spec.yml new file mode 100644 index 00000000..17401d7c --- /dev/null +++ b/.github/workflows/auto-update-keptn-spec.yml @@ -0,0 +1,45 @@ +name: Auto Update - Spec +on: + repository_dispatch: + types: [spec-update] +defaults: + run: + shell: bash +env: + KEPTN_BOT_USER: "keptn-bot <86361500+keptn-bot@users.noreply.github.com>" +jobs: + update-spec: + env: + SPEC_REF: ${{ github.event.client_payload.ref }} + runs-on: ubuntu-20.04 + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Extract spec tag + run: echo "SPEC_TAG=${SPEC_REF#refs/tags/}" >> $GITHUB_ENV + + - name: Update keptn/spec + env: + SPEC_TAG: ${{ env.SPEC_TAG }} + run: | + echo "shkeptnspecversion: '$SPEC_TAG'" > ./config/config.yaml + + - name: Create Pull Request + uses: peter-evans/create-pull-request@v3 + with: + token: ${{ secrets.KEPTN_BOT_TOKEN }} + commit-message: "Update keptn/spec to release ${{ env.SPEC_TAG }}" + committer: ${{ env.KEPTN_BOT_USER }} + author: ${{ env.KEPTN_BOT_USER }} + signoff: true + branch: patch/update-keptn-spec-${{ env.SPEC_TAG }} + delete-branch: true + base: master + labels: "dependencies" + title: "Update keptn/spec to release ${{ env.SPEC_TAG }}" + body: | + **This is an automated PR!** + + Update to the keptn/spec + New version: ${{ env.SPEC_TAG }} diff --git a/config/config.go b/config/config.go new file mode 100644 index 00000000..ffd29109 --- /dev/null +++ b/config/config.go @@ -0,0 +1,30 @@ +package config + +import ( + // allow to load config from file + _ "embed" + "gopkg.in/yaml.v3" + "sync" +) + +//go:embed config.yaml +var yamlConfig []byte +var cfg KeptnGoUtilsConfig + +var doOnce = sync.Once{} + +// KeptnGoUtilsConfig contains config for the keptn go-utils +type KeptnGoUtilsConfig struct { + ShKeptnSpecVersion string `yaml:"shkeptnspecversion"` +} + +// GetKeptnGoUtilsConfig take the config.yaml file and reads it into the KeptnGoUtilsConfig struct +func GetKeptnGoUtilsConfig() KeptnGoUtilsConfig { + doOnce.Do(func() { + err := yaml.Unmarshal(yamlConfig, &cfg) + if err != nil { + cfg = KeptnGoUtilsConfig{} + } + }) + return cfg +} diff --git a/config/config.yaml b/config/config.yaml new file mode 100644 index 00000000..ae62d11e --- /dev/null +++ b/config/config.yaml @@ -0,0 +1 @@ +shkeptnspecversion: "0.2.1" diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 00000000..c2addd5c --- /dev/null +++ b/config/config_test.go @@ -0,0 +1,11 @@ +package config + +import ( + "github.com/stretchr/testify/require" + "testing" +) + +func TestGetKeptnGoUtilsConfig(t *testing.T) { + got := GetKeptnGoUtilsConfig() + require.NotEmpty(t, got.ShKeptnSpecVersion) +} diff --git a/pkg/lib/v0_2_0/events.go b/pkg/lib/v0_2_0/events.go index 6c629f09..80824ae3 100644 --- a/pkg/lib/v0_2_0/events.go +++ b/pkg/lib/v0_2_0/events.go @@ -7,6 +7,7 @@ import ( "fmt" "github.com/cloudevents/sdk-go/v2/protocol" "github.com/google/uuid" + "github.com/keptn/go-utils/config" "github.com/keptn/go-utils/pkg/api/models" "github.com/keptn/go-utils/pkg/common/strutils" "github.com/keptn/go-utils/pkg/lib/keptn" @@ -22,7 +23,6 @@ const MAX_SEND_RETRIES = 3 const DefaultHTTPEventEndpoint = "http://localhost:8081/event" const defaultSpecVersion = "1.0" -const defaultKeptnSpecVersion = "0.2.1" const keptnEventTypePrefix = "sh.keptn.event." const keptnTriggeredEventSuffix = ".triggered" @@ -323,13 +323,13 @@ func EventDataAs(in models.KeptnContextExtendedCE, out interface{}) error { // KeptnEvent creates a builder for a new KeptnContextExtendedCE func KeptnEvent(eventType string, source string, payload interface{}) *KeptnEventBuilder { - + cfg := config.GetKeptnGoUtilsConfig() ce := models.KeptnContextExtendedCE{ ID: uuid.NewString(), Contenttype: cloudevents.ApplicationJSON, Data: payload, Source: strutils.Stringp(source), - Shkeptnspecversion: defaultKeptnSpecVersion, + Shkeptnspecversion: cfg.ShKeptnSpecVersion, Specversion: defaultSpecVersion, Time: time.Now().UTC(), Type: strutils.Stringp(eventType), diff --git a/pkg/lib/v0_2_0/events_test.go b/pkg/lib/v0_2_0/events_test.go index 58971371..f8ce55b4 100644 --- a/pkg/lib/v0_2_0/events_test.go +++ b/pkg/lib/v0_2_0/events_test.go @@ -1,6 +1,7 @@ package v0_2_0 import ( + "github.com/keptn/go-utils/config" "github.com/keptn/go-utils/pkg/api/models" api "github.com/keptn/go-utils/pkg/api/utils" "github.com/keptn/go-utils/pkg/common/strutils" @@ -118,7 +119,7 @@ func TestKeptn_SendCloudEvent(t *testing.T) { assert.Equal(t, 1, len(testEventSender.SentEvents)) keptnEvent, err := ToKeptnEvent(testEventSender.SentEvents[0]) assert.Nil(t, err) - assert.Equal(t, defaultKeptnSpecVersion, keptnEvent.Shkeptnspecversion) + assert.Equal(t, config.GetKeptnGoUtilsConfig().ShKeptnSpecVersion, keptnEvent.Shkeptnspecversion) assert.Equal(t, defaultSpecVersion, keptnEvent.Specversion) assert.Equal(t, "sh.keptn.events.test", *keptnEvent.Type) assert.Equal(t, "test-context", keptnEvent.Shkeptncontext) @@ -276,7 +277,7 @@ func TestCreateSimpleKeptnEvent(t *testing.T) { require.Equal(t, testData, event.Data) require.Equal(t, "", event.Shkeptncontext) require.Equal(t, time.Now().UTC().Round(time.Minute), event.Time.Round(time.Minute)) - require.Equal(t, defaultKeptnSpecVersion, event.Shkeptnspecversion) + require.NotEmpty(t, event.Shkeptnspecversion) require.Equal(t, defaultSpecVersion, event.Specversion) require.Equal(t, "", event.Triggeredid) require.Equal(t, strutils.Stringp("sh.keptn.event.dev.delivery.triggered"), event.Type) @@ -318,7 +319,7 @@ func TestToCloudEvent(t *testing.T) { expected.SetSpecVersion(defaultSpecVersion) expected.SetExtension(keptnContextCEExtension, "my-keptn-context") expected.SetExtension(triggeredIDCEExtension, "my-triggered-id") - expected.SetExtension(keptnSpecVersionCEExtension, defaultKeptnSpecVersion) + expected.SetExtension(keptnSpecVersionCEExtension, config.GetKeptnGoUtilsConfig().ShKeptnSpecVersion) keptnEvent := models.KeptnContextExtendedCE{ Contenttype: "application/json", @@ -326,7 +327,7 @@ func TestToCloudEvent(t *testing.T) { ID: "my-id", Shkeptncontext: "my-keptn-context", Source: strutils.Stringp("source"), - Shkeptnspecversion: defaultKeptnSpecVersion, + Shkeptnspecversion: config.GetKeptnGoUtilsConfig().ShKeptnSpecVersion, Specversion: defaultSpecVersion, Triggeredid: "my-triggered-id", Type: strutils.Stringp("sh.keptn.event.dev.delivery.triggered"), @@ -348,7 +349,7 @@ func TestToKeptnEvent(t *testing.T) { ID: "my-id", Shkeptncontext: "my-keptn-context", Source: strutils.Stringp("my-source"), - Shkeptnspecversion: defaultKeptnSpecVersion, + Shkeptnspecversion: config.GetKeptnGoUtilsConfig().ShKeptnSpecVersion, Specversion: defaultSpecVersion, Triggeredid: "my-triggered-id", Type: strutils.Stringp("sh.keptn.event.dev.delivery.triggered"), @@ -364,7 +365,7 @@ func TestToKeptnEvent(t *testing.T) { ce.SetData(cloudevents.ApplicationJSON, TestData{Content: "testdata"}) ce.SetExtension(keptnContextCEExtension, "my-keptn-context") ce.SetExtension(triggeredIDCEExtension, "my-triggered-id") - ce.SetExtension(keptnSpecVersionCEExtension, defaultKeptnSpecVersion) + ce.SetExtension(keptnSpecVersionCEExtension, config.GetKeptnGoUtilsConfig().ShKeptnSpecVersion) keptnEvent, err := ToKeptnEvent(ce) diff --git a/pkg/lib/v0_2_0/keptn.go b/pkg/lib/v0_2_0/keptn.go index cc8ed4b4..83297f3a 100644 --- a/pkg/lib/v0_2_0/keptn.go +++ b/pkg/lib/v0_2_0/keptn.go @@ -5,6 +5,7 @@ import ( "fmt" cloudevents "github.com/cloudevents/sdk-go/v2" "github.com/google/uuid" + "github.com/keptn/go-utils/config" api "github.com/keptn/go-utils/pkg/api/utils" "github.com/keptn/go-utils/pkg/lib/keptn" "gopkg.in/yaml.v3" @@ -90,7 +91,7 @@ func (k *Keptn) GetShipyard() (*Shipyard, error) { // SendCloudEvent sends a cloudevent to the event broker func (k *Keptn) SendCloudEvent(event cloudevents.Event) error { - event.SetExtension(keptnSpecVersionCEExtension, defaultKeptnSpecVersion) + event.SetExtension(keptnSpecVersionCEExtension, config.GetKeptnGoUtilsConfig().ShKeptnSpecVersion) if k.UseLocalFileSystem { log.Println(fmt.Printf("%v", string(event.Data()))) return nil diff --git a/version b/version deleted file mode 100644 index b6160487..00000000 --- a/version +++ /dev/null @@ -1 +0,0 @@ -0.6.2