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-27 17:55:03 +02:00
|
|
|
Error error
|
|
|
|
GitCommiter string
|
|
|
|
GitPath string
|
2024-08-28 00:45:47 +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-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
|
|
|
}
|
|
|
|
|
|
|
|
func CreateRequestHandler(git_author, name string) *RequestHandler {
|
|
|
|
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
|
|
|
|
|
|
|
h.GitCommiter = git_author
|
|
|
|
h.GitPath, h.Error = os.MkdirTemp("", name)
|
|
|
|
if h.Error != nil {
|
2024-08-24 13:32:39 +02:00
|
|
|
h.ErrLogger.Printf("Cannot create temp dir: %v\n", h.Error)
|
2024-07-07 21:08:41 +02:00
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.Error = os.Chmod(h.GitPath, 0700); h.Error != nil {
|
2024-08-24 13:32:39 +02:00
|
|
|
h.ErrLogger.Printf("Cannot fix permissions of temp dir: %v\n", h.Error)
|
2024-07-07 21:08:41 +02:00
|
|
|
return h
|
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|