autogits/bots-common/request_handler.go
2024-09-02 17:27:23 +02:00

96 lines
2.3 KiB
Go

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