Skip to content

Commit 5f6fc3d

Browse files
committed
First implementation of git appraise
Issue #189
1 parent 1257d43 commit 5f6fc3d

File tree

19 files changed

+3081
-11
lines changed

19 files changed

+3081
-11
lines changed

cmd/web.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ func runWeb(ctx *cli.Context) error {
536536
m.Get("/releases", repo.Releases)
537537
m.Get("/^:type(issues|pulls)$", repo.RetrieveLabels, repo.Issues)
538538
m.Get("/^:type(issues|pulls)$/:index", repo.ViewIssue)
539+
m.Get("/reviews", repo.Reviews)
539540
m.Get("/labels/", repo.RetrieveLabels, repo.Labels)
540541
m.Get("/milestones", repo.Milestones)
541542
}, context.RepoRef())

models/repo.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ type Repository struct {
189189
NumPulls int
190190
NumClosedPulls int
191191
NumOpenPulls int `xorm:"-"`
192+
NumOpenReviews int `xorm:"-"`
192193
NumMilestones int `xorm:"NOT NULL DEFAULT 0"`
193194
NumClosedMilestones int `xorm:"NOT NULL DEFAULT 0"`
194195
NumOpenMilestones int `xorm:"-"`
@@ -211,6 +212,7 @@ type Repository struct {
211212
ExternalTrackerStyle string
212213
ExternalMetas map[string]string `xorm:"-"`
213214
EnablePulls bool `xorm:"NOT NULL DEFAULT true"`
215+
EnableReviews bool `xorm:"NOT NULL DEFAULT true"`
214216

215217
IsFork bool `xorm:"INDEX NOT NULL DEFAULT false"`
216218
ForkID int64 `xorm:"INDEX"`
@@ -485,6 +487,11 @@ func (repo *Repository) AllowsPulls() bool {
485487
return repo.CanEnablePulls() && repo.EnablePulls
486488
}
487489

490+
// AllowsReviews returns true if repository meets the requirements of accepting pulls and has them enabled.
491+
func (repo *Repository) AllowsReviews() bool {
492+
return repo.EnableReviews
493+
}
494+
488495
// CanEnableEditor returns true if repository meets the requirements of web editor.
489496
func (repo *Repository) CanEnableEditor() bool {
490497
return !repo.IsMirror
@@ -1035,15 +1042,16 @@ func CreateRepository(u *User, opts CreateRepoOptions) (_ *Repository, err error
10351042
}
10361043

10371044
repo := &Repository{
1038-
OwnerID: u.ID,
1039-
Owner: u,
1040-
Name: opts.Name,
1041-
LowerName: strings.ToLower(opts.Name),
1042-
Description: opts.Description,
1043-
IsPrivate: opts.IsPrivate,
1044-
EnableWiki: true,
1045-
EnableIssues: true,
1046-
EnablePulls: true,
1045+
OwnerID: u.ID,
1046+
Owner: u,
1047+
Name: opts.Name,
1048+
LowerName: strings.ToLower(opts.Name),
1049+
Description: opts.Description,
1050+
IsPrivate: opts.IsPrivate,
1051+
EnableWiki: true,
1052+
EnableIssues: true,
1053+
EnablePulls: true,
1054+
EnableReviews: true,
10471055
}
10481056

10491057
sess := x.NewSession()

modules/auth/repo_form.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ type RepoSettingForm struct {
104104
TrackerURLFormat string
105105
TrackerIssueStyle string
106106
EnablePulls bool
107+
EnableReviews bool
107108
}
108109

109110
// Validate valideates the fields
@@ -267,7 +268,7 @@ type NewReleaseForm struct {
267268
Content string
268269
Draft string
269270
Prerelease bool
270-
Files []string
271+
Files []string
271272
}
272273

273274
// Validate valideates the fields
@@ -281,7 +282,7 @@ type EditReleaseForm struct {
281282
Content string `form:"content"`
282283
Draft string `form:"draft"`
283284
Prerelease bool `form:"prerelease"`
284-
Files []string
285+
Files []string
285286
}
286287

287288
// Validate valideates the fields

modules/context/repo.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"code.gitea.io/gitea/modules/log"
1616
"code.gitea.io/gitea/modules/setting"
1717
"github.com/Unknwon/com"
18+
"github.com/google/git-appraise/repository"
19+
"github.com/google/git-appraise/review"
1820
editorconfig "gopkg.in/editorconfig/editorconfig-core-go.v1"
1921
macaron "gopkg.in/macaron.v1"
2022
)
@@ -286,6 +288,15 @@ func RepoAssignment(args ...bool) macaron.Handler {
286288
ctx.Data["Branches"] = brs
287289
ctx.Data["BrancheCount"] = len(brs)
288290

291+
// Count open reviews
292+
if repo.AllowsReviews() {
293+
appraiseRepo, err := repository.NewGitRepo(ctx.Repo.GitRepo.Path)
294+
if err != nil {
295+
ctx.Handle(500, "OpenGitRepository", err)
296+
}
297+
ctx.Repo.Repository.NumOpenReviews = len(review.ListOpen(appraiseRepo))
298+
}
299+
289300
// If not branch selected, try default one.
290301
// If default branch doesn't exists, fall back to some other branch.
291302
if len(ctx.Repo.BranchName) == 0 {

options/locale/locale_en-US.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ branches = Branches
459459
tags = Tags
460460
issues = Issues
461461
pulls = Pull Requests
462+
reviews = Reviews
462463
labels = Labels
463464
milestones = Milestones
464465
commits = Commits
@@ -708,6 +709,7 @@ settings.tracker_issue_style.numeric = Numeric
708709
settings.tracker_issue_style.alphanumeric = Alphanumeric
709710
settings.tracker_url_format_desc = You can use placeholder <code>{user} {repo} {index}</code> for user name, repository name and issue index.
710711
settings.pulls_desc = Enable pull requests to accept public contributions
712+
settings.reviews_desc = Enable appraise reviews
711713
settings.danger_zone = Danger Zone
712714
settings.new_owner_has_same_repo = The new owner already has a repository with same name. Please choose another name.
713715
settings.convert = Convert To Regular Repository

routers/repo/reviews.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package repo
6+
7+
import (
8+
"strconv"
9+
"strings"
10+
"time"
11+
12+
"github.com/google/git-appraise/repository"
13+
"github.com/google/git-appraise/review"
14+
15+
"code.gitea.io/gitea/models"
16+
"code.gitea.io/gitea/modules/base"
17+
"code.gitea.io/gitea/modules/context"
18+
)
19+
20+
const (
21+
tplReviews base.TplName = "repo/review/list"
22+
)
23+
24+
// MustAllowReviews check if repository enable reviews
25+
func MustAllowReviews(ctx *context.Context) {
26+
if !ctx.Repo.Repository.AllowsReviews() {
27+
ctx.Handle(404, "MustAllowReviews", nil)
28+
return
29+
}
30+
}
31+
32+
// Review struct to Issue representation
33+
type Review struct {
34+
Index string
35+
Poster *models.User
36+
Title string
37+
Labels []*models.Label
38+
Milestone *models.Milestone
39+
Assignee *models.User
40+
NumComments int
41+
Created time.Time
42+
CreatedUnix int64
43+
Updated time.Time
44+
UpdatedUnix int64
45+
IsClosed bool
46+
IsRead bool
47+
IsPull bool
48+
}
49+
50+
// Reviews render issues page
51+
func Reviews(ctx *context.Context) {
52+
repo, err := repository.NewGitRepo(ctx.Repo.GitRepo.Path)
53+
if err != nil {
54+
ctx.Handle(500, "OpenGitRepository", err)
55+
}
56+
total := review.ListAll(repo)
57+
58+
issues := []*Review{}
59+
60+
for _, review := range total {
61+
timestamp, _ := strconv.Atoi(review.Request.Timestamp)
62+
time := time.Unix(int64(timestamp), 0)
63+
user, err := models.GetUserByEmail(review.Request.Requester)
64+
read := false
65+
if review.Resolved != nil && *review.Resolved {
66+
read = true
67+
}
68+
if err != nil {
69+
user = &models.User{Name: review.Request.Requester}
70+
}
71+
issue := Review{
72+
Index: review.Revision,
73+
Title: strings.Split(review.Request.Description, "\n\n")[0],
74+
Poster: user,
75+
Created: time,
76+
CreatedUnix: int64(timestamp),
77+
IsClosed: review.Submitted,
78+
IsRead: read,
79+
IsPull: true,
80+
}
81+
if !review.Submitted {
82+
issues = append(issues, &issue)
83+
}
84+
}
85+
86+
ctx.Data["Title"] = ctx.Tr("repo.reviews")
87+
ctx.Data["PageIsReviewList"] = true
88+
// ctx.Data["Page"] = 0
89+
ctx.Data["Issues"] = issues
90+
// ctx.Data["Milestones"], err = models.GetMilestonesByRepoID(repo.ID)
91+
// ctx.Data["Assignees"], err = repo.GetAssignees()
92+
ctx.Data["IssueStats"] = models.IssueStats{
93+
OpenCount: int64(len(issues)),
94+
ClosedCount: int64(len(total) - len(issues)),
95+
AllCount: int64(len(total))}
96+
// ctx.Data["SelectLabels"] = com.StrTo(selectLabels).MustInt64()
97+
ctx.Data["ViewType"] = "all"
98+
ctx.Data["SortType"] = ""
99+
// ctx.Data["MilestoneID"] = milestoneID
100+
// ctx.Data["AssigneeID"] = assigneeID
101+
ctx.Data["IsShowClosed"] = false
102+
ctx.HTML(200, tplIssues)
103+
}

routers/repo/setting.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
152152
repo.ExternalTrackerFormat = form.TrackerURLFormat
153153
repo.ExternalTrackerStyle = form.TrackerIssueStyle
154154
repo.EnablePulls = form.EnablePulls
155+
repo.EnableReviews = form.EnableReviews
155156

156157
if err := models.UpdateRepository(repo, false); err != nil {
157158
ctx.Handle(500, "UpdateRepository", err)

templates/repo/header.tmpl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@
6262
<i class="octicon octicon-git-pull-request"></i> {{.i18n.Tr "repo.pulls"}} <span class="ui {{if not .Repository.NumOpenPulls}}gray{{else}}blue{{end}} small label">{{.Repository.NumOpenPulls}}</span>
6363
</a>
6464
{{end}}
65+
{{if .Repository.AllowsReviews}}
66+
<a class="{{if .PageIsReviewList}}active{{end}} item" href="{{.RepoLink}}/reviews">
67+
<i class="octicon octicon-git-review-request"></i> {{.i18n.Tr "repo.reviews"}} <span class="ui {{if not .Repository.NumOpenReviews}}gray{{else}}blue{{end}} small label">{{.Repository.NumOpenReviews}}</span>
68+
</a>
69+
{{end}}
6570
<a class="{{if (or (.PageIsCommits) (.PageIsDiff))}}active{{end}} item" href="{{.RepoLink}}/commits/{{EscapePound .BranchName}}">
6671
<i class="octicon octicon-history"></i> {{.i18n.Tr "repo.commits"}} <span class="ui {{if not .CommitsCount}}gray{{else}}blue{{end}} small label">{{.CommitsCount}}</span>
6772
</a>

templates/repo/settings/options.tmpl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,16 @@
201201
</div>
202202
{{end}}
203203

204+
<div class="ui divider"></div>
205+
206+
<div class="inline field">
207+
<label>{{.i18n.Tr "repo.reviews"}}</label>
208+
<div class="ui checkbox">
209+
<input name="enable_reviews" type="checkbox" {{if .Repository.EnableReviews}}checked{{end}}>
210+
<label>{{.i18n.Tr "repo.settings.reviews_desc"}}</label>
211+
</div>
212+
</div>
213+
204214
<div class="ui divider"></div>
205215
<div class="field">
206216
<button class="ui green button">{{$.i18n.Tr "repo.settings.update_settings"}}</button>

0 commit comments

Comments
 (0)