generated from actions/container-action
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.go
87 lines (67 loc) · 2.15 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/yanzay/tbot/v2"
)
func composer(status, event, actor, repo, workflow, link string) string {
var text string
// choose icon based on the build status
icons := map[string]string{
"failure": "❗️❗️❗️",
"cancelled": "❕❕❕",
"success": "✅✅✅",
}
replacer := strings.NewReplacer("_", "\\_", "-", "\\-", ".", "\\.")
// removing symbols to avoide markdown parser error
event = replacer.Replace(event)
repo = replacer.Replace(repo)
actor = replacer.Replace(actor)
// Message text composing
text = icons[strings.ToLower(status)] + " *" + strings.ToUpper(event) + "*\n"
text += "was made at " + repo + " \nby " + actor + "\n"
text += "Check here " + "[" + workflow + "](" + link + ")"
return text
}
func linkgen(repo, event string) string {
context := map[string]string{
"issue_comment": "issues",
"issues": "issues",
"pull_request": "pulls",
"pull_request_review_comment": "pulls",
"push": "commits",
"project_card": "projects",
}
event = context[strings.ToLower(event)]
// generates link based on the triggered event
return fmt.Sprintf("https://github.com/%s/%s/", repo, event)
}
func main() {
var (
// inputs provided by Github Actions runtime
// should be defined in the action.yml
token = os.Getenv("INPUT_TOKEN")
chat = os.Getenv("INPUT_CHAT")
status = os.Getenv("INPUT_STATUS")
event = os.Getenv("INPUT_EVENT")
actor = os.Getenv("INPUT_ACTOR")
// github environment context
workflow = os.Getenv("GITHUB_WORKFLOW")
repo = os.Getenv("GITHUB_REPOSITORY")
// commit = os.Getenv("GITHUB_SHA")
)
// Create Telegram client using token
c := tbot.NewClient(token, http.DefaultClient, "https://api.telegram.org")
// link to the commit
link := linkgen(repo, event)
// Prepare message to send
msg := composer(status, event, actor, repo, workflow, link)
// Send to chat using Markdown format
_, err := c.SendMessage(chat, msg, tbot.OptParseModeMarkdown)
if err != nil {
log.Fatalf("unable to send message: %v", err)
}
}