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.GitCommiter = git_author h.Git.GitPath, err = os.MkdirTemp("", name) if err != nil { return nil, fmt.Errorf("Cannot create temp dir: %w", err) } if err = os.Chmod(h.Git.GitPath, 0700); err != nil { return nil, fmt.Errorf("Cannot fix permissions of temp dir: %w", err) } return h, nil }