Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add admin API email endpoints #22792

Merged
merged 15 commits into from
Mar 14, 2023
Merged
3 changes: 3 additions & 0 deletions modules/structs/user_email.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2015 The Gogs Authors. All rights reserved.
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package structs
Expand All @@ -9,6 +10,8 @@ type Email struct {
Email string `json:"email"`
Verified bool `json:"verified"`
Primary bool `json:"primary"`
UserID int64 `json:"user_id"`
UserName string `json:"username"`
}

// CreateEmailOption options when creating email addresses
Expand Down
48 changes: 48 additions & 0 deletions routers/api/v1/admin/email.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package admin

import (
"net/http"

user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/context"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/routers/api/v1/utils"
"code.gitea.io/gitea/services/convert"
)

// GetAllEmails
func GetAllEmails(ctx *context.APIContext) {
listOptions := utils.GetListOptions(ctx)

emails, maxResults, err := user_model.SearchEmails(&user_model.SearchEmailOptions{
Keyword: ctx.Params(":email"),
ListOptions: listOptions,
})
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetAllEmails", err)
return
}

results := make([]*api.Email, len(emails))
for i := range emails {
results[i] = convert.ToEmailSearch(emails[i])
}

ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
ctx.SetTotalCountHeader(maxResults)
ctx.JSON(http.StatusOK, &results)
}

// GetEmail
func GetEmail(ctx *context.APIContext) {
GetAllEmails(ctx)
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
}

// SearchEmail
func SearchEmail(ctx *context.APIContext) {
ctx.SetParams(":email", ctx.FormTrim("q"))
GetAllEmails(ctx)
}
7 changes: 7 additions & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,13 @@ func Routes(ctx gocontext.Context) *web.Route {
m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo)
}, context_service.UserAssignmentAPI())
})
m.Group("/emails", func() {
m.Get("", admin.GetAllEmails)
m.Get("/search", admin.SearchEmail)
m.Group("/{email}", func() {
m.Get("", admin.GetEmail)
})
})
m.Group("/unadopted", func() {
m.Get("", admin.ListUnadoptedRepositories)
m.Post("/{username}/{reponame}", admin.AdoptRepository)
Expand Down
11 changes: 11 additions & 0 deletions services/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ func ToEmail(email *user_model.EmailAddress) *api.Email {
}
}

// ToEmail convert models.EmailAddress to api.Email
func ToEmailSearch(email *user_model.SearchEmailResult) *api.Email {
return &api.Email{
Email: email.Email,
Verified: email.IsActivated,
Primary: email.IsPrimary,
UserID: email.UID,
UserName: email.Name,
}
}

// ToBranch convert a git.Commit and git.Branch to an api.Branch
func ToBranch(ctx context.Context, repo *repo_model.Repository, b *git.Branch, c *git.Commit, bp *git_model.ProtectedBranch, user *user_model.User, isRepoAdmin bool) (*api.Branch, error) {
if bp == nil {
Expand Down
9 changes: 9 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -16943,6 +16943,15 @@
"type": "boolean",
"x-go-name": "Primary"
},
"user_id": {
"type": "integer",
"format": "int64",
"x-go-name": "UserID"
},
"username": {
"type": "string",
"x-go-name": "UserName"
},
"verified": {
"type": "boolean",
"x-go-name": "Verified"
Expand Down