<commit_before>package plugins

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"os"
	"os/exec"
	"strings"

	"github.com/fatih/color"
	"github.com/servehub/serve/manifest"
	"github.com/servehub/utils"
)

func init() {
	manifest.PluginRegestry.Add("notify", Notify{})
}

type Notify struct{}

func (p Notify) Run(data manifest.Manifest) error {
	ch := data.GetTree("channels").GetTree(data.GetString("event"))

	switch ch.GetString("type") {
	case "slack":
		webhook := ch.GetString("webhook")

		if strings.HasPrefix(webhook, "$") {
			value, ok := os.LookupEnv(strings.TrimPrefix(webhook, "$"))
			if !ok || webhook == "" {
				return fmt.Errorf("slack webhook url is empty: %s", ch.GetString("webhook"))
			}
			webhook = value
		}

		attachments := []map[string]string{
			{
				"color": ch.GetString("color"),
				"text":  ch.GetString("message"),
			},
		}

		changelogFor := ch.GetStringOr("changelog-for", "")

		if changelogFor != "" {
			consul, _ := utils.ConsulClient(data.GetString("consul.address"))

			if pair, _, err := consul.KV().Get(data.GetString("consul.path"), nil); err == nil && pair != nil {
				latestRelease := latestRelease{}
				if err := json.Unmarshal(pair.Value, &latestRelease); err != nil {
					return err
				}

				gitCmd := fmt.Sprintf(`git log --pretty=format:" — %%an: %%s" %s..%s`, latestRelease.CommitHash, changelogFor)
				log.Println(color.YellowString("> %s", gitCmd))

				out, err := exec.Command("/bin/bash", "-ec", gitCmd).CombinedOutput()
				if err != nil {
					if strings.Contains(err.Error(), "fatal: Invalid revision range") {
						if err2 := exec.Command("/bin/bash", "-ec", "git fetch --depth=100").Run(); err2 != nil {
							return fmt.Errorf("%s", out)
						}

						out, err = exec.Command("/bin/bash", "-ec", gitCmd).CombinedOutput()
					}

					if err != nil {
						return fmt.Errorf("%s", out)
					}
				}

				attachments = append(attachments, map[string]string{
					"color": "#CCC",
					"text":  string(out),
				})
			}

			bts, _ := json.Marshal(latestRelease{changelogFor})
			if err := utils.PutConsulKv(consul, data.GetString("consul.path"), string(bts)); err != nil {
				return err<commit_msg>fix notify changelog: fix git fetch<commit_after>package plugins

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"os"
	"os/exec"
	"strings"

	"github.com/fatih/color"
	"github.com/servehub/serve/manifest"
	"github.com/servehub/utils"
)

func init() {
	manifest.PluginRegestry.Add("notify", Notify{})
}

type Notify struct{}

func (p Notify) Run(data manifest.Manifest) error {
	ch := data.GetTree("channels").GetTree(data.GetString("event"))

	switch ch.GetString("type") {
	case "slack":
		webhook := ch.GetString("webhook")

		if strings.HasPrefix(webhook, "$") {
			value, ok := os.LookupEnv(strings.TrimPrefix(webhook, "$"))
			if !ok || webhook == "" {
				return fmt.Errorf("slack webhook url is empty: %s", ch.GetString("webhook"))
			}
			webhook = value
		}

		attachments := []map[string]string{
			{
				"color": ch.GetString("color"),
				"text":  ch.GetString("message"),
			},
		}

		changelogFor := ch.GetStringOr("changelog-for", "")

		if changelogFor != "" {
			consul, _ := utils.ConsulClient(data.GetString("consul.address"))

			if pair, _, err := consul.KV().Get(data.GetString("consul.path"), nil); err == nil && pair != nil {
				latestRelease := latestRelease{}
				if err := json.Unmarshal(pair.Value, &latestRelease); err != nil {
					return err
				}

				gitCmd := fmt.Sprintf(`git log --pretty=format:" — %%an: %%s" %s..%s`, latestRelease.CommitHash, changelogFor)
				log.Println(color.YellowString("> %s", gitCmd))

				out, err := exec.Command("/bin/bash", "-ec", gitCmd).CombinedOutput()
				if err != nil {
					if strings.Contains(err.Error(), "fatal: Invalid revision range") {
						if err2 := exec.Command("/bin/bash", "-ec", "git fetch --depth=100").Run(); err2 == nil {
							out, err = exec.Command("/bin/bash", "-ec", gitCmd).CombinedOutput()
						}
					}

					if err != nil {
						return fmt.Errorf("%s", out)
					}
				}

				attachments = append(attachments, map[string]string{
					"color": "#CCC",
					"text":  string(out),
				})
			}

			bts, _ := json.Marshal(latestRelease{changelogFor})
			if err := utils.PutConsulKv(consul, data.GetString("consul.path"), string(bts)); err != nil {
				return err
{'fraction_non_alphanumeric': 0.1902796882164145, 'fraction_numerical': 0.0022925263640531865, 'mean_word_length': 3.474871794871795, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1729684', 'n_tokens_mistral': 1798, 'n_tokens_neox': 1530, 'n_words': 305}