mirror of
https://github.com/harness/drone.git
synced 2025-05-06 12:41:08 +08:00

This change introduces the concept of a principal (abstraction of call identity), and adds a new service account type principal. Also adds support for different tokens (session, PAT, SAT, OAuth2) and adds auth.Session which is being used to capture information about the caller and call method.
144 lines
3.4 KiB
Go
144 lines
3.4 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"
|
|
)
|
|
|
|
// ParseAsInt64 tries to retrieve the parameter from the request and parse it to in64.
|
|
func ParseAsInt64(r *http.Request, paramName string) (int64, error) {
|
|
rawID := chi.URLParam(r, paramName)
|
|
if rawID == "" {
|
|
return 0, fmt.Errorf("parameter '%s' not found in request", paramName)
|
|
}
|
|
|
|
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),
|
|
}
|
|
}
|