Exemple #1
0
func Releases(ctx *middleware.Context) {
	ctx.Data["Title"] = ctx.Tr("repo.release.releases")
	ctx.Data["PageIsReleaseList"] = true

	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 {
		ctx.Handle(500, "GetReleasesByRepoId", err)
		return
	}

	// Temproray cache commits count of used branches to speed up.
	countCache := make(map[string]int)

	tags := make([]*models.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] = 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] = &models.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
		}

		rel.Note = base.RenderMarkdownString(rel.Note, ctx.Repo.RepoLink)
		tags = append(tags, rel)
	}
	models.SortReleases(tags)
	ctx.Data["Releases"] = tags
	ctx.HTML(200, RELEASES)
}
Exemple #2
0
func Releases(ctx *middleware.Context) {
	ctx.Data["Title"] = "Releases"
	ctx.Data["IsRepoToolbarReleases"] = true
	ctx.Data["IsRepoReleaseNew"] = false
	rawTags, err := ctx.Repo.GitRepo.GetTags()
	if err != nil {
		ctx.Handle(500, "release.Releases(GetTags)", err)
		return
	}

	rels, err := models.GetReleasesByRepoId(ctx.Repo.Repository.Id)
	if err != nil {
		ctx.Handle(500, "release.Releases(GetReleasesByRepoId)", err)
		return
	}

	commitsCount, err := ctx.Repo.Commit.CommitsCount()
	if err != nil {
		ctx.Handle(500, "release.Releases(CommitsCount)", err)
		return
	}

	var tags ReleaseSorter
	tags.rels = make([]*models.Release, len(rawTags))
	for i, rawTag := range rawTags {
		for _, rel := range rels {
			if rel.TagName == rawTag {
				rel.Publisher, err = models.GetUserById(rel.PublisherId)
				if err != nil {
					ctx.Handle(500, "release.Releases(GetUserById)", err)
					return
				}
				rel.NumCommitsBehind = commitsCount - rel.NumCommits
				rel.Note = base.RenderMarkdownString(rel.Note, ctx.Repo.RepoLink)
				tags.rels[i] = rel
				break
			}
		}

		if tags.rels[i] == nil {
			commit, err := ctx.Repo.GitRepo.GetCommitOfTag(rawTag)
			if err != nil {
				ctx.Handle(500, "release.Releases(GetCommitOfTag)", err)
				return
			}

			tags.rels[i] = &models.Release{
				Title:   rawTag,
				TagName: rawTag,
				SHA1:    commit.Id.String(),
			}
			tags.rels[i].NumCommits, err = ctx.Repo.GitRepo.CommitsCount(commit.Id.String())
			if err != nil {
				ctx.Handle(500, "release.Releases(CommitsCount)", err)
				return
			}
			tags.rels[i].NumCommitsBehind = commitsCount - tags.rels[i].NumCommits
		}
	}

	sort.Sort(&tags)

	ctx.Data["Releases"] = tags.rels
	ctx.HTML(200, "release/list")
}
Exemple #3
0
func Releases(ctx *middleware.Context) {
	ctx.Data["Title"] = "Releases"
	ctx.Data["IsRepoToolbarReleases"] = true
	ctx.Data["IsRepoReleaseNew"] = false
	rawTags, err := ctx.Repo.GitRepo.GetTags()
	if err != nil {
		ctx.Handle(500, "release.Releases(GetTags)", err)
		return
	}

	rels, err := models.GetReleasesByRepoId(ctx.Repo.Repository.Id)
	if err != nil {
		ctx.Handle(500, "release.Releases(GetReleasesByRepoId)", err)
		return
	}

	commitsCount, err := ctx.Repo.Commit.CommitsCount()
	if err != nil {
		ctx.Handle(500, "release.Releases(CommitsCount)", err)
		return
	}

	// Temproray cache commits count of used branches to speed up.
	countCache := make(map[string]int)

	tags := make([]*models.Release, len(rawTags))
	for i, rawTag := range rawTags {
		for _, rel := range rels {
			if rel.IsDraft && !ctx.Repo.IsOwner {
				continue
			}
			if rel.TagName == rawTag {
				rel.Publisher, err = models.GetUserById(rel.PublisherId)
				if err != nil {
					ctx.Handle(500, "release.Releases(GetUserById)", err)
					return
				}
				// 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.GetCommitOfTag(rel.TagName)
						if err != nil {
							ctx.Handle(500, "release.Releases(GetCommitOfTag)", err)
							return
						}
						countCache[rel.Target], err = commit.CommitsCount()
						if err != nil {
							ctx.Handle(500, "release.Releases(CommitsCount2)", err)
							return
						}
					}
					rel.NumCommitsBehind = countCache[rel.Target] - rel.NumCommits
				} else {
					rel.NumCommitsBehind = commitsCount - rel.NumCommits
				}

				rel.Note = base.RenderMarkdownString(rel.Note, ctx.Repo.RepoLink)
				tags[i] = rel
				break
			}
		}

		if tags[i] == nil {
			commit, err := ctx.Repo.GitRepo.GetCommitOfTag(rawTag)
			if err != nil {
				ctx.Handle(500, "release.Releases(GetCommitOfTag2)", err)
				return
			}

			tags[i] = &models.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, "release.Releases(CommitsCount)", err)
				return
			}
			tags[i].NumCommitsBehind = commitsCount - tags[i].NumCommits
		}
	}
	models.SortReleases(tags)
	ctx.Data["Releases"] = tags
	ctx.HTML(200, RELEASES)
}