Skip to content

Commit

Permalink
Export thread timestamp (#82)
Browse files Browse the repository at this point in the history
* adding new "created_thread_ts_env_name" flag

* cleaning up the code

* fixing code and adding log

* code cleanup

* Update outputs.go

* Update outputs.go with proper function comments

Co-authored-by: Isaac Halvorson <[email protected]>

* Update outputs.go with proper variable naming

Co-authored-by: Isaac Halvorson <[email protected]>

* code fix variable name.

* Handle error from exportOutputs method

Co-authored-by: Isaac Halvorson <[email protected]>
  • Loading branch information
georgesjamous and hisaac authored Jul 5, 2022
1 parent 2d0d0a9 commit 831f0aa
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
19 changes: 13 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ type Config struct {
TimeStamp bool `env:"timestamp,opt[yes,no]"`
Fields string `env:"fields"`
Buttons string `env:"buttons"`

// Step Outputs
ThreadTsOutputVariableName string `env:"output_thread_ts"`
}

// success is true if the build is successful, false otherwise.
Expand Down Expand Up @@ -103,12 +106,12 @@ func newMessage(c Config) Message {
FooterIcon: c.FooterIcon,
Buttons: parseButtons(c.Buttons),
}},
IconEmoji: selectValue(c.IconEmoji, c.IconEmojiOnError),
IconURL: selectValue(c.IconURL, c.IconURLOnError),
LinkNames: c.LinkNames,
Username: selectValue(c.Username, c.UsernameOnError),
ThreadTs: selectValue(c.ThreadTs, c.ThreadTsOnError),
ReplyBroadcast: selectBool(c.ReplyBroadcast, c.ReplyBroadcastOnError),
IconEmoji: selectValue(c.IconEmoji, c.IconEmojiOnError),
IconURL: selectValue(c.IconURL, c.IconURLOnError),
LinkNames: c.LinkNames,
Username: selectValue(c.Username, c.UsernameOnError),
ThreadTs: selectValue(c.ThreadTs, c.ThreadTsOnError),
ReplyBroadcast: selectBool(c.ReplyBroadcast, c.ReplyBroadcastOnError),
}
if c.TimeStamp {
msg.Attachments[0].TimeStamp = int(time.Now().Unix())
Expand Down Expand Up @@ -156,6 +159,10 @@ func postMessage(conf Config, msg Message) error {
return fmt.Errorf("server error: %s, response: %s", resp.Status, body)
}

if err := exportOutputs(&conf, resp); err != nil {
return fmt.Errorf("failed to export outputs: %s", err)
}

return nil
}

Expand Down
66 changes: 66 additions & 0 deletions outputs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"encoding/json"
"fmt"
"net/http"
"os/exec"
"strings"

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

// SendMessageResponse is the response from Slack POST
type SendMessageResponse struct {
/// The Thread Timestamp
Timestamp string `json:"ts"`
}

/// Export the output variables after a successful response
func exportOutputs(conf *Config, resp *http.Response) error {

if !isRequestingOutput(conf) {
log.Debugf("Not requesting any outputs")
return nil
}

isWebhook := strings.TrimSpace(selectValue(string(conf.WebhookURL), string(conf.WebhookURLOnError))) != ""

// Slack webhooks do not return any useful response information
if isWebhook {
return fmt.Errorf("For output support, do not submit a WebHook URL")
}

var response SendMessageResponse
parseError := json.NewDecoder(resp.Body).Decode(&response)
if parseError != nil {
// here we want to fail, because the user is expecting an output
return fmt.Errorf("Failed to parse response: %s", parseError)
}

if string(conf.ThreadTsOutputVariableName) != "" {
log.Debugf("Exporting output: %s=%s\n", string(conf.ThreadTsOutputVariableName), response.Timestamp)
err := exportEnvVariable(string(conf.ThreadTsOutputVariableName), response.Timestamp)
if err != nil {
return err
}
}

return nil

}

/// Checks if we are requesting an output of anything
func isRequestingOutput(conf *Config) bool {
return string(conf.ThreadTsOutputVariableName) != ""
}

/// Exports env using envman
func exportEnvVariable(variable string, value string) error {
c := exec.Command("envman", "add", "--key", variable, "--value", value)
err := c.Run()
if err != nil {
return fmt.Errorf("Failed to run envman %s", err)
}
return nil
}
9 changes: 9 additions & 0 deletions step.yml
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,12 @@ inputs:
The *text* is the label for the button.
The *url* is the fully qualified http or https url to deliver users to.
An attachment may contain 1 to 5 buttons.
# Step Outputs

- output_thread_ts:
opts:
title: The newly created thread timestamp environment variable name
description: Will export the created thread's timestamp to the environment with the supplied name (if not already in thread)
is_required: false
is_sensitive: false

0 comments on commit 831f0aa

Please sign in to comment.