Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
453 changes: 358 additions & 95 deletions api/jiaozifs.gen.go

Large diffs are not rendered by default.

46 changes: 34 additions & 12 deletions api/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,20 @@ components:
schema:
type: string

PaginationAfter:
PaginationBranchAfter:
in: query
name: after
description: return items after this value
schema:
type: string

PaginationRepoAfter:
in: query
name: after
description: return items after this value
schema:
type: string
format: date-time

PaginationAmount:
in: query
Expand Down Expand Up @@ -186,6 +194,18 @@ components:
properties:
Description:
type: string
RepositoryList:
type: object
required:
- pagination
- results
properties:
pagination:
$ref: "#/components/schemas/Pagination"
results:
type: array
items:
$ref: "#/components/schemas/Repository"
Repository:
type: object
required:
Expand Down Expand Up @@ -1293,20 +1313,16 @@ paths:
operationId: listRepository
summary: list repository in specific owner
parameters:
- in: query
name: repoPrefix
required: false
schema:
type: string
- $ref: "#/components/parameters/PaginationPrefix"
- $ref: "#/components/parameters/PaginationRepoAfter"
- $ref: "#/components/parameters/PaginationAmount"
responses:
200:
description: repository list
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/Repository"
$ref: "#/components/schemas/RepositoryList"
400:
description: ValidationError
401:
Expand All @@ -1320,15 +1336,17 @@ paths:
- repo
operationId: listRepositoryOfAuthenticatedUser"
summary: list repository
parameters:
- $ref: "#/components/parameters/PaginationPrefix"
- $ref: "#/components/parameters/PaginationRepoAfter"
- $ref: "#/components/parameters/PaginationAmount"
responses:
200:
description: list repository
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/Repository"
$ref: "#/components/schemas/RepositoryList"
400:
description: ValidationError
401:
Expand Down Expand Up @@ -1379,6 +1397,10 @@ paths:
- branches
operationId: listBranches
summary: list branches
parameters:
- $ref: "#/components/parameters/PaginationPrefix"
- $ref: "#/components/parameters/PaginationBranchAfter"
- $ref: "#/components/parameters/PaginationAmount"
responses:
200:
description: branch list
Expand Down
2 changes: 1 addition & 1 deletion config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var defaultCfg = Config{
LoginCookieNames []string `mapstructure:"login_cookie_names"`
LogoutURL string `mapstructure:"logout_url"`
}{RBAC: AuthRBACSimplified,
LoginURL: "api/v1/login",
LoginURL: "api/v1/auth/login",
LoginFailedMessage: "",
LoginCookieNames: nil,
LogoutURL: "auth/logout",
Expand Down
45 changes: 35 additions & 10 deletions controller/branch_ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/jiaozifs/jiaozifs/api"
"github.com/jiaozifs/jiaozifs/auth"
"github.com/jiaozifs/jiaozifs/models"
"github.com/jiaozifs/jiaozifs/utils"
"github.com/jiaozifs/jiaozifs/utils/hash"
"go.uber.org/fx"
)
Expand Down Expand Up @@ -47,7 +48,7 @@ type BranchController struct {
Repo models.IRepo
}

