handle system events

This commit is contained in:
Martin/Geno 2019-03-09 00:09:45 +01:00
parent a0493bd805
commit bef47512bc
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
4 changed files with 38 additions and 7 deletions

View File

@ -46,5 +46,6 @@ deploy:
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
- ssh -6 -o StrictHostKeyChecking=no -p $SSH_PORT "$CI_PROJECT_NAME@$SSH_HOST" sudo /usr/bin/systemctl stop $CI_PROJECT_NAME
- scp -6 -o StrictHostKeyChecking=no -P $SSH_PORT "/go/bin/$CI_PROJECT_NAME" "$CI_PROJECT_NAME@$SSH_HOST":/opt/$CI_PROJECT_NAME/bin
- ssh -6 -o StrictHostKeyChecking=no -p $SSH_PORT "$CI_PROJECT_NAME@$SSH_HOST" sudo /usr/bin/systemctl restart $CI_PROJECT_NAME
- ssh -6 -o StrictHostKeyChecking=no -p $SSH_PORT "$CI_PROJECT_NAME@$SSH_HOST" sudo /usr/bin/systemctl start $CI_PROJECT_NAME

View File

@ -1,7 +1,10 @@
package gitlab
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
@ -89,6 +92,27 @@ func init() {
err = libHTTP.Read(r, &pl)
msg = pl.String()
case SystemEvents:
var data map[string]interface{}
var buf bytes.Buffer
tee := io.TeeReader(r.Body, &buf)
if err = json.NewDecoder(tee).Decode(&data); err != nil {
msg = fmt.Sprintf("unable to decode gitlab system event")
} else if event, ok := data["event_name"]; ok {
switch event {
case "push":
var pl PushEventPayload
err = json.NewDecoder(&buf).Decode(&pl)
msg = fmt.Sprintf("[S]%s", pl.String())
default:
err = nil
msg = fmt.Sprintf("unknown gitlab system event '%s' received", event)
}
} else {
err = nil
msg = fmt.Sprintf("unable to get 'event_name' of gitlab '%s'", gitLabEvent)
}
default:
err = nil
msg = fmt.Sprintf("unknown gitlab event '%s' received", gitLabEvent)

View File

@ -19,6 +19,7 @@ const (
WikiPageEvents Event = "Wiki Page Hook"
PipelineEvents Event = "Pipeline Hook"
BuildEvents Event = "Build Hook"
SystemEvents Event = "System Hook"
)
type customTime struct {
@ -91,6 +92,7 @@ func (pl *MergeRequestEventPayload) String() string {
// PushEventPayload contains the information for GitLab's push event
type PushEventPayload struct {
EventName string `json:"event_name"`
ObjectKind string `json:"object_kind"`
Before string `json:"before"`
After string `json:"after"`
@ -98,10 +100,11 @@ type PushEventPayload struct {
CheckoutSHA string `json:"checkout_sha"`
UserID int64 `json:"user_id"`
UserName string `json:"user_name"`
UserUsername string `json:"user_username"`
UserEmail string `json:"user_email"`
UserAvatar string `json:"user_avatar"`
ProjectID int64 `json:"project_id"`
Project Project `json:"Project"`
Project Project `json:"project"`
Repository Repository `json:"repository"`
Commits []Commit `json:"commits"`
TotalCommitsCount int64 `json:"total_commits_count"`
@ -335,6 +338,9 @@ type Repository struct {
URL string `json:"url"`
Description string `json:"description"`
Homepage string `json:"homepage"`
GitSSSHURL string `json:"git_ssh_url"`
GitHTTPURL string `json:"git_http_url"`
VisibilityLevel int64 `json:"visibility_level"`
}
// ObjectAttributes contains all of the GitLab object attributes information

View File

@ -62,7 +62,7 @@ func main() {
Addr: config.WebserverBind,
}
go func() {
if err := srv.ListenAndServe(); err != nil {
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
panic(err)
}
}()