feat: add context support to Exec and SendMessage functions

- Update the `Exec` function to accept a context parameter.
- Modify the `SendMessage` function to also accept a context parameter.
- Add context import to the necessary files.
- Update test cases to pass a context when calling `Exec`.

Signed-off-by: appleboy <appleboy.tw@gmail.com>
This commit is contained in:
appleboy
2025-03-08 16:43:06 +08:00
parent ffa230b880
commit 6c782afaae
3 changed files with 34 additions and 16 deletions
+1 -1
View File
@@ -292,5 +292,5 @@ func run(c *cli.Context) error {
},
}
return plugin.Exec()
return plugin.Exec(c.Context)
}
+23 -6
View File
@@ -2,6 +2,7 @@ package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
@@ -12,6 +13,7 @@ import (
"path/filepath"
"strconv"
"strings"
"time"
"github.com/appleboy/drone-template-lib/template"
)
@@ -169,7 +171,7 @@ func newfileUploadRequest(uri string, params map[string]string, paramName, path
}
// Exec executes the plugin.
func (p *Plugin) Exec() error {
func (p *Plugin) Exec(ctx context.Context) error {
if p.Config.WebhookID == "" || p.Config.WebhookToken == "" {
return errors.New("missing discord config")
}
@@ -177,7 +179,7 @@ func (p *Plugin) Exec() error {
if len(p.Config.Message) == 0 {
object := p.Template()
p.Payload.Embeds = []EmbedObject{object}
err := p.SendMessage()
err := p.SendMessage(ctx)
if err != nil {
return err
}
@@ -195,7 +197,7 @@ func (p *Plugin) Exec() error {
p.Payload.Embeds = append(p.Payload.Embeds, object)
} else {
p.Payload.Content = txt
err = p.SendMessage()
err = p.SendMessage(ctx)
if err != nil {
return err
}
@@ -203,7 +205,7 @@ func (p *Plugin) Exec() error {
}
if len(p.Payload.Embeds) > 0 {
err := p.SendMessage()
err := p.SendMessage(ctx)
if err != nil {
return err
}
@@ -259,16 +261,31 @@ func (p *Plugin) SendFile(file string) error {
}
// SendMessage to send discord message.
func (p *Plugin) SendMessage() error {
func (p *Plugin) SendMessage(ctx context.Context) error {
webhookURL := fmt.Sprintf("https://discordapp.com/api/webhooks/%s/%s", p.Config.WebhookID, p.Config.WebhookToken)
b := new(bytes.Buffer)
if err := json.NewEncoder(b).Encode(p.Payload); err != nil {
return err
}
_, err := http.Post(webhookURL, "application/json; charset=utf-8", b)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, webhookURL, b)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
client := &http.Client{
Timeout: 10 * time.Second,
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to send message, status code: %d", resp.StatusCode)
}
return nil
}
+10 -9
View File
@@ -1,6 +1,7 @@
package main
import (
"context"
"os"
"testing"
"time"
@@ -11,7 +12,7 @@ import (
func TestMissingConfig(t *testing.T) {
var plugin Plugin
err := plugin.Exec()
err := plugin.Exec(context.Background())
assert.NotNil(t, err)
}
@@ -53,14 +54,14 @@ func TestTemplate(t *testing.T) {
},
}
err := plugin.Exec()
err := plugin.Exec(context.Background())
assert.Nil(t, err)
plugin.Clear()
plugin.Config.Message = []string{"I am appleboy"}
plugin.Payload.TTS = true
plugin.Payload.Wait = true
err = plugin.Exec()
err = plugin.Exec(context.Background())
assert.Nil(t, err)
// send success embed message
@@ -68,14 +69,14 @@ func TestTemplate(t *testing.T) {
plugin.Payload.TTS = false
plugin.Payload.Wait = false
plugin.Clear()
err = plugin.Exec()
err = plugin.Exec(context.Background())
assert.Nil(t, err)
// send success embed message
plugin.Build.Status = "failure"
plugin.Commit.Message = "send failure embed message"
plugin.Clear()
err = plugin.Exec()
err = plugin.Exec(context.Background())
assert.Nil(t, err)
time.Sleep(1 * time.Second)
@@ -83,14 +84,14 @@ func TestTemplate(t *testing.T) {
plugin.Build.Status = "test"
plugin.Commit.Message = "send default embed message"
plugin.Clear()
err = plugin.Exec()
err = plugin.Exec(context.Background())
assert.Nil(t, err)
// change color for embed message
plugin.Config.Color = "#4842f4"
plugin.Commit.Message = "Change embed color to #4842f4"
plugin.Clear()
err = plugin.Exec()
err = plugin.Exec(context.Background())
assert.Nil(t, err)
}
@@ -112,12 +113,12 @@ func TestDefaultTemplate(t *testing.T) {
time.Sleep(1 * time.Second)
plugin.Clear()
err := plugin.Exec()
err := plugin.Exec(context.Background())
assert.Nil(t, err)
plugin.Config.Color = "#f4be41"
time.Sleep(1 * time.Second)
plugin.Clear()
err = plugin.Exec()
err = plugin.Exec(context.Background())
assert.Nil(t, err)
}