From adf9091e86d7a93ec043459995a4b4d4812cce18 Mon Sep 17 00:00:00 2001 From: Kim Lindhardt Madsen Date: Mon, 28 Sep 2015 22:00:35 +0200 Subject: [PATCH] releases also takes the raw tags just as the release page --- cmd/web.go | 2 + routers/api/v1/repo.go | 4 + routers/api/v1/repo_commits.go | 6 +- routers/api/v1/repo_releases.go | 151 ++++++++++++++++++++++++++------ 4 files changed, 131 insertions(+), 32 deletions(-) diff --git a/cmd/web.go b/cmd/web.go index 264c08d73..240a46ecb 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -237,6 +237,8 @@ func runWeb(ctx *cli.Context) { }, middleware.ApiReqToken()) m.Group("/:username/:reponame", func() { + m.Get("", v1.GetRepo) + m.Combo("/hooks").Get(v1.ListRepoHooks). Post(bind(api.CreateHookOption{}), v1.CreateRepoHook) m.Patch("/hooks/:id:int", bind(api.EditHookOption{}), v1.EditRepoHook) diff --git a/routers/api/v1/repo.go b/routers/api/v1/repo.go index 2e185caa0..c6d93070b 100644 --- a/routers/api/v1/repo.go +++ b/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})) } +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 func CreateRepo(ctx *middleware.Context, opt api.CreateRepoOption) { // Shouldn't reach this condition, but just in case. diff --git a/routers/api/v1/repo_commits.go b/routers/api/v1/repo_commits.go index 6c4a7fb40..bf51d6339 100644 --- a/routers/api/v1/repo_commits.go +++ b/routers/api/v1/repo_commits.go @@ -32,12 +32,12 @@ func ToApiCommit(commit *git.Commit) *api.Commit { } 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) { - - commit, err := ctx.Repo.GitRepo.GetCommit(ctx.Params(":commitid")) if err != nil { log.Error(4, "GetCommit: %v", err) diff --git a/routers/api/v1/repo_releases.go b/routers/api/v1/repo_releases.go index f9d84f766..56d6f4a81 100644 --- a/routers/api/v1/repo_releases.go +++ b/routers/api/v1/repo_releases.go @@ -9,52 +9,144 @@ import ( 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/middleware" "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{ - ID: release.Id, - Publisher: *ToApiUser(publisher), - TagName: release.TagName, - LowerTagName: release.LowerTagName, - Target: release.Target, - Title: release.Title, - Sha1: release.Sha1, - NumCommits: release.NumCommits, - Note: release.Note, - IsDraft: release.IsDraft, - IsPrerelease: release.IsPrerelease, - Created: release.Created, + ID: release.Id, + Publisher: *ToApiUser(release.Publisher), + TagName: release.TagName, + LowerTagName: release.LowerTagName, + Target: release.Target, + Title: release.Title, + Sha1: release.Sha1, + NumCommits: release.NumCommits, + Note: release.Note, + IsDraft: release.IsDraft, + IsPrerelease: release.IsPrerelease, + Created: release.Created, } } 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) if err != nil { - log.Error(4, "GetReleasesByRepoId: %v", err) - ctx.Status(500) + ctx.Handle(500, "GetReleasesByRepoId", err) return } - apiReleases := make([]*api.Release, len(rels)) - for i, rel := range rels { - publisher, err := models.GetUserByID(rel.PublisherId) - if err != nil { - if models.IsErrUserNotExist(err) { - publisher = models.NewFakeUser() - } else { - log.Error(4, "GetUserByID: %v", err) - ctx.Status(500) + // Temproray cache commits count of used branches to speed up. + countCache := make(map[string]int) + + 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 { + 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 } + + 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) { @@ -73,10 +165,10 @@ func ReleaseByName(ctx *middleware.Context) { ctx.Handle(422, "GetUserByID", err) 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{ RepoId: ctx.Repo.Repository.ID, + Publisher: ctx.User, PublisherId: ctx.User.Id, Title: form.Title, 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)) } \ No newline at end of file