Browse Source

initial add gitea support and code change to our new repo

Andreas Peters 5 months ago
parent
commit
326bf105ca
Signed by: Andreas Peters <ap@aventer.biz> GPG Key ID: 6A83F12541CFCCF9

+ 2
- 2
Dockerfile View File

@@ -8,11 +8,11 @@ ARG BRANCH=v0.0.4
8 8
 RUN apk update && \
9 9
     apk add git gcc libc-dev && \
10 10
     go get github.com/sirupsen/logrus && \
11
-    go get github.com/AVENTER-UG/util && \
11
+    go get git.aventer.biz/AVENTER/util && \
12 12
     go get github.com/mattn/go-sqlite3 && \
13 13
     go get github.com/prometheus/client_golang/prometheus && \
14 14
     go get github.com/matrix-org/dugong && \
15
-    go get github.com/AVENTER-UG/gomatrix && \
15
+    go get git.aventer.biz/AVENTER/gomatrix && \
16 16
     go get github.com/mattn/go-shellwords && \
17 17
     go get gopkg.in/yaml.v2 && \
18 18
     go get golang.org/x/oauth2 && \

+ 5
- 4
README.md View File

@@ -1,10 +1,8 @@
1 1
 # go-avbot - the aventer bot
2 2
 
3
-[![Build Status](https://travis-ci.org/AVENTER-UG/go-avbot.svg?branch=master)](https://travis-ci.org/AVENTER-UG/go-avbot)
4
-
5 3
 ## Github Repo
6 4
 
7
-[https://github.com/AVENTER-UG](https://github.com/AVENTER-UG)
5
+[https://git.aventer.biz/AVENTER](https://git.aventer.biz/AVENTER)
8 6
 
9 7
 ## Security
10 8
 
@@ -66,7 +64,7 @@ go get github.com/matrix-org/util
66 64
 go get github.com/mattn/go-sqlite3
67 65
 go get github.com/prometheus/client_golang/prometheus
68 66
 go get github.com/matrix-org/dugong
69
-go get github.com/AVENTER-UG/gomatrix
67
+go get git.aventer.biz/AVENTER/gomatrix
70 68
 go get github.com/mattn/go-shellwords
71 69
 go get gopkg.in/yaml.v2
72 70
 go get golang.org/x/oauth2
@@ -106,3 +104,6 @@ go get github.com/aws/aws-sdk-go
106 104
 - add wekan webhook support (is a fork of the travis version)
107 105
 
108 106
 ### v0.0.4
107
+
108
+- modify repo to git.aventer.biz
109
+- add gitea support

+ 1
- 1
api/handlers/auth.go View File

@@ -11,7 +11,7 @@ import (
11 11
 	"../../database"
12 12
 	"../../metrics"
13 13
 	"../../types"
14
-	"github.com/AVENTER-UG/util"
14
+	"git.aventer.biz/AVENTER/util"
15 15
 	log "github.com/sirupsen/logrus"
16 16
 )
17 17
 

+ 1
- 1
api/handlers/client.go View File

@@ -6,7 +6,7 @@ import (
6 6
 
7 7
 	"../../api"
8 8
 	"../../clients"
9
-	"github.com/AVENTER-UG/util"
9
+	"git.aventer.biz/AVENTER/util"
10 10
 )
11 11
 
12 12
 // ConfigureClient represents an HTTP handler capable of processing /admin/configureClient requests.

+ 1
- 1
api/handlers/heartbeat.go View File

@@ -11,7 +11,7 @@ package handlers
11 11
 import (
12 12
 	"net/http"
13 13
 
14
-	"github.com/AVENTER-UG/util"
14
+	"git.aventer.biz/AVENTER/util"
15 15
 )
16 16
 
17 17
 // Heartbeat implements the heartbeat API

+ 2
- 2
api/handlers/service.go View File

@@ -14,8 +14,8 @@ import (
14 14
 	"../../metrics"
15 15
 	"../../polling"
16 16
 	"../../types"
17
-	"github.com/AVENTER-UG/gomatrix"
18
-	"github.com/AVENTER-UG/util"
17
+	"git.aventer.biz/AVENTER/gomatrix"
18
+	"git.aventer.biz/AVENTER/util"
19 19
 	log "github.com/sirupsen/logrus"
20 20
 )
21 21
 

+ 1
- 1
app.go View File

@@ -22,7 +22,7 @@ import (
22 22
 	_ "./services/travisci"
23 23
 	_ "./services/wekan"
24 24
 	"./types"
25
-	"github.com/AVENTER-UG/util"
25
+	"git.aventer.biz/AVENTER/util"
26 26
 	"github.com/matrix-org/dugong"
27 27
 	_ "github.com/mattn/go-sqlite3"
28 28
 	"github.com/prometheus/client_golang/prometheus"

+ 1
- 1
clients/clients.go View File

@@ -13,7 +13,7 @@ import (
13 13
 	"../matrix"
14 14
 	"../metrics"
15 15
 	"../types"
16
-	"github.com/AVENTER-UG/gomatrix"
16
+	"git.aventer.biz/AVENTER/gomatrix"
17 17
 	shellwords "github.com/mattn/go-shellwords"
18 18
 	log "github.com/sirupsen/logrus"
19 19
 )

+ 1
- 1
clients/clients_test.go View File

@@ -6,7 +6,7 @@ import (
6 6
 	"reflect"
7 7
 	"testing"
8 8
 
9
-	"github.com/AVENTER-UG/gomatrix"
9
+	"git.aventer.biz/AVENTER/gomatrix"
10 10
 	"github.com/matrix-org/go-neb/database"
11 11
 	"github.com/matrix-org/go-neb/types"
12 12
 )

+ 1
- 1
matrix/matrix.go View File

@@ -5,7 +5,7 @@ import (
5 5
 
6 6
 	"../api"
7 7
 	"../database"
8
-	"github.com/AVENTER-UG/gomatrix"
8
+	"git.aventer.biz/AVENTER/gomatrix"
9 9
 	log "github.com/sirupsen/logrus"
10 10
 )
11 11
 

+ 1
- 1
services/aws/aws.go View File

@@ -10,7 +10,7 @@ import (
10 10
 	"../../types"
11 11
 	log "github.com/sirupsen/logrus"
12 12
 
13
-	"github.com/AVENTER-UG/gomatrix"
13
+	"git.aventer.biz/AVENTER/gomatrix"
14 14
 	"github.com/aws/aws-sdk-go/aws"
15 15
 )
16 16
 

+ 1
- 1
services/aws/instance.go View File

@@ -4,7 +4,7 @@ import (
4 4
 	"fmt"
5 5
 	"strings"
6 6
 
7
-	"github.com/AVENTER-UG/gomatrix"
7
+	"git.aventer.biz/AVENTER/gomatrix"
8 8
 	log "github.com/sirupsen/logrus"
9 9
 
10 10
 	"github.com/aws/aws-sdk-go/aws"

+ 1
- 1
services/echo/echo.go View File

@@ -5,7 +5,7 @@ import (
5 5
 	"strings"
6 6
 
7 7
 	"../../types"
8
-	"github.com/AVENTER-UG/gomatrix"
8
+	"git.aventer.biz/AVENTER/gomatrix"
9 9
 )
10 10
 
11 11
 // ServiceType of the Echo service

+ 185
- 0
services/gitea/gitea.go View File

@@ -0,0 +1,185 @@
1
+// Package gitea implements a Service capable of processing webhooks from Gitea
2
+package gitea
3
+
4
+import (
5
+	"encoding/json"
6
+	"io/ioutil"
7
+	"net/http"
8
+	"strings"
9
+
10
+	"../../database"
11
+	"../../types"
12
+
13
+	"git.aventer.biz/AVENTER/gomatrix"
14
+	log "github.com/sirupsen/logrus"
15
+)
16
+
17
+// ServiceType of the Gitea service.
18
+const ServiceType = "gitea"
19
+
20
+// DefaultTemplate contains the template that will be used if none is supplied.
21
+const DefaultTemplate = (`%{boardsitory}#%{build_number} (%{branch} - %{commit} : %{author}): %{message}
22
+	Change view : %{compare_url}
23
+	Build details : %{build_url}`)
24
+
25
+var httpClient = &http.Client{}
26
+
27
+// Service contains the Config fields for the Gitea service.
28
+//
29
+// This service will send notifications into a Matrix room when Gitea sends
30
+// webhook events to it. It requires a public domain which Gitea can reach.
31
+// Notices will be sent as the service user ID.
32
+//
33
+// Example JSON request:
34
+//   {
35
+//       rooms: {
36
+//           "!ewfug483gsfe:localhost": {
37
+//               boards: {
38
+//                   "1" {
39
+//                   }
40
+//               }
41
+//           }
42
+//       }
43
+//   }
44
+type Service struct {
45
+	types.DefaultService
46
+	webhookEndpointURL string
47
+	// The URL which should be added to .gitea.yml - Populated by Go-NEB after Service registration.
48
+	WebhookURL string `json:"webhook_url"`
49
+	// A map from Matrix room ID to Github-style owner/board boardsitories.
50
+	Rooms map[string]struct {
51
+		// A map of "boardID's" to configuration information
52
+		Boards map[string]struct {
53
+			Template string `json:"template"`
54
+		} `json:"boards"`
55
+	} `json:"rooms"`
56
+}
57
+
58
+// The payload from Gitea
59
+type webhookNotification struct {
60
+	ID          string `json:"cardId"`
61
+	Text        string `json:"text"`
62
+	ListID      string `json:"listId"`
63
+	OldListID   string `json:"oldListId"`
64
+	BoardID     string `json:"boardId"`
65
+	User        string `json:"user"`
66
+	Card        string `json:"card"`
67
+	Description string `json:"description"`
68
+}
69
+
70
+func outputForTemplate(giteaTmpl string, tmpl map[string]string) (out string) {
71
+	if giteaTmpl == "" {
72
+		giteaTmpl = DefaultTemplate
73
+	}
74
+	out = giteaTmpl
75
+	for tmplVar, tmplValue := range tmpl {
76
+		out = strings.Replace(out, "%{"+tmplVar+"}", tmplValue, -1)
77
+	}
78
+	return out
79
+}
80
+
81
+// OnReceiveWebhook receives requests from gitea and possibly sends requests to Matrix as a result.
82
+//
83
+// If the boardsitory matches a known gitea board, a notification will be formed from the
84
+// template for that boardsitory and a notice will be sent to Matrix.
85
+//
86
+// Go-AVBOT cannot register with gitea for webhooks automatically. The user must manually add the
87
+// webhook endpoint URL to their .gitea.yml file:
88
+//    notifications:
89
+//        webhooks: http://go-avbot-endpoint.com/gitea_webhook_service
90
+//
91
+func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli *gomatrix.Client) {
92
+	if err := req.ParseForm(); err != nil {
93
+		log.WithError(err).Error("Failed to read incoming Gitea webhook form")
94
+		w.WriteHeader(400)
95
+		return
96
+	}
97
+	payload, err := ioutil.ReadAll(req.Body)
98
+	if err != nil {
99
+		log.Error("Gitea webhook is missing payload= form value", err)
100
+		w.WriteHeader(400)
101
+		return
102
+	}
103
+
104
+	var notif webhookNotification
105
+	if err := json.Unmarshal([]byte(payload), &notif); err != nil {
106
+		log.WithError(err).Error("Gitea webhook received an invalid JSON payload=", payload)
107
+		w.WriteHeader(400)
108
+		return
109
+	}
110
+
111
+	whForBoard := notif.BoardID
112
+
113
+	logger := log.WithFields(log.Fields{
114
+		"board": whForBoard,
115
+	})
116
+
117
+	for roomID, roomData := range s.Rooms {
118
+		for boardData := range roomData.Boards {
119
+			if boardData != whForBoard {
120
+				continue
121
+			}
122
+			msg := gomatrix.TextMessage{
123
+				Body:    notif.Text,
124
+				MsgType: "m.notice",
125
+			}
126
+
127
+			logger.WithFields(log.Fields{
128
+				"message": msg,
129
+				"room_id": roomID,
130
+			}).Print("Sending Gitea notification to room")
131
+			if _, e := cli.SendMessageEvent(roomID, "m.room.message", msg); e != nil {
132
+				logger.WithError(e).WithField("room_id", roomID).Print(
133
+					"Failed to send Gitea notification to room.")
134
+			}
135
+		}
136
+	}
137
+	w.WriteHeader(200)
138
+}
139
+
140
+// Register makes sure the Config information supplied is valid.
141
+func (s *Service) Register(oldService types.Service, client *gomatrix.Client) error {
142
+	s.WebhookURL = s.webhookEndpointURL
143
+	log.Info("Gitea WebhookURL: ", s.WebhookURL)
144
+	s.joinRooms(client)
145
+	return nil
146
+}
147
+
148
+// PostRegister deletes this service if there are no registered boards.
149
+func (s *Service) PostRegister(oldService types.Service) {
150
+	for _, roomData := range s.Rooms {
151
+		for range roomData.Boards {
152
+			return // at least 1 board exists
153
+		}
154
+	}
155
+	// Delete this service since no boards are configured
156
+	logger := log.WithFields(log.Fields{
157
+		"service_type": s.ServiceType(),
158
+		"service_id":   s.ServiceID(),
159
+	})
160
+	logger.Info("Removing service as no boardsitories are registered.")
161
+	if err := database.GetServiceDB().DeleteService(s.ServiceID()); err != nil {
162
+		logger.WithError(err).Error("Failed to delete service")
163
+	}
164
+}
165
+
166
+func (s *Service) joinRooms(client *gomatrix.Client) {
167
+	for roomID := range s.Rooms {
168
+		if _, err := client.JoinRoom(roomID, "", nil); err != nil {
169
+			log.WithFields(log.Fields{
170
+				log.ErrorKey: err,
171
+				"room_id":    roomID,
172
+				"user_id":    client.UserID,
173
+			}).Error("Failed to join room")
174
+		}
175
+	}
176
+}
177
+
178
+func init() {
179
+	types.RegisterService(func(serviceID, serviceUserID, webhookEndpointURL string) types.Service {
180
+		return &Service{
181
+			DefaultService:     types.NewDefaultService(serviceID, serviceUserID, ServiceType),
182
+			webhookEndpointURL: webhookEndpointURL,
183
+		}
184
+	})
185
+}

+ 1
- 1
services/github/github.go View File

@@ -17,7 +17,7 @@ import (
17 17
 	"../../realms/github"
18 18
 	"../../services/github/client"
19 19
 	"../../types"
20
-	"github.com/AVENTER-UG/gomatrix"
20
+	"git.aventer.biz/AVENTER/gomatrix"
21 21
 	gogithub "github.com/google/go-github/github"
22 22
 	log "github.com/sirupsen/logrus"
23 23
 )

+ 1
- 1
services/github/github_webhook.go View File

@@ -11,7 +11,7 @@ import (
11 11
 	"../../services/github/client"
12 12
 	"../../services/github/webhook"
13 13
 	"../../types"
14
-	"github.com/AVENTER-UG/gomatrix"
14
+	"git.aventer.biz/AVENTER/gomatrix"
15 15
 	gogithub "github.com/google/go-github/github"
16 16
 	log "github.com/sirupsen/logrus"
17 17
 )

+ 2
- 2
services/github/webhook/webhook.go View File

@@ -11,8 +11,8 @@ import (
11 11
 	"net/http"
12 12
 	"strings"
13 13
 
14
-	"github.com/AVENTER-UG/gomatrix"
15
-	"github.com/AVENTER-UG/util"
14
+	"git.aventer.biz/AVENTER/gomatrix"
15
+	"git.aventer.biz/AVENTER/util"
16 16
 	"github.com/google/go-github/github"
17 17
 	log "github.com/sirupsen/logrus"
18 18
 )

+ 1
- 1
services/invoice/invoice.go View File

@@ -9,7 +9,7 @@ import (
9 9
 	"strings"
10 10
 
11 11
 	"../../types"
12
-	"github.com/AVENTER-UG/gomatrix"
12
+	"git.aventer.biz/AVENTER/gomatrix"
13 13
 	"github.com/russross/blackfriday"
14 14
 	log "github.com/sirupsen/logrus"
15 15
 )

+ 1
- 1
services/pentest/pentest.go View File

@@ -7,7 +7,7 @@ import (
7 7
 	"strings"
8 8
 
9 9
 	"../../types"
10
-	"github.com/AVENTER-UG/gomatrix"
10
+	"git.aventer.biz/AVENTER/gomatrix"
11 11
 	log "github.com/sirupsen/logrus"
12 12
 )
13 13
 

+ 1
- 1
services/travisci/travisci.go View File

@@ -13,7 +13,7 @@ import (
13 13
 	"../../database"
14 14
 	"../../types"
15 15
 
16
-	"github.com/AVENTER-UG/gomatrix"
16
+	"git.aventer.biz/AVENTER/gomatrix"
17 17
 	log "github.com/sirupsen/logrus"
18 18
 )
19 19
 

+ 1
- 1
services/wekan/wekan.go View File

@@ -10,7 +10,7 @@ import (
10 10
 	"../../database"
11 11
 	"../../types"
12 12
 
13
-	"github.com/AVENTER-UG/gomatrix"
13
+	"git.aventer.biz/AVENTER/gomatrix"
14 14
 	log "github.com/sirupsen/logrus"
15 15
 )
16 16
 

+ 1
- 1
types/service.go View File

@@ -8,7 +8,7 @@ import (
8 8
 	"strings"
9 9
 	"time"
10 10
 
11
-	"github.com/AVENTER-UG/gomatrix"
11
+	"git.aventer.biz/AVENTER/gomatrix"
12 12
 )
13 13
 
14 14
 // BotOptions for a given bot user in a given room

Loading…
Cancel
Save