Files
autogits/common/request_handler.go
Adam Majer f47da62481
All checks were successful
go-generate-check / go-generate-check (pull_request) Successful in 27s
common: replace legacy logger with standard impl
2026-01-31 10:18:14 +01:00

99 lines
2.7 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"
)
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 {
Request *Request
}
func (r *RequestHandler) WriteError() {
LogError("internal error sent")
}
func CreateRequestHandler() (*RequestHandler, error) {
var h *RequestHandler = new(RequestHandler)
return h, nil
}