From 750a691f88d605054ecc090f9a9f649ae37506c4 Mon Sep 17 00:00:00 2001 From: Kim Lindhardt Madsen Date: Fri, 11 Sep 2015 15:39:52 +0200 Subject: [PATCH] commits service and releases service --- cmd/web.go | 10 ++- routers/api/v1/repo_commits.go | 38 ++++------- routers/api/v1/repo_releases.go | 109 ++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+), 28 deletions(-) create mode 100644 routers/api/v1/repo_releases.go diff --git a/cmd/web.go b/cmd/web.go index 13dfdb9ef..db2f483bc 100644 --- a/cmd/web.go +++ b/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()) }) diff --git a/routers/api/v1/repo_commits.go b/routers/api/v1/repo_commits.go index 151d575e4..f74176411 100644 --- a/routers/api/v1/repo_commits.go +++ b/routers/api/v1/repo_commits.go @@ -8,54 +8,38 @@ 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" ) func ToApiSignature(signature *git.Signature) *api.Signature { return &api.Signature{ - Email: signature.Email, - Name: signature.Name, - When: signature.When, + Email: signature.Email, + Name: signature.Name, + When: signature.When, } } func ToApiCommit(commit *git.Commit) *api.Commit { return &api.Commit{ - ID: commit.Id.String(), - Author: *ToApiSignature(commit.Author), - Committer: *ToApiSignature(commit.Committer), + ID: commit.Id.String(), + Author: *ToApiSignature(commit.Author), + Committer: *ToApiSignature(commit.Committer), CommitMessage: commit.CommitMessage, } } -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 } diff --git a/routers/api/v1/repo_releases.go b/routers/api/v1/repo_releases.go new file mode 100644 index 000000000..863abd7b0 --- /dev/null +++ b/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)) +} \ No newline at end of file