-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
2d0d0a9
commit 831f0aa
Showing
3 changed files
with
88 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters