diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..19ec34e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM alpine +RUN apk --no-cache add go ca-certificates +WORKDIR /app +ADD go.mod . +ADD go.sum . +ADD main.go . +RUN go build main.go + + +FROM alpine +RUN apk --no-cache add ca-certificates +WORKDIR /app +COPY --from=0 /app/main /app/main + +ENTRYPOINT /app/main diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4bcefcb --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +TAG=3 + +build: + docker build -t lxsameer/notify:$(TAG) . + +push: + docker push lxsameer/notify:$(TAG) diff --git a/README.md b/README.md index fea181e..4ad60df 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,19 @@ # notify -A woodpecker plugin that sends notification to matrix and IRC. \ No newline at end of file +A woodpecker plugin that sends notification to matrix and IRC. + +Example: +```yaml + notify: + image: lxsameer/notify:3 + settings: + matrix_room_id: + from_secret: matrix_room + matrix_access_token: + from_secret: matrix_token + + matrix_user: + from_secret: matrix_user + + matrix_msg: "[${CI_REPO}] JOB #${CI_BUILD_NUMBER} - ${CI_JOB_STATUS}" +``` diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c9b9d19 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module lxsameer.com/notify + +go 1.20 + +require ( + github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530 // indirect + github.com/thoj/go-ircevent v0.0.0-20210723090443-73e444401d64 // indirect + golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect + golang.org/x/text v0.3.6 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..1ab96a6 --- /dev/null +++ b/go.sum @@ -0,0 +1,12 @@ +github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530 h1:kHKxCOLcHH8r4Fzarl4+Y3K5hjothkVW5z7T1dUM11U= +github.com/matrix-org/gomatrix v0.0.0-20220926102614-ceba4d9f7530/go.mod h1:/gBX06Kw0exX1HrwmoBibFA98yBk/jxKpGVeyQbff+s= +github.com/thoj/go-ircevent v0.0.0-20210723090443-73e444401d64 h1:l/T7dYuJEQZOwVOpjIXr1180aM9PZL/d1MnMVIxefX4= +github.com/thoj/go-ircevent v0.0.0-20210723090443-73e444401d64/go.mod h1:Q1NAJOuRdQCqN/VIWdnaaEhV8LpeO2rtlBP7/iDJNII= +golang.org/x/net v0.0.0-20210614182718-04defd469f4e h1:XpT3nA5TvE525Ne3hInMh6+GETgn27Zfm9dxsThnX2Q= +golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000..65af0ae --- /dev/null +++ b/main.go @@ -0,0 +1,143 @@ +package main + +import ( + "crypto/tls" + "fmt" + "log" + "os" + "strings" + "sync" + + "github.com/matrix-org/gomatrix" + irc "github.com/thoj/go-ircevent" +) + +func notifyMatrix(url string, room string, user string, token string, msg string) error { + server := url + if server == "" { + server = "https://matrix.org" + } + + if token == "" || user == "" || msg == "" { + return fmt.Errorf("'matrix_user', 'matrix_access_token', and 'matrix_msg' are required") + } + + matrixClient, err := gomatrix.NewClient(server, "", "") + if err != nil { + return err + } + matrixClient.SetCredentials(user, token) + + _, err = matrixClient.JoinRoom(room, "", nil) + if err != nil { + return err + } + + _, err = matrixClient.SendMessageEvent(room, "m.room.message", gomatrix.TextMessage{ + Body: msg, + FormattedBody: msg, + Format: "org.matrix.custom.html", + MsgType: "m.text", + }) + if err != nil { + return err + } + return nil +} + +func notifyIRC(server string, channel string, nick string, pass string, msg string) error { + ircServer := server + if ircServer == "" { + ircServer = "irc.libera.chat:6697" + } + + serverDomain := strings.Split(ircServer, ":")[0] + + irccon := irc.IRC(nick, nick) + if pass != "" { + irccon.Password = pass + } + + irccon.UseTLS = true + irccon.TLSConfig = &tls.Config{ServerName: serverDomain} + + err := irccon.Connect(ircServer) + if err != nil { + return err + } + + defer irccon.Quit() + + irccon.AddCallback("001", func(e *irc.Event) { + fmt.Println("Joining IRC channel: " + channel) + irccon.Join(channel) + }) + + irccon.AddCallback("366", func(e *irc.Event) { + fmt.Println("Posting on the IRC channel...") + irccon.Privmsg(channel, msg) + irccon.Quit() + }) + + irccon.Loop() + return nil +} + +func main() { + matrixURL := os.Getenv("PLUGIN_MATRIX_URL") + matrixRoomID := os.Getenv("PLUGIN_MATRIX_ROOM_ID") + matrixUser := os.Getenv("PLUGIN_MATRIX_USER") + matrixAccessToken := os.Getenv("PLUGIN_MATRIX_ACCESS_TOKEN") + matrixMsg := os.Getenv("PLUGIN_MATRIX_MSG") + + ircServer := os.Getenv("PLUGIN_IRC_SERVER") + ircChannel := os.Getenv("PLUGIN_IRC_CHANNEL") + ircNickname := os.Getenv("PLUGIN_IRC_NICKNAME") + ircPassword := os.Getenv("PLUGIN_IRC_PASSWORD") + ircMsg := os.Getenv("PLUGIN_IRC_MSG") + + var waitGroup sync.WaitGroup + c := make(chan error) + + waitGroup.Add(2) + + go func() { + waitGroup.Wait() + close(c) + }() + + go func() { + defer waitGroup.Done() + + var err error = nil + if matrixRoomID != "" { + err = notifyMatrix(matrixURL, matrixRoomID, matrixUser, matrixAccessToken, matrixMsg) + } + c <- err + }() + + go func() { + defer waitGroup.Done() + + var err error = nil + if ircChannel != "" { + err = notifyIRC(ircServer, ircChannel, ircNickname, ircPassword, ircMsg) + } + c <- err + }() + + var errs []error + for err := range c { + if err != nil { + errs = append(errs, err) + fmt.Println("Error: " + err.Error()) + } + } + + if len(errs) > 0 { + log.Fatal("Notify Faild!") + return + } + + log.Println("Message sent successfully") +} diff --git a/notify b/notify new file mode 100755 index 0000000..97392c3 Binary files /dev/null and b/notify differ