Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for threaded Slack messages #73

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ description: 'This action will send a notification to Slack'
author: 'rtCamp'
runs:
using: 'docker'
image: 'docker://ghcr.io/rtcamp/action-slack-notify:v2.1.2'
image: 'Dockerfile'
branding:
icon: 'bell'
color: 'yellow'
outputs:
SLACK_THREAD_TS:
description: 'The identifier of the sent Slack message'
34 changes: 34 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
Expand All @@ -19,12 +20,17 @@ const (
EnvSlackColor = "SLACK_COLOR"
EnvSlackUserName = "SLACK_USERNAME"
EnvSlackFooter = "SLACK_FOOTER"
EnvSlackThreadTs = "SLACK_THREAD_TS"
EnvGithubActor = "GITHUB_ACTOR"
EnvSiteName = "SITE_NAME"
EnvHostName = "HOST_NAME"
EnvMinimal = "MSG_MINIMAL"
)

const (
OutputSlackThreadTs = "SLACK_THREAD_TS"
)

type Webhook struct {
Text string `json:"text,omitempty"`
UserName string `json:"username,omitempty"`
Expand All @@ -33,6 +39,11 @@ type Webhook struct {
Channel string `json:"channel,omitempty"`
UnfurlLinks bool `json:"unfurl_links"`
Attachments []Attachment `json:"attachments,omitempty"`
ThreadTs string `json:"thread_ts,omitempty"`
}

type WebhookResponse struct {
ThreadTs string `json:"ts"`
}

type Attachment struct {
Expand Down Expand Up @@ -194,6 +205,7 @@ func main() {
Fields: fields,
},
},
ThreadTs: os.Getenv(EnvSlackThreadTs),
}

if err := send(endpoint, msg); err != nil {
Expand Down Expand Up @@ -223,6 +235,28 @@ func send(endpoint string, msg Webhook) error {
if res.StatusCode >= 299 {
return fmt.Errorf("Error on message: %s\n", res.Status)
}

defer res.Body.Close()

bodyBytes, err := ioutil.ReadAll(res.Body)

//decoder := json.NewDecoder(bodyBytes)
var data WebhookResponse
json.Unmarshal([]byte(bodyBytes), &data)
// err = decoder.Decode(&data)

if err != nil {
return fmt.Errorf("Failed to parse response data: %s\n", err)
}

fmt.Println(string(bodyBytes))

fmt.Println(res.Status)
setOutput(OutputSlackThreadTs, data.ThreadTs)
return nil
}

func setOutput(name string, value string) {
fmt.Printf("DEBUG name=%s::%s\n", name, value)
fmt.Printf("::set-output name=%s::%s\n", name, value)
}