Skip to content

Commit 21a2b4f

Browse files
committed
first step on comment in pull files
1 parent ccff571 commit 21a2b4f

File tree

11 files changed

+615
-3
lines changed

11 files changed

+615
-3
lines changed

models/issue_comment.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ const (
6060
CommentTypeAddTimeManual
6161
// Cancel a stopwatch for time tracking
6262
CommentTypeCancelTracking
63+
// Comment on pull files
64+
CommentTypePullFiles
6365
)
6466

6567
// CommentTag defines comment tag type
@@ -94,7 +96,8 @@ type Comment struct {
9496
NewTitle string
9597

9698
CommitID int64
97-
Line int64
99+
TreePath string
100+
Line int64 // + is left; - is right
98101
Content string `xorm:"TEXT"`
99102
RenderedContent string `xorm:"-"`
100103

@@ -210,6 +213,16 @@ func (c *Comment) EventTag() string {
210213
return "event-" + com.ToStr(c.ID)
211214
}
212215

216+
// LoadPoster loads poster from database
217+
func (c *Comment) LoadPoster() (err error) {
218+
if c.Poster != nil {
219+
return nil
220+
}
221+
222+
c.Poster, err = getUserByID(x, c.PosterID)
223+
return
224+
}
225+
213226
// LoadLabel if comment.Type is CommentTypeLabel, then load Label
214227
func (c *Comment) LoadLabel() error {
215228
var label Label
@@ -415,6 +428,32 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err
415428
return comment, nil
416429
}
417430

431+
// CreatePullFilesComment creates comment on pull files
432+
func CreatePullFilesComment(doer *User, repo *Repository, issue *Issue, lineNum int64, treePath, content string) (*Comment, error) {
433+
sess := x.NewSession()
434+
if err := sess.Begin(); err != nil {
435+
return nil, err
436+
}
437+
comment, err := createPullFilesComment(sess, doer, repo, issue, lineNum, treePath, content)
438+
if err != nil {
439+
return nil, err
440+
}
441+
442+
return comment, sess.Commit()
443+
}
444+
445+
func createPullFilesComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue, lineNum int64, treePath, content string) (*Comment, error) {
446+
return createComment(e, &CreateCommentOptions{
447+
Type: CommentTypePullFiles,
448+
Doer: doer,
449+
Repo: repo,
450+
Issue: issue,
451+
Content: content,
452+
TreePath: treePath,
453+
LineNum: lineNum,
454+
})
455+
}
456+
418457
func createStatusComment(e *xorm.Session, doer *User, repo *Repository, issue *Issue) (*Comment, error) {
419458
cmtType := CommentTypeClose
420459
if !issue.IsClosed {
@@ -502,6 +541,7 @@ type CreateCommentOptions struct {
502541
NewTitle string
503542
CommitID int64
504543
CommitSHA string
544+
TreePath string
505545
LineNum int64
506546
Content string
507547
Attachments []string // UUIDs of attachments

models/pull.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ type PullRequest struct {
7373
Merger *User `xorm:"-"`
7474
Merged time.Time `xorm:"-"`
7575
MergedUnix int64 `xorm:"INDEX"`
76+
77+
CodeComments map[string]map[int64][]*Comment `xorm:"-"`
7678
}
7779

7880
// BeforeUpdate is invoked from XORM before updating an object of this type.
@@ -127,6 +129,41 @@ func (pr *PullRequest) loadIssue(e Engine) (err error) {
127129
return err
128130
}
129131

132+
// LoadCodeComments loads pull request code comments from database
133+
func (pr *PullRequest) LoadCodeComments() error {
134+
return pr.loadCodeComments(x)
135+
}
136+
137+
func (pr *PullRequest) loadCodeComments(e Engine) error {
138+
if err := pr.loadIssue(e); err != nil {
139+
return err
140+
}
141+
142+
if pr.CodeComments != nil {
143+
return nil
144+
}
145+
146+
pr.CodeComments = make(map[string]map[int64][]*Comment)
147+
148+
return e.Where("issue_id = ?", pr.Issue.ID).
149+
And("type = ?", CommentTypePullFiles).
150+
Iterate(new(Comment), func(i int, bean interface{}) error {
151+
comment := bean.(*Comment)
152+
if err := comment.LoadPoster(); err != nil {
153+
return err
154+
}
155+
156+
if _, ok := pr.CodeComments[comment.TreePath]; !ok {
157+
pr.CodeComments[comment.TreePath] = make(map[int64][]*Comment)
158+
}
159+
if _, ok := pr.CodeComments[comment.TreePath][comment.Line]; !ok {
160+
pr.CodeComments[comment.TreePath][comment.Line] = make([]*Comment, 0)
161+
}
162+
pr.CodeComments[comment.TreePath][comment.Line] = append(pr.CodeComments[comment.TreePath][comment.Line], comment)
163+
return nil
164+
})
165+
}
166+
130167
// APIFormat assumes following fields have been assigned with valid values:
131168
// Required - Issue
132169
// Optional - Merger

models/user.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ func (u *User) AfterSet(colName string, _ xorm.Cell) {
165165
}
166166
}
167167

168+
// GetEmail returns an noreply email, if the user has set to keep his
169+
// email address private, otherwise the primary email address.
170+
func (u *User) GetEmail() string {
171+
return u.getEmail()
172+
}
173+
168174
// getEmail returns an noreply email, if the user has set to keep his
169175
// email address private, otherwise the primary email address.
170176
func (u *User) getEmail() string {

public/css/main.css

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
.ui.blue.button.code-comment {
2+
font-size: 14px;
3+
height: 100%;
4+
left: 0;
5+
padding: 0;
6+
padding-top: 2px;
7+
position: absolute;
8+
top: 0;
9+
width: 100%;
10+
display: none;
11+
}
12+
13+
.code-comment-section .single-comment .content {
14+
padding: 10px 5px;
15+
}
16+
17+
.repository .diff-file-box .code-diff .code-comment-section td {
18+
padding: 15px 12px;
19+
padding-top: 5px;
20+
border: 1px solid #dddddd;
21+
}
22+
23+
.repository .diff-file-box .file-body.file-code .lines-num-old {
24+
position: relative;
25+
}
26+
27+
.repository .diff-file-box .code-diff td .comment-code-cloud:before {
28+
content: " ";
29+
width: 0;
30+
height: 0;
31+
border-left: 13px solid transparent;
32+
border-right: 13px solid transparent;
33+
border-bottom: 13px solid #f1f1f1;
34+
left: 49px;
35+
position: absolute;
36+
top: -13px;
37+
}
38+
39+
.repository .diff-file-box .code-diff td .comment-code-cloud {
40+
width: 75%;
41+
padding: 14px 20px;
42+
margin: 0 auto;
43+
position: relative;
44+
border: 1px solid #f1f1f1;
45+
margin-top: 13px;
46+
}
47+
48+
.repository .diff-file-box .code-diff td .comment-code-cloud .attached.tab {
49+
border: none;
50+
padding: 0;
51+
margin: 0;
52+
}
53+
54+
.repository .diff-file-box .code-diff td .comment-code-cloud .right.menu .item {
55+
padding: 0.85714286em 0.442857em;
56+
}
57+
58+
.repository .diff-file-box .code-diff .comment-code-cloud textarea {
59+
border: 0px;
60+
}
61+
62+
.comment-code-cloud .ui.attached.tabular.menu {
63+
background: #f7f7f7;
64+
border: 1px solid #d4d4d5;
65+
padding-top: 5px;
66+
padding-left: 5px;
67+
}
68+
69+
.comment-code-cloud .ui.top.attached .item {
70+
cursor: pointer;
71+
}
72+
73+
.comment-code-cloud .markdown-info {
74+
display: inline-block;
75+
margin: 5px 0;
76+
font-size: 12px;
77+
}
78+
79+
.comment-code-cloud .preview-msg {
80+
min-height: 168px;
81+
}
82+
83+
.comment-code-cloud .footer {
84+
padding-top: 12px;
85+
border-top: 1px solid #f1f1f1;
86+
margin-top: 10px;
87+
}
88+

0 commit comments

Comments
 (0)