autogits/bots-common/request_handler.go

96 lines
2.3 KiB
Go
Raw Normal View History

2024-07-07 21:08:41 +02:00
package common
2024-08-24 13:32:39 +02:00
import (
2024-08-27 17:55:03 +02:00
"encoding/json"
"fmt"
2024-08-24 13:32:39 +02:00
"log"
"os"
)
2024-07-07 21:08:41 +02:00
2024-08-28 00:45:47 +02:00
type RequestType interface {
GetAction() string
}
2024-07-07 21:08:41 +02:00
type Request struct {
Type string
2024-08-28 00:45:47 +02:00
Data RequestType
2024-07-07 21:08:41 +02:00
}
2024-08-28 00:45:47 +02:00
func ParseRequestJSON(reqType string, data []byte) (req *Request, err error) {
req = &Request{
Type: reqType,
}
2024-08-27 17:55:03 +02:00
switch reqType {
case RequestType_CreateBrachTag, RequestType_DeleteBranchTag:
2024-08-28 00:45:47 +02:00
req.Data = &CreateWebhookEvent{}
2024-08-27 17:55:03 +02:00
case RequestType_Fork:
2024-08-28 00:45:47 +02:00
req.Data = &ForkWebhookEvent{}
2024-08-27 17:55:03 +02:00
case RequestType_Push:
2024-08-28 00:45:47 +02:00
req.Data = &PushWebhookEvent{}
2024-08-27 17:55:03 +02:00
case RequestType_Repository:
2024-08-28 00:45:47 +02:00
req.Data = &RepositoryWebhookEvent{}
2024-08-27 17:55:03 +02:00
case RequestType_Release:
2024-08-28 00:45:47 +02:00
req.Data = &ReleaseWebhookEvent{}
2024-08-27 17:55:03 +02:00
case RequestType_Issue:
2024-08-28 00:45:47 +02:00
req.Data = &IssueWebhookEvent{}
2024-08-27 17:55:03 +02:00
case RequestType_IssueAssign:
2024-08-28 00:45:47 +02:00
req.Data = &IssueWebhookEvent{}
2024-08-27 17:55:03 +02:00
case RequestType_IssueComment, RequestType_PRComment:
2024-08-28 00:45:47 +02:00
req.Data = &IssueCommentWebhookEvent{}
2024-08-27 17:55:03 +02:00
case RequestType_IssueLabel:
2024-08-28 00:45:47 +02:00
req.Data = &IssueWebhookEvent{}
2024-08-27 17:55:03 +02:00
case RequestType_IssueMilestone:
2024-08-28 00:45:47 +02:00
req.Data = &IssueWebhookEvent{}
2024-08-27 17:55:03 +02:00
case RequestType_PR:
2024-08-28 00:45:47 +02:00
req.Data = &PullRequestWebhookEvent{}
2024-08-27 17:55:03 +02:00
case RequestType_PRLabel:
2024-08-28 00:45:47 +02:00
req.Data = &PullRequestWebhookEvent{}
2024-08-27 17:55:03 +02:00
case RequestType_PRMilestone:
2024-08-28 00:45:47 +02:00
req.Data = &PullRequestWebhookEvent{}
2024-08-27 17:55:03 +02:00
case RequestType_PRAssign:
2024-08-28 00:45:47 +02:00
req.Data = &PullRequestWebhookEvent{}
2024-08-27 17:55:03 +02:00
case RequestType_PRReviewRequest:
2024-08-28 00:45:47 +02:00
req.Data = &PullRequestWebhookEvent{}
2024-08-27 17:55:03 +02:00
case RequestType_PRReviewAccepted, RequestType_PRReviewRejected:
2024-08-28 00:45:47 +02:00
req.Data = &PullRequestWebhookEvent{}
2024-08-27 17:55:03 +02:00
case RequestType_PRSync:
2024-08-28 00:45:47 +02:00
req.Data = &PullRequestWebhookEvent{}
2024-08-27 17:55:03 +02:00
case RequestType_Wiki:
2024-08-28 00:45:47 +02:00
req.Data = &WikiWebhookEvent{}
default:
return nil, fmt.Errorf("Unknown webhook request type: %s", reqType)
2024-08-27 17:55:03 +02:00
}
2024-08-28 00:45:47 +02:00
if err := json.Unmarshal(data, req.Data); err != nil {
return nil, err
}
return req, nil
2024-08-27 17:55:03 +02:00
}
2024-07-07 21:08:41 +02:00
type RequestHandler struct {
2024-08-28 17:20:09 +02:00
Branch string
PrjGit string
2024-07-07 21:08:41 +02:00
2024-08-27 17:55:03 +02:00
StdLogger, ErrLogger *log.Logger
2024-08-28 00:45:47 +02:00
Request *Request
2024-09-02 17:27:23 +02:00
Git *GitHandler
2024-07-07 21:08:41 +02:00
}
func (r *RequestHandler) WriteError() {
2024-08-24 13:32:39 +02:00
r.ErrLogger.Println("internal error sent")
2024-07-07 21:08:41 +02:00
}
2024-09-02 17:27:23 +02:00
func CreateRequestHandler(git_author, name string) (*RequestHandler, error) {
2024-07-07 21:08:41 +02:00
var h *RequestHandler = new(RequestHandler)
2024-08-24 13:32:39 +02:00
h.StdLogger, h.ErrLogger = CreateStdoutLogger(os.Stdout, os.Stderr)
2024-07-07 21:08:41 +02:00
2024-08-28 17:20:09 +02:00
var err error
2024-09-02 17:27:23 +02:00
h.Git, err = CreateGitHandler(git_author, name)
2024-08-28 17:20:09 +02:00
if err != nil {
2024-09-02 17:27:23 +02:00
return nil, err
2024-07-07 21:08:41 +02:00
}
2024-08-28 17:20:09 +02:00
return h, nil
2024-07-07 21:08:41 +02:00
}