Files
autogits/common/request_push.go
Adam Majer f47da62481
All checks were successful
go-generate-check / go-generate-check (pull_request) Successful in 27s
common: replace legacy logger with standard impl
2026-01-31 10:18:14 +01:00

76 lines
1.9 KiB
Go

package common
/*
* 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/>.
*/
import (
"encoding/json"
"fmt"
"io"
"strings"
)
type Commit struct {
Id string
Message string
Added []string
Removed []string
Modified []string
}
type PushWebhookEvent struct {
Ref string
Total_Commits int
Head_Commit Commit
Commits []Commit
Pusher User
Sender User
Repository *Repository
}
func (*PushWebhookEvent) GetAction() string {
return "push"
}
func (h *RequestHandler) ParsePushRequest(data io.Reader) (*PushWebhookEvent, error) {
action := new(PushWebhookEvent)
err := json.NewDecoder(data).Decode(&action)
if err != nil {
return nil, fmt.Errorf("Got error while parsing: %w", err)
}
repoIdx := strings.LastIndex(action.Repository.Ssh_Url, "/")
if repoIdx == -1 || action.Repository.Ssh_Url[repoIdx+1:] != action.Repository.Name+".git" {
return nil, fmt.Errorf("Unexpected URL for SSH repository: '%s'", action.Repository.Name)
}
LogInfo("Request push for repo:", action.Repository.Full_Name)
h.Request = &Request{
Type: RequestType_Push,
Data: action,
}
if len(action.Commits) < 1 || len(action.Head_Commit.Id) != 64 {
return nil, fmt.Errorf("Request has no action .... skipping")
}
return action, nil
}