First release

This commit is contained in:
Sameer Rahmani 2023-05-13 21:28:46 +01:00
parent bd8f6aaaac
commit 5e93278007
Signed by: lxsameer
GPG Key ID: B0A4AF28AB9FD90B
7 changed files with 204 additions and 1 deletions

15
Dockerfile Normal file
View File

@ -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

7
Makefile Normal file
View File

@ -0,0 +1,7 @@
TAG=3
build:
docker build -t lxsameer/notify:$(TAG) .
push:
docker push lxsameer/notify:$(TAG)

View File

@ -1,3 +1,19 @@
# notify
A woodpecker plugin that sends notification to matrix and IRC.
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}"
```

10
go.mod Normal file
View File

@ -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
)

12
go.sum Normal file
View File

@ -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=

143
main.go Normal file
View File

@ -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")
}

BIN
notify Executable file

Binary file not shown.