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 { Error error GitCommiter string GitPath string Branch string PrjGit string StdLogger, ErrLogger *log.Logger Request *Request } func (r *RequestHandler) WriteError() { r.ErrLogger.Println("internal error sent") } func CreateRequestHandler(git_author, name string) *RequestHandler { var h *RequestHandler = new(RequestHandler) h.StdLogger, h.ErrLogger = CreateStdoutLogger(os.Stdout, os.Stderr) h.GitCommiter = git_author h.GitPath, h.Error = os.MkdirTemp("", name) if h.Error != nil { h.ErrLogger.Printf("Cannot create temp dir: %v\n", h.Error) return h } if h.Error = os.Chmod(h.GitPath, 0700); h.Error != nil { h.ErrLogger.Printf("Cannot fix permissions of temp dir: %v\n", h.Error) return h } return h }