Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
chore: Auto update shkeptnspecversion on release of new keptn/spec ve…
Browse files Browse the repository at this point in the history
…rsion (#4404)
  • Loading branch information
mowies committed Jul 22, 2021
1 parent 87adc38 commit 5bf3171
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/auto-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: |
Expand Down
45 changes: 45 additions & 0 deletions .github/workflows/auto-update-keptn-spec.yml
Original file line number Diff line number Diff line change
@@ -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 }}
30 changes: 30 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
shkeptnspecversion: "0.2.1"
11 changes: 11 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
6 changes: 3 additions & 3 deletions pkg/lib/v0_2_0/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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),
Expand Down
13 changes: 7 additions & 6 deletions pkg/lib/v0_2_0/events_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -318,15 +319,15 @@ 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",
Data: TestData{Content: "testdata"},
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"),
Expand All @@ -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"),
Expand All @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion pkg/lib/v0_2_0/keptn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion version

This file was deleted.

0 comments on commit 5bf3171

Please sign in to comment.