autogits/bots-common/request_repo.go

131 lines
3.0 KiB
Go
Raw Permalink 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"
"fmt"
"io"
"strings"
2024-09-30 15:09:45 +02:00
"src.opensuse.org/autogits/common/gitea-generated/models"
2024-07-07 21:08:41 +02:00
)
type Repository struct {
Id uint
Name string
Full_Name string
Fork bool
Parent *Repository
2024-07-09 23:22:42 +02:00
Owner *Organization
2024-07-07 21:08:41 +02:00
Clone_Url string
Ssh_Url string
Default_Branch string
Object_Format_Name string
}
type Organization struct {
Id uint
Username string
}
type User struct {
2024-09-11 12:17:35 +02:00
Id int
2024-07-07 21:08:41 +02:00
Username string
}
2024-08-28 00:45:47 +02:00
type RepositoryWebhookEvent struct {
2024-07-07 21:08:41 +02:00
Action string
2024-08-24 21:39:50 +02:00
Sender *User
Organization *Organization
Repository *Repository
2024-07-07 21:08:41 +02:00
PrjGit string
}
2024-09-30 15:09:45 +02:00
func UserFromModel(user *models.User) *User {
return &User {
Id: int(user.ID),
Username: user.UserName,
}
}
func UsersFromModel(users []*models.User) []*User {
u := make([]*User, len(users))
for i := range users {
u[i] = UserFromModel(users[i])
}
return u
}
func RepositoryFromModel(repo *models.Repository) *Repository {
if repo == nil {
return nil
}
return &Repository{
Id: uint(repo.ID),
Name: repo.Name,
Full_Name: repo.FullName,
Fork: repo.Fork,
Parent: RepositoryFromModel(repo.Parent),
Owner: &Organization{
Id: uint(repo.Owner.ID),
Username: repo.Owner.UserName,
},
Clone_Url: repo.CloneURL,
Ssh_Url: repo.SSHURL,
Default_Branch: repo.DefaultBranch,
Object_Format_Name: repo.ObjectFormatName,
}
}
2024-08-28 00:45:47 +02:00
func (r *RepositoryWebhookEvent) GetAction() string {
return r.Action
}
2024-07-07 21:08:41 +02:00
// TODO: sanity check values!!!!
2024-08-28 17:20:09 +02:00
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
2024-07-07 21:08:41 +02:00
}
repoIdx := strings.LastIndex(data.Repository.Ssh_Url, "/")
if repoIdx == -1 || data.Repository.Ssh_Url[repoIdx+1:] != data.Repository.Name+".git" {
2024-08-28 17:53:15 +02:00
return nil, fmt.Errorf("Unexpected URL for SSH repository: %s", data.Repository.Name)
2024-07-07 21:08:41 +02:00
}
data.PrjGit = data.Repository.Ssh_Url[:repoIdx+1] + DefaultGitPrj + ".git"
2024-08-24 13:32:39 +02:00
h.StdLogger.Printf("Request '%s' for repo: %s\n", data.Action, data.Repository.Full_Name)
2024-07-07 21:08:41 +02:00
if len(data.Action) < 1 {
2024-08-28 17:20:09 +02:00
return nil, fmt.Errorf("Request has no data.... skipping")
2024-07-07 21:08:41 +02:00
}
2024-09-10 18:24:41 +02:00
h.Request = &Request{
2024-08-30 14:31:10 +02:00
Data: data,
Type: RequestType_Repository,
}
2024-08-28 17:20:09 +02:00
return data, nil
2024-07-07 21:08:41 +02:00
}