Browse Source

releases also takes the raw tags just as the release page

pull/1630/head
Kim Lindhardt Madsen 9 years ago
parent
commit
adf9091e86
  1. 2
      cmd/web.go
  2. 4
      routers/api/v1/repo.go
  3. 6
      routers/api/v1/repo_commits.go
  4. 125
      routers/api/v1/repo_releases.go

2
cmd/web.go

@ -237,6 +237,8 @@ func runWeb(ctx *cli.Context) {
}, middleware.ApiReqToken()) }, middleware.ApiReqToken())
m.Group("/:username/:reponame", func() { m.Group("/:username/:reponame", func() {
m.Get("", v1.GetRepo)
m.Combo("/hooks").Get(v1.ListRepoHooks). m.Combo("/hooks").Get(v1.ListRepoHooks).
Post(bind(api.CreateHookOption{}), v1.CreateRepoHook) Post(bind(api.CreateHookOption{}), v1.CreateRepoHook)
m.Patch("/hooks/:id:int", bind(api.EditHookOption{}), v1.EditRepoHook) m.Patch("/hooks/:id:int", bind(api.EditHookOption{}), v1.EditRepoHook)

4
routers/api/v1/repo.go

@ -163,6 +163,10 @@ func createRepo(ctx *middleware.Context, owner *models.User, opt api.CreateRepoO
ctx.JSON(201, ToApiRepository(owner, repo, api.Permission{true, true, true})) ctx.JSON(201, ToApiRepository(owner, repo, api.Permission{true, true, true}))
} }
func GetRepo(ctx *middleware.Context) {
ctx.JSON(200, ToApiRepository(ctx.Repo.Repository.Owner, ctx.Repo.Repository, api.Permission{}))
}
// https://github.com/gogits/go-gogs-client/wiki/Repositories#create // https://github.com/gogits/go-gogs-client/wiki/Repositories#create
func CreateRepo(ctx *middleware.Context, opt api.CreateRepoOption) { func CreateRepo(ctx *middleware.Context, opt api.CreateRepoOption) {
// Shouldn't reach this condition, but just in case. // Shouldn't reach this condition, but just in case.

6
routers/api/v1/repo_commits.go

@ -32,12 +32,12 @@ func ToApiCommit(commit *git.Commit) *api.Commit {
} }
func HEADCommit(ctx *middleware.Context) { func HEADCommit(ctx *middleware.Context) {
ctx.JSON(200, ctx.Repo.Commit.Id.String()) ctx.JSON(200, &api.Sha1{
Sha1: ctx.Repo.Commit.Id.String(),
})
} }
func CommitByID(ctx *middleware.Context) { func CommitByID(ctx *middleware.Context) {
commit, err := ctx.Repo.GitRepo.GetCommit(ctx.Params(":commitid")) commit, err := ctx.Repo.GitRepo.GetCommit(ctx.Params(":commitid"))
if err != nil { if err != nil {
log.Error(4, "GetCommit: %v", err) log.Error(4, "GetCommit: %v", err)

125
routers/api/v1/repo_releases.go

@ -9,16 +9,17 @@ 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,
@ -33,28 +34,119 @@ func ToApiRelease(release *models.Release, publisher *models.User) *api.Release
} }
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)
tags := make([]*api.Release, len(rawTags))
for i, rawTag := range rawTags {
for j, rel := range rels {
if rel == nil || (rel.IsDraft && !ctx.Repo.IsOwner()) {
continue
}
if rel.TagName == rawTag {
rel.Publisher, err = models.GetUserByID(rel.PublisherId)
if err != nil { if err != nil {
if models.IsErrUserNotExist(err) { ctx.Handle(500, "GetUserById", err)
publisher = models.NewFakeUser() 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 { } else {
log.Error(4, "GetUserByID: %v", err) rel.NumCommitsBehind = ctx.Repo.CommitsCount - rel.NumCommits
ctx.Status(500) }
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
} }
apiReleases[i] = ToApiRelease(rel, publisher)
} }
ctx.JSON(200, &apiReleases)
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
}
rel.Note = base.RenderMarkdownString(rel.Note, ctx.Repo.RepoLink)
tags = append(tags, ToApiRelease(rel))
}
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))
} }
Loading…
Cancel
Save