This repository was archived by the owner on Nov 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgitTrackerGotify.go
More file actions
95 lines (85 loc) · 2.69 KB
/
gitTrackerGotify.go
File metadata and controls
95 lines (85 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Copyright (c) 2020-present unTill Pro, Ltd. and Contributors
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package main
import (
"log"
"net/http"
"net/url"
"path"
"time"
"github.com/gotify/go-api-client/v2/auth"
"github.com/gotify/go-api-client/v2/client"
"github.com/gotify/go-api-client/v2/client/application"
"github.com/gotify/go-api-client/v2/client/message"
"github.com/gotify/go-api-client/v2/gotify"
"github.com/gotify/go-api-client/v2/models"
gc "github.com/untillpro/gochips"
)
var (
gToken string
gURL string
)
type gitTrackerGotify struct {
notfications chan string
not map[string]chan string
}
// note: websocket stream
func (wcn *gitTrackerGotify) GetLastCommit(repoURL string, repoPath string) (lastCommit string, ok bool) {
gotifyClient := getGotifyClient(gURL)
authInfo := auth.TokenAuth(gToken)
appsResponse, err := gotifyClient.Application.GetApps(nil, authInfo)
gc.PanicIfError(err)
app := findApp(appsResponse.Payload, repoURL)
if app == nil {
// create application
createAppParams := application.NewCreateAppParams().WithBody(&models.Application{
Name: repoURL,
Description: "Created by cder " + time.Now().Format(time.RFC3339),
})
createAppResponse, err := gotifyClient.Application.CreateApp(createAppParams, authInfo)
gc.PanicIfError(err)
app = createAppResponse.Payload
printPushVerCommand(app)
}
appMessagesParams := message.NewGetAppMessagesParams()
appMessagesParams.ID = int64(app.ID)
limit := int64(1)
appMessagesParams.Limit = &limit
appMessagesResponse, err := gotifyClient.Message.GetAppMessages(appMessagesParams, authInfo)
gc.PanicIfError(err)
ok = len(appMessagesResponse.Payload.Messages) != 0
if ok {
lastCommit = appMessagesResponse.Payload.Messages[0].Title
}
return
}
func getGotifyClient(rawURL string) *client.GotifyREST {
parsedURL, err := url.Parse(rawURL)
gc.PanicIfError(err)
return gotify.NewClient(parsedURL, &http.Client{})
}
func findApp(apps []*models.Application, appName string) *models.Application {
for _, app := range apps {
if app.Name == appName {
return app
}
}
return nil
}
func printPushVerCommand(app *models.Application) {
// $ curl "https://push.example.de/message?token=<apptoken>" -F "title=<version>" -F "message=<url>"
parsedURL, err := url.Parse(gURL)
gc.PanicIfError(err)
parsedURL.Path = path.Join(parsedURL.Path, "message")
{
pq, err := url.ParseQuery(parsedURL.RawQuery)
gc.PanicIfError(err)
pq.Add("token", app.Token)
parsedURL.RawQuery = pq.Encode()
}
log.Println("Command for push versions: curl \"" + parsedURL.String() + "\" -F \"title=<version>\" -F \"message=<url>\"")
}