|
|
@ -9,52 +9,144 @@ import ( |
|
|
|
|
|
|
|
|
|
|
|
api "github.com/gogits/go-gogs-client" |
|
|
|
api "github.com/gogits/go-gogs-client" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
base "github.com/gogits/gogs/modules/base" |
|
|
|
"github.com/gogits/gogs/modules/log" |
|
|
|
"github.com/gogits/gogs/modules/log" |
|
|
|
"github.com/gogits/gogs/modules/middleware" |
|
|
|
"github.com/gogits/gogs/modules/middleware" |
|
|
|
"github.com/gogits/gogs/models" |
|
|
|
"github.com/gogits/gogs/models" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func ToApiRelease(release *models.Release, publisher *models.User) *api.Release { |
|
|
|
func ToApiRelease(release *models.Release) *api.Release { |
|
|
|
|
|
|
|
|
|
|
|
return &api.Release{ |
|
|
|
return &api.Release{ |
|
|
|
ID: release.Id, |
|
|
|
ID: release.Id, |
|
|
|
Publisher: *ToApiUser(publisher), |
|
|
|
Publisher: *ToApiUser(release.Publisher), |
|
|
|
TagName: release.TagName, |
|
|
|
TagName: release.TagName, |
|
|
|
LowerTagName: release.LowerTagName, |
|
|
|
LowerTagName: release.LowerTagName, |
|
|
|
Target: release.Target, |
|
|
|
Target: release.Target, |
|
|
|
Title: release.Title, |
|
|
|
Title: release.Title, |
|
|
|
Sha1: release.Sha1, |
|
|
|
Sha1: release.Sha1, |
|
|
|
NumCommits: release.NumCommits, |
|
|
|
NumCommits: release.NumCommits, |
|
|
|
Note: release.Note, |
|
|
|
Note: release.Note, |
|
|
|
IsDraft: release.IsDraft, |
|
|
|
IsDraft: release.IsDraft, |
|
|
|
IsPrerelease: release.IsPrerelease, |
|
|
|
IsPrerelease: release.IsPrerelease, |
|
|
|
Created: release.Created, |
|
|
|
Created: release.Created, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func ListReleases(ctx *middleware.Context) { |
|
|
|
func ListReleases(ctx *middleware.Context) { |
|
|
|
|
|
|
|
rawTags, err := ctx.Repo.GitRepo.GetTags() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
ctx.Handle(500, "GetTags", err) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
rels, err := models.GetReleasesByRepoId(ctx.Repo.Repository.ID) |
|
|
|
rels, err := models.GetReleasesByRepoId(ctx.Repo.Repository.ID) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Error(4, "GetReleasesByRepoId: %v", err) |
|
|
|
ctx.Handle(500, "GetReleasesByRepoId", err) |
|
|
|
ctx.Status(500) |
|
|
|
|
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
apiReleases := make([]*api.Release, len(rels)) |
|
|
|
// Temproray cache commits count of used branches to speed up.
|
|
|
|
for i, rel := range rels { |
|
|
|
countCache := make(map[string]int) |
|
|
|
publisher, err := models.GetUserByID(rel.PublisherId) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
tags := make([]*api.Release, len(rawTags)) |
|
|
|
if models.IsErrUserNotExist(err) { |
|
|
|
for i, rawTag := range rawTags { |
|
|
|
publisher = models.NewFakeUser() |
|
|
|
for j, rel := range rels { |
|
|
|
} else { |
|
|
|
if rel == nil || (rel.IsDraft && !ctx.Repo.IsOwner()) { |
|
|
|
log.Error(4, "GetUserByID: %v", err) |
|
|
|
continue |
|
|
|
ctx.Status(500) |
|
|
|
} |
|
|
|
|
|
|
|
if rel.TagName == rawTag { |
|
|
|
|
|
|
|
rel.Publisher, err = models.GetUserByID(rel.PublisherId) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
ctx.Handle(500, "GetUserById", err) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// FIXME: duplicated code.
|
|
|
|
|
|
|
|
// Get corresponding target if it's not the current branch.
|
|
|
|
|
|
|
|
if ctx.Repo.BranchName != rel.Target { |
|
|
|
|
|
|
|
// Get count if not exists.
|
|
|
|
|
|
|
|
if _, ok := countCache[rel.Target]; !ok { |
|
|
|
|
|
|
|
commit, err := ctx.Repo.GitRepo.GetCommitOfBranch(ctx.Repo.BranchName) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
ctx.Handle(500, "GetCommitOfBranch", err) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
countCache[ctx.Repo.BranchName], err = commit.CommitsCount() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
ctx.Handle(500, "CommitsCount2", err) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
rel.NumCommitsBehind = countCache[ctx.Repo.BranchName] - rel.NumCommits |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
rel.NumCommitsBehind = ctx.Repo.CommitsCount - rel.NumCommits |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rel.Note = base.RenderMarkdownString(rel.Note, ctx.Repo.RepoLink) |
|
|
|
|
|
|
|
tags[i] = ToApiRelease(rel) |
|
|
|
|
|
|
|
rels[j] = nil // Mark as used.
|
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if tags[i] == nil { |
|
|
|
|
|
|
|
commit, err := ctx.Repo.GitRepo.GetCommitOfTag(rawTag) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
ctx.Handle(500, "GetCommitOfTag2", err) |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tags[i] = &api.Release{ |
|
|
|
|
|
|
|
Title: rawTag, |
|
|
|
|
|
|
|
TagName: rawTag, |
|
|
|
|
|
|
|
Sha1: commit.Id.String(), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tags[i].NumCommits, err = ctx.Repo.GitRepo.CommitsCount(commit.Id.String()) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
ctx.Handle(500, "CommitsCount", err) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
tags[i].NumCommitsBehind = ctx.Repo.CommitsCount - tags[i].NumCommits |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, rel := range rels { |
|
|
|
|
|
|
|
if rel == nil { |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rel.Publisher, err = models.GetUserByID(rel.PublisherId) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
ctx.Handle(500, "GetUserById", err) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// FIXME: duplicated code.
|
|
|
|
|
|
|
|
// Get corresponding target if it's not the current branch.
|
|
|
|
|
|
|
|
if ctx.Repo.BranchName != rel.Target { |
|
|
|
|
|
|
|
// Get count if not exists.
|
|
|
|
|
|
|
|
if _, ok := countCache[rel.Target]; !ok { |
|
|
|
|
|
|
|
commit, err := ctx.Repo.GitRepo.GetCommitOfBranch(ctx.Repo.BranchName) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
ctx.Handle(500, "GetCommitOfBranch", err) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
countCache[ctx.Repo.BranchName], err = commit.CommitsCount() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
ctx.Handle(500, "CommitsCount2", err) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
rel.NumCommitsBehind = countCache[ctx.Repo.BranchName] - rel.NumCommits |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
rel.NumCommitsBehind = ctx.Repo.CommitsCount - rel.NumCommits |
|
|
|
} |
|
|
|
} |
|
|
|
apiReleases[i] = ToApiRelease(rel, publisher) |
|
|
|
|
|
|
|
|
|
|
|
rel.Note = base.RenderMarkdownString(rel.Note, ctx.Repo.RepoLink) |
|
|
|
|
|
|
|
tags = append(tags, ToApiRelease(rel)) |
|
|
|
} |
|
|
|
} |
|
|
|
ctx.JSON(200, &apiReleases) |
|
|
|
|
|
|
|
|
|
|
|
ctx.JSON(200, &tags) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func ReleaseByName(ctx *middleware.Context) { |
|
|
|
func ReleaseByName(ctx *middleware.Context) { |
|
|
@ -73,10 +165,10 @@ func ReleaseByName(ctx *middleware.Context) { |
|
|
|
ctx.Handle(422, "GetUserByID", err) |
|
|
|
ctx.Handle(422, "GetUserByID", err) |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
rel.Publisher = publisher |
|
|
|
|
|
|
|
|
|
|
|
ctx.JSON(200, ToApiRelease(rel, publisher)) |
|
|
|
ctx.JSON(200, ToApiRelease(rel)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -100,6 +192,7 @@ func CreateRelease(ctx *middleware.Context, form api.CreateReleaseOption) { |
|
|
|
|
|
|
|
|
|
|
|
rel := &models.Release{ |
|
|
|
rel := &models.Release{ |
|
|
|
RepoId: ctx.Repo.Repository.ID, |
|
|
|
RepoId: ctx.Repo.Repository.ID, |
|
|
|
|
|
|
|
Publisher: ctx.User, |
|
|
|
PublisherId: ctx.User.Id, |
|
|
|
PublisherId: ctx.User.Id, |
|
|
|
Title: form.Title, |
|
|
|
Title: form.Title, |
|
|
|
TagName: form.TagName, |
|
|
|
TagName: form.TagName, |
|
|
@ -118,5 +211,5 @@ func CreateRelease(ctx *middleware.Context, form api.CreateReleaseOption) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ctx.JSON(201, ToApiRelease(rel, ctx.User)) |
|
|
|
ctx.JSON(201, ToApiRelease(rel)) |
|
|
|
} |
|
|
|
} |