mirror of https://github.com/gogits/gogs.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
333 lines
8.9 KiB
333 lines
8.9 KiB
11 years ago
|
// 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 repo
|
||
|
|
||
|
import (
|
||
9 years ago
|
"fmt"
|
||
8 years ago
|
"strings"
|
||
9 years ago
|
|
||
8 years ago
|
log "gopkg.in/clog.v1"
|
||
|
|
||
10 years ago
|
"github.com/gogits/gogs/models"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/context"
|
||
|
"github.com/gogits/gogs/pkg/form"
|
||
|
"github.com/gogits/gogs/pkg/markup"
|
||
|
"github.com/gogits/gogs/pkg/setting"
|
||
11 years ago
|
)
|
||
|
|
||
11 years ago
|
const (
|
||
8 years ago
|
RELEASES = "repo/release/list"
|
||
|
RELEASE_NEW = "repo/release/new"
|
||
11 years ago
|
)
|
||
|
|
||
8 years ago
|
// calReleaseNumCommitsBehind calculates given release has how many commits behind release target.
|
||
9 years ago
|
func calReleaseNumCommitsBehind(repoCtx *context.Repository, release *models.Release, countCache map[string]int64) error {
|
||
9 years ago
|
// Get count if not exists
|
||
|
if _, ok := countCache[release.Target]; !ok {
|
||
8 years ago
|
if repoCtx.GitRepo.IsBranchExist(release.Target) {
|
||
|
commit, err := repoCtx.GitRepo.GetBranchCommit(release.Target)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("GetBranchCommit: %v", err)
|
||
|
}
|
||
|
countCache[release.Target], err = commit.CommitsCount()
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("CommitsCount: %v", err)
|
||
|
}
|
||
|
} else {
|
||
|
// Use NumCommits of the newest release on that target
|
||
|
countCache[release.Target] = release.NumCommits
|
||
9 years ago
|
}
|
||
|
}
|
||
8 years ago
|
release.NumCommitsBehind = countCache[release.Target] - release.NumCommits
|
||
9 years ago
|
return nil
|
||
|
}
|
||
|
|
||
8 years ago
|
func Releases(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("repo.release.releases")
|
||
|
c.Data["PageIsViewFiles"] = true
|
||
|
c.Data["PageIsReleaseList"] = true
|
||
10 years ago
|
|
||
8 years ago
|
tagsResult, err := c.Repo.GitRepo.GetTagsAfter(c.Query("after"), 10)
|
||
10 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, fmt.Sprintf("GetTags '%s'", c.Repo.Repository.RepoPath()), err)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
releases, err := models.GetPublishedReleasesByRepoID(c.Repo.Repository.ID, tagsResult.Tags...)
|
||
10 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetPublishedReleasesByRepoID", err)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
10 years ago
|
// Temproray cache commits count of used branches to speed up.
|
||
9 years ago
|
countCache := make(map[string]int64)
|
||
10 years ago
|
|
||
8 years ago
|
results := make([]*models.Release, len(tagsResult.Tags))
|
||
8 years ago
|
for i, rawTag := range tagsResult.Tags {
|
||
9 years ago
|
for j, r := range releases {
|
||
8 years ago
|
if r == nil || r.TagName != rawTag {
|
||
10 years ago
|
continue
|
||
|
}
|
||
8 years ago
|
releases[j] = nil // Mark as used.
|
||
9 years ago
|
|
||
8 years ago
|
if err = r.LoadAttributes(); err != nil {
|
||
8 years ago
|
c.Handle(500, "LoadAttributes", err)
|
||
8 years ago
|
return
|
||
8 years ago
|
}
|
||
10 years ago
|
|
||
8 years ago
|
if err := calReleaseNumCommitsBehind(c.Repo, r, countCache); err != nil {
|
||
|
c.Handle(500, "calReleaseNumCommitsBehind", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
r.Note = string(markup.Markdown(r.Note, c.Repo.RepoLink, c.Repo.Repository.ComposeMetas()))
|
||
8 years ago
|
results[i] = r
|
||
|
break
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
// No published release matches this tag
|
||
|
if results[i] == nil {
|
||
8 years ago
|
commit, err := c.Repo.GitRepo.GetTagCommit(rawTag)
|
||
10 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetTagCommit", err)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
results[i] = &models.Release{
|
||
10 years ago
|
Title: rawTag,
|
||
|
TagName: rawTag,
|
||
9 years ago
|
Sha1: commit.ID.String(),
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
results[i].NumCommits, err = commit.CommitsCount()
|
||
10 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "CommitsCount", err)
|
||
10 years ago
|
return
|
||
|
}
|
||
8 years ago
|
results[i].NumCommitsBehind = c.Repo.CommitsCount - results[i].NumCommits
|
||
10 years ago
|
}
|
||
|
}
|
||
8 years ago
|
models.SortReleases(results)
|
||
|
|
||
|
// Only show drafts if user is viewing the latest page
|
||
|
var drafts []*models.Release
|
||
|
if tagsResult.HasLatest {
|
||
8 years ago
|
drafts, err = models.GetDraftReleasesByRepoID(c.Repo.Repository.ID)
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetDraftReleasesByRepoID", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
|
for _, r := range drafts {
|
||
|
if err = r.LoadAttributes(); err != nil {
|
||
8 years ago
|
c.Handle(500, "LoadAttributes", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
if err := calReleaseNumCommitsBehind(c.Repo, r, countCache); err != nil {
|
||
|
c.Handle(500, "calReleaseNumCommitsBehind", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
r.Note = string(markup.Markdown(r.Note, c.Repo.RepoLink, c.Repo.Repository.ComposeMetas()))
|
||
8 years ago
|
}
|
||
|
|
||
|
if len(drafts) > 0 {
|
||
|
results = append(drafts, results...)
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
c.Data["Releases"] = results
|
||
|
c.Data["HasPrevious"] = !tagsResult.HasLatest
|
||
|
c.Data["ReachEnd"] = tagsResult.ReachEnd
|
||
|
c.Data["PreviousAfter"] = tagsResult.PreviousAfter
|
||
8 years ago
|
if len(results) > 0 {
|
||
8 years ago
|
c.Data["NextAfter"] = results[len(results)-1].TagName
|
||
8 years ago
|
}
|
||
8 years ago
|
c.HTML(200, RELEASES)
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func renderReleaseAttachmentSettings(c *context.Context) {
|
||
|
c.Data["RequireDropzone"] = true
|
||
|
c.Data["IsAttachmentEnabled"] = setting.Release.Attachment.Enabled
|
||
|
c.Data["AttachmentAllowedTypes"] = strings.Join(setting.Release.Attachment.AllowedTypes, ",")
|
||
|
c.Data["AttachmentMaxSize"] = setting.Release.Attachment.MaxSize
|
||
|
c.Data["AttachmentMaxFiles"] = setting.Release.Attachment.MaxFiles
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func NewRelease(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("repo.release.new_release")
|
||
|
c.Data["PageIsReleaseList"] = true
|
||
|
c.Data["tag_target"] = c.Repo.Repository.DefaultBranch
|
||
|
renderReleaseAttachmentSettings(c)
|
||
|
c.HTML(200, RELEASE_NEW)
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func NewReleasePost(c *context.Context, f form.NewRelease) {
|
||
|
c.Data["Title"] = c.Tr("repo.release.new_release")
|
||
|
c.Data["PageIsReleaseList"] = true
|
||
|
renderReleaseAttachmentSettings(c)
|
||
10 years ago
|
|
||
8 years ago
|
if c.HasError() {
|
||
|
c.HTML(200, RELEASE_NEW)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
if !c.Repo.GitRepo.IsBranchExist(f.Target) {
|
||
|
c.RenderWithErr(c.Tr("form.target_branch_not_exist"), RELEASE_NEW, &f)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
// Use current time if tag not yet exist, otherwise get time from Git
|
||
8 years ago
|
var tagCreatedUnix int64
|
||
8 years ago
|
tag, err := c.Repo.GitRepo.GetTag(f.TagName)
|
||
8 years ago
|
if err == nil {
|
||
|
commit, err := tag.Commit()
|
||
|
if err == nil {
|
||
|
tagCreatedUnix = commit.Author.When.Unix()
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
commit, err := c.Repo.GitRepo.GetBranchCommit(f.Target)
|
||
10 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetBranchCommit", err)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
10 years ago
|
commitsCount, err := commit.CommitsCount()
|
||
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "CommitsCount", err)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
var attachments []string
|
||
|
if setting.Release.Attachment.Enabled {
|
||
|
attachments = f.Files
|
||
|
}
|
||
|
|
||
10 years ago
|
rel := &models.Release{
|
||
8 years ago
|
RepoID: c.Repo.Repository.ID,
|
||
|
PublisherID: c.User.ID,
|
||
8 years ago
|
Title: f.Title,
|
||
|
TagName: f.TagName,
|
||
|
Target: f.Target,
|
||
9 years ago
|
Sha1: commit.ID.String(),
|
||
10 years ago
|
NumCommits: commitsCount,
|
||
8 years ago
|
Note: f.Content,
|
||
|
IsDraft: len(f.Draft) > 0,
|
||
|
IsPrerelease: f.Prerelease,
|
||
8 years ago
|
CreatedUnix: tagCreatedUnix,
|
||
10 years ago
|
}
|
||
8 years ago
|
if err = models.NewRelease(c.Repo.GitRepo, rel, attachments); err != nil {
|
||
|
c.Data["Err_TagName"] = true
|
||
8 years ago
|
switch {
|
||
|
case models.IsErrReleaseAlreadyExist(err):
|
||
8 years ago
|
c.RenderWithErr(c.Tr("repo.release.tag_name_already_exist"), RELEASE_NEW, &f)
|
||
8 years ago
|
case models.IsErrInvalidTagName(err):
|
||
8 years ago
|
c.RenderWithErr(c.Tr("repo.release.tag_name_invalid"), RELEASE_NEW, &f)
|
||
8 years ago
|
default:
|
||
8 years ago
|
c.Handle(500, "NewRelease", err)
|
||
10 years ago
|
}
|
||
|
return
|
||
|
}
|
||
8 years ago
|
log.Trace("Release created: %s/%s:%s", c.User.LowerName, c.Repo.Repository.Name, f.TagName)
|
||
10 years ago
|
|
||
8 years ago
|
c.Redirect(c.Repo.RepoLink + "/releases")
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func EditRelease(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("repo.release.edit_release")
|
||
|
c.Data["PageIsReleaseList"] = true
|
||
|
c.Data["PageIsEditRelease"] = true
|
||
|
renderReleaseAttachmentSettings(c)
|
||
10 years ago
|
|
||
8 years ago
|
tagName := c.Params("*")
|
||
|
rel, err := models.GetRelease(c.Repo.Repository.ID, tagName)
|
||
10 years ago
|
if err != nil {
|
||
9 years ago
|
if models.IsErrReleaseNotExist(err) {
|
||
8 years ago
|
c.Handle(404, "GetRelease", err)
|
||
10 years ago
|
} else {
|
||
8 years ago
|
c.Handle(500, "GetRelease", err)
|
||
10 years ago
|
}
|
||
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["ID"] = rel.ID
|
||
|
c.Data["tag_name"] = rel.TagName
|
||
|
c.Data["tag_target"] = rel.Target
|
||
|
c.Data["title"] = rel.Title
|
||
|
c.Data["content"] = rel.Note
|
||
|
c.Data["attachments"] = rel.Attachments
|
||
|
c.Data["prerelease"] = rel.IsPrerelease
|
||
|
c.Data["IsDraft"] = rel.IsDraft
|
||
|
|
||
|
c.HTML(200, RELEASE_NEW)
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func EditReleasePost(c *context.Context, f form.EditRelease) {
|
||
|
c.Data["Title"] = c.Tr("repo.release.edit_release")
|
||
|
c.Data["PageIsReleaseList"] = true
|
||
|
c.Data["PageIsEditRelease"] = true
|
||
|
renderReleaseAttachmentSettings(c)
|
||
10 years ago
|
|
||
8 years ago
|
tagName := c.Params("*")
|
||
|
rel, err := models.GetRelease(c.Repo.Repository.ID, tagName)
|
||
10 years ago
|
if err != nil {
|
||
9 years ago
|
if models.IsErrReleaseNotExist(err) {
|
||
8 years ago
|
c.Handle(404, "GetRelease", err)
|
||
10 years ago
|
} else {
|
||
8 years ago
|
c.Handle(500, "GetRelease", err)
|
||
10 years ago
|
}
|
||
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["tag_name"] = rel.TagName
|
||
|
c.Data["tag_target"] = rel.Target
|
||
|
c.Data["title"] = rel.Title
|
||
|
c.Data["content"] = rel.Note
|
||
|
c.Data["attachments"] = rel.Attachments
|
||
|
c.Data["prerelease"] = rel.IsPrerelease
|
||
|
c.Data["IsDraft"] = rel.IsDraft
|
||
|
|
||
|
if c.HasError() {
|
||
|
c.HTML(200, RELEASE_NEW)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
var attachments []string
|
||
|
if setting.Release.Attachment.Enabled {
|
||
|
attachments = f.Files
|
||
|
}
|
||
|
|
||
8 years ago
|
isPublish := rel.IsDraft && len(f.Draft) == 0
|
||
8 years ago
|
rel.Title = f.Title
|
||
|
rel.Note = f.Content
|
||
|
rel.IsDraft = len(f.Draft) > 0
|
||
|
rel.IsPrerelease = f.Prerelease
|
||
8 years ago
|
if err = models.UpdateRelease(c.User, c.Repo.GitRepo, rel, isPublish, attachments); err != nil {
|
||
|
c.Handle(500, "UpdateRelease", err)
|
||
10 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Redirect(c.Repo.RepoLink + "/releases")
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
func UploadReleaseAttachment(c *context.Context) {
|
||
8 years ago
|
if !setting.Release.Attachment.Enabled {
|
||
8 years ago
|
c.NotFound()
|
||
8 years ago
|
return
|
||
|
}
|
||
8 years ago
|
uploadAttachment(c, setting.Release.Attachment.AllowedTypes)
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func DeleteRelease(c *context.Context) {
|
||
|
if err := models.DeleteReleaseOfRepoByID(c.Repo.Repository.ID, c.QueryInt64("id")); err != nil {
|
||
|
c.Flash.Error("DeleteReleaseByID: " + err.Error())
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Flash.Success(c.Tr("repo.release.deletion_success"))
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
c.JSON(200, map[string]interface{}{
|
||
|
"redirect": c.Repo.RepoLink + "/releases",
|
||
9 years ago
|
})
|
||
|
}
|