This commit is contained in:
Adam Majer 2024-07-22 17:35:48 +02:00
parent 6d650d68b1
commit 18bfc87a1c
3 changed files with 51 additions and 19 deletions

View File

@ -30,21 +30,20 @@ func (h *RequestHandler) allocateGiteaTransport() (*transport.Runtime, *apiclien
const ( const (
// from Gitea // from Gitea
// ReviewStateApproved pr is approved // ReviewStateApproved pr is approved
ReviewStateApproved models.ReviewStateType = "APPROVED" ReviewStateApproved models.ReviewStateType = "APPROVED"
// ReviewStatePending pr state is pending // ReviewStatePending pr state is pending
ReviewStatePending models.ReviewStateType = "PENDING" ReviewStatePending models.ReviewStateType = "PENDING"
// ReviewStateComment is a comment review // ReviewStateComment is a comment review
ReviewStateComment models.ReviewStateType = "COMMENT" ReviewStateComment models.ReviewStateType = "COMMENT"
// ReviewStateRequestChanges changes for pr are requested // ReviewStateRequestChanges changes for pr are requested
ReviewStateRequestChanges models.ReviewStateType = "REQUEST_CHANGES" ReviewStateRequestChanges models.ReviewStateType = "REQUEST_CHANGES"
// ReviewStateRequestReview review is requested from user // ReviewStateRequestReview review is requested from user
ReviewStateRequestReview models.ReviewStateType = "REQUEST_REVIEW" ReviewStateRequestReview models.ReviewStateType = "REQUEST_REVIEW"
// ReviewStateUnknown state of pr is unknown // ReviewStateUnknown state of pr is unknown
ReviewStateUnknown models.ReviewStateType = "" ReviewStateUnknown models.ReviewStateType = ""
) )
func (h *RequestHandler) GetPullRequestAndReviews(org, project string, num int64) (*models.PullRequest, []*models.PullReview, error) { func (h *RequestHandler) GetPullRequestAndReviews(org, project string, num int64) (*models.PullRequest, []*models.PullReview, error) {
if h.HasError() { if h.HasError() {
return nil, nil, h.Error return nil, nil, h.Error
@ -56,12 +55,12 @@ func (h *RequestHandler) GetPullRequestAndReviews(org, project string, num int64
WithDefaults(). WithDefaults().
WithOwner(org). WithOwner(org).
WithRepo(project). WithRepo(project).
WithIndex(num), WithIndex(num),
transport.DefaultAuthentication, transport.DefaultAuthentication,
) )
if err != nil { if err != nil {
h.Error = err; h.Error = err
h.LogPlainError(err) h.LogPlainError(err)
return nil, nil, err return nil, nil, err
} }
@ -86,7 +85,7 @@ func (h *RequestHandler) GetPullRequestAndReviews(org, project string, num int64
return pr.Payload, reviews.Payload, nil return pr.Payload, reviews.Payload, nil
} }
func (h *RequestHandler) GetNotifications(since *time.Time) ([]*models.NotificationThread, error){ func (h *RequestHandler) GetNotifications(since *time.Time) ([]*models.NotificationThread, error) {
if h.HasError() { if h.HasError() {
return nil, h.Error return nil, h.Error
} }
@ -95,14 +94,14 @@ func (h *RequestHandler) GetNotifications(since *time.Time) ([]*models.Notificat
transport, client := h.allocateGiteaTransport() transport, client := h.allocateGiteaTransport()
params := notification.NewNotifyGetListParams(). params := notification.NewNotifyGetListParams().
WithDefaults(). WithDefaults().
WithLimit(&bigLimit) WithLimit(&bigLimit)
if since != nil { if since != nil {
s := strfmt.DateTime(*since) s := strfmt.DateTime(*since)
params.SetSince(&s) params.SetSince(&s)
} }
list, err := client.Notification.NotifyGetList(params, transport.DefaultAuthentication) list, err := client.Notification.NotifyGetList(params, transport.DefaultAuthentication)
if err != nil { if err != nil {
h.Error = err h.Error = err
@ -335,3 +334,31 @@ func (h *RequestHandler) GetAssociatedPrjGitPR(pr *PullRequestAction) *models.Pu
return nil return nil
} }
func (h *RequestHandler) GetRepositoryFileContent(pr *models.PullRequest, path string) ([]byte, error) {
if h.HasError() {
return nil, h.Error
}
transport, client := h.allocateGiteaTransport()
repo := pr.Head.Repo
file, err := client.Repository.RepoGetRawFile(
repository.NewRepoGetRawFileParams().
WithOwner(repo.Owner.UserName).
WithRepo(repo.Name).
WithFilepath(path).
WithRef(&pr.Head.Ref),
transport.DefaultAuthentication,
)
if err != nil {
return nil, err
}
if !file.IsSuccess() {
return nil, fmt.Errorf("Invalid response from server (%d): %s", file.Code(), file.Error())
}
return file.Body(), nil
}

View File

@ -14,6 +14,8 @@ type ObsClient struct {
client *http.Client client *http.Client
user, password string user, password string
cookie string cookie string
HomeProject string
} }
func NewObsClient(host string) (*ObsClient, error) { func NewObsClient(host string) (*ObsClient, error) {
@ -27,6 +29,8 @@ func NewObsClient(host string) (*ObsClient, error) {
client: &http.Client{}, client: &http.Client{},
user: obsUser, user: obsUser,
password: obsPassword, password: obsPassword,
HomeProject: fmt.Sprintf("home:%s", obsUser),
}, nil }, nil
} }

View File

@ -70,6 +70,7 @@ func processPullNotification(h *common.RequestHandler, notification *models.Noti
switch review.State { switch review.State {
case common.ReviewStateUnknown, common.ReviewStateRequestReview: case common.ReviewStateUnknown, common.ReviewStateRequestReview:
// create build project, if doesn't exist, and add it to pending requests // create build project, if doesn't exist, and add it to pending requests
case common.ReviewStatePending: case common.ReviewStatePending:
// waiting for build results // waiting for build results