autogits/bots-common/request_handler.go

115 lines
3.0 KiB
Go
Raw Normal View History

2024-07-07 21:08:41 +02:00
package common
2024-09-10 18:24:41 +02:00
/*
* 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/>.
*/
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-09-11 12:15:51 +02:00
case RequestType_PRReviewAccepted, RequestType_PRReviewRejected, RequestType_PRReviewComment:
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-28 17:20:09 +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-09-10 18:24:41 +02:00
// Git *GitHandler
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
}
2024-09-04 14:04:13 +02:00
func CreateRequestHandler() (*RequestHandler, error) {
2024-07-07 21:08:41 +02:00
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
2024-09-10 18:24:41 +02:00
/* var err error
h.Git, err = CreateGitHandler(git_author, name)
if err != nil {
return nil, err
}
2024-09-04 14:04:13 +02:00
*/
2024-08-28 17:20:09 +02:00
return h, nil
2024-07-07 21:08:41 +02:00
}