Initial commit. Should work, but no promises (yet).
This commit is contained in:
43
message/payload.go
Normal file
43
message/payload.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package message
|
||||
|
||||
type (
|
||||
// Payload that will be sent to Discord.
|
||||
payload struct {
|
||||
Wait bool `json:"wait"`
|
||||
Content string `json:"content"`
|
||||
Username string `json:"username"`
|
||||
AvatarURL string `json:"avatar_url"`
|
||||
TTS bool `json:"tts"`
|
||||
Embeds []EmbedObject `json:"embeds"`
|
||||
}
|
||||
|
||||
// EmbedFooterObject for Embed Footer Structure.
|
||||
EmbedFooterObject struct {
|
||||
Text string `json:"text"`
|
||||
IconURL string `json:"icon_url"`
|
||||
}
|
||||
|
||||
// EmbedAuthorObject for Embed Author Structure
|
||||
EmbedAuthorObject struct {
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
IconURL string `json:"icon_url"`
|
||||
}
|
||||
|
||||
// EmbedFieldObject for Embed Field Structure
|
||||
EmbedFieldObject struct {
|
||||
Name string `json:"name"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
// EmbedObject is for Embed Structure
|
||||
EmbedObject struct {
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
URL string `json:"url"`
|
||||
Color int `json:"color"`
|
||||
Footer EmbedFooterObject `json:"footer"`
|
||||
Author EmbedAuthorObject `json:"author"`
|
||||
Fields []EmbedFieldObject `json:"fields"`
|
||||
}
|
||||
)
|
8
message/prepare.go
Normal file
8
message/prepare.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package message
|
||||
|
||||
// stdlib
|
||||
|
||||
// local
|
||||
|
||||
// other
|
||||
//"github.com/drone/drone-template-lib/template"
|
55
message/process.go
Normal file
55
message/process.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"errors"
|
||||
"log"
|
||||
|
||||
// local
|
||||
"go.dev.pztrn.name/discordrone/env"
|
||||
|
||||
// other
|
||||
"github.com/drone/drone-template-lib/template"
|
||||
)
|
||||
|
||||
// Process starts message(s) processing.
|
||||
func Process() error {
|
||||
if env.Data.Debug {
|
||||
log.Println("Preparing message for sending...")
|
||||
}
|
||||
|
||||
// Webhook data should present.
|
||||
if env.Data.Plugin.Webhook.ID == "" && env.Data.Plugin.Webhook.Token == "" {
|
||||
return errors.New("webhook id or token is missing")
|
||||
}
|
||||
|
||||
// Payload.
|
||||
p := &payload{
|
||||
Embeds: []EmbedObject{
|
||||
createEmbed(),
|
||||
},
|
||||
}
|
||||
|
||||
// If no additional message was passed - send what we got.
|
||||
if len(env.Data.Plugin.Message) == 0 {
|
||||
err := sendMessage(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// If message was set - format it.
|
||||
if len(env.Data.Plugin.Message) > 0 {
|
||||
text, err := template.RenderTrim(env.Data.Plugin.Message, env.Data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.Content = text
|
||||
err1 := sendMessage(p)
|
||||
if err1 != nil {
|
||||
return err1
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
28
message/send_message.go
Normal file
28
message/send_message.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
// local
|
||||
"go.dev.pztrn.name/discordrone/env"
|
||||
)
|
||||
|
||||
// Sends message to Discord.
|
||||
func sendMessage(message interface{}) error {
|
||||
webhookURL := fmt.Sprintf("https://discordapp.com/api/webhooks/%s/%s", env.Data.Plugin.Webhook.ID, env.Data.Plugin.Webhook.Token)
|
||||
b := new(bytes.Buffer)
|
||||
if err := json.NewEncoder(b).Encode(message); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := http.Post(webhookURL, "application/json; charset=utf-8", b)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
79
message/template.go
Normal file
79
message/template.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
// local
|
||||
"go.dev.pztrn.name/discordrone/env"
|
||||
)
|
||||
|
||||
const (
|
||||
// DroneIconURL default drone logo url
|
||||
droneIconURL = "https://c1.staticflickr.com/5/4236/34957940160_435d83114f_z.jpg"
|
||||
// DroneDesc default drone description
|
||||
droneDesc = "Powered by DiscoDrone Plugin"
|
||||
)
|
||||
|
||||
func createEmbed() EmbedObject {
|
||||
// Create initial payload object.
|
||||
// Description.
|
||||
// Color.
|
||||
embed := EmbedObject{
|
||||
Title: env.Data.Drone.Commit.Message,
|
||||
URL: env.Data.Drone.Build.Link,
|
||||
Author: EmbedAuthorObject{
|
||||
Name: env.Data.Drone.Commit.Author.Name,
|
||||
IconURL: env.Data.Drone.Commit.Author.Avatar,
|
||||
},
|
||||
Footer: EmbedFooterObject{
|
||||
Text: droneDesc,
|
||||
IconURL: droneIconURL,
|
||||
},
|
||||
}
|
||||
|
||||
// Compose description.
|
||||
var description string
|
||||
// ToDo: promote/rollback?
|
||||
switch env.Data.Drone.Build.Event {
|
||||
case "pull_request":
|
||||
var branch string
|
||||
if env.Data.Drone.Commit.Ref != "" {
|
||||
branch = env.Data.Drone.Commit.Ref
|
||||
} else {
|
||||
branch = env.Data.Drone.Commit.Branch
|
||||
}
|
||||
description = env.Data.Drone.Commit.Author.Name + " updated pull request " + branch
|
||||
case "push":
|
||||
description = env.Data.Drone.Commit.Author.Name + " pushed to " + env.Data.Drone.Commit.Branch
|
||||
case "tag":
|
||||
description = env.Data.Drone.Commit.Author.Name + " pushed tag " + env.Data.Drone.Commit.Branch
|
||||
}
|
||||
|
||||
embed.Description = description
|
||||
|
||||
// Compose color.
|
||||
var color int
|
||||
if env.Data.Plugin.Color != "" {
|
||||
env.Data.Plugin.Color = strings.Replace(env.Data.Plugin.Color, "#", "", -1)
|
||||
if s, err := strconv.ParseInt(env.Data.Plugin.Color, 16, 32); err == nil {
|
||||
color = int(s)
|
||||
}
|
||||
}
|
||||
|
||||
switch env.Data.Drone.Build.Status {
|
||||
case "success":
|
||||
// green
|
||||
color = 0x1ac600
|
||||
case "failure", "error", "killed":
|
||||
// red
|
||||
color = 0xff3232
|
||||
default:
|
||||
// yellow
|
||||
color = 0xffd930
|
||||
}
|
||||
embed.Color = color
|
||||
|
||||
return embed
|
||||
}
|
Reference in New Issue
Block a user