autogits/bots-common/request_repo.go
2024-09-10 18:24:41 +02:00

91 lines
2.2 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 Repository struct {
Id uint
Name string
Full_Name string
Fork bool
Parent *Repository
Owner *Organization
Clone_Url string
Ssh_Url string
Default_Branch string
Object_Format_Name string
}
type Organization struct {
Id uint
Username string
}
type User struct {
Id uint
Username string
}
type RepositoryWebhookEvent struct {
Action string
Sender *User
Organization *Organization
Repository *Repository
PrjGit string
}
func (r *RepositoryWebhookEvent) GetAction() string {
return r.Action
}
// TODO: sanity check values!!!!
func (h *RequestHandler) parseRepositoryRequest(dataReader io.Reader) (data *RepositoryWebhookEvent, err error) {
data = new(RepositoryWebhookEvent)
if err = json.NewDecoder(dataReader).Decode(&data); err != nil {
return nil, err
}
repoIdx := strings.LastIndex(data.Repository.Ssh_Url, "/")
if repoIdx == -1 || data.Repository.Ssh_Url[repoIdx+1:] != data.Repository.Name+".git" {
return nil, fmt.Errorf("Unexpected URL for SSH repository: %s", data.Repository.Name)
}
data.PrjGit = data.Repository.Ssh_Url[:repoIdx+1] + DefaultGitPrj + ".git"
h.StdLogger.Printf("Request '%s' for repo: %s\n", data.Action, data.Repository.Full_Name)
if len(data.Action) < 1 {
return nil, fmt.Errorf("Request has no data.... skipping")
}
h.Request = &Request{
Data: data,
Type: RequestType_Repository,
}
return data, nil
}