Skip to content

Commit 2c858d6

Browse files
sokolovstaslafriks
authored andcommitted
First implementation of git appraise
Issue go-gitea#189
1 parent b0308d8 commit 2c858d6

File tree

21 files changed

+3108
-7
lines changed

21 files changed

+3108
-7
lines changed

models/repo.go

+11
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ type Repository struct {
190190
NumPulls int
191191
NumClosedPulls int
192192
NumOpenPulls int `xorm:"-"`
193+
NumOpenReviews int `xorm:"-"`
193194
NumMilestones int `xorm:"NOT NULL DEFAULT 0"`
194195
NumClosedMilestones int `xorm:"NOT NULL DEFAULT 0"`
195196
NumOpenMilestones int `xorm:"-"`
@@ -641,11 +642,21 @@ func (repo *Repository) CanEnablePulls() bool {
641642
return !repo.IsMirror && !repo.IsBare
642643
}
643644

645+
// CanEnableReviews returns true if repository meets the requirements of code reviewing.
646+
func (repo *Repository) CanEnableReviews() bool {
647+
return !repo.IsMirror && !repo.IsBare
648+
}
649+
644650
// AllowsPulls returns true if repository meets the requirements of accepting pulls and has them enabled.
645651
func (repo *Repository) AllowsPulls() bool {
646652
return repo.CanEnablePulls() && repo.EnableUnit(UnitTypePullRequests)
647653
}
648654

655+
// AllowsReviews returns true if repository meets the requirements of accepting pulls and has them enabled.
656+
func (repo *Repository) AllowsReviews() bool {
657+
return repo.CanEnableReviews() && repo.EnableUnit(UnitTypePullRequests)
658+
}
659+
649660
// CanEnableEditor returns true if repository meets the requirements of web editor.
650661
func (repo *Repository) CanEnableEditor() bool {
651662
return !repo.IsMirror

models/repo_unit.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) {
7575
switch colName {
7676
case "type":
7777
switch UnitType(Cell2Int64(val)) {
78-
case UnitTypeCode, UnitTypeIssues, UnitTypePullRequests, UnitTypeCommits, UnitTypeReleases,
78+
case UnitTypeCode, UnitTypeIssues, UnitTypePullRequests, UnitTypeReviews, UnitTypeCommits, UnitTypeReleases,
7979
UnitTypeWiki, UnitTypeSettings:
8080
r.Config = new(UnitConfig)
8181
case UnitTypeExternalWiki:
@@ -116,6 +116,11 @@ func (r *RepoUnit) PullRequestsConfig() *UnitConfig {
116116
return r.Config.(*UnitConfig)
117117
}
118118

119+
// ReviewsConfig returns config for UnitTypeReviews
120+
func (r *RepoUnit) ReviewsConfig() *UnitConfig {
121+
return r.Config.(*UnitConfig)
122+
}
123+
119124
// CommitsConfig returns config for UnitTypeCommits
120125
func (r *RepoUnit) CommitsConfig() *UnitConfig {
121126
return r.Config.(*UnitConfig)

models/unit.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const (
1818
UnitTypeSettings // 7 Settings
1919
UnitTypeExternalWiki // 8 ExternalWiki
2020
UnitTypeExternalTracker // 9 ExternalTracker
21+
UnitTypeReviews // 10 Reviews
2122
)
2223

2324
var (
@@ -26,6 +27,7 @@ var (
2627
UnitTypeCode,
2728
UnitTypeIssues,
2829
UnitTypePullRequests,
30+
UnitTypeReviews,
2931
UnitTypeCommits,
3032
UnitTypeReleases,
3133
UnitTypeWiki,
@@ -39,6 +41,7 @@ var (
3941
UnitTypeCode,
4042
UnitTypeIssues,
4143
UnitTypePullRequests,
44+
UnitTypeReviews,
4245
UnitTypeCommits,
4346
UnitTypeReleases,
4447
UnitTypeWiki,
@@ -102,44 +105,52 @@ var (
102105
2,
103106
}
104107

108+
UnitReviews = Unit{
109+
UnitTypeReviews,
110+
"repo.reviews",
111+
"/reviews",
112+
"repo.reviews.desc",
113+
3,
114+
}
115+
105116
UnitCommits = Unit{
106117
UnitTypeCommits,
107118
"repo.commits",
108119
"/commits/master",
109120
"repo.commits.desc",
110-
3,
121+
4,
111122
}
112123

113124
UnitReleases = Unit{
114125
UnitTypeReleases,
115126
"repo.releases",
116127
"/releases",
117128
"repo.releases.desc",
118-
4,
129+
5,
119130
}
120131

121132
UnitWiki = Unit{
122133
UnitTypeWiki,
123134
"repo.wiki",
124135
"/wiki",
125136
"repo.wiki.desc",
126-
5,
137+
6,
127138
}
128139

129140
UnitExternalWiki = Unit{
130141
UnitTypeExternalWiki,
131142
"repo.ext_wiki",
132143
"/wiki",
133144
"repo.ext_wiki.desc",
134-
5,
145+
6,
135146
}
136147

137148
UnitSettings = Unit{
138149
UnitTypeSettings,
139150
"repo.settings",
140151
"/settings",
141152
"repo.settings.desc",
142-
6,
153+
7,
143154
}
144155

145156
// Units contains all the units
@@ -148,6 +159,7 @@ var (
148159
UnitTypeIssues: UnitIssues,
149160
UnitTypeExternalTracker: UnitExternalTracker,
150161
UnitTypePullRequests: UnitPullRequests,
162+
UnitTypeReviews: UnitReviews,
151163
UnitTypeCommits: UnitCommits,
152164
UnitTypeReleases: UnitReleases,
153165
UnitTypeWiki: UnitWiki,

modules/auth/repo_form.go

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ type RepoSettingForm struct {
103103
TrackerURLFormat string
104104
TrackerIssueStyle string
105105
EnablePulls bool
106+
EnableReviews bool
106107
}
107108

108109
// Validate validates the fields

modules/context/repo.go

+12
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"code.gitea.io/gitea/models"
1515
"code.gitea.io/gitea/modules/setting"
1616
"github.com/Unknwon/com"
17+
"github.com/google/git-appraise/repository"
18+
"github.com/google/git-appraise/review"
1719
editorconfig "gopkg.in/editorconfig/editorconfig-core-go.v1"
1820
macaron "gopkg.in/macaron.v1"
1921
)
@@ -309,6 +311,15 @@ func RepoAssignment() macaron.Handler {
309311
ctx.Data["Branches"] = brs
310312
ctx.Data["BrancheCount"] = len(brs)
311313

314+
// Count open reviews
315+
if repo.AllowsReviews() {
316+
appraiseRepo, err := repository.NewGitRepo(ctx.Repo.GitRepo.Path)
317+
if err != nil {
318+
ctx.Handle(500, "OpenGitRepository", err)
319+
}
320+
ctx.Repo.Repository.NumOpenReviews = len(review.ListOpen(appraiseRepo))
321+
}
322+
312323
// If not branch selected, try default one.
313324
// If default branch doesn't exists, fall back to some other branch.
314325
if len(ctx.Repo.BranchName) == 0 {
@@ -545,6 +556,7 @@ func UnitTypes() macaron.Handler {
545556
ctx.Data["UnitTypeCode"] = models.UnitTypeCode
546557
ctx.Data["UnitTypeIssues"] = models.UnitTypeIssues
547558
ctx.Data["UnitTypePullRequests"] = models.UnitTypePullRequests
559+
ctx.Data["UnitTypeReviews"] = models.UnitTypeReviews
548560
ctx.Data["UnitTypeCommits"] = models.UnitTypeCommits
549561
ctx.Data["UnitTypeReleases"] = models.UnitTypeReleases
550562
ctx.Data["UnitTypeWiki"] = models.UnitTypeWiki

options/locale/locale_en-US.ini

+2
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ branches = Branches
513513
tags = Tags
514514
issues = Issues
515515
pulls = Pull Requests
516+
reviews = Reviews
516517
labels = Labels
517518
milestones = Milestones
518519
commits = Commits
@@ -798,6 +799,7 @@ settings.tracker_issue_style.numeric = Numeric
798799
settings.tracker_issue_style.alphanumeric = Alphanumeric
799800
settings.tracker_url_format_desc = You can use placeholder <code>{user} {repo} {index}</code> for user name, repository name and issue index.
800801
settings.pulls_desc = Enable pull requests to accept public contributions
802+
settings.reviews_desc = Enable appraise reviews
801803
settings.danger_zone = Danger Zone
802804
settings.new_owner_has_same_repo = The new owner already has a repository with same name. Please choose another name.
803805
settings.convert = Convert To Regular Repository

routers/repo/reviews.go

+103
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

+9
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
215215
})
216216
}
217217

218+
if form.EnableReviews {
219+
units = append(units, models.RepoUnit{
220+
RepoID: repo.ID,
221+
Type: models.UnitTypeReviews,
222+
Index: int(models.UnitTypeReviews),
223+
Config: new(models.UnitConfig),
224+
})
225+
}
226+
218227
if err := models.UpdateRepositoryUnits(repo, units); err != nil {
219228
ctx.Handle(500, "UpdateRepositoryUnits", err)
220229
return

routers/routes/routes.go

+2
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,8 @@ func RegisterRoutes(m *macaron.Macaron) {
584584
m.Post("/merge", reqRepoWriter, repo.MergePullRequest)
585585
}, repo.MustAllowPulls, context.CheckUnit(models.UnitTypePullRequests))
586586

587+
m.Get("/reviews", repo.Reviews)
588+
587589
m.Group("", func() {
588590
m.Get("/src/*", repo.SetEditorconfigIfExists, repo.Home)
589591
m.Get("/raw/*", repo.SingleDownload)

templates/repo/header.tmpl

+5-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@
7272
<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>
7373
</a>
7474
{{end}}
75-
75+
{{if .Repository.EnableUnit $.UnitTypeReviews}}
76+
<a class="{{if .PageIsReviewList}}active{{end}} item" href="{{.RepoLink}}/reviews">
77+
<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>
78+
</a>
79+
{{end}}
7680
{{if and (.Repository.EnableUnit $.UnitTypeCommits) (not .IsBareRepo)}}
7781
<a class="{{if (or (.PageIsCommits) (.PageIsDiff))}}active{{end}} item" href="{{.RepoLink}}/commits/{{EscapePound .BranchName}}">
7882
<i class="octicon octicon-history"></i> {{.i18n.Tr "repo.commits"}} <span class="ui {{if not .CommitsCount}}gray{{else}}blue{{end}} small label">{{.CommitsCount}}</span>

templates/repo/settings/options.tmpl

+12
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,18 @@
187187
</div>
188188
{{end}}
189189

190+
{{if .Repository.CanEnableReviews}}
191+
<div class="ui divider"></div>
192+
193+
<div class="inline field">
194+
<label>{{.i18n.Tr "repo.reviews"}}</label>
195+
<div class="ui checkbox">
196+
<input name="enable_reviews" type="checkbox" {{if .Repository.EnableUnit $.UnitTypeReviews}}checked{{end}}>
197+
<label>{{.i18n.Tr "repo.settings.reviews_desc"}}</label>
198+
</div>
199+
</div>
200+
{{end}}
201+
190202
<div class="ui divider"></div>
191203
<div class="field">
192204
<button class="ui green button">{{$.i18n.Tr "repo.settings.update_settings"}}</button>

0 commit comments

Comments
 (0)