2020-01-11 10:59:41 +08:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-01-11 10:59:41 +08:00
|
|
|
|
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
2022-11-19 16:12:33 +08:00
|
|
|
"context"
|
2021-09-11 00:03:16 +08:00
|
|
|
"fmt"
|
2021-11-17 02:18:25 +08:00
|
|
|
"net/url"
|
2020-02-29 10:49:50 +08:00
|
|
|
"strings"
|
|
|
|
|
2021-12-10 09:27:50 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-04-08 17:11:15 +08:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-09-11 00:03:16 +08:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-01-11 10:59:41 +08:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
)
|
|
|
|
|
2020-02-29 10:49:50 +08:00
|
|
|
// ToAPIIssue converts an Issue to API format
|
|
|
|
// it assumes some fields assigned with values:
|
|
|
|
// Required - Poster, Labels,
|
|
|
|
// Optional - Milestone, Assignee, PullRequest
|
2022-11-19 16:12:33 +08:00
|
|
|
func ToAPIIssue(ctx context.Context, issue *issues_model.Issue) *api.Issue {
|
|
|
|
if err := issue.LoadLabels(ctx); err != nil {
|
2020-02-29 10:49:50 +08:00
|
|
|
return &api.Issue{}
|
|
|
|
}
|
2022-11-19 16:12:33 +08:00
|
|
|
if err := issue.LoadPoster(ctx); err != nil {
|
2020-02-29 10:49:50 +08:00
|
|
|
return &api.Issue{}
|
|
|
|
}
|
2022-11-19 16:12:33 +08:00
|
|
|
if err := issue.LoadRepo(ctx); err != nil {
|
2020-02-29 10:49:50 +08:00
|
|
|
return &api.Issue{}
|
|
|
|
}
|
2022-11-19 16:12:33 +08:00
|
|
|
if err := issue.Repo.GetOwner(ctx); err != nil {
|
2021-09-11 00:03:16 +08:00
|
|
|
return &api.Issue{}
|
|
|
|
}
|
2020-02-29 10:49:50 +08:00
|
|
|
|
|
|
|
apiIssue := &api.Issue{
|
|
|
|
ID: issue.ID,
|
|
|
|
URL: issue.APIURL(),
|
|
|
|
HTMLURL: issue.HTMLURL(),
|
|
|
|
Index: issue.Index,
|
2021-03-28 00:45:26 +08:00
|
|
|
Poster: ToUser(issue.Poster, nil),
|
2020-02-29 10:49:50 +08:00
|
|
|
Title: issue.Title,
|
|
|
|
Body: issue.Content,
|
2020-12-13 19:34:11 +08:00
|
|
|
Ref: issue.Ref,
|
2021-09-11 00:03:16 +08:00
|
|
|
Labels: ToLabelList(issue.Labels, issue.Repo, issue.Repo.Owner),
|
2020-02-29 10:49:50 +08:00
|
|
|
State: issue.State(),
|
2020-06-02 05:01:55 +08:00
|
|
|
IsLocked: issue.IsLocked,
|
2020-02-29 10:49:50 +08:00
|
|
|
Comments: issue.NumComments,
|
|
|
|
Created: issue.CreatedUnix.AsTime(),
|
|
|
|
Updated: issue.UpdatedUnix.AsTime(),
|
|
|
|
}
|
|
|
|
|
|
|
|
apiIssue.Repo = &api.RepositoryMeta{
|
|
|
|
ID: issue.Repo.ID,
|
|
|
|
Name: issue.Repo.Name,
|
|
|
|
Owner: issue.Repo.OwnerName,
|
|
|
|
FullName: issue.Repo.FullName(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if issue.ClosedUnix != 0 {
|
|
|
|
apiIssue.Closed = issue.ClosedUnix.AsTimePtr()
|
|
|
|
}
|
|
|
|
|
2022-11-19 16:12:33 +08:00
|
|
|
if err := issue.LoadMilestone(ctx); err != nil {
|
2020-02-29 10:49:50 +08:00
|
|
|
return &api.Issue{}
|
|
|
|
}
|
|
|
|
if issue.Milestone != nil {
|
2020-05-13 05:54:35 +08:00
|
|
|
apiIssue.Milestone = ToAPIMilestone(issue.Milestone)
|
2020-02-29 10:49:50 +08:00
|
|
|
}
|
|
|
|
|
2022-11-19 16:12:33 +08:00
|
|
|
if err := issue.LoadAssignees(ctx); err != nil {
|
2020-02-29 10:49:50 +08:00
|
|
|
return &api.Issue{}
|
|
|
|
}
|
|
|
|
if len(issue.Assignees) > 0 {
|
|
|
|
for _, assignee := range issue.Assignees {
|
2021-03-28 00:45:26 +08:00
|
|
|
apiIssue.Assignees = append(apiIssue.Assignees, ToUser(assignee, nil))
|
2020-02-29 10:49:50 +08:00
|
|
|
}
|
2021-03-28 00:45:26 +08:00
|
|
|
apiIssue.Assignee = ToUser(issue.Assignees[0], nil) // For compatibility, we're keeping the first assignee as `apiIssue.Assignee`
|
2020-02-29 10:49:50 +08:00
|
|
|
}
|
|
|
|
if issue.IsPull {
|
2022-11-19 16:12:33 +08:00
|
|
|
if err := issue.LoadPullRequest(ctx); err != nil {
|
2020-02-29 10:49:50 +08:00
|
|
|
return &api.Issue{}
|
|
|
|
}
|
|
|
|
apiIssue.PullRequest = &api.PullRequestMeta{
|
|
|
|
HasMerged: issue.PullRequest.HasMerged,
|
|
|
|
}
|
|
|
|
if issue.PullRequest.HasMerged {
|
|
|
|
apiIssue.PullRequest.Merged = issue.PullRequest.MergedUnix.AsTimePtr()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if issue.DeadlineUnix != 0 {
|
|
|
|
apiIssue.Deadline = issue.DeadlineUnix.AsTimePtr()
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiIssue
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToAPIIssueList converts an IssueList to API format
|
2022-11-19 16:12:33 +08:00
|
|
|
func ToAPIIssueList(ctx context.Context, il issues_model.IssueList) []*api.Issue {
|
2020-02-29 10:49:50 +08:00
|
|
|
result := make([]*api.Issue, len(il))
|
|
|
|
for i := range il {
|
2022-11-19 16:12:33 +08:00
|
|
|
result[i] = ToAPIIssue(ctx, il[i])
|
2020-02-29 10:49:50 +08:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-01-11 10:59:41 +08:00
|
|
|
// ToTrackedTime converts TrackedTime to API format
|
2022-11-19 16:12:33 +08:00
|
|
|
func ToTrackedTime(ctx context.Context, t *issues_model.TrackedTime) (apiT *api.TrackedTime) {
|
2020-01-11 10:59:41 +08:00
|
|
|
apiT = &api.TrackedTime{
|
|
|
|
ID: t.ID,
|
|
|
|
IssueID: t.IssueID,
|
|
|
|
UserID: t.UserID,
|
|
|
|
UserName: t.User.Name,
|
|
|
|
Time: t.Time,
|
|
|
|
Created: t.Created,
|
|
|
|
}
|
|
|
|
if t.Issue != nil {
|
2022-11-19 16:12:33 +08:00
|
|
|
apiT.Issue = ToAPIIssue(ctx, t.Issue)
|
2020-01-11 10:59:41 +08:00
|
|
|
}
|
|
|
|
if t.User != nil {
|
|
|
|
apiT.UserName = t.User.Name
|
|
|
|
}
|
2022-06-20 18:02:49 +08:00
|
|
|
return apiT
|
2020-01-11 10:59:41 +08:00
|
|
|
}
|
|
|
|
|
2020-09-18 20:09:26 +08:00
|
|
|
// ToStopWatches convert Stopwatch list to api.StopWatches
|
2022-06-13 17:37:59 +08:00
|
|
|
func ToStopWatches(sws []*issues_model.Stopwatch) (api.StopWatches, error) {
|
2020-09-18 20:09:26 +08:00
|
|
|
result := api.StopWatches(make([]api.StopWatch, 0, len(sws)))
|
|
|
|
|
2022-06-13 17:37:59 +08:00
|
|
|
issueCache := make(map[int64]*issues_model.Issue)
|
2021-12-10 09:27:50 +08:00
|
|
|
repoCache := make(map[int64]*repo_model.Repository)
|
2020-09-18 20:09:26 +08:00
|
|
|
var (
|
2022-06-13 17:37:59 +08:00
|
|
|
issue *issues_model.Issue
|
2021-12-10 09:27:50 +08:00
|
|
|
repo *repo_model.Repository
|
2020-09-18 20:09:26 +08:00
|
|
|
ok bool
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, sw := range sws {
|
|
|
|
issue, ok = issueCache[sw.IssueID]
|
|
|
|
if !ok {
|
2022-06-13 17:37:59 +08:00
|
|
|
issue, err = issues_model.GetIssueByID(db.DefaultContext, sw.IssueID)
|
2020-09-18 20:09:26 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
repo, ok = repoCache[issue.RepoID]
|
|
|
|
if !ok {
|
2021-12-10 09:27:50 +08:00
|
|
|
repo, err = repo_model.GetRepositoryByID(issue.RepoID)
|
2020-09-18 20:09:26 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, api.StopWatch{
|
|
|
|
Created: sw.CreatedUnix.AsTime(),
|
2021-01-21 22:51:52 +08:00
|
|
|
Seconds: sw.Seconds(),
|
|
|
|
Duration: sw.Duration(),
|
2020-09-18 20:09:26 +08:00
|
|
|
IssueIndex: issue.Index,
|
|
|
|
IssueTitle: issue.Title,
|
|
|
|
RepoOwnerName: repo.OwnerName,
|
|
|
|
RepoName: repo.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-01-11 10:59:41 +08:00
|
|
|
// ToTrackedTimeList converts TrackedTimeList to API format
|
2022-11-19 16:12:33 +08:00
|
|
|
func ToTrackedTimeList(ctx context.Context, tl issues_model.TrackedTimeList) api.TrackedTimeList {
|
2020-01-11 10:59:41 +08:00
|
|
|
result := make([]*api.TrackedTime, 0, len(tl))
|
|
|
|
for _, t := range tl {
|
2022-11-19 16:12:33 +08:00
|
|
|
result = append(result, ToTrackedTime(ctx, t))
|
2020-01-11 10:59:41 +08:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2020-02-29 10:49:50 +08:00
|
|
|
|
|
|
|
// ToLabel converts Label to API format
|
2022-06-13 17:37:59 +08:00
|
|
|
func ToLabel(label *issues_model.Label, repo *repo_model.Repository, org *user_model.User) *api.Label {
|
2021-09-11 00:03:16 +08:00
|
|
|
result := &api.Label{
|
2020-02-29 10:49:50 +08:00
|
|
|
ID: label.ID,
|
|
|
|
Name: label.Name,
|
|
|
|
Color: strings.TrimLeft(label.Color, "#"),
|
|
|
|
Description: label.Description,
|
|
|
|
}
|
2021-09-11 00:03:16 +08:00
|
|
|
|
|
|
|
// calculate URL
|
|
|
|
if label.BelongsToRepo() && repo != nil {
|
|
|
|
if repo != nil {
|
|
|
|
result.URL = fmt.Sprintf("%s/labels/%d", repo.APIURL(), label.ID)
|
|
|
|
} else {
|
|
|
|
log.Error("ToLabel did not get repo to calculate url for label with id '%d'", label.ID)
|
|
|
|
}
|
|
|
|
} else { // BelongsToOrg
|
|
|
|
if org != nil {
|
2021-11-17 02:18:25 +08:00
|
|
|
result.URL = fmt.Sprintf("%sapi/v1/orgs/%s/labels/%d", setting.AppURL, url.PathEscape(org.Name), label.ID)
|
2021-09-11 00:03:16 +08:00
|
|
|
} else {
|
|
|
|
log.Error("ToLabel did not get org to calculate url for label with id '%d'", label.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
2020-02-29 10:49:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToLabelList converts list of Label to API format
|
2022-06-13 17:37:59 +08:00
|
|
|
func ToLabelList(labels []*issues_model.Label, repo *repo_model.Repository, org *user_model.User) []*api.Label {
|
2020-02-29 10:49:50 +08:00
|
|
|
result := make([]*api.Label, len(labels))
|
|
|
|
for i := range labels {
|
2021-09-11 00:03:16 +08:00
|
|
|
result[i] = ToLabel(labels[i], repo, org)
|
2020-02-29 10:49:50 +08:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2020-05-13 05:54:35 +08:00
|
|
|
|
|
|
|
// ToAPIMilestone converts Milestone into API Format
|
2022-04-08 17:11:15 +08:00
|
|
|
func ToAPIMilestone(m *issues_model.Milestone) *api.Milestone {
|
2020-05-13 05:54:35 +08:00
|
|
|
apiMilestone := &api.Milestone{
|
|
|
|
ID: m.ID,
|
|
|
|
State: m.State(),
|
|
|
|
Title: m.Name,
|
|
|
|
Description: m.Content,
|
|
|
|
OpenIssues: m.NumOpenIssues,
|
|
|
|
ClosedIssues: m.NumClosedIssues,
|
2020-09-06 01:38:54 +08:00
|
|
|
Created: m.CreatedUnix.AsTime(),
|
|
|
|
Updated: m.UpdatedUnix.AsTimePtr(),
|
2020-05-13 05:54:35 +08:00
|
|
|
}
|
|
|
|
if m.IsClosed {
|
|
|
|
apiMilestone.Closed = m.ClosedDateUnix.AsTimePtr()
|
|
|
|
}
|
|
|
|
if m.DeadlineUnix.Year() < 9999 {
|
|
|
|
apiMilestone.Deadline = m.DeadlineUnix.AsTimePtr()
|
|
|
|
}
|
|
|
|
return apiMilestone
|
|
|
|
}
|