mirror of
https://github.com/harness/drone.git
synced 2025-05-05 17:49:08 +08:00

This change adds the initial stepping stones for harness integration: - Authentication: JWT/PAT/SAT support - Authorization: ACL integration (acl currently denies requests as gitness hasn't been integrated yet) - Remote Clients for Token, User, ServiceAccount, ACL - User Integration: Syncs harness users during authentication if unknown - SA integration: syncs harness service accounts during authentication if unknown - Initial harness API: THIS WILL BE CHANGED IN THE FUTURE! - single harness subpackage (all marked with harness build flag) - harness & standalone wire + make build commands
155 lines
3.8 KiB
Go
155 lines
3.8 KiB
Go
// Copyright 2021 Harness Inc. All rights reserved.
|
|
// Use of this source code is governed by the Polyform Free Trial License
|
|
// that can be found in the LICENSE.md file for this repository.
|
|
|
|
package request
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/go-chi/chi"
|
|
"github.com/harness/gitness/types"
|
|
"github.com/harness/gitness/types/enum"
|
|
)
|
|
|
|
// ParamOrError tries to retrieve the parameter from the request and
|
|
// returns the parameter if it exists and is not empty, otherwise returns an error.
|
|
func ParamOrError(r *http.Request, paramName string) (string, error) {
|
|
value := chi.URLParam(r, paramName)
|
|
if value == "" {
|
|
return "", fmt.Errorf("parameter '%s' not found in request", paramName)
|
|
}
|
|
|
|
return value, nil
|
|
}
|
|
|
|
// ParseAsInt64 tries to retrieve the parameter from the request and parse it to in64.
|
|
func ParseAsInt64(r *http.Request, paramName string) (int64, error) {
|
|
rawID, err := ParamOrError(r, paramName)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
id, err := strconv.ParseInt(rawID, 10, 64)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("failed to parse value '%s' of parameter '%s' to int64: %w", rawID, paramName, err)
|
|
}
|
|
|
|
return id, nil
|
|
}
|
|
|
|
// ParsePage extracts the page parameter from the url.
|
|
func ParsePage(r *http.Request) int {
|
|
s := r.FormValue("page")
|
|
i, _ := strconv.Atoi(s)
|
|
if i == 0 {
|
|
i = 1
|
|
}
|
|
return i
|
|
}
|
|
|
|
// ParseSize extracts the size parameter from the url.
|
|
func ParseSize(r *http.Request) int {
|
|
const itemsPerPage = 100
|
|
s := r.FormValue("per_page")
|
|
i, _ := strconv.Atoi(s)
|
|
if i == 0 {
|
|
i = itemsPerPage
|
|
} else if i > itemsPerPage {
|
|
i = itemsPerPage
|
|
}
|
|
return i
|
|
}
|
|
|
|
// ParseOrder extracts the order parameter from the url.
|
|
func ParseOrder(r *http.Request) enum.Order {
|
|
return enum.ParseOrder(
|
|
r.FormValue("direction"),
|
|
)
|
|
}
|
|
|
|
// ParseSort extracts the sort parameter from the url.
|
|
func ParseSort(r *http.Request) string {
|
|
return r.FormValue("sort")
|
|
}
|
|
|
|
// ParseSortUser extracts the user sort parameter from the url.
|
|
func ParseSortUser(r *http.Request) enum.UserAttr {
|
|
return enum.ParseUserAttr(
|
|
r.FormValue("sort"),
|
|
)
|
|
}
|
|
|
|
// ParseSortSpace extracts the space sort parameter from the url.
|
|
func ParseSortSpace(r *http.Request) enum.SpaceAttr {
|
|
return enum.ParseSpaceAttr(
|
|
r.FormValue("sort"),
|
|
)
|
|
}
|
|
|
|
// ParseSortRepo extracts the repo sort parameter from the url.
|
|
func ParseSortRepo(r *http.Request) enum.RepoAttr {
|
|
return enum.ParseRepoAtrr(
|
|
r.FormValue("sort"),
|
|
)
|
|
}
|
|
|
|
// ParseSortPath extracts the path sort parameter from the url.
|
|
func ParseSortPath(r *http.Request) enum.PathAttr {
|
|
return enum.ParsePathAttr(
|
|
r.FormValue("sort"),
|
|
)
|
|
}
|
|
|
|
// ParseParams extracts the query parameter from the url.
|
|
func ParseParams(r *http.Request) types.Params {
|
|
return types.Params{
|
|
Order: ParseOrder(r),
|
|
Page: ParsePage(r),
|
|
Sort: ParseSort(r),
|
|
Size: ParseSize(r),
|
|
}
|
|
}
|
|
|
|
// ParseUserFilter extracts the user query parameter from the url.
|
|
func ParseUserFilter(r *http.Request) *types.UserFilter {
|
|
return &types.UserFilter{
|
|
Order: ParseOrder(r),
|
|
Page: ParsePage(r),
|
|
Sort: ParseSortUser(r),
|
|
Size: ParseSize(r),
|
|
}
|
|
}
|
|
|
|
// ParseSpaceFilter extracts the space query parameter from the url.
|
|
func ParseSpaceFilter(r *http.Request) *types.SpaceFilter {
|
|
return &types.SpaceFilter{
|
|
Order: ParseOrder(r),
|
|
Page: ParsePage(r),
|
|
Sort: ParseSortSpace(r),
|
|
Size: ParseSize(r),
|
|
}
|
|
}
|
|
|
|
// ParseRepoFilter extracts the repository query parameter from the url.
|
|
func ParseRepoFilter(r *http.Request) *types.RepoFilter {
|
|
return &types.RepoFilter{
|
|
Order: ParseOrder(r),
|
|
Page: ParsePage(r),
|
|
Sort: ParseSortRepo(r),
|
|
Size: ParseSize(r),
|
|
}
|
|
}
|
|
|
|
// ParsePathFilter extracts the path query parameter from the url.
|
|
func ParsePathFilter(r *http.Request) *types.PathFilter {
|
|
return &types.PathFilter{
|
|
Order: ParseOrder(r),
|
|
Page: ParsePage(r),
|
|
Sort: ParseSortPath(r),
|
|
Size: ParseSize(r),
|
|
}
|
|
}
|