autogits/bots-common/request_pr.go
2024-08-28 00:45:47 +02:00

81 lines
1.5 KiB
Go

package common
import (
"encoding/json"
"io"
"strings"
)
type Head struct {
Ref string
Repo *Repository
Sha string
}
type IssueLabelDetail struct {
Id int
Name string
Exclusive bool
Is_archived bool
Color string
Description string
Url string
}
type PullRequest struct {
Id int
Url string
Number int
State string
Base Head
Head Head
Labels []IssueLabelDetail
Requested_reviewers []*User
User User
}
type PullRequestWebhookEvent struct {
Action string
Number int
Pull_Request *PullRequest
Repository *Repository
Requested_reviewer *User
Sender User
}
func (p *PullRequestWebhookEvent) GetAction() string {
return p.Action
}
func (h *RequestHandler) parsePullRequest(data io.Reader) *PullRequestWebhookEvent {
if h.HasError() {
return nil
}
var action PullRequestWebhookEvent
h.Error = json.NewDecoder(data).Decode(&action)
if h.HasError() {
h.ErrLogger.Printf("Got error while parsing: %v\n", h.Error)
return nil
}
repoIdx := strings.LastIndex(action.Repository.Ssh_Url, "/")
if repoIdx == -1 || action.Repository.Ssh_Url[repoIdx+1:] != action.Repository.Name+".git" {
h.ErrLogger.Printf("Unexpected URL for SSH repository: '%s'\n", action.Repository.Name)
h.ErrLogger.Printf("%#v\n", action)
return nil
}
h.Request.Data = &action
// sanity checks on request
return &action
}
func (h *RequestHandler) parsePullRequestSync(data io.Reader) *PullRequestWebhookEvent {
return h.parsePullRequest(data)
}