* USER APIFormat -> ToUser * Migrate more and mark APIFormat deprecated * models.Comment APIFormat() -> convert.ToComment * models.Release APIFormat() -> convert.ToRelease * models.Attachments APIFormat() -> convert.ToReleaseAttachments * models.CommitStatus APIFormat() -> convert.ToCommitStatus * finish migration to convert.ToUser * Move Test * Imprufe Test * fix test Co-authored-by: techknowlogick <techknowlogick@gitea.io>tags/v1.15.0-dev
"testing" | "testing" | ||||
"code.gitea.io/gitea/models" | "code.gitea.io/gitea/models" | ||||
"code.gitea.io/gitea/modules/convert" | |||||
api "code.gitea.io/gitea/modules/structs" | api "code.gitea.io/gitea/modules/structs" | ||||
"github.com/stretchr/testify/assert" | "github.com/stretchr/testify/assert" | ||||
DecodeJSON(t, resp, &apiComment) | DecodeJSON(t, resp, &apiComment) | ||||
assert.NoError(t, comment.LoadPoster()) | assert.NoError(t, comment.LoadPoster()) | ||||
expect := comment.APIFormat() | |||||
expect := convert.ToComment(comment) | |||||
assert.Equal(t, expect.ID, apiComment.ID) | assert.Equal(t, expect.ID, apiComment.ID) | ||||
assert.Equal(t, expect.Poster.FullName, apiComment.Poster.FullName) | assert.Equal(t, expect.Poster.FullName, apiComment.Poster.FullName) |
"time" | "time" | ||||
"code.gitea.io/gitea/models" | "code.gitea.io/gitea/models" | ||||
"code.gitea.io/gitea/modules/convert" | |||||
api "code.gitea.io/gitea/modules/structs" | api "code.gitea.io/gitea/modules/structs" | ||||
"github.com/stretchr/testify/assert" | "github.com/stretchr/testify/assert" | ||||
DecodeJSON(t, resp, &apiReactions) | DecodeJSON(t, resp, &apiReactions) | ||||
expectResponse := make(map[int]api.Reaction) | expectResponse := make(map[int]api.Reaction) | ||||
expectResponse[0] = api.Reaction{ | expectResponse[0] = api.Reaction{ | ||||
User: user2.APIFormat(), | |||||
User: convert.ToUser(user2, true, true), | |||||
Reaction: "eyes", | Reaction: "eyes", | ||||
Created: time.Unix(1573248003, 0), | Created: time.Unix(1573248003, 0), | ||||
} | } | ||||
DecodeJSON(t, resp, &apiReactions) | DecodeJSON(t, resp, &apiReactions) | ||||
expectResponse := make(map[int]api.Reaction) | expectResponse := make(map[int]api.Reaction) | ||||
expectResponse[0] = api.Reaction{ | expectResponse[0] = api.Reaction{ | ||||
User: user2.APIFormat(), | |||||
User: convert.ToUser(user2, true, true), | |||||
Reaction: "laugh", | Reaction: "laugh", | ||||
Created: time.Unix(1573248004, 0), | Created: time.Unix(1573248004, 0), | ||||
} | } | ||||
expectResponse[1] = api.Reaction{ | expectResponse[1] = api.Reaction{ | ||||
User: user1.APIFormat(), | |||||
User: convert.ToUser(user1, true, true), | |||||
Reaction: "laugh", | Reaction: "laugh", | ||||
Created: time.Unix(1573248005, 0), | Created: time.Unix(1573248005, 0), | ||||
} | } |
"code.gitea.io/gitea/modules/setting" | "code.gitea.io/gitea/modules/setting" | ||||
"code.gitea.io/gitea/modules/storage" | "code.gitea.io/gitea/modules/storage" | ||||
api "code.gitea.io/gitea/modules/structs" | |||||
"code.gitea.io/gitea/modules/timeutil" | "code.gitea.io/gitea/modules/timeutil" | ||||
gouuid "github.com/google/uuid" | gouuid "github.com/google/uuid" | ||||
return nil | return nil | ||||
} | } | ||||
// APIFormat converts models.Attachment to api.Attachment | |||||
func (a *Attachment) APIFormat() *api.Attachment { | |||||
return &api.Attachment{ | |||||
ID: a.ID, | |||||
Name: a.Name, | |||||
Created: a.CreatedUnix.AsTime(), | |||||
DownloadCount: a.DownloadCount, | |||||
Size: a.Size, | |||||
UUID: a.UUID, | |||||
DownloadURL: a.DownloadURL(), | |||||
} | |||||
} | |||||
// AttachmentRelativePath returns the relative path | // AttachmentRelativePath returns the relative path | ||||
func AttachmentRelativePath(uuid string) string { | func AttachmentRelativePath(uuid string) string { | ||||
return path.Join(uuid[0:1], uuid[1:2], uuid) | return path.Join(uuid[0:1], uuid[1:2], uuid) |
setting.AppURL, status.Repo.FullName(), status.SHA) | setting.AppURL, status.Repo.FullName(), status.SHA) | ||||
} | } | ||||
// APIFormat assumes some fields assigned with values: | |||||
// Required - Repo, Creator | |||||
func (status *CommitStatus) APIFormat() *api.Status { | |||||
_ = status.loadRepo(x) | |||||
apiStatus := &api.Status{ | |||||
Created: status.CreatedUnix.AsTime(), | |||||
Updated: status.CreatedUnix.AsTime(), | |||||
State: api.StatusState(status.State), | |||||
TargetURL: status.TargetURL, | |||||
Description: status.Description, | |||||
ID: status.Index, | |||||
URL: status.APIURL(), | |||||
Context: status.Context, | |||||
} | |||||
if status.Creator != nil { | |||||
apiStatus.Creator = status.Creator.APIFormat() | |||||
} | |||||
return apiStatus | |||||
} | |||||
// CalcCommitStatus returns commit status state via some status, the commit statues should order by id desc | // CalcCommitStatus returns commit status state via some status, the commit statues should order by id desc | ||||
func CalcCommitStatus(statuses []*CommitStatus) *CommitStatus { | func CalcCommitStatus(statuses []*CommitStatus) *CommitStatus { | ||||
var lastStatus *CommitStatus | var lastStatus *CommitStatus |
"code.gitea.io/gitea/modules/markup/markdown" | "code.gitea.io/gitea/modules/markup/markdown" | ||||
"code.gitea.io/gitea/modules/references" | "code.gitea.io/gitea/modules/references" | ||||
"code.gitea.io/gitea/modules/structs" | "code.gitea.io/gitea/modules/structs" | ||||
api "code.gitea.io/gitea/modules/structs" | |||||
"code.gitea.io/gitea/modules/timeutil" | "code.gitea.io/gitea/modules/timeutil" | ||||
"github.com/unknwon/com" | "github.com/unknwon/com" | ||||
return c.Issue.HTMLURL() | return c.Issue.HTMLURL() | ||||
} | } | ||||
// APIFormat converts a Comment to the api.Comment format | |||||
func (c *Comment) APIFormat() *api.Comment { | |||||
return &api.Comment{ | |||||
ID: c.ID, | |||||
Poster: c.Poster.APIFormat(), | |||||
HTMLURL: c.HTMLURL(), | |||||
IssueURL: c.IssueURL(), | |||||
PRURL: c.PRURL(), | |||||
Body: c.Content, | |||||
Created: c.CreatedUnix.AsTime(), | |||||
Updated: c.UpdatedUnix.AsTime(), | |||||
} | |||||
} | |||||
// CommentHashTag returns unique hash tag for comment id. | // CommentHashTag returns unique hash tag for comment id. | ||||
func CommentHashTag(id int64) string { | func CommentHashTag(id int64) string { | ||||
return fmt.Sprintf("issuecomment-%d", id) | return fmt.Sprintf("issuecomment-%d", id) |
"code.gitea.io/gitea/modules/setting" | "code.gitea.io/gitea/modules/setting" | ||||
"code.gitea.io/gitea/modules/structs" | "code.gitea.io/gitea/modules/structs" | ||||
api "code.gitea.io/gitea/modules/structs" | |||||
"code.gitea.io/gitea/modules/timeutil" | "code.gitea.io/gitea/modules/timeutil" | ||||
"xorm.io/builder" | "xorm.io/builder" | ||||
return fmt.Sprintf("%s/releases/tag/%s", r.Repo.HTMLURL(), r.TagName) | return fmt.Sprintf("%s/releases/tag/%s", r.Repo.HTMLURL(), r.TagName) | ||||
} | } | ||||
// APIFormat convert a Release to api.Release | |||||
func (r *Release) APIFormat() *api.Release { | |||||
assets := make([]*api.Attachment, 0) | |||||
for _, att := range r.Attachments { | |||||
assets = append(assets, att.APIFormat()) | |||||
} | |||||
return &api.Release{ | |||||
ID: r.ID, | |||||
TagName: r.TagName, | |||||
Target: r.Target, | |||||
Title: r.Title, | |||||
Note: r.Note, | |||||
URL: r.APIURL(), | |||||
HTMLURL: r.HTMLURL(), | |||||
TarURL: r.TarURL(), | |||||
ZipURL: r.ZipURL(), | |||||
IsDraft: r.IsDraft, | |||||
IsPrerelease: r.IsPrerelease, | |||||
CreatedAt: r.CreatedUnix.AsTime(), | |||||
PublishedAt: r.CreatedUnix.AsTime(), | |||||
Publisher: r.Publisher.APIFormat(), | |||||
Attachments: assets, | |||||
} | |||||
} | |||||
// IsReleaseExist returns true if release with given tag name already exists. | // IsReleaseExist returns true if release with given tag name already exists. | ||||
func IsReleaseExist(repoID int64, tagName string) (bool, error) { | func IsReleaseExist(repoID int64, tagName string) (bool, error) { | ||||
if len(tagName) == 0 { | if len(tagName) == 0 { |
numReleases, _ := GetReleaseCountByRepoID(repo.ID, FindReleasesOptions{IncludeDrafts: false, IncludeTags: true}) | numReleases, _ := GetReleaseCountByRepoID(repo.ID, FindReleasesOptions{IncludeDrafts: false, IncludeTags: true}) | ||||
return &api.Repository{ | return &api.Repository{ | ||||
ID: repo.ID, | |||||
Owner: repo.Owner.APIFormat(), | |||||
ID: repo.ID, | |||||
// TODO use convert.ToUser(repo.Owner) | |||||
Owner: &api.User{ | |||||
ID: repo.Owner.ID, | |||||
UserName: repo.Owner.Name, | |||||
FullName: repo.Owner.FullName, | |||||
Email: repo.Owner.GetEmail(), | |||||
AvatarURL: repo.Owner.AvatarLink(), | |||||
LastLogin: repo.Owner.LastLoginUnix.AsTime(), | |||||
Created: repo.Owner.CreatedUnix.AsTime(), | |||||
}, | |||||
Name: repo.Name, | Name: repo.Name, | ||||
FullName: repo.FullName(), | FullName: repo.FullName(), | ||||
Description: repo.Description, | Description: repo.Description, |
"code.gitea.io/gitea/modules/setting" | "code.gitea.io/gitea/modules/setting" | ||||
"code.gitea.io/gitea/modules/storage" | "code.gitea.io/gitea/modules/storage" | ||||
"code.gitea.io/gitea/modules/structs" | "code.gitea.io/gitea/modules/structs" | ||||
api "code.gitea.io/gitea/modules/structs" | |||||
"code.gitea.io/gitea/modules/timeutil" | "code.gitea.io/gitea/modules/timeutil" | ||||
"code.gitea.io/gitea/modules/util" | "code.gitea.io/gitea/modules/util" | ||||
return users, x.OrderBy("id").Find(&users) | return users, x.OrderBy("id").Find(&users) | ||||
} | } | ||||
// APIFormat converts a User to api.User | |||||
func (u *User) APIFormat() *api.User { | |||||
if u == nil { | |||||
return nil | |||||
} | |||||
return &api.User{ | |||||
ID: u.ID, | |||||
UserName: u.Name, | |||||
FullName: u.FullName, | |||||
Email: u.GetEmail(), | |||||
AvatarURL: u.AvatarLink(), | |||||
Language: u.Language, | |||||
IsAdmin: u.IsAdmin, | |||||
LastLogin: u.LastLoginUnix.AsTime(), | |||||
Created: u.CreatedUnix.AsTime(), | |||||
} | |||||
} | |||||
// IsLocal returns true if user login type is LoginPlain. | // IsLocal returns true if user login type is LoginPlain. | ||||
func (u *User) IsLocal() bool { | func (u *User) IsLocal() bool { | ||||
return u.LoginType <= LoginPlain | return u.LoginType <= LoginPlain |
assert.Equal(t, []string{"user8@example.com"}, GetUserEmailsByNames([]string{"user8", "user7"})) | assert.Equal(t, []string{"user8@example.com"}, GetUserEmailsByNames([]string{"user8", "user7"})) | ||||
} | } | ||||
func TestUser_APIFormat(t *testing.T) { | |||||
user, err := GetUserByID(1) | |||||
assert.NoError(t, err) | |||||
assert.True(t, user.IsAdmin) | |||||
apiUser := user.APIFormat() | |||||
assert.True(t, apiUser.IsAdmin) | |||||
user, err = GetUserByID(2) | |||||
assert.NoError(t, err) | |||||
assert.False(t, user.IsAdmin) | |||||
apiUser = user.APIFormat() | |||||
assert.False(t, apiUser.IsAdmin) | |||||
} | |||||
func TestCanCreateOrganization(t *testing.T) { | func TestCanCreateOrganization(t *testing.T) { | ||||
assert.NoError(t, PrepareTestDatabase()) | assert.NoError(t, PrepareTestDatabase()) | ||||
"fmt" | "fmt" | ||||
"code.gitea.io/gitea/modules/log" | "code.gitea.io/gitea/modules/log" | ||||
api "code.gitea.io/gitea/modules/structs" | |||||
) | ) | ||||
//UserList is a list of user. | //UserList is a list of user. | ||||
} | } | ||||
return tokenMaps, nil | return tokenMaps, nil | ||||
} | } | ||||
//APIFormat return list of users in api format | |||||
func (users UserList) APIFormat() []*api.User { | |||||
result := make([]*api.User, 0, len(users)) | |||||
for _, u := range users { | |||||
result = append(result, u.APIFormat()) | |||||
} | |||||
return result | |||||
} |
Created: app.CreatedUnix.AsTime(), | Created: app.CreatedUnix.AsTime(), | ||||
} | } | ||||
} | } | ||||
// ToCommitStatus converts models.CommitStatus to api.Status | |||||
func ToCommitStatus(status *models.CommitStatus) *api.Status { | |||||
apiStatus := &api.Status{ | |||||
Created: status.CreatedUnix.AsTime(), | |||||
Updated: status.CreatedUnix.AsTime(), | |||||
State: api.StatusState(status.State), | |||||
TargetURL: status.TargetURL, | |||||
Description: status.Description, | |||||
ID: status.Index, | |||||
URL: status.APIURL(), | |||||
Context: status.Context, | |||||
} | |||||
if status.CreatorID != 0 { | |||||
creator, _ := models.GetUserByID(status.CreatorID) | |||||
apiStatus.Creator = ToUser(creator, false, false) | |||||
} | |||||
return apiStatus | |||||
} |
} | } | ||||
if ok { | if ok { | ||||
apiAuthor = cacheAuthor.APIFormat() | |||||
apiAuthor = ToUser(cacheAuthor, false, false) | |||||
} else { | } else { | ||||
author, err := models.GetUserByEmail(commit.Author.Email) | author, err := models.GetUserByEmail(commit.Author.Email) | ||||
if err != nil && !models.IsErrUserNotExist(err) { | if err != nil && !models.IsErrUserNotExist(err) { | ||||
return nil, err | return nil, err | ||||
} else if err == nil { | } else if err == nil { | ||||
apiAuthor = author.APIFormat() | |||||
apiAuthor = ToUser(author, false, false) | |||||
if userCache != nil { | if userCache != nil { | ||||
userCache[commit.Author.Email] = author | userCache[commit.Author.Email] = author | ||||
} | } | ||||
} | } | ||||
if ok { | if ok { | ||||
apiCommitter = cacheCommitter.APIFormat() | |||||
apiCommitter = ToUser(cacheCommitter, false, false) | |||||
} else { | } else { | ||||
committer, err := models.GetUserByEmail(commit.Committer.Email) | committer, err := models.GetUserByEmail(commit.Committer.Email) | ||||
if err != nil && !models.IsErrUserNotExist(err) { | if err != nil && !models.IsErrUserNotExist(err) { | ||||
return nil, err | return nil, err | ||||
} else if err == nil { | } else if err == nil { | ||||
apiCommitter = committer.APIFormat() | |||||
apiCommitter = ToUser(committer, false, false) | |||||
if userCache != nil { | if userCache != nil { | ||||
userCache[commit.Committer.Email] = committer | userCache[commit.Committer.Email] = committer | ||||
} | } |
URL: issue.APIURL(), | URL: issue.APIURL(), | ||||
HTMLURL: issue.HTMLURL(), | HTMLURL: issue.HTMLURL(), | ||||
Index: issue.Index, | Index: issue.Index, | ||||
Poster: issue.Poster.APIFormat(), | |||||
Poster: ToUser(issue.Poster, false, false), | |||||
Title: issue.Title, | Title: issue.Title, | ||||
Body: issue.Content, | Body: issue.Content, | ||||
Labels: ToLabelList(issue.Labels), | Labels: ToLabelList(issue.Labels), | ||||
} | } | ||||
if len(issue.Assignees) > 0 { | if len(issue.Assignees) > 0 { | ||||
for _, assignee := range issue.Assignees { | for _, assignee := range issue.Assignees { | ||||
apiIssue.Assignees = append(apiIssue.Assignees, assignee.APIFormat()) | |||||
apiIssue.Assignees = append(apiIssue.Assignees, ToUser(assignee, false, false)) | |||||
} | } | ||||
apiIssue.Assignee = issue.Assignees[0].APIFormat() // For compatibility, we're keeping the first assignee as `apiIssue.Assignee` | |||||
apiIssue.Assignee = ToUser(issue.Assignees[0], false, false) // For compatibility, we're keeping the first assignee as `apiIssue.Assignee` | |||||
} | } | ||||
if issue.IsPull { | if issue.IsPull { | ||||
if err := issue.LoadPullRequest(); err != nil { | if err := issue.LoadPullRequest(); err != nil { |
// Copyright 2020 The Gitea Authors. All rights reserved. | |||||
// Use of this source code is governed by a MIT-style | |||||
// license that can be found in the LICENSE file. | |||||
package convert | |||||
import ( | |||||
"code.gitea.io/gitea/models" | |||||
api "code.gitea.io/gitea/modules/structs" | |||||
) | |||||
// ToComment converts a models.Comment to the api.Comment format | |||||
func ToComment(c *models.Comment) *api.Comment { | |||||
return &api.Comment{ | |||||
ID: c.ID, | |||||
Poster: ToUser(c.Poster, false, false), | |||||
HTMLURL: c.HTMLURL(), | |||||
IssueURL: c.IssueURL(), | |||||
PRURL: c.PRURL(), | |||||
Body: c.Content, | |||||
Created: c.CreatedUnix.AsTime(), | |||||
Updated: c.UpdatedUnix.AsTime(), | |||||
} | |||||
} |
if pr.HasMerged { | if pr.HasMerged { | ||||
apiPullRequest.Merged = pr.MergedUnix.AsTimePtr() | apiPullRequest.Merged = pr.MergedUnix.AsTimePtr() | ||||
apiPullRequest.MergedCommitID = &pr.MergedCommitID | apiPullRequest.MergedCommitID = &pr.MergedCommitID | ||||
apiPullRequest.MergedBy = pr.Merger.APIFormat() | |||||
apiPullRequest.MergedBy = ToUser(pr.Merger, false, false) | |||||
} | } | ||||
return apiPullRequest | return apiPullRequest |
// Copyright 2020 The Gitea Authors. All rights reserved. | |||||
// Use of this source code is governed by a MIT-style | |||||
// license that can be found in the LICENSE file. | |||||
package convert | |||||
import ( | |||||
"code.gitea.io/gitea/models" | |||||
api "code.gitea.io/gitea/modules/structs" | |||||
) | |||||
// ToRelease convert a models.Release to api.Release | |||||
func ToRelease(r *models.Release) *api.Release { | |||||
assets := make([]*api.Attachment, 0) | |||||
for _, att := range r.Attachments { | |||||
assets = append(assets, ToReleaseAttachment(att)) | |||||
} | |||||
return &api.Release{ | |||||
ID: r.ID, | |||||
TagName: r.TagName, | |||||
Target: r.Target, | |||||
Title: r.Title, | |||||
Note: r.Note, | |||||
URL: r.APIURL(), | |||||
HTMLURL: r.HTMLURL(), | |||||
TarURL: r.TarURL(), | |||||
ZipURL: r.ZipURL(), | |||||
IsDraft: r.IsDraft, | |||||
IsPrerelease: r.IsPrerelease, | |||||
CreatedAt: r.CreatedUnix.AsTime(), | |||||
PublishedAt: r.CreatedUnix.AsTime(), | |||||
Publisher: ToUser(r.Publisher, false, false), | |||||
Attachments: assets, | |||||
} | |||||
} | |||||
// ToReleaseAttachment converts models.Attachment to api.Attachment | |||||
func ToReleaseAttachment(a *models.Attachment) *api.Attachment { | |||||
return &api.Attachment{ | |||||
ID: a.ID, | |||||
Name: a.Name, | |||||
Created: a.CreatedUnix.AsTime(), | |||||
DownloadCount: a.DownloadCount, | |||||
Size: a.Size, | |||||
UUID: a.UUID, | |||||
DownloadURL: a.DownloadURL(), | |||||
} | |||||
} |
// ToUser convert models.User to api.User | // ToUser convert models.User to api.User | ||||
// signed shall only be set if requester is logged in. authed shall only be set if user is site admin or user himself | // signed shall only be set if requester is logged in. authed shall only be set if user is site admin or user himself | ||||
func ToUser(user *models.User, signed, authed bool) *api.User { | func ToUser(user *models.User, signed, authed bool) *api.User { | ||||
if user == nil { | |||||
return nil | |||||
} | |||||
result := &api.User{ | result := &api.User{ | ||||
ID: user.ID, | ID: user.ID, | ||||
UserName: user.Name, | UserName: user.Name, |
// Copyright 2020 The Gitea Authors. All rights reserved. | |||||
// Use of this source code is governed by a MIT-style | |||||
// license that can be found in the LICENSE file. | |||||
package convert | |||||
import ( | |||||
"testing" | |||||
"code.gitea.io/gitea/models" | |||||
"github.com/stretchr/testify/assert" | |||||
) | |||||
func TestUser_ToUser(t *testing.T) { | |||||
user1 := models.AssertExistsAndLoadBean(t, &models.User{ID: 1, IsAdmin: true}).(*models.User) | |||||
apiUser := ToUser(user1, true, true) | |||||
assert.True(t, apiUser.IsAdmin) | |||||
user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2, IsAdmin: false}).(*models.User) | |||||
apiUser = ToUser(user2, true, true) | |||||
assert.False(t, apiUser.IsAdmin) | |||||
apiUser = ToUser(user1, false, false) | |||||
assert.False(t, apiUser.IsAdmin) | |||||
} |
Index: issue.Index, | Index: issue.Index, | ||||
PullRequest: convert.ToAPIPullRequest(issue.PullRequest), | PullRequest: convert.ToAPIPullRequest(issue.PullRequest), | ||||
Repository: issue.Repo.APIFormat(mode), | Repository: issue.Repo.APIFormat(mode), | ||||
Sender: doer.APIFormat(), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
}) | }) | ||||
} else { | } else { | ||||
err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssueLabel, &api.IssuePayload{ | err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssueLabel, &api.IssuePayload{ | ||||
Index: issue.Index, | Index: issue.Index, | ||||
Issue: convert.ToAPIIssue(issue), | Issue: convert.ToAPIIssue(issue), | ||||
Repository: issue.Repo.APIFormat(mode), | Repository: issue.Repo.APIFormat(mode), | ||||
Sender: doer.APIFormat(), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
}) | }) | ||||
} | } | ||||
if err != nil { | if err != nil { | ||||
if err := webhook_module.PrepareWebhooks(oldRepo, models.HookEventFork, &api.ForkPayload{ | if err := webhook_module.PrepareWebhooks(oldRepo, models.HookEventFork, &api.ForkPayload{ | ||||
Forkee: oldRepo.APIFormat(oldMode), | Forkee: oldRepo.APIFormat(oldMode), | ||||
Repo: repo.APIFormat(mode), | Repo: repo.APIFormat(mode), | ||||
Sender: doer.APIFormat(), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
}); err != nil { | }); err != nil { | ||||
log.Error("PrepareWebhooks [repo_id: %d]: %v", oldRepo.ID, err) | log.Error("PrepareWebhooks [repo_id: %d]: %v", oldRepo.ID, err) | ||||
} | } | ||||
if err := webhook_module.PrepareWebhooks(repo, models.HookEventRepository, &api.RepositoryPayload{ | if err := webhook_module.PrepareWebhooks(repo, models.HookEventRepository, &api.RepositoryPayload{ | ||||
Action: api.HookRepoCreated, | Action: api.HookRepoCreated, | ||||
Repository: repo.APIFormat(models.AccessModeOwner), | Repository: repo.APIFormat(models.AccessModeOwner), | ||||
Organization: u.APIFormat(), | |||||
Sender: doer.APIFormat(), | |||||
Organization: convert.ToUser(u, false, false), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
}); err != nil { | }); err != nil { | ||||
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err) | log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err) | ||||
} | } | ||||
if err := webhook_module.PrepareWebhooks(repo, models.HookEventRepository, &api.RepositoryPayload{ | if err := webhook_module.PrepareWebhooks(repo, models.HookEventRepository, &api.RepositoryPayload{ | ||||
Action: api.HookRepoCreated, | Action: api.HookRepoCreated, | ||||
Repository: repo.APIFormat(models.AccessModeOwner), | Repository: repo.APIFormat(models.AccessModeOwner), | ||||
Organization: u.APIFormat(), | |||||
Sender: doer.APIFormat(), | |||||
Organization: convert.ToUser(u, false, false), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
}); err != nil { | }); err != nil { | ||||
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err) | log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err) | ||||
} | } | ||||
if err := webhook_module.PrepareWebhooks(repo, models.HookEventRepository, &api.RepositoryPayload{ | if err := webhook_module.PrepareWebhooks(repo, models.HookEventRepository, &api.RepositoryPayload{ | ||||
Action: api.HookRepoDeleted, | Action: api.HookRepoDeleted, | ||||
Repository: repo.APIFormat(models.AccessModeOwner), | Repository: repo.APIFormat(models.AccessModeOwner), | ||||
Organization: u.APIFormat(), | |||||
Sender: doer.APIFormat(), | |||||
Organization: convert.ToUser(u, false, false), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
}); err != nil { | }); err != nil { | ||||
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err) | log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err) | ||||
} | } | ||||
Index: issue.Index, | Index: issue.Index, | ||||
PullRequest: convert.ToAPIPullRequest(issue.PullRequest), | PullRequest: convert.ToAPIPullRequest(issue.PullRequest), | ||||
Repository: issue.Repo.APIFormat(mode), | Repository: issue.Repo.APIFormat(mode), | ||||
Sender: doer.APIFormat(), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
} | } | ||||
if removed { | if removed { | ||||
apiPullRequest.Action = api.HookIssueUnassigned | apiPullRequest.Action = api.HookIssueUnassigned | ||||
Index: issue.Index, | Index: issue.Index, | ||||
Issue: convert.ToAPIIssue(issue), | Issue: convert.ToAPIIssue(issue), | ||||
Repository: issue.Repo.APIFormat(mode), | Repository: issue.Repo.APIFormat(mode), | ||||
Sender: doer.APIFormat(), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
} | } | ||||
if removed { | if removed { | ||||
apiIssue.Action = api.HookIssueUnassigned | apiIssue.Action = api.HookIssueUnassigned | ||||
}, | }, | ||||
PullRequest: convert.ToAPIPullRequest(issue.PullRequest), | PullRequest: convert.ToAPIPullRequest(issue.PullRequest), | ||||
Repository: issue.Repo.APIFormat(mode), | Repository: issue.Repo.APIFormat(mode), | ||||
Sender: doer.APIFormat(), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
}) | }) | ||||
} else { | } else { | ||||
err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssues, &api.IssuePayload{ | err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssues, &api.IssuePayload{ | ||||
}, | }, | ||||
Issue: convert.ToAPIIssue(issue), | Issue: convert.ToAPIIssue(issue), | ||||
Repository: issue.Repo.APIFormat(mode), | Repository: issue.Repo.APIFormat(mode), | ||||
Sender: issue.Poster.APIFormat(), | |||||
Sender: convert.ToUser(issue.Poster, false, false), | |||||
}) | }) | ||||
} | } | ||||
Index: issue.Index, | Index: issue.Index, | ||||
PullRequest: convert.ToAPIPullRequest(issue.PullRequest), | PullRequest: convert.ToAPIPullRequest(issue.PullRequest), | ||||
Repository: issue.Repo.APIFormat(mode), | Repository: issue.Repo.APIFormat(mode), | ||||
Sender: doer.APIFormat(), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
} | } | ||||
if isClosed { | if isClosed { | ||||
apiPullRequest.Action = api.HookIssueClosed | apiPullRequest.Action = api.HookIssueClosed | ||||
Index: issue.Index, | Index: issue.Index, | ||||
Issue: convert.ToAPIIssue(issue), | Issue: convert.ToAPIIssue(issue), | ||||
Repository: issue.Repo.APIFormat(mode), | Repository: issue.Repo.APIFormat(mode), | ||||
Sender: doer.APIFormat(), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
} | } | ||||
if isClosed { | if isClosed { | ||||
apiIssue.Action = api.HookIssueClosed | apiIssue.Action = api.HookIssueClosed | ||||
Index: issue.Index, | Index: issue.Index, | ||||
Issue: convert.ToAPIIssue(issue), | Issue: convert.ToAPIIssue(issue), | ||||
Repository: issue.Repo.APIFormat(mode), | Repository: issue.Repo.APIFormat(mode), | ||||
Sender: issue.Poster.APIFormat(), | |||||
Sender: convert.ToUser(issue.Poster, false, false), | |||||
}); err != nil { | }); err != nil { | ||||
log.Error("PrepareWebhooks: %v", err) | log.Error("PrepareWebhooks: %v", err) | ||||
} | } | ||||
Index: pull.Issue.Index, | Index: pull.Issue.Index, | ||||
PullRequest: convert.ToAPIPullRequest(pull), | PullRequest: convert.ToAPIPullRequest(pull), | ||||
Repository: pull.Issue.Repo.APIFormat(mode), | Repository: pull.Issue.Repo.APIFormat(mode), | ||||
Sender: pull.Issue.Poster.APIFormat(), | |||||
Sender: convert.ToUser(pull.Issue.Poster, false, false), | |||||
}); err != nil { | }); err != nil { | ||||
log.Error("PrepareWebhooks: %v", err) | log.Error("PrepareWebhooks: %v", err) | ||||
} | } | ||||
}, | }, | ||||
PullRequest: convert.ToAPIPullRequest(issue.PullRequest), | PullRequest: convert.ToAPIPullRequest(issue.PullRequest), | ||||
Repository: issue.Repo.APIFormat(mode), | Repository: issue.Repo.APIFormat(mode), | ||||
Sender: doer.APIFormat(), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
}) | }) | ||||
} else { | } else { | ||||
err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssues, &api.IssuePayload{ | err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssues, &api.IssuePayload{ | ||||
}, | }, | ||||
Issue: convert.ToAPIIssue(issue), | Issue: convert.ToAPIIssue(issue), | ||||
Repository: issue.Repo.APIFormat(mode), | Repository: issue.Repo.APIFormat(mode), | ||||
Sender: doer.APIFormat(), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
}) | }) | ||||
} | } | ||||
if err != nil { | if err != nil { | ||||
err = webhook_module.PrepareWebhooks(c.Issue.Repo, models.HookEventPullRequestComment, &api.IssueCommentPayload{ | err = webhook_module.PrepareWebhooks(c.Issue.Repo, models.HookEventPullRequestComment, &api.IssueCommentPayload{ | ||||
Action: api.HookIssueCommentEdited, | Action: api.HookIssueCommentEdited, | ||||
Issue: convert.ToAPIIssue(c.Issue), | Issue: convert.ToAPIIssue(c.Issue), | ||||
Comment: c.APIFormat(), | |||||
Comment: convert.ToComment(c), | |||||
Changes: &api.ChangesPayload{ | Changes: &api.ChangesPayload{ | ||||
Body: &api.ChangesFromPayload{ | Body: &api.ChangesFromPayload{ | ||||
From: oldContent, | From: oldContent, | ||||
}, | }, | ||||
}, | }, | ||||
Repository: c.Issue.Repo.APIFormat(mode), | Repository: c.Issue.Repo.APIFormat(mode), | ||||
Sender: doer.APIFormat(), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
IsPull: true, | IsPull: true, | ||||
}) | }) | ||||
} else { | } else { | ||||
err = webhook_module.PrepareWebhooks(c.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{ | err = webhook_module.PrepareWebhooks(c.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{ | ||||
Action: api.HookIssueCommentEdited, | Action: api.HookIssueCommentEdited, | ||||
Issue: convert.ToAPIIssue(c.Issue), | Issue: convert.ToAPIIssue(c.Issue), | ||||
Comment: c.APIFormat(), | |||||
Comment: convert.ToComment(c), | |||||
Changes: &api.ChangesPayload{ | Changes: &api.ChangesPayload{ | ||||
Body: &api.ChangesFromPayload{ | Body: &api.ChangesFromPayload{ | ||||
From: oldContent, | From: oldContent, | ||||
}, | }, | ||||
}, | }, | ||||
Repository: c.Issue.Repo.APIFormat(mode), | Repository: c.Issue.Repo.APIFormat(mode), | ||||
Sender: doer.APIFormat(), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
IsPull: false, | IsPull: false, | ||||
}) | }) | ||||
} | } | ||||
err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventPullRequestComment, &api.IssueCommentPayload{ | err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventPullRequestComment, &api.IssueCommentPayload{ | ||||
Action: api.HookIssueCommentCreated, | Action: api.HookIssueCommentCreated, | ||||
Issue: convert.ToAPIIssue(issue), | Issue: convert.ToAPIIssue(issue), | ||||
Comment: comment.APIFormat(), | |||||
Comment: convert.ToComment(comment), | |||||
Repository: repo.APIFormat(mode), | Repository: repo.APIFormat(mode), | ||||
Sender: doer.APIFormat(), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
IsPull: true, | IsPull: true, | ||||
}) | }) | ||||
} else { | } else { | ||||
err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{ | err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{ | ||||
Action: api.HookIssueCommentCreated, | Action: api.HookIssueCommentCreated, | ||||
Issue: convert.ToAPIIssue(issue), | Issue: convert.ToAPIIssue(issue), | ||||
Comment: comment.APIFormat(), | |||||
Comment: convert.ToComment(comment), | |||||
Repository: repo.APIFormat(mode), | Repository: repo.APIFormat(mode), | ||||
Sender: doer.APIFormat(), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
IsPull: false, | IsPull: false, | ||||
}) | }) | ||||
} | } | ||||
err = webhook_module.PrepareWebhooks(comment.Issue.Repo, models.HookEventPullRequestComment, &api.IssueCommentPayload{ | err = webhook_module.PrepareWebhooks(comment.Issue.Repo, models.HookEventPullRequestComment, &api.IssueCommentPayload{ | ||||
Action: api.HookIssueCommentDeleted, | Action: api.HookIssueCommentDeleted, | ||||
Issue: convert.ToAPIIssue(comment.Issue), | Issue: convert.ToAPIIssue(comment.Issue), | ||||
Comment: comment.APIFormat(), | |||||
Comment: convert.ToComment(comment), | |||||
Repository: comment.Issue.Repo.APIFormat(mode), | Repository: comment.Issue.Repo.APIFormat(mode), | ||||
Sender: doer.APIFormat(), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
IsPull: true, | IsPull: true, | ||||
}) | }) | ||||
} else { | } else { | ||||
err = webhook_module.PrepareWebhooks(comment.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{ | err = webhook_module.PrepareWebhooks(comment.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{ | ||||
Action: api.HookIssueCommentDeleted, | Action: api.HookIssueCommentDeleted, | ||||
Issue: convert.ToAPIIssue(comment.Issue), | Issue: convert.ToAPIIssue(comment.Issue), | ||||
Comment: comment.APIFormat(), | |||||
Comment: convert.ToComment(comment), | |||||
Repository: comment.Issue.Repo.APIFormat(mode), | Repository: comment.Issue.Repo.APIFormat(mode), | ||||
Sender: doer.APIFormat(), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
IsPull: false, | IsPull: false, | ||||
}) | }) | ||||
} | } | ||||
Index: issue.Index, | Index: issue.Index, | ||||
PullRequest: convert.ToAPIPullRequest(issue.PullRequest), | PullRequest: convert.ToAPIPullRequest(issue.PullRequest), | ||||
Repository: issue.Repo.APIFormat(models.AccessModeNone), | Repository: issue.Repo.APIFormat(models.AccessModeNone), | ||||
Sender: doer.APIFormat(), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
}) | }) | ||||
} else { | } else { | ||||
err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssueLabel, &api.IssuePayload{ | err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssueLabel, &api.IssuePayload{ | ||||
Index: issue.Index, | Index: issue.Index, | ||||
Issue: convert.ToAPIIssue(issue), | Issue: convert.ToAPIIssue(issue), | ||||
Repository: issue.Repo.APIFormat(mode), | Repository: issue.Repo.APIFormat(mode), | ||||
Sender: doer.APIFormat(), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
}) | }) | ||||
} | } | ||||
if err != nil { | if err != nil { | ||||
Index: issue.Index, | Index: issue.Index, | ||||
PullRequest: convert.ToAPIPullRequest(issue.PullRequest), | PullRequest: convert.ToAPIPullRequest(issue.PullRequest), | ||||
Repository: issue.Repo.APIFormat(mode), | Repository: issue.Repo.APIFormat(mode), | ||||
Sender: doer.APIFormat(), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
}) | }) | ||||
} else { | } else { | ||||
err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssueMilestone, &api.IssuePayload{ | err = webhook_module.PrepareWebhooks(issue.Repo, models.HookEventIssueMilestone, &api.IssuePayload{ | ||||
Index: issue.Index, | Index: issue.Index, | ||||
Issue: convert.ToAPIIssue(issue), | Issue: convert.ToAPIIssue(issue), | ||||
Repository: issue.Repo.APIFormat(mode), | Repository: issue.Repo.APIFormat(mode), | ||||
Sender: doer.APIFormat(), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
}) | }) | ||||
} | } | ||||
if err != nil { | if err != nil { | ||||
} | } | ||||
func (m *webhookNotifier) NotifyPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *repository.PushCommits) { | func (m *webhookNotifier) NotifyPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *repository.PushCommits) { | ||||
apiPusher := pusher.APIFormat() | |||||
apiPusher := convert.ToUser(pusher, false, false) | |||||
apiCommits, err := commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL()) | apiCommits, err := commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL()) | ||||
if err != nil { | if err != nil { | ||||
log.Error("commits.ToAPIPayloadCommits failed: %v", err) | log.Error("commits.ToAPIPayloadCommits failed: %v", err) | ||||
Index: pr.Issue.Index, | Index: pr.Issue.Index, | ||||
PullRequest: convert.ToAPIPullRequest(pr), | PullRequest: convert.ToAPIPullRequest(pr), | ||||
Repository: pr.Issue.Repo.APIFormat(mode), | Repository: pr.Issue.Repo.APIFormat(mode), | ||||
Sender: doer.APIFormat(), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
Action: api.HookIssueClosed, | Action: api.HookIssueClosed, | ||||
} | } | ||||
}, | }, | ||||
PullRequest: convert.ToAPIPullRequest(issue.PullRequest), | PullRequest: convert.ToAPIPullRequest(issue.PullRequest), | ||||
Repository: issue.Repo.APIFormat(mode), | Repository: issue.Repo.APIFormat(mode), | ||||
Sender: doer.APIFormat(), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
}) | }) | ||||
if err != nil { | if err != nil { | ||||
Index: review.Issue.Index, | Index: review.Issue.Index, | ||||
PullRequest: convert.ToAPIPullRequest(pr), | PullRequest: convert.ToAPIPullRequest(pr), | ||||
Repository: review.Issue.Repo.APIFormat(mode), | Repository: review.Issue.Repo.APIFormat(mode), | ||||
Sender: review.Reviewer.APIFormat(), | |||||
Sender: convert.ToUser(review.Reviewer, false, false), | |||||
Review: &api.ReviewPayload{ | Review: &api.ReviewPayload{ | ||||
Type: string(reviewHookType), | Type: string(reviewHookType), | ||||
Content: review.Content, | Content: review.Content, | ||||
} | } | ||||
func (m *webhookNotifier) NotifyCreateRef(pusher *models.User, repo *models.Repository, refType, refFullName string) { | func (m *webhookNotifier) NotifyCreateRef(pusher *models.User, repo *models.Repository, refType, refFullName string) { | ||||
apiPusher := pusher.APIFormat() | |||||
apiPusher := convert.ToUser(pusher, false, false) | |||||
apiRepo := repo.APIFormat(models.AccessModeNone) | apiRepo := repo.APIFormat(models.AccessModeNone) | ||||
refName := git.RefEndName(refFullName) | refName := git.RefEndName(refFullName) | ||||
Index: pr.Issue.Index, | Index: pr.Issue.Index, | ||||
PullRequest: convert.ToAPIPullRequest(pr), | PullRequest: convert.ToAPIPullRequest(pr), | ||||
Repository: pr.Issue.Repo.APIFormat(models.AccessModeNone), | Repository: pr.Issue.Repo.APIFormat(models.AccessModeNone), | ||||
Sender: doer.APIFormat(), | |||||
Sender: convert.ToUser(doer, false, false), | |||||
}); err != nil { | }); err != nil { | ||||
log.Error("PrepareWebhooks [pull_id: %v]: %v", pr.ID, err) | log.Error("PrepareWebhooks [pull_id: %v]: %v", pr.ID, err) | ||||
} | } | ||||
} | } | ||||
func (m *webhookNotifier) NotifyDeleteRef(pusher *models.User, repo *models.Repository, refType, refFullName string) { | func (m *webhookNotifier) NotifyDeleteRef(pusher *models.User, repo *models.Repository, refType, refFullName string) { | ||||
apiPusher := pusher.APIFormat() | |||||
apiPusher := convert.ToUser(pusher, false, false) | |||||
apiRepo := repo.APIFormat(models.AccessModeNone) | apiRepo := repo.APIFormat(models.AccessModeNone) | ||||
refName := git.RefEndName(refFullName) | refName := git.RefEndName(refFullName) | ||||
mode, _ := models.AccessLevel(rel.Publisher, rel.Repo) | mode, _ := models.AccessLevel(rel.Publisher, rel.Repo) | ||||
if err := webhook_module.PrepareWebhooks(rel.Repo, models.HookEventRelease, &api.ReleasePayload{ | if err := webhook_module.PrepareWebhooks(rel.Repo, models.HookEventRelease, &api.ReleasePayload{ | ||||
Action: action, | Action: action, | ||||
Release: rel.APIFormat(), | |||||
Release: convert.ToRelease(rel), | |||||
Repository: rel.Repo.APIFormat(mode), | Repository: rel.Repo.APIFormat(mode), | ||||
Sender: rel.Publisher.APIFormat(), | |||||
Sender: convert.ToUser(rel.Publisher, false, false), | |||||
}); err != nil { | }); err != nil { | ||||
log.Error("PrepareWebhooks: %v", err) | log.Error("PrepareWebhooks: %v", err) | ||||
} | } | ||||
} | } | ||||
func (m *webhookNotifier) NotifySyncPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *repository.PushCommits) { | func (m *webhookNotifier) NotifySyncPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *repository.PushCommits) { | ||||
apiPusher := pusher.APIFormat() | |||||
apiPusher := convert.ToUser(pusher, false, false) | |||||
apiCommits, err := commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL()) | apiCommits, err := commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL()) | ||||
if err != nil { | if err != nil { | ||||
log.Error("commits.ToAPIPayloadCommits failed: %v", err) | log.Error("commits.ToAPIPayloadCommits failed: %v", err) |
"code.gitea.io/gitea/models" | "code.gitea.io/gitea/models" | ||||
"code.gitea.io/gitea/modules/context" | "code.gitea.io/gitea/modules/context" | ||||
"code.gitea.io/gitea/modules/convert" | |||||
api "code.gitea.io/gitea/modules/structs" | api "code.gitea.io/gitea/modules/structs" | ||||
"code.gitea.io/gitea/routers/api/v1/utils" | "code.gitea.io/gitea/routers/api/v1/utils" | ||||
comment_service "code.gitea.io/gitea/services/comments" | comment_service "code.gitea.io/gitea/services/comments" | ||||
apiComments := make([]*api.Comment, len(comments)) | apiComments := make([]*api.Comment, len(comments)) | ||||
for i, comment := range comments { | for i, comment := range comments { | ||||
comment.Issue = issue | comment.Issue = issue | ||||
apiComments[i] = comments[i].APIFormat() | |||||
apiComments[i] = convert.ToComment(comments[i]) | |||||
} | } | ||||
ctx.JSON(http.StatusOK, &apiComments) | ctx.JSON(http.StatusOK, &apiComments) | ||||
} | } | ||||
return | return | ||||
} | } | ||||
for i := range comments { | for i := range comments { | ||||
apiComments[i] = comments[i].APIFormat() | |||||
apiComments[i] = convert.ToComment(comments[i]) | |||||
} | } | ||||
ctx.JSON(http.StatusOK, &apiComments) | ctx.JSON(http.StatusOK, &apiComments) | ||||
} | } | ||||
return | return | ||||
} | } | ||||
ctx.JSON(http.StatusCreated, comment.APIFormat()) | |||||
ctx.JSON(http.StatusCreated, convert.ToComment(comment)) | |||||
} | } | ||||
// GetIssueComment Get a comment by ID | // GetIssueComment Get a comment by ID | ||||
return | return | ||||
} | } | ||||
ctx.JSON(http.StatusOK, comment.APIFormat()) | |||||
ctx.JSON(http.StatusOK, convert.ToComment(comment)) | |||||
} | } | ||||
// EditIssueComment modify a comment of an issue | // EditIssueComment modify a comment of an issue | ||||
return | return | ||||
} | } | ||||
ctx.JSON(http.StatusOK, comment.APIFormat()) | |||||
ctx.JSON(http.StatusOK, convert.ToComment(comment)) | |||||
} | } | ||||
// DeleteIssueComment delete a comment from an issue | // DeleteIssueComment delete a comment from an issue |
"code.gitea.io/gitea/models" | "code.gitea.io/gitea/models" | ||||
"code.gitea.io/gitea/modules/context" | "code.gitea.io/gitea/modules/context" | ||||
"code.gitea.io/gitea/modules/convert" | |||||
api "code.gitea.io/gitea/modules/structs" | api "code.gitea.io/gitea/modules/structs" | ||||
"code.gitea.io/gitea/routers/api/v1/utils" | "code.gitea.io/gitea/routers/api/v1/utils" | ||||
) | ) | ||||
var result []api.Reaction | var result []api.Reaction | ||||
for _, r := range reactions { | for _, r := range reactions { | ||||
result = append(result, api.Reaction{ | result = append(result, api.Reaction{ | ||||
User: r.User.APIFormat(), | |||||
User: convert.ToUser(r.User, ctx.IsSigned, false), | |||||
Reaction: r.Type, | Reaction: r.Type, | ||||
Created: r.CreatedUnix.AsTime(), | Created: r.CreatedUnix.AsTime(), | ||||
}) | }) | ||||
ctx.Error(http.StatusForbidden, err.Error(), err) | ctx.Error(http.StatusForbidden, err.Error(), err) | ||||
} else if models.IsErrReactionAlreadyExist(err) { | } else if models.IsErrReactionAlreadyExist(err) { | ||||
ctx.JSON(http.StatusOK, api.Reaction{ | ctx.JSON(http.StatusOK, api.Reaction{ | ||||
User: ctx.User.APIFormat(), | |||||
User: convert.ToUser(ctx.User, true, true), | |||||
Reaction: reaction.Type, | Reaction: reaction.Type, | ||||
Created: reaction.CreatedUnix.AsTime(), | Created: reaction.CreatedUnix.AsTime(), | ||||
}) | }) | ||||
} | } | ||||
ctx.JSON(http.StatusCreated, api.Reaction{ | ctx.JSON(http.StatusCreated, api.Reaction{ | ||||
User: ctx.User.APIFormat(), | |||||
User: convert.ToUser(ctx.User, true, true), | |||||
Reaction: reaction.Type, | Reaction: reaction.Type, | ||||
Created: reaction.CreatedUnix.AsTime(), | Created: reaction.CreatedUnix.AsTime(), | ||||
}) | }) | ||||
var result []api.Reaction | var result []api.Reaction | ||||
for _, r := range reactions { | for _, r := range reactions { | ||||
result = append(result, api.Reaction{ | result = append(result, api.Reaction{ | ||||
User: r.User.APIFormat(), | |||||
User: convert.ToUser(r.User, ctx.IsSigned, false), | |||||
Reaction: r.Type, | Reaction: r.Type, | ||||
Created: r.CreatedUnix.AsTime(), | Created: r.CreatedUnix.AsTime(), | ||||
}) | }) | ||||
ctx.Error(http.StatusForbidden, err.Error(), err) | ctx.Error(http.StatusForbidden, err.Error(), err) | ||||
} else if models.IsErrReactionAlreadyExist(err) { | } else if models.IsErrReactionAlreadyExist(err) { | ||||
ctx.JSON(http.StatusOK, api.Reaction{ | ctx.JSON(http.StatusOK, api.Reaction{ | ||||
User: ctx.User.APIFormat(), | |||||
User: convert.ToUser(ctx.User, true, true), | |||||
Reaction: reaction.Type, | Reaction: reaction.Type, | ||||
Created: reaction.CreatedUnix.AsTime(), | Created: reaction.CreatedUnix.AsTime(), | ||||
}) | }) | ||||
} | } | ||||
ctx.JSON(http.StatusCreated, api.Reaction{ | ctx.JSON(http.StatusCreated, api.Reaction{ | ||||
User: ctx.User.APIFormat(), | |||||
User: convert.ToUser(ctx.User, true, true), | |||||
Reaction: reaction.Type, | Reaction: reaction.Type, | ||||
Created: reaction.CreatedUnix.AsTime(), | Created: reaction.CreatedUnix.AsTime(), | ||||
}) | }) |
"code.gitea.io/gitea/models" | "code.gitea.io/gitea/models" | ||||
"code.gitea.io/gitea/modules/context" | "code.gitea.io/gitea/modules/context" | ||||
"code.gitea.io/gitea/modules/convert" | |||||
api "code.gitea.io/gitea/modules/structs" | api "code.gitea.io/gitea/modules/structs" | ||||
"code.gitea.io/gitea/routers/api/v1/utils" | "code.gitea.io/gitea/routers/api/v1/utils" | ||||
) | ) | ||||
ctx.Error(http.StatusInternalServerError, "GetUsersByIDs", err) | ctx.Error(http.StatusInternalServerError, "GetUsersByIDs", err) | ||||
return | return | ||||
} | } | ||||
apiUsers := make([]*api.User, 0, len(users)) | |||||
for i := range users { | |||||
apiUsers[i] = convert.ToUser(users[i], ctx.IsSigned, false) | |||||
} | |||||
ctx.JSON(http.StatusOK, users.APIFormat()) | |||||
ctx.JSON(http.StatusOK, apiUsers) | |||||
} | } |
"code.gitea.io/gitea/models" | "code.gitea.io/gitea/models" | ||||
"code.gitea.io/gitea/modules/context" | "code.gitea.io/gitea/modules/context" | ||||
"code.gitea.io/gitea/modules/convert" | |||||
api "code.gitea.io/gitea/modules/structs" | api "code.gitea.io/gitea/modules/structs" | ||||
"code.gitea.io/gitea/routers/api/v1/utils" | "code.gitea.io/gitea/routers/api/v1/utils" | ||||
releaseservice "code.gitea.io/gitea/services/release" | releaseservice "code.gitea.io/gitea/services/release" | ||||
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err) | ctx.Error(http.StatusInternalServerError, "LoadAttributes", err) | ||||
return | return | ||||
} | } | ||||
ctx.JSON(http.StatusOK, release.APIFormat()) | |||||
ctx.JSON(http.StatusOK, convert.ToRelease(release)) | |||||
} | } | ||||
// ListReleases list a repository's releases | // ListReleases list a repository's releases | ||||
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err) | ctx.Error(http.StatusInternalServerError, "LoadAttributes", err) | ||||
return | return | ||||
} | } | ||||
rels[i] = release.APIFormat() | |||||
rels[i] = convert.ToRelease(release) | |||||
} | } | ||||
ctx.JSON(http.StatusOK, rels) | ctx.JSON(http.StatusOK, rels) | ||||
} | } | ||||
return | return | ||||
} | } | ||||
} | } | ||||
ctx.JSON(http.StatusCreated, rel.APIFormat()) | |||||
ctx.JSON(http.StatusCreated, convert.ToRelease(rel)) | |||||
} | } | ||||
// EditRelease edit a release | // EditRelease edit a release | ||||
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err) | ctx.Error(http.StatusInternalServerError, "LoadAttributes", err) | ||||
return | return | ||||
} | } | ||||
ctx.JSON(http.StatusOK, rel.APIFormat()) | |||||
ctx.JSON(http.StatusOK, convert.ToRelease(rel)) | |||||
} | } | ||||
// DeleteRelease delete a release from a repository | // DeleteRelease delete a release from a repository |
"code.gitea.io/gitea/models" | "code.gitea.io/gitea/models" | ||||
"code.gitea.io/gitea/modules/context" | "code.gitea.io/gitea/modules/context" | ||||
"code.gitea.io/gitea/modules/convert" | |||||
"code.gitea.io/gitea/modules/log" | "code.gitea.io/gitea/modules/log" | ||||
"code.gitea.io/gitea/modules/setting" | "code.gitea.io/gitea/modules/setting" | ||||
api "code.gitea.io/gitea/modules/structs" | api "code.gitea.io/gitea/modules/structs" | ||||
return | return | ||||
} | } | ||||
// FIXME Should prove the existence of the given repo, but results in unnecessary database requests | // FIXME Should prove the existence of the given repo, but results in unnecessary database requests | ||||
ctx.JSON(http.StatusOK, attach.APIFormat()) | |||||
ctx.JSON(http.StatusOK, convert.ToReleaseAttachment(attach)) | |||||
} | } | ||||
// ListReleaseAttachments lists all attachments of the release | // ListReleaseAttachments lists all attachments of the release | ||||
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err) | ctx.Error(http.StatusInternalServerError, "LoadAttributes", err) | ||||
return | return | ||||
} | } | ||||
ctx.JSON(http.StatusOK, release.APIFormat().Attachments) | |||||
ctx.JSON(http.StatusOK, convert.ToRelease(release).Attachments) | |||||
} | } | ||||
// CreateReleaseAttachment creates an attachment and saves the given file | // CreateReleaseAttachment creates an attachment and saves the given file | ||||
return | return | ||||
} | } | ||||
ctx.JSON(http.StatusCreated, attach.APIFormat()) | |||||
ctx.JSON(http.StatusCreated, convert.ToReleaseAttachment(attach)) | |||||
} | } | ||||
// EditReleaseAttachment updates the given attachment | // EditReleaseAttachment updates the given attachment | ||||
if err := models.UpdateAttachment(attach); err != nil { | if err := models.UpdateAttachment(attach); err != nil { | ||||
ctx.Error(http.StatusInternalServerError, "UpdateAttachment", attach) | ctx.Error(http.StatusInternalServerError, "UpdateAttachment", attach) | ||||
} | } | ||||
ctx.JSON(http.StatusCreated, attach.APIFormat()) | |||||
ctx.JSON(http.StatusCreated, convert.ToReleaseAttachment(attach)) | |||||
} | } | ||||
// DeleteReleaseAttachment delete a given attachment | // DeleteReleaseAttachment delete a given attachment |
"code.gitea.io/gitea/models" | "code.gitea.io/gitea/models" | ||||
"code.gitea.io/gitea/modules/context" | "code.gitea.io/gitea/modules/context" | ||||
"code.gitea.io/gitea/modules/convert" | |||||
) | ) | ||||
// GetReleaseTag get a single release of a repository by its tagname | // GetReleaseTag get a single release of a repository by its tagname | ||||
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err) | ctx.Error(http.StatusInternalServerError, "LoadAttributes", err) | ||||
return | return | ||||
} | } | ||||
ctx.JSON(http.StatusOK, release.APIFormat()) | |||||
ctx.JSON(http.StatusOK, convert.ToRelease(release)) | |||||
} | } |
"code.gitea.io/gitea/models" | "code.gitea.io/gitea/models" | ||||
"code.gitea.io/gitea/modules/context" | "code.gitea.io/gitea/modules/context" | ||||
"code.gitea.io/gitea/modules/convert" | |||||
"code.gitea.io/gitea/modules/repofiles" | "code.gitea.io/gitea/modules/repofiles" | ||||
api "code.gitea.io/gitea/modules/structs" | api "code.gitea.io/gitea/modules/structs" | ||||
"code.gitea.io/gitea/routers/api/v1/utils" | "code.gitea.io/gitea/routers/api/v1/utils" | ||||
return | return | ||||
} | } | ||||
ctx.JSON(http.StatusCreated, status.APIFormat()) | |||||
ctx.JSON(http.StatusCreated, convert.ToCommitStatus(status)) | |||||
} | } | ||||
// GetCommitStatuses returns all statuses for any given commit hash | // GetCommitStatuses returns all statuses for any given commit hash | ||||
apiStatuses := make([]*api.Status, 0, len(statuses)) | apiStatuses := make([]*api.Status, 0, len(statuses)) | ||||
for _, status := range statuses { | for _, status := range statuses { | ||||
apiStatuses = append(apiStatuses, status.APIFormat()) | |||||
apiStatuses = append(apiStatuses, convert.ToCommitStatus(status)) | |||||
} | } | ||||
ctx.SetLinkHeader(int(maxResults), listOptions.PageSize) | ctx.SetLinkHeader(int(maxResults), listOptions.PageSize) | ||||
retStatus.Statuses = make([]*api.Status, 0, len(statuses)) | retStatus.Statuses = make([]*api.Status, 0, len(statuses)) | ||||
for _, status := range statuses { | for _, status := range statuses { | ||||
retStatus.Statuses = append(retStatus.Statuses, status.APIFormat()) | |||||
retStatus.Statuses = append(retStatus.Statuses, convert.ToCommitStatus(status)) | |||||
if status.State.NoBetterThan(retStatus.State) { | if status.State.NoBetterThan(retStatus.State) { | ||||
retStatus.State = status.State | retStatus.State = status.State | ||||
} | } |
"code.gitea.io/gitea/modules/auth" | "code.gitea.io/gitea/modules/auth" | ||||
"code.gitea.io/gitea/modules/base" | "code.gitea.io/gitea/modules/base" | ||||
"code.gitea.io/gitea/modules/context" | "code.gitea.io/gitea/modules/context" | ||||
"code.gitea.io/gitea/modules/convert" | |||||
"code.gitea.io/gitea/modules/git" | "code.gitea.io/gitea/modules/git" | ||||
issue_indexer "code.gitea.io/gitea/modules/indexer/issues" | issue_indexer "code.gitea.io/gitea/modules/indexer/issues" | ||||
"code.gitea.io/gitea/modules/log" | "code.gitea.io/gitea/modules/log" | ||||
issue := GetActionIssue(ctx) | issue := GetActionIssue(ctx) | ||||
var attachments = make([]*api.Attachment, len(issue.Attachments)) | var attachments = make([]*api.Attachment, len(issue.Attachments)) | ||||
for i := 0; i < len(issue.Attachments); i++ { | for i := 0; i < len(issue.Attachments); i++ { | ||||
attachments[i] = issue.Attachments[i].APIFormat() | |||||
attachments[i] = convert.ToReleaseAttachment(issue.Attachments[i]) | |||||
} | } | ||||
ctx.JSON(200, attachments) | ctx.JSON(200, attachments) | ||||
} | } | ||||
return | return | ||||
} | } | ||||
for i := 0; i < len(comment.Attachments); i++ { | for i := 0; i < len(comment.Attachments); i++ { | ||||
attachments = append(attachments, comment.Attachments[i].APIFormat()) | |||||
attachments = append(attachments, convert.ToReleaseAttachment(comment.Attachments[i])) | |||||
} | } | ||||
} | } | ||||
ctx.JSON(200, attachments) | ctx.JSON(200, attachments) |
"code.gitea.io/gitea/modules/auth" | "code.gitea.io/gitea/modules/auth" | ||||
"code.gitea.io/gitea/modules/base" | "code.gitea.io/gitea/modules/base" | ||||
"code.gitea.io/gitea/modules/context" | "code.gitea.io/gitea/modules/context" | ||||
"code.gitea.io/gitea/modules/convert" | |||||
"code.gitea.io/gitea/modules/git" | "code.gitea.io/gitea/modules/git" | ||||
"code.gitea.io/gitea/modules/setting" | "code.gitea.io/gitea/modules/setting" | ||||
api "code.gitea.io/gitea/modules/structs" | api "code.gitea.io/gitea/modules/structs" | ||||
} | } | ||||
} | } | ||||
apiUser := ctx.User.APIFormat() | |||||
apiUser := convert.ToUser(ctx.User, true, true) | |||||
p := &api.PushPayload{ | p := &api.PushPayload{ | ||||
Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch, | Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch, | ||||
Before: commit.ID.String(), | Before: commit.ID.String(), |