diff --git a/action.yml b/action.yml index eca4c5f..73b2af4 100644 --- a/action.yml +++ b/action.yml @@ -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' diff --git a/main.go b/main.go index a594ff1..d06ef66 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "io/ioutil" "net/http" "os" "strings" @@ -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"` @@ -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 { @@ -194,6 +205,7 @@ func main() { Fields: fields, }, }, + ThreadTs: os.Getenv(EnvSlackThreadTs), } if err := send(endpoint, msg); err != nil { @@ -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) +}