-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 add functionality to message user when build fails (#548)
* feat: 🎸 add functionality to message user when build fails * docs(cobra): update auto-generated documentation --------- Co-authored-by: jaskaransarkaria <[email protected]>
- Loading branch information
1 parent
3624992
commit 140e5a0
Showing
10 changed files
with
127 additions
and
15 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
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
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
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,7 @@ | ||
package slack | ||
|
||
import "github.com/slack-go/slack" | ||
|
||
func initSlack(token string) *slack.Client { | ||
return slack.New(token) | ||
} |
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,26 @@ | ||
package slack | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/slack-go/slack" | ||
) | ||
|
||
func Notify(prNumber, token, webhookUrl, buildUrl string) error { | ||
slackClient := initSlack(token) | ||
|
||
defaultSearchParams := slack.NewSearchParameters() | ||
|
||
results, searchErr := search(slackClient, defaultSearchParams, prNumber) | ||
|
||
if searchErr != nil { | ||
fmt.Printf("Failed to find pr in slack %v\n", searchErr) | ||
return nil | ||
} | ||
|
||
// get the user who posted | ||
user := results.Matches[0].User | ||
ts := results.Matches[0].Timestamp | ||
|
||
return post(user, ts, webhookUrl, buildUrl) | ||
} |
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,21 @@ | ||
package slack | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/slack-go/slack" | ||
) | ||
|
||
func post(user, ts, webhookUrl, buildUrl string) error { | ||
// https://pkg.go.dev/github.com/slack-go/slack#PostWebhook | ||
message := fmt.Sprintf("<@%s> <%s|your build failed>, please address immediately or add a <https://user-guide.cloud-platform.service.justice.gov.uk/documentation/other-topics/long-running-env-operations.html|APPLY_PIPELINE_SKIP_THIS_NAMESPACE> to your namespace to prevent our pipelines from being blocked", user, buildUrl) | ||
|
||
webhookMsg := slack.WebhookMessage{ | ||
Channel: "ask-cloud-platform", | ||
Text: message, | ||
ThreadTimestamp: ts, | ||
ReplyBroadcast: true, | ||
} | ||
|
||
return slack.PostWebhook(webhookUrl, &webhookMsg) | ||
} |
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,22 @@ | ||
package slack | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/slack-go/slack" | ||
) | ||
|
||
func search(client *slack.Client, params slack.SearchParameters, query string) (*slack.SearchMessages, error) { | ||
results, searchErr := client.SearchMessages(query, params) | ||
if searchErr != nil { | ||
fmt.Printf("Error: searching for slack messages: %v\n", searchErr.Error()) | ||
return nil, searchErr | ||
} | ||
|
||
if results == nil { | ||
fmt.Printf("Failed to find a slack message with the PR number %s\n", query) | ||
return nil, nil | ||
} | ||
|
||
return results, nil | ||
} |