autogits/bots-common/request_pr.go

128 lines
2.8 KiB
Go
Raw Normal View History

2024-07-09 12:06:24 +02:00
package common
2024-07-07 21:08:41 +02:00
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"
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 Head struct {
Ref string
2024-08-27 12:15:59 +02:00
Repo *Repository
2024-07-07 21:08:41 +02:00
Sha string
}
2024-08-26 12:51:58 +02:00
type IssueLabelDetail struct {
Id int
Name string
Exclusive bool
Is_archived bool
Color string
Description string
Url string
}
2024-07-07 21:08:41 +02:00
type PullRequest struct {
Id int
Url string
Number int
State string
2024-08-26 13:06:44 +02:00
Base Head
Head Head
Labels []IssueLabelDetail
Requested_reviewers []*User
2024-07-07 21:08:41 +02:00
2024-07-10 10:14:56 +02:00
User User
2024-07-07 21:08:41 +02:00
}
2024-08-25 22:47:44 +02:00
type PullRequestWebhookEvent struct {
2024-07-07 21:08:41 +02:00
Action string
Number int
2024-08-27 12:15:59 +02:00
Pull_Request *PullRequest
Repository *Repository
2024-08-26 17:07:52 +02:00
Requested_reviewer *User
2024-08-26 13:06:44 +02:00
Sender User
2024-07-07 21:08:41 +02:00
}
2024-08-28 00:45:47 +02:00
func (p *PullRequestWebhookEvent) GetAction() string {
return p.Action
}
2024-09-30 15:09:45 +02:00
func PullRequestLabelFromModel(labels []*models.Label) []IssueLabelDetail {
l := make([]IssueLabelDetail, len(labels))
for i := range labels {
l[i].Id = int(labels[i].ID)
l[i].Name = labels[i].Name
l[i].Exclusive = labels[i].Exclusive
l[i].Is_archived = labels[i].IsArchived
l[i].Color = labels[i].Color
l[i].Description = labels[i].Description
l[i].Url = labels[i].URL
}
return l
}
func PullRequestFromModel(pr *models.PullRequest) *PullRequest {
return &PullRequest{
Id: int(pr.ID),
Url: pr.URL,
Number: int(pr.Index),
State: string(pr.State),
Base: Head{
Ref: pr.Base.Ref,
Sha: pr.Base.Sha,
Repo: RepositoryFromModel(pr.Base.Repo),
},
Head: Head{
Ref: pr.Head.Ref,
Sha: pr.Head.Sha,
Repo: RepositoryFromModel(pr.Head.Repo),
},
Labels: PullRequestLabelFromModel(pr.Labels),
User: *UserFromModel(pr.User),
}
}
2024-08-28 17:20:09 +02:00
func (h *RequestHandler) parsePullRequest(data io.Reader) (action *PullRequestWebhookEvent, err error) {
2024-09-10 18:24:41 +02:00
action = new(PullRequestWebhookEvent)
2024-08-28 17:20:09 +02:00
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 json: %w", err)
2024-07-07 21:08:41 +02:00
}
2024-09-10 18:24:41 +02:00
h.Request = &Request{
2024-08-28 17:53:15 +02:00
Data: action,
Type: RequestType_PR,
}
2024-08-28 17:20:09 +02:00
return
2024-07-07 21:08:41 +02:00
}
2024-07-10 17:20:23 +02:00
2024-08-28 17:20:09 +02:00
func (h *RequestHandler) parsePullRequestSync(data io.Reader) (*PullRequestWebhookEvent, error) {
2024-07-10 17:20:23 +02:00
return h.parsePullRequest(data)
}