func (bct BranchController) ListBranches(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, ownerName string, repositoryName string) {
func (bct BranchController) ListBranches(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, ownerName string, repositoryName string, params api.ListBranchesParams) {
operator, err := auth.GetOperator(ctx)
if err != nil {
w.Error(err)
Expand All @@ -60,25 +61,39 @@ func (bct BranchController) ListBranches(ctx context.Context, w *api.JiaozifsRes
return
}

if operator.Name != owner.Name {
w.Forbidden()
return
}

repository, err := bct.Repo.RepositoryRepo().Get(ctx, models.NewGetRepoParams().SetName(repositoryName).SetOwnerID(owner.ID))
if err != nil {
w.Error(err)
return
}

if operator.Name != owner.Name {
w.Forbidden()
return
listBranchParams := models.NewListBranchParams()
if params.Prefix != nil && len(*params.Prefix) > 0 {
listBranchParams.SetName(*params.Prefix, models.PrefixMatch)
}
if params.After != nil && len(*params.After) > 0 {
listBranchParams.SetAfter(*params.After)
}
pageAmount := utils.IntValue(params.Amount)
if pageAmount > utils.DefaultMaxPerPage || pageAmount <= 0 {
listBranchParams.SetAmount(utils.DefaultMaxPerPage)
} else {
listBranchParams.SetAmount(pageAmount)
}

branches, err := bct.Repo.BranchRepo().List(ctx, models.NewListBranchParams().SetRepositoryID(repository.ID))
branches, hasMore, err := bct.Repo.BranchRepo().List(ctx, listBranchParams.SetRepositoryID(repository.ID))
if err != nil {
w.Error(err)
return
}
var apiBranches []api.Branch
results := make([]api.Branch, 0, len(branches))
for _, branch := range branches {
branch := api.Branch{
r := api.Branch{
CommitHash: branch.CommitHash.Hex(),
CreatedAt: branch.CreatedAt,
CreatorID: branch.CreatorID,
Expand All @@ -88,9 +103,19 @@ func (bct BranchController) ListBranches(ctx context.Context, w *api.JiaozifsRes
RepositoryID: branch.RepositoryID,
UpdatedAt: branch.UpdatedAt,
}
apiBranches = append(apiBranches, branch)
}
w.JSON(api.BranchList{Results: apiBranches})
results = append(results, r)
}
pagMag := utils.PaginationFor(hasMore, results, "Name")
pagination := api.Pagination{
HasMore: pagMag.HasMore,
MaxPerPage: pagMag.MaxPerPage,
NextOffset: pagMag.NextOffset,
Results: pagMag.Results,
}
w.JSON(api.BranchList{
Pagination: pagination,
Results: results,
})
}

func (bct BranchController) CreateBranch(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, body api.CreateBranchJSONRequestBody, ownerName string, repositoryName string) {
Expand Down
87 changes: 79 additions & 8 deletions controller/repository_ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,57 @@ type RepositoryController struct {
PublicStorageConfig params.AdapterConfig
}

func (repositoryCtl RepositoryController) ListRepositoryOfAuthenticatedUser(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request) {
func (repositoryCtl RepositoryController) ListRepositoryOfAuthenticatedUser(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, params api.ListRepositoryOfAuthenticatedUserParams) {
operator, err := auth.GetOperator(ctx)
if err != nil {
w.Error(err)
return
}

repositories, err := repositoryCtl.Repo.RepositoryRepo().List(ctx, models.NewListRepoParams().SetOwnerID(operator.ID)) //operator is owner
listRepoParams := models.NewListRepoParams()
if params.Prefix != nil && len(*params.Prefix) > 0 {
listRepoParams.SetName(*params.Prefix, models.PrefixMatch)
}
if params.After != nil {
listRepoParams.SetAfter(*params.After)
}
pageAmount := utils.IntValue(params.Amount)
if pageAmount > utils.DefaultMaxPerPage || pageAmount <= 0 {
listRepoParams.SetAmount(utils.DefaultMaxPerPage)
} else {
listRepoParams.SetAmount(pageAmount)
}

repositories, hasMore, err := repositoryCtl.Repo.RepositoryRepo().List(ctx, listRepoParams.
SetOwnerID(operator.ID))
if err != nil {
w.Error(err)
return
}
w.JSON(repositories)
results := make([]api.Repository, 0, len(repositories))
for _, repo := range repositories {
r := api.Repository{
CreatedAt: repo.CreatedAt,
CreatorID: repo.CreatorID,
Description: repo.Description,
Head: repo.HEAD,
ID: repo.ID,
Name: repo.Name,
UpdatedAt: repo.UpdatedAt,
}
results = append(results, r)
}
pagMag := utils.PaginationFor(hasMore, results, "UpdatedAt")
pagination := api.Pagination{
HasMore: pagMag.HasMore,
MaxPerPage: pagMag.MaxPerPage,
NextOffset: pagMag.NextOffset,
Results: pagMag.Results,
}
w.JSON(api.RepositoryList{
Pagination: pagination,
Results: results,
})
}

func (repositoryCtl RepositoryController) ListRepository(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, ownerName string, params api.ListRepositoryParams) {
Expand All @@ -88,16 +126,49 @@ func (repositoryCtl RepositoryController) ListRepository(ctx context.Context, w
return
}

listParams := models.NewListRepoParams().SetOwnerID(owner.ID)
if params.RepoPrefix != nil && len(*params.RepoPrefix) > 0 {
listParams.SetName(*params.RepoPrefix, models.PrefixMatch)
listRepoParams := models.NewListRepoParams().SetOwnerID(owner.ID)
if params.Prefix != nil && len(*params.Prefix) > 0 {
listRepoParams.SetName(*params.Prefix, models.PrefixMatch)
}
repositories, err := repositoryCtl.Repo.RepositoryRepo().List(ctx, listParams)
if params.After != nil {
listRepoParams.SetAfter(*params.After)
}
pageAmount := utils.IntValue(params.Amount)
if pageAmount > utils.DefaultMaxPerPage || pageAmount <= 0 {
listRepoParams.SetAmount(utils.DefaultMaxPerPage)
} else {
listRepoParams.SetAmount(pageAmount)
}

repositories, hasMore, err := repositoryCtl.Repo.RepositoryRepo().List(ctx, listRepoParams)
if err != nil {
w.Error(err)
return
}
w.JSON(repositories)
results := make([]api.Repository, 0, len(repositories))
for _, repo := range repositories {
r := api.Repository{
CreatedAt: repo.CreatedAt,
CreatorID: repo.CreatorID,
Description: repo.Description,
Head: repo.HEAD,
ID: repo.ID,
Name: repo.Name,
UpdatedAt: repo.UpdatedAt,
}
results = append(results, r)
}
pagMag := utils.PaginationFor(hasMore, results, "UpdatedAt")
pagination := api.Pagination{
HasMore: pagMag.HasMore,
MaxPerPage: pagMag.MaxPerPage,
NextOffset: pagMag.NextOffset,
Results: pagMag.Results,
}
w.JSON(api.RepositoryList{
Pagination: pagination,
Results: results,
})
}

func (repositoryCtl RepositoryController) CreateRepository(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, body api.CreateRepositoryJSONRequestBody) {
Expand Down
Loading