autogits/bots-common/request_handler.go
2024-09-12 16:40:43 +02:00

111 lines
2.9 KiB
Go

package common
/*
* This file is part of Autogits.
*
* Copyright © 2024 SUSE LLC
*
* Autogits is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* Autogits is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* Foobar. If not, see <https://www.gnu.org/licenses/>.
*/
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, RequestType_PRReviewComment:
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 {
StdLogger, ErrLogger *log.Logger
Request *Request
}
func (r *RequestHandler) WriteError() {
r.ErrLogger.Println("internal error sent")
}
func CreateRequestHandler() (*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
}