-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gateway: add api to get commit status deliveries
add an api to get commit status deliveries by project ref.
- Loading branch information
1 parent
e83156f
commit e515f66
Showing
7 changed files
with
481 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2023 Sorint.lab | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package action | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/sorintlab/errors" | ||
|
||
"agola.io/agola/internal/util" | ||
"agola.io/agola/services/notification/client" | ||
nstypes "agola.io/agola/services/notification/types" | ||
) | ||
|
||
type GetProjectCommitStatusDeliveriesRequest struct { | ||
ProjectRef string | ||
DeliveryStatusFilter []string | ||
|
||
Cursor string | ||
|
||
Limit int | ||
SortDirection SortDirection | ||
} | ||
|
||
type GetProjectCommitStatusDeliveriesResponse struct { | ||
CommitStatusDeliveries []*nstypes.CommitStatusDelivery | ||
Cursor string | ||
} | ||
|
||
func (h *ActionHandler) GetProjectCommitStatusDeliveries(ctx context.Context, req *GetProjectCommitStatusDeliveriesRequest) (*GetProjectCommitStatusDeliveriesResponse, error) { | ||
project, _, err := h.configstoreClient.GetProject(ctx, req.ProjectRef) | ||
if err != nil { | ||
return nil, util.NewAPIError(util.KindFromRemoteError(err), err) | ||
} | ||
isUserOwner, err := h.IsAuthUserProjectOwner(ctx, project.OwnerType, project.OwnerID) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to determine permissions") | ||
} | ||
if !isUserOwner { | ||
return nil, util.NewAPIError(util.ErrForbidden, errors.Errorf("user not authorized")) | ||
} | ||
|
||
inCursor := &StartSequenceCursor{} | ||
sortDirection := req.SortDirection | ||
if req.Cursor != "" { | ||
if err := UnmarshalCursor(req.Cursor, inCursor); err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
sortDirection = inCursor.SortDirection | ||
} | ||
|
||
commitStatusDeliveries, resp, err := h.notificationClient.GetProjectCommitStatusDeliveries(ctx, project.ID, &client.GetProjectCommitStatusDeliveriesOptions{ListOptions: &client.ListOptions{Limit: req.Limit, SortDirection: nstypes.SortDirection(sortDirection)}, StartSequence: inCursor.StartSequence, DeliveryStatusFilter: req.DeliveryStatusFilter}) | ||
if err != nil { | ||
return nil, util.NewAPIError(util.KindFromRemoteError(err), err) | ||
} | ||
|
||
var outCursor string | ||
if resp.HasMore && len(commitStatusDeliveries) > 0 { | ||
lastCommitStatusDeliverySequence := commitStatusDeliveries[len(commitStatusDeliveries)-1].Sequence | ||
outCursor, err = MarshalCursor(&StartSequenceCursor{StartSequence: lastCommitStatusDeliverySequence}) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
} | ||
|
||
res := &GetProjectCommitStatusDeliveriesResponse{ | ||
CommitStatusDeliveries: commitStatusDeliveries, | ||
Cursor: outCursor, | ||
} | ||
|
||
return res, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright 2023 Sorint.lab | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/rs/zerolog" | ||
"github.com/sorintlab/errors" | ||
|
||
"agola.io/agola/internal/services/gateway/action" | ||
util "agola.io/agola/internal/util" | ||
gwapitypes "agola.io/agola/services/gateway/api/types" | ||
nstypes "agola.io/agola/services/notification/types" | ||
) | ||
|
||
func createCommitStatusDeliveryResponse(r *nstypes.CommitStatusDelivery) *gwapitypes.CommitStatusDeliveryResponse { | ||
commitStatusDelivery := &gwapitypes.CommitStatusDeliveryResponse{ | ||
ID: r.ID, | ||
Sequence: r.Sequence, | ||
DeliveryStatus: gwapitypes.DeliveryStatus(r.DeliveryStatus), | ||
DeliveredAt: r.DeliveredAt, | ||
} | ||
return commitStatusDelivery | ||
} | ||
|
||
type ProjectCommitStatusDeliveries struct { | ||
log zerolog.Logger | ||
ah *action.ActionHandler | ||
} | ||
|
||
func NewProjectCommitStatusDeliveriesHandler(log zerolog.Logger, ah *action.ActionHandler) *ProjectCommitStatusDeliveries { | ||
return &ProjectCommitStatusDeliveries{log: log, ah: ah} | ||
} | ||
|
||
func (h *ProjectCommitStatusDeliveries) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
res, err := h.do(w, r) | ||
if util.HTTPError(w, err) { | ||
h.log.Err(err).Send() | ||
return | ||
} | ||
|
||
if err := util.HTTPResponse(w, http.StatusOK, res); err != nil { | ||
h.log.Err(err).Send() | ||
} | ||
} | ||
|
||
func (h *ProjectCommitStatusDeliveries) do(w http.ResponseWriter, r *http.Request) ([]*gwapitypes.CommitStatusDeliveryResponse, error) { | ||
ctx := r.Context() | ||
query := r.URL.Query() | ||
|
||
vars := mux.Vars(r) | ||
projectRef := vars["projectref"] | ||
|
||
deliveryStatusFilter := query["deliverystatus"] | ||
|
||
ropts, err := parseRequestOptions(r) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
areq := &action.GetProjectCommitStatusDeliveriesRequest{ | ||
ProjectRef: projectRef, | ||
|
||
DeliveryStatusFilter: deliveryStatusFilter, | ||
Cursor: ropts.Cursor, | ||
Limit: ropts.Limit, | ||
SortDirection: action.SortDirection(ropts.SortDirection), | ||
} | ||
ares, err := h.ah.GetProjectCommitStatusDeliveries(ctx, areq) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
commitStatusDeliveries := make([]*gwapitypes.CommitStatusDeliveryResponse, len(ares.CommitStatusDeliveries)) | ||
for i, r := range ares.CommitStatusDeliveries { | ||
commitStatusDeliveries[i] = createCommitStatusDeliveryResponse(r) | ||
} | ||
|
||
addCursorHeader(w, ares.Cursor) | ||
|
||
return commitStatusDeliveries, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2023 Sorint.lab | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package types | ||
|
||
import "time" | ||
|
||
type CommitStatusDeliveryResponse struct { | ||
ID string `json:"id"` | ||
Sequence uint64 `json:"sequence"` | ||
DeliveryStatus DeliveryStatus `json:"delivery_status"` | ||
DeliveredAt *time.Time `json:"delivered_at"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.