Skip to content

Commit

Permalink
separate script
Browse files Browse the repository at this point in the history
  • Loading branch information
jbohanon committed Oct 17, 2024
1 parent be2d1d5 commit 7a32f65
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,12 @@ runs:
with:
name: go-test-summary-${{ inputs.cluster-name }}-${{ inputs.matrix-label }}-attempt-${{ github.run_attempt }}
path: ./_test/test_log/go-test-summary
- name: Read summary file
id: getsummary
if: ${{ always() }}
shell: bash
run: echo "summary=$(cat ./_test/test_log/go-test-summary)" >> $GITHUB_OUTPUT
- name: Send Slack Message
if: ${{ always() }}
env:
PARENT_JOB_URL: https://github.com/solo-io/gloo/actions/runs/${{github.run_id}} # parent job hyperlink
SLACKBOT_BEARER: ${{ inputs.slackbot-bearer }}
# SLACK_CHANNEL: C04CJMXAH7A #edge-nightly-results by default
SLACK_CHANNEL: C0314KESVNV #slack-integration-testing for testing
shell: bash
run: |
go run .github/workflows/helpers/notify/slack.go '${{ steps.getsummary.outputs.summary }}'
go run .github/workflows/helpers/notify/slack_test_summary.go './_test/test_log/go-test-summary'
85 changes: 85 additions & 0 deletions .github/workflows/helpers/notify/slack_test_summary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Helper script designed to send a cohesive Slack notification about the result of a collection of GitHub workflows
// This is a simpler variant of notify-from-json.go, due to some issues we
// encountered around the accuracy of the notification: https://github.com/solo-io/solo-projects/issues/5191
//
// This works by:
// 1. Read in the test summary
// 2. Send a Slack notification with the contents
//
// Example usage:
// export SLACKBOT_BEARER=${{ secrets.SLACKBOT_BEARER }}
// export SLACK_CHANNEL=C0314KESVNV
// go run .github/workflows/helpers/notify/slack_test_summary.go ./_test/test_log/go-test-summary

package main

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"time"
)

const (
postMessageEndpoint = "https://slack.com/api/chat.postMessage"
)

type Payload struct {
Channel string `json:"channel"`
Text string `json:"text"`
}

func main() {
summaryFile := os.Args[1]
fmt.Printf("slack_test_summary.go invoked with: %v", summaryFile)

b, err := os.ReadFile(summaryFile)
if err != nil {
panic(err)
}

mustSendSlackText(string(b))
}

func mustSendSlackText(text string) {
fmt.Printf("send slack message with text: %s", text)
mustSendSlackMessage(Payload{
Channel: os.ExpandEnv("$SLACK_CHANNEL"),
Text: os.ExpandEnv(text),
})
}

func mustSendSlackMessage(data Payload) {
payloadBytes, err := json.Marshal(data)
if err != nil {
panic(err)
}

req, err := http.NewRequest(http.MethodPost, postMessageEndpoint, bytes.NewReader(payloadBytes))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Authorization", os.ExpandEnv("Bearer $SLACKBOT_BEARER"))

var netClient = &http.Client{
Timeout: time.Second * 10,
}

resp, err := netClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()

fmt.Println(resp.Status)
b, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(b))

}

0 comments on commit 7a32f65

Please sign in to comment.