autogits/bots-common/request_pr.go

81 lines
1.5 KiB
Go
Raw Normal View History

2024-07-09 12:06:24 +02:00
package common
2024-07-07 21:08:41 +02:00
import (
"encoding/json"
"io"
"strings"
)
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-08-25 22:47:44 +02:00
func (h *RequestHandler) parsePullRequest(data io.Reader) *PullRequestWebhookEvent {
2024-07-07 21:08:41 +02:00
if h.HasError() {
return nil
}
2024-08-25 22:47:44 +02:00
var action PullRequestWebhookEvent
2024-07-07 21:08:41 +02:00
h.Error = json.NewDecoder(data).Decode(&action)
if h.HasError() {
2024-08-24 13:32:39 +02:00
h.ErrLogger.Printf("Got error while parsing: %v\n", h.Error)
2024-07-07 21:08:41 +02:00
return nil
}
repoIdx := strings.LastIndex(action.Repository.Ssh_Url, "/")
if repoIdx == -1 || action.Repository.Ssh_Url[repoIdx+1:] != action.Repository.Name+".git" {
2024-08-24 13:32:39 +02:00
h.ErrLogger.Printf("Unexpected URL for SSH repository: '%s'\n", action.Repository.Name)
h.ErrLogger.Printf("%#v\n", action)
2024-07-07 21:08:41 +02:00
return nil
}
2024-08-28 00:45:47 +02:00
h.Request.Data = &action
2024-07-07 21:08:41 +02:00
// sanity checks on request
return &action
}
2024-07-10 17:20:23 +02:00
2024-08-25 22:47:44 +02:00
func (h *RequestHandler) parsePullRequestSync(data io.Reader) *PullRequestWebhookEvent {
2024-07-10 17:20:23 +02:00
return h.parsePullRequest(data)
}