autogits/bots-common/request_push.go

71 lines
1.8 KiB
Go
Raw Normal View History

2024-07-07 21:08:41 +02:00
package common
2024-09-10 18:24:41 +02:00
/*
* This file is part of Autogits.
*
* Copyright © 2024 SUSE LLC
*
* Autogits is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* Autogits is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* Foobar. If not, see <https://www.gnu.org/licenses/>.
*/
2024-07-07 21:08:41 +02:00
import (
"encoding/json"
2024-08-28 17:20:09 +02:00
"fmt"
2024-07-07 21:08:41 +02:00
"io"
"strings"
)
type Commit struct {
Id string
Message string
}
2024-08-28 00:45:47 +02:00
type PushWebhookEvent struct {
2024-07-07 21:08:41 +02:00
Total_Commits int
Head_Commit Commit
Commits []Commit
Pusher User
Sender User
2024-08-27 12:15:59 +02:00
Repository *Repository
2024-07-07 21:08:41 +02:00
}
2024-08-28 00:45:47 +02:00
func (*PushWebhookEvent) GetAction() string {
return "push"
}
2024-08-28 17:20:09 +02:00
func (h *RequestHandler) parsePushRequest(data io.Reader) (*PushWebhookEvent, error) {
action := new(PushWebhookEvent)
err := json.NewDecoder(data).Decode(&action)
2024-07-07 21:08:41 +02:00
2024-08-28 17:20:09 +02:00
if err != nil {
return nil, fmt.Errorf("Got error while parsing: %w", err)
2024-07-07 21:08:41 +02:00
}
repoIdx := strings.LastIndex(action.Repository.Ssh_Url, "/")
if repoIdx == -1 || action.Repository.Ssh_Url[repoIdx+1:] != action.Repository.Name+".git" {
2024-08-28 17:20:09 +02:00
return nil, fmt.Errorf("Unexpected URL for SSH repository: '%s'", action.Repository.Name)
2024-07-07 21:08:41 +02:00
}
2024-08-24 13:32:39 +02:00
h.StdLogger.Printf("Request push for repo: %s\n", action.Repository.Full_Name)
2024-09-10 18:24:41 +02:00
h.Request = &Request{
2024-08-28 17:53:15 +02:00
Type: RequestType_Push,
Data: action,
}
2024-07-07 21:08:41 +02:00
if len(action.Commits) < 1 || len(action.Head_Commit.Id) != 64 {
2024-08-28 17:20:09 +02:00
return nil, fmt.Errorf("Request has no action .... skipping")
2024-07-07 21:08:41 +02:00
}
2024-08-28 17:20:09 +02:00
return action, nil
2024-07-07 21:08:41 +02:00
}