Skip to content
This repository has been archived by the owner on May 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #103 from appnexus/notify-chart-version
Browse files Browse the repository at this point in the history
Add new formatting keys for jira/slack notifications
  • Loading branch information
rcousineau-xandr authored Apr 7, 2021
2 parents 5707b8f + be0c961 commit 3b5cef0
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 79 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,12 @@ When invoked, Ankh will operate over both the `haste-server` and `myservice` cha

#### `Format Variables`
| Variable | Description
| ------------- | :---:
| `%USER%` | Current username |
| `%CHART%` | Current chart being used |
| `%VERSION%` | Version of the primary container |
| `%TARGET%` | Target environment or context |

Example format: `format: "_%USER%_ is releasing *%CHART%@%VERSION%* to *%TARGET%*"`
| ------------- | :---:
| `%USER%` | Current username |
| `%CHART%` | Current chart being used (`<name>@<version>`) |
| `%CHART_NAME%` | Name of chart |
| `%CHART_VERSION%` | Version of chart |
| `%VERSION%` | Version of the primary container |
| `%TARGET%` | Target environment or context |

Example format: `format: "_%USER%_ is releasing *%CHART_NAME%* chart:*%CHART_VERSION%* tag:*%VERSION%* to *%TARGET%*"`
49 changes: 22 additions & 27 deletions jira/jira.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,8 @@ func getSummary(ctx *ankh.ExecutionContext, chart *ankh.Chart, envOrContext stri
format = ctx.AnkhConfig.Jira.RollbackSummaryFormat
}

chartString, err := util.GetChartString(chart)
if err != nil {
return "", err
}

versionString := ""
if chart.Tag != nil {
versionString = *chart.Tag
}

if format != "" {
message, err := util.ReplaceFormatVariables(format, chartString, versionString, envOrContext)
message, err := util.NotificationString(format, chart, envOrContext)
if err != nil {
ctx.Logger.Infof("Unable to use format: '%v'. Will prompt for subject", format)
} else {
Expand All @@ -169,7 +159,7 @@ func getSummary(ctx *ankh.ExecutionContext, chart *ankh.Chart, envOrContext stri
}

// Otherwise, prompt for message
message, err := promptForSummary(ctx, chartString, versionString, envOrContext)
message, err := promptForSummary(ctx, chart, envOrContext)
if err != nil {
ctx.Logger.Infof("Unable to prompt for subject. Will use default subject")
}
Expand All @@ -189,13 +179,8 @@ func getDescription(ctx *ankh.ExecutionContext, chart *ankh.Chart, envOrContext
format = ctx.AnkhConfig.Jira.RollbackDescriptionFormat
}

chartString, err := util.GetChartString(chart)
if err != nil {
return "", err
}

if format != "" {
message, err := util.ReplaceFormatVariables(format, chartString, versionString, envOrContext)
message, err := util.NotificationString(format, chart, envOrContext)
if err != nil {
ctx.Logger.Infof("Unable to use format: '%v'. Will prompt for description", format)
} else {
Expand All @@ -204,32 +189,42 @@ func getDescription(ctx *ankh.ExecutionContext, chart *ankh.Chart, envOrContext
}

// Otherwise, prompt for message
message, err := promptForDescription(ctx, chartString, versionString, envOrContext)
message, err := promptForDescription(ctx, chart, envOrContext)
if err != nil {
ctx.Logger.Infof("Unable to prompt for description. Will use default description")
}

return message, nil
}

func promptForSummary(ctx *ankh.ExecutionContext, chart string, version string, envOrContext string) (string, error) {
defaultSubject := fmt.Sprintf("Deployment of %v verson:%v to *%v*", chart, version, envOrContext)
func promptForSummary(ctx *ankh.ExecutionContext, chart *ankh.Chart, envOrContext string) (string, error) {
version := ""
if chart.Tag != nil {
version = *chart.Tag
}

defaultSummary := fmt.Sprintf("Deployment of %s chart %s verson %s to *%s*", chart.Name, chart.Version, version, envOrContext)
if ctx.Mode == ankh.Rollback {
defaultSubject = fmt.Sprintf("Rollback of %v in *%v*", chart, envOrContext)
defaultSummary = fmt.Sprintf("Rollback of %s in *%s*", chart.Name, envOrContext)
}

message, err := util.PromptForInput(defaultSubject, "Jira Summary")
message, err := util.PromptForInput(defaultSummary, "Jira Summary")
if err != nil {
return defaultSubject, err
return defaultSummary, err
}

return message, nil
}

func promptForDescription(ctx *ankh.ExecutionContext, chart string, version string, envOrContext string) (string, error) {
defaultSubject := fmt.Sprintf("Ticket to track the deployment of %v verson:%v to *%v*", chart, version, envOrContext)
func promptForDescription(ctx *ankh.ExecutionContext, chart *ankh.Chart, envOrContext string) (string, error) {
version := ""
if chart.Tag != nil {
version = *chart.Tag
}

defaultSubject := fmt.Sprintf("Ticket to track the deployment of %s chart %s verson %s to *%s*", chart.Name, chart.Version, version, envOrContext)
if ctx.Mode == ankh.Rollback {
defaultSubject = fmt.Sprintf("Ticket to track the rollback of %v in *%v*", chart, envOrContext)
defaultSubject = fmt.Sprintf("Ticket to track the rollback of %s in *%s*", chart.Name, envOrContext)
}

message, err := util.PromptForInput(defaultSubject, "Jira Description")
Expand Down
26 changes: 11 additions & 15 deletions slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,8 @@ func getMessageText(ctx *ankh.ExecutionContext, chart *ankh.Chart, envOrContext
format = ctx.AnkhConfig.Slack.RollbackFormat
}

versionString := ""
if chart.Tag != nil {
versionString = *chart.Tag
}

chartString, err := util.GetChartString(chart)
if err != nil {
return "", err
}

if format != "" {
message, err := util.ReplaceFormatVariables(format, chartString, versionString, envOrContext)
message, err := util.NotificationString(format, chart, envOrContext)
if err != nil {
ctx.Logger.Infof("Unable to use format: '%v'. Will prompt for message", format)
} else {
Expand All @@ -144,22 +134,28 @@ func getMessageText(ctx *ankh.ExecutionContext, chart *ankh.Chart, envOrContext
}

// Otherwise, prompt for message
message, err := promptForMessageText(ctx, chartString, versionString, envOrContext)
message, err := promptForMessageText(ctx, chart, envOrContext)
if err != nil {
ctx.Logger.Infof("Unable to prompt for message. Will use default message")
}

return message, nil
}

func promptForMessageText(ctx *ankh.ExecutionContext, chart string, version string, envOrContext string) (string, error) {
func promptForMessageText(ctx *ankh.ExecutionContext, chart *ankh.Chart, envOrContext string) (string, error) {
currentUser, err := user.Current()
if err != nil {
return "", err
}
defaultMessage := fmt.Sprintf("%v is releasing %v@%v to *%v*", currentUser.Username, chart, version, envOrContext)

version := ""
if chart.Tag != nil {
version = *chart.Tag
}

defaultMessage := fmt.Sprintf("%s is releasing %s chart %s version %s to *%s*", currentUser.Username, chart.Name, chart.Version, version, envOrContext)
if ctx.Mode == ankh.Rollback {
defaultMessage = fmt.Sprintf("%v is rolling back %v in *%v*", currentUser, chart, envOrContext)
defaultMessage = fmt.Sprintf("%s is rolling back %s in *%v*", currentUser, chart.Name, envOrContext)
}

message, err := util.PromptForInput(defaultMessage, "Slack Message")
Expand Down
41 changes: 22 additions & 19 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,37 +626,40 @@ func GetEnvironmentOrContext(environment string, context string) string {
return ""
}

func GetChartString(chart *ankh.Chart) (string, error) {
func NotificationString(notificationFormat string, chart *ankh.Chart, envOrContext string) (string, error) {

currentUser, err := user.Current()
if err != nil {
return "", err
}

chartName := chart.Name
chartVersion := ""
chartString := ""
if chart.Path != "" {
absChartPath, err := filepath.Abs(chart.Path)
if err != nil {
return "", nil
}
return fmt.Sprintf("%v (local)", absChartPath), nil
chartVersion = fmt.Sprintf("%s (local)", absChartPath)
chartString = chartVersion
} else {
return fmt.Sprintf("%v@%v", chart.Name, chart.Version), nil
chartVersion = chart.Version
chartString = fmt.Sprintf("%s@%s", chartName, chartVersion)
}
}

func ReplaceFormatVariables(format string, chart string, version string, env string) (string, error) {

result := format
currentUser, err := user.Current()
if err != nil {
return "", err
version := ""
if chart.Tag != nil {
version = *chart.Tag
}

// Replace %USER%
result := notificationFormat
result = strings.Replace(result, "%USER%", currentUser.Username, -1)

// Replace %CHART%
result = strings.Replace(result, "%CHART%", chart, -1)

// Replace %VERSION%
result = strings.Replace(result, "%CHART_NAME%", chartName, -1)
result = strings.Replace(result, "%CHART_VERSION%", chartVersion, -1)
result = strings.Replace(result, "%CHART%", chartString, -1)
result = strings.Replace(result, "%VERSION%", version, -1)

// Replace %TARGET%
result = strings.Replace(result, "%TARGET%", env, -1)
result = strings.Replace(result, "%TARGET%", envOrContext, -1)

return result, nil
}
Expand Down
115 changes: 104 additions & 11 deletions util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os/user"
"testing"

ankh "github.com/appnexus/ankh/context"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -109,21 +110,53 @@ func TestGetEnviroment(t *testing.T) {
}
}

func TestReplaceFormatVariables(t *testing.T) {
func TestNotificationString(t *testing.T) {

format := "%USER% is doing a release of %CHART% version %VERSION% to %TARGET%"
chart := "best app ever"
// replace %USER%, %CHART%, %VERSION%, %TARGET% (non-local chart)

notificationFormat := "%USER% is doing a release of %CHART% version %VERSION% to %TARGET%"
version := "1.33.7"
env := "production"
chart := &ankh.Chart{
Path: "",
Name: "best app ever",
Version: "1.2.3",
Tag: &version,
}
envOrContext := "production"

currentUser, err := user.Current()
if err != nil {
t.Logf("Unable to get currnet user for test. Error: %v", err)
t.Fail()
}

expectedResult := fmt.Sprintf("%v is doing a release of best app ever version 1.33.7 to production", currentUser.Username)
result, err := ReplaceFormatVariables(format, chart, version, env)
expectedResult := fmt.Sprintf("%v is doing a release of best app [email protected] version 1.33.7 to production", currentUser.Username)
result, err := NotificationString(notificationFormat, chart, envOrContext)
if err != nil {
t.Logf("Failed to replace message text. Error: %v", err)
t.Fail()
}
if result != expectedResult {
t.Logf("got %s but was expecting '%s'", result, expectedResult)
t.Fail()
}

// -----------------------------------------------------------------

// replace %CHART%, %VERSION%, %TARGET% (local chart)

notificationFormat = "Releasing %CHART% version %VERSION% to %TARGET%"
version = "1.33.7"
chart = &ankh.Chart{
Path: "/home/someone/app/helm/app",
Name: "best app ever",
Version: "1.2.3",
Tag: &version,
}
envOrContext = "production"

expectedResult = "Releasing /home/someone/app/helm/app (local) version 1.33.7 to production"
result, err = NotificationString(notificationFormat, chart, envOrContext)
if err != nil {
t.Logf("Failed to replace message text. Error: %v", err)
t.Fail()
Expand All @@ -133,13 +166,73 @@ func TestReplaceFormatVariables(t *testing.T) {
t.Fail()
}

format = "Someone is doing a release of %CHAT% version %VERSION% to %TARGET%"
chart = "best app ever"
// -----------------------------------------------------------------

// replace %CHART_NAME%, %CHART_VERSION%, %VERSION%, %TARGET% (non-local chart)

notificationFormat = "Releasing %CHART_NAME% chart %CHART_VERSION% version %VERSION% to %TARGET%"
version = "1.33.7"
env = "production"
chart = &ankh.Chart{
Path: "",
Name: "best app ever",
Version: "1.2.3",
Tag: &version,
}
envOrContext = "production"

expectedResult = "Releasing best app ever chart 1.2.3 version 1.33.7 to production"
result, err = NotificationString(notificationFormat, chart, envOrContext)
if err != nil {
t.Logf("Failed to replace message text. Error: %v", err)
t.Fail()
}
if result != expectedResult {
t.Logf("got %s but was expecting '%s'", result, expectedResult)
t.Fail()
}

// -----------------------------------------------------------------

// replace %CHART_NAME%, %CHART_VERSION%, %VERSION%, %TARGET% (local chart)

notificationFormat = "Releasing %CHART_NAME% chart %CHART_VERSION% version %VERSION% to %TARGET%"
version = "1.33.7"
chart = &ankh.Chart{
Path: "/home/someone/app/helm/app",
Name: "best app ever",
Version: "1.2.3",
Tag: &version,
}
envOrContext = "production"

expectedResult = "Releasing best app ever chart /home/someone/app/helm/app (local) version 1.33.7 to production"
result, err = NotificationString(notificationFormat, chart, envOrContext)
if err != nil {
t.Logf("Failed to replace message text. Error: %v", err)
t.Fail()
}
if result != expectedResult {
t.Logf("got %s but was expecting '%s'", result, expectedResult)
t.Fail()
}

// -----------------------------------------------------------------

// replace %VERSION%, %TARGET (non-local chart)
// format also includes (typo?) %CHAT% which is not replaced

notificationFormat = "Releasing %CHAT% version %VERSION% to %TARGET%"
version = "1.33.7"
chart = &ankh.Chart{
Path: "",
Name: "best app ever",
Version: "1.2.3",
Tag: &version,
}
envOrContext = "production"

expectedResult = "Someone is doing a release of %CHAT% version 1.33.7 to production"
result, err = ReplaceFormatVariables(format, chart, version, env)
expectedResult = "Releasing %CHAT% version 1.33.7 to production"
result, err = NotificationString(notificationFormat, chart, envOrContext)
if err != nil {
t.Logf("Failed to replace message text. Error: %v", err)
t.Fail()
Expand Down

0 comments on commit 3b5cef0

Please sign in to comment.