From 9c212669bc3dededd5433822737c1fbfb76f2adc Mon Sep 17 00:00:00 2001 From: L0RD-ZER0 <68327382+L0RD-ZER0@users.noreply.github.com> Date: Mon, 18 Mar 2024 18:51:30 +0530 Subject: [PATCH] Add ability to send files over slack using an app token. --- entrypoint.sh | 11 ++++++++ main.go | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/entrypoint.sh b/entrypoint.sh index db190bc..b55ffd1 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -27,6 +27,17 @@ fi export MSG_MODE="$mode" +if [[ -n "$SLACK_FILE_UPLOAD" ]]; then + if [[ -z "$SLACK_TOKEN" ]]; then + echo -e "[\e[0;31mERROR\e[0m] Secret \`SLACK_TOKEN\` is missing and a file upload is specified. File Uploads require an application token to be present.\n" + exit 1 + fi + if [[ -z "$SLACK_CHANNEL" ]]; then + echo -e "[\e[0;31mERROR\e[0m] Secret \`SLACK_CHANNEL\` is missing and a file upload is specified. File Uploads require a channel to be specified.\n" + exit 1 + fi +fi + # custom path for files to override default files custom_path="$GITHUB_WORKSPACE/.github/slack" main_script="/main.sh" diff --git a/main.go b/main.go index f3b37d6..36e25d7 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,8 @@ import ( "bytes" "encoding/json" "fmt" + "io" + "mime/multipart" "net/http" "os" "strings" @@ -30,6 +32,7 @@ const ( EnvMinimal = "MSG_MINIMAL" EnvSlackLinkNames = "SLACK_LINK_NAMES" EnvThreadTs = "SLACK_THREAD_TS" + EnvSlackUpload = "SLACK_FILE_UPLOAD" EnvMessageMode = "MSG_MODE" ) @@ -300,6 +303,74 @@ func send_raw(endpoint string, payload []byte) error { return err } + if res.StatusCode >= 299 { + return fmt.Errorf("Error on message: %s\n", res.Status) + } + + if os.Getenv(EnvSlackUpload) != "" { + err = sendFile(os.Getenv(EnvSlackUpload), "", os.Getenv(EnvSlackChannel), os.Getenv(EnvThreadTs)) + if err != nil { + return err + } + } + + return nil +} + +func sendFile(filename string, message string, channel string, thread_ts string) error { + file, err := os.Open(filename) + if err != nil { + return err + } + defer file.Close() + + fileData := &bytes.Buffer{} + writer := multipart.NewWriter(fileData) + + part, err := writer.CreateFormFile("file", filename) + if err != nil { + return err + } + + _, err = io.Copy(part, file) + + err = writer.WriteField("initial_comment", message) + if err != nil { + return err + } + + err = writer.WriteField("channels", channel) + if err != nil { + return err + } + + if thread_ts != "" { + err = writer.WriteField("thread_ts", thread_ts) + if err != nil { + return err + } + } + + err = writer.Close() + if err != nil { + return err + } + + req, err := http.NewRequest("POST", "https://slack.com/api/files.upload", fileData) + + if err != nil { + return err + } + + req.Header.Set("Content-Type", writer.FormDataContentType()) + req.Header.Set("Authorization", "Bearer "+os.Getenv("SLACK_TOKEN")) + + client := &http.Client{} + res, err := client.Do(req) + if err != nil { + return err + } + if res.StatusCode >= 299 { return fmt.Errorf("Error on message: %s\n", res.Status) }