Skip to content

Commit

Permalink
Merge branch 'feature/28-timezone' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
jamieastley committed May 12, 2021
2 parents 1a2fe6c + 27b81a7 commit 731c769
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## [2.1.0] -

* [#28] Allow setting a custom timezone for 'Build Triggered' time
* [#29] Added ability to add custom image to `MessageCard`.

## [2.0.0] - 28th Feb 2021
Expand Down
6 changes: 4 additions & 2 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"encoding/json"
"fmt"

"github.com/bitrise-io/go-utils/colorstring"
)

// Action object within the `actions` JSON object array.
Expand All @@ -21,8 +23,8 @@ func parseActions(jsonString string) []Action {
var actionList []Action
err := json.Unmarshal([]byte(jsonString), &actionList)
if err != nil {
fmt.Println(fmt.Sprintf("Couldn't Unmarshal JSON: %v, \n %s", jsonString, err))
fmt.Println(colorstring.Redf("Couldn't Unmarshal JSON: %v, \n %s", jsonString, err))
}
fmt.Println(fmt.Sprintf("JSON value: %v", actionList))
fmt.Printf("JSON value: %v", actionList)
return actionList
}
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ type config struct {
EnableDebug string `env:"enable_debug"`
RepoURL string `env:"repository_url"`
Actions string `env:"actions"`
Timezone string `env:"timezone"`
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ go 1.16

require (
github.com/bitrise-io/go-steputils v0.0.0-20201016102104-03ae3a6ded35
github.com/bitrise-io/go-utils v0.0.0-20201211082830-859032e9adf0 // indirect
github.com/bitrise-io/go-utils v0.0.0-20201211082830-859032e9adf0
)
40 changes: 26 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"time"

"github.com/bitrise-io/go-steputils/stepconf"
"github.com/bitrise-io/go-utils/colorstring"
)

var buildSucceeded = os.Getenv("BITRISE_BUILD_STATUS") == "0"
Expand All @@ -33,6 +35,25 @@ func valueOptionToBool(userValue string) bool {
return userValue == "yes"
}

func parseTimeString(cfg config) string {
var timeAtLoc time.Time
i, err := strconv.ParseInt(cfg.BuildTime, 10, 64)
if err != nil {
return string("Couldn't parse build time")
}
loc, err := time.LoadLocation(cfg.Timezone)
if err != nil {
fmt.Println(colorstring.Redf("\n%s", err))
fmt.Println(colorstring.Cyan("\nExporting time in UTC...\n"))

timeAtLoc = time.Unix(i, 0).In(time.UTC)
return timeAtLoc.Format(time.RFC1123)
}
timeAtLoc = time.Unix(i, 0).In(loc)

return timeAtLoc.Format(time.RFC1123)
}

func newMessage(cfg config, buildSuccessful bool) Message {
message := Message{}
message.Type = "MessageCard"
Expand Down Expand Up @@ -130,15 +151,9 @@ func buildFactsSection(cfg config, buildSuccessful bool) Section {
Value: cfg.GitBranch,
}

i, err := strconv.ParseInt(cfg.BuildTime, 10, 64)
if err != nil {
_ = fmt.Errorf("failed to parse the given build time: %s", err)
}
// Force UTC, as it otherwise defaults to locale of executing system
parsedTime := time.Unix(i, 0).In(time.UTC)
buildTimeFact := Fact{
Name: "Build Triggered",
Value: parsedTime.Format(time.RFC1123),
Value: parseTimeString(cfg),
}

workflowFact := Fact{
Expand Down Expand Up @@ -174,9 +189,8 @@ func postMessage(webhookURL string, msg Message, debugEnabled bool) error {
if err != nil {
return err
}
fmt.Println(fmt.Sprintf("Request to Microsoft Teams: %s", webhookURL))
if debugEnabled {
fmt.Println(fmt.Sprintf("JSON body: %s\n", b))
log.Print(colorstring.Yellowf("\nRequest to Microsoft Teams:\n%s", b))
}

resp, err := http.Post(webhookURL, "application/json", bytes.NewReader(b))
Expand All @@ -203,16 +217,14 @@ func postMessage(webhookURL string, msg Message, debugEnabled bool) error {
func main() {
var cfg config
if err := stepconf.Parse(&cfg); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
log.Fatalf("Error: %s\n", err)
}
stepconf.Print(cfg)

message := newMessage(cfg, buildSucceeded)
if err := postMessage(cfg.WebhookURL, message, valueOptionToBool(cfg.EnableDebug)); err != nil {
fmt.Println(fmt.Sprintf("Error: %s", err))
os.Exit(1)
log.Fatalf("Error: %s", err)
}

fmt.Println("Message successfully sent!")
fmt.Println(colorstring.Cyan("\nMessage successfully sent!"))
}
43 changes: 43 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,49 @@ func TestOptionalUserValue(t *testing.T) {
}
}

func TestParseTimeString(t *testing.T) {

var tests = []struct {
input config
expected string
}{
// successful UTC
{
mockConfig,
parsedUnixTime,
},
// successful local
{
config{
BuildTime: unixTimeString,
Timezone: "Australia/Sydney",
},
"Sat, 16 Jan 2021 14:44:52 AEDT",
},
// invalid timezone, returns UTC
{
config{
BuildTime: unixTimeString,
Timezone: "Bermuda Triangle",
},
parsedUnixTime,
},
// invalid `buildTime``
{
config{
BuildTime: "unixTimeString",
},
string("Couldn't parse build time"),
},
}

for _, test := range tests {
if output := parseTimeString(test.input); output != test.expected {
t.Errorf("Test failed: output was %v, expected %v", output, test.expected)
}
}
}

func TestBuildPrimarySection(t *testing.T) {
var defaultValuesConfig = config{
SectionTitle: "Some author",
Expand Down
12 changes: 12 additions & 0 deletions step.yml
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,15 @@ inputs:
* Select **Incoming Webhook* and in the text input type **Bitrise**
* Save and copy the link to the input for this step
is_required: true

- timezone: UTC
opts:
category: "MessageCard Content"
title: "Timezone"
summary: "Sets the timezone to use for the 'Build triggered' time"
description: |
Sets the timezone to use for the 'Build triggered' time in the MessageCard.
Defaults to UTC if no value is given.
Valid timezone name inputs can be [found here](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones).

0 comments on commit 731c769

Please sign in to comment.