Browse Source

commits service and releases service

pull/1630/head
Kim Lindhardt Madsen 9 years ago
parent
commit
750a691f88
  1. 10
      cmd/web.go
  2. 26
      routers/api/v1/repo_commits.go
  3. 109
      routers/api/v1/repo_releases.go

10
cmd/web.go

@ -243,7 +243,15 @@ func runWeb(ctx *cli.Context) {
m.Get("/raw/*", middleware.RepoRef(), v1.GetRepoRawFile)
m.Get("/archive/*", v1.GetRepoArchive)
m.Post("/forks", v1.ForkRepo)
m.Get("/commits/:commitId", v1.CommitById)
m.Get("/commits/:commitId", middleware.RepoRef(), v1.CommitById)
m.Get("/commits/current", middleware.RepoRef(), v1.CurrentCommit)
m.Group("/releases", func() {
m.Combo("").Get(v1.ListReleases).
Post(bindIgnErr(api.CreateReleaseOption{}), v1.CreateRelease)
m.Get("/:release", v1.ReleaseByName)
}, middleware.RepoRef())
}, middleware.ApiRepoAssignment(), middleware.ApiReqToken())
})

26
routers/api/v1/repo_commits.go

@ -8,7 +8,6 @@ import (
api "github.com/gogits/go-gogs-client"
"github.com/gogits/gogs/modules/git"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/middleware"
)
@ -30,32 +29,17 @@ func ToApiCommit(commit *git.Commit) *api.Commit {
}
}
func convertToGitRepo(repository *models.Repository) (*git.Repository, error) {
repoPath, err := repository.RepoPath()
if err != nil {
return nil, err
}
gitRepo, err := git.OpenRepository(repoPath)
if err != nil {
return nil, err
}
return gitRepo, nil
func CurrentCommit(ctx *middleware.Context) {
ctx.JSON(200, ToApiCommit(ctx.Repo.Commit))
}
func CommitById(ctx *middleware.Context) {
gitRepo, err := convertToGitRepo(ctx.Repo.Repository)
if err != nil {
log.Error(4, "convertToGitRepo: %v", err)
ctx.Error(500)
return
}
commit, err := gitRepo.GetCommit(ctx.Params(":commitId"))
commit, err := ctx.Repo.GitRepo.GetCommit(ctx.Params(":commitId"))
if err != nil {
log.Error(4, "GetCommit: %v", err)
ctx.Error(500, err)
ctx.Error(500, err.Error())
return
}

109
routers/api/v1/repo_releases.go

@ -0,0 +1,109 @@
// Copyright 2014 The Gogs 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 v1
import (
"errors"
api "github.com/gogits/go-gogs-client"
"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 {
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,
}
}
func ListReleases(ctx *middleware.Context) {
rels, err := models.GetReleasesByRepoId(ctx.Repo.Repository.ID)
if err != nil {
log.Error(4, "GetReleasesByRepoId: %v", err)
ctx.Status(500)
return
}
apiReleases := make([]*api.Release, len(rels))
for i, rel := range rels {
publisher, err := models.GetUserByID(rel.PublisherId)
if err != nil {
log.Error(4, "GetUserByID: %v", err)
return
}
apiReleases[i] = ToApiRelease(rel, publisher)
}
ctx.JSON(200, &apiReleases)
}
func ReleaseByName(ctx *middleware.Context) {
rel, err := models.GetRelease(ctx.Repo.Repository.ID, ctx.Params(":release"))
if err != nil {
log.Error(4, "GetRelease: %v", err)
ctx.Status(500)
return
}
publisher, err := models.GetUserByID(rel.PublisherId)
if err != nil {
log.Error(4, "GetUserByID: %v", err)
return
}
ctx.JSON(200, ToApiRelease(rel, publisher))
}
func CreateRelease(ctx *middleware.Context, form api.CreateReleaseOption) {
if !ctx.Repo.GitRepo.IsBranchExist(form.Target) {
ctx.Handle(500, "IsBranchExist", errors.New("Branch did not exist, " + form.Target))
return
}
commit, err := ctx.Repo.GitRepo.GetCommitOfBranch(form.Target)
if err != nil {
ctx.Handle(500, "GetCommitOfBranch", err)
return
}
commitsCount, err := commit.CommitsCount()
if err != nil {
ctx.Handle(500, "CommitsCount", err)
return
}
rel := &models.Release{
RepoId: ctx.Repo.Repository.ID,
PublisherId: ctx.User.Id,
Title: form.Title,
TagName: form.TagName,
Target: form.Target,
Sha1: commit.Id.String(),
NumCommits: commitsCount,
Note: form.Note,
IsDraft: form.IsDraft,
IsPrerelease: form.IsPrerelease,
}
models.CreateRelease(ctx.Repo.GitRepo, rel)
ctx.JSON(201, ToApiRelease(rel, ctx.User))
}
Loading…
Cancel
Save