autogits/bots-common/request_handler.go
2024-08-27 17:55:03 +02:00

182 lines
3.9 KiB
Go

package common
import (
"encoding/json"
"fmt"
"log"
"os"
)
type Request struct {
Type string
Data interface{}
PrjGit string
}
func ParseRequestJSON(reqType string, data []byte) (interface{}, error) {
switch reqType {
case RequestType_CreateBrachTag, RequestType_DeleteBranchTag:
create := CreateWebhookEvent{}
if err := json.Unmarshal(data, &create); err != nil {
return nil, err
}
return create, nil
case RequestType_Fork:
fork := ForkWebhookEvent{}
if err := json.Unmarshal(data, &fork); err != nil {
return nil, err
}
return fork, nil
case RequestType_Push:
push := PushRequest{}
if err := json.Unmarshal(data, &push); err != nil {
return nil, err
}
return push, nil
case RequestType_Repository:
repoAction := RepositoryAction{}
if err := json.Unmarshal(data, &repoAction); err != nil {
return nil, err
}
return repoAction, nil
case RequestType_Release:
release := ReleaseWebhookEvent{}
if err := json.Unmarshal(data, &release); err != nil {
return nil, err
}
return release, nil
case RequestType_Issue:
issue := IssueWebhookEvent{}
if err := json.Unmarshal(data, &issue); err != nil {
return nil, err
}
return issue, nil
case RequestType_IssueAssign:
issue := IssueWebhookEvent{}
if err := json.Unmarshal(data, &issue); err != nil {
return nil, err
}
return issue, nil
case RequestType_IssueComment, RequestType_PRComment:
issue := IssueCommentWebhookEvent{}
if err := json.Unmarshal(data, &issue); err != nil {
return nil, err
}
return issue, nil
case RequestType_IssueLabel:
issue := IssueWebhookEvent{}
if err := json.Unmarshal(data, &issue); err != nil {
return nil, err
}
return issue, nil
case RequestType_IssueMilestone:
issue := IssueWebhookEvent{}
if err := json.Unmarshal(data, &issue); err != nil {
return nil, err
}
return issue, nil
case RequestType_PR:
pr := PullRequestWebhookEvent{}
if err := json.Unmarshal(data, &pr); err != nil {
return nil, err
}
return pr, nil
case RequestType_PRLabel:
pr := PullRequestWebhookEvent{}
if err := json.Unmarshal(data, &pr); err != nil {
return nil, err
}
return pr, nil
case RequestType_PRMilestone:
pr := PullRequestWebhookEvent{}
if err := json.Unmarshal(data, &pr); err != nil {
return nil, err
}
return pr, nil
case RequestType_PRAssign:
issue := PullRequestWebhookEvent{}
if err := json.Unmarshal(data, &issue); err != nil {
return nil, err
}
return issue, nil
case RequestType_PRReviewRequest:
issue := PullRequestWebhookEvent{}
if err := json.Unmarshal(data, &issue); err != nil {
return nil, err
}
return issue, nil
case RequestType_PRReviewAccepted, RequestType_PRReviewRejected:
pr := PullRequestWebhookEvent{}
if err := json.Unmarshal(data, &pr); err != nil {
return nil, err
}
return pr, nil
case RequestType_PRSync:
pr := PullRequestWebhookEvent{}
if err := json.Unmarshal(data, &pr); err != nil {
return nil, err
}
return pr, nil
case RequestType_Wiki:
wiki := WikiWebhookEvent{}
if err := json.Unmarshal(data, &wiki); err != nil {
return nil, err
}
return wiki, nil
}
return nil, fmt.Errorf("Unknown webhook request type: %s", reqType)
}
type RequestHandler struct {
Error error
GitCommiter string
GitPath string
Branch []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
}