-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Reference issues from pull requests and other issues #8137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
78c3778
Update ref comment
guillep2k b4d5dd3
Generate comment on simple ref
guillep2k 235694a
Make fmt + remove unneeded repo load
guillep2k 64e4b42
Add TODO comments
guillep2k 7edef31
Add ref-check in issue creation; re-arrange template
guillep2k 2fbdf4c
Merge branch 'master' of github.com:go-gitea/gitea into xref-issues
guillep2k 11eb10c
Make unit tests pass; rearrange code
guillep2k e4c21f6
Make fmt
guillep2k 9f85400
Filter out xref comment if user can't see the referencing issue
guillep2k dd8cbe2
Add TODOs
guillep2k 272aeda
Merge branch 'master' of github.com:go-gitea/gitea into xref-issues
guillep2k ecba703
Add cross reference
guillep2k cc85b0f
Rearrange code; add cross-repository references
guillep2k c92b919
Merge branch 'master' of github.com:go-gitea/gitea into xref-issues
guillep2k 145e1a9
Striketrhough obsolete references
guillep2k 3fd20eb
Remove unnecesary TODO
guillep2k ba36bc6
Add "not supported" note
guillep2k 0bf9216
Support for edits and deletes, and issue title
guillep2k fefd405
Revert changes to go.mod
guillep2k 7f4aedf
Fix fmt
guillep2k 7fef0b7
Merge branch 'master' of github.com:go-gitea/gitea into xref-issues
guillep2k 4f24ee9
Add support for xref from API
guillep2k 2c83866
Add first integration test
guillep2k c4fcdd6
Add integration tests
guillep2k 31304f5
Correct formatting
guillep2k 8b2b0d6
Fix add comment test
guillep2k fcc6bf0
Add migration
guillep2k 610291c
Remove outdated comments; fix typo
guillep2k 5312037
Some code refactoring and rearranging
guillep2k ca2f7bf
Merge branch 'master' into xref-issues
guillep2k f7ffc51
Rename findCrossReferences to createCrossReferences
guillep2k 9ffb74b
Delete xrefs when repository is deleted
guillep2k 66392e1
Corrections as suggested by @lafriks
guillep2k 49bbe79
Prepare for merge
guillep2k d157ea8
Merge master into xref-issues
guillep2k 92246b5
Fix log for errors
guillep2k ec51461
Merge branch 'master' into xref-issues
guillep2k File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Make unit tests pass; rearrange code
- Loading branch information
commit 11eb10c4c8408ddcc9431ff2a1e6006c0d2670b5
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// Copyright 2019 The Gitea 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 models | ||
|
||
import ( | ||
"regexp" | ||
"strconv" | ||
|
||
"github.com/go-xorm/xorm" | ||
) | ||
|
||
var ( | ||
// TODO: Unify all regexp treatment of cross references in one place | ||
|
||
// issueNumericPattern matches string that references to a numeric issue, e.g. #1287 | ||
issueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(#[0-9]+)(?:\s|$|\)|\]|:|\.(\s|$))`) | ||
// crossReferenceIssueNumericPattern matches string that references a numeric issue in a different repository | ||
// e.g. gogits/gogs#12345 | ||
// crossReferenceIssueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([0-9a-zA-Z-_\.]+/[0-9a-zA-Z-_\.]+#[0-9]+)(?:\s|$|\)|\]|\.(\s|$))`) | ||
) | ||
|
||
// XRefAction represents the kind of effect a cross reference has once is resolved | ||
type XRefAction int64 | ||
|
||
const ( | ||
// XRefActionNone means the cross-reference is a mention (commit, etc.) | ||
XRefActionNone XRefAction = iota | ||
// XRefActionCloses means the cross-reference should close an issue if it is resolved | ||
XRefActionCloses // Not implemented yet | ||
// XRefActionReopens means the cross-reference should reopen an issue if it is resolved | ||
XRefActionReopens // Not implemented yet | ||
) | ||
|
||
type crossReference struct { | ||
Issue *Issue | ||
Action XRefAction | ||
} | ||
|
||
// ParseReferencesOptions represents a comment or issue that might make references to other issues | ||
type ParseReferencesOptions struct { | ||
Type CommentType | ||
Doer *User | ||
OrigIssue *Issue | ||
OrigComment *Comment | ||
} | ||
|
||
func (issue *Issue) parseCommentReferences(e *xorm.Session, refopts *ParseReferencesOptions, content string) error { | ||
xreflist, err := issue.getCrossReferences(e, refopts, content) | ||
if err != nil { | ||
return err | ||
} | ||
for _, xref := range xreflist { | ||
if err = addCommentReference(e, refopts, xref); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (issue *Issue) getCrossReferences(e *xorm.Session, refopts *ParseReferencesOptions, content string) ([]*crossReference, error) { | ||
xreflist := make([]*crossReference,0,5) | ||
var xref *crossReference | ||
|
||
// Issues in the same repository | ||
// FIXME: Should we support IssueNameStyleAlphanumeric? | ||
matches := issueNumericPattern.FindAllStringSubmatch(content, -1) | ||
for _, match := range matches { | ||
if i, err := strconv.ParseInt(match[1][1:], 10, 64); err == nil { | ||
if err = refopts.OrigIssue.loadRepo(e); err != nil { | ||
return nil, err | ||
} | ||
if xref, err = issue.checkCommentReference(e, refopts, issue.Repo, i); err != nil { | ||
return nil, err | ||
} | ||
if xref != nil { | ||
xreflist = issue.updateCrossReferenceList(xreflist, xref) | ||
} | ||
} | ||
} | ||
|
||
// Issues in other repositories | ||
// GAP: TODO: use crossReferenceIssueNumericPattern to parse references to other repos | ||
|
||
return xreflist, nil | ||
} | ||
|
||
func (issue *Issue) updateCrossReferenceList(list []*crossReference, xref *crossReference) []*crossReference { | ||
if xref.Issue.ID == issue.ID { | ||
return list | ||
} | ||
for i, r := range list { | ||
if r.Issue.ID == xref.Issue.ID { | ||
if xref.Action != XRefActionNone { | ||
list[i].Action = xref.Action | ||
} | ||
return list | ||
} | ||
} | ||
return append(list, xref) | ||
} | ||
|
||
func (issue *Issue) checkCommentReference(e *xorm.Session, refopts *ParseReferencesOptions, repo *Repository, index int64) (*crossReference, error) { | ||
refIssue := &Issue{RepoID: repo.ID, Index: index} | ||
if has, _ := e.Get(refIssue); !has { | ||
return nil, nil | ||
} | ||
if err := refIssue.loadRepo(e); err != nil { | ||
return nil, err | ||
} | ||
// Check user permissions | ||
if refIssue.Repo.ID != refopts.OrigIssue.Repo.ID { | ||
perm, err := getUserRepoPermission(e, refIssue.Repo, refopts.Doer) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !perm.CanReadIssuesOrPulls(refIssue.IsPull) { | ||
return nil, nil | ||
} | ||
} | ||
return &crossReference { | ||
Issue: refIssue, | ||
Action: XRefActionNone, | ||
}, nil | ||
} | ||
|
||
func addCommentReference(e *xorm.Session, refopts *ParseReferencesOptions, xref *crossReference) error { | ||
var refCommentID int64 | ||
if refopts.OrigComment != nil { | ||
refCommentID = refopts.OrigComment.ID | ||
} | ||
_, err := createComment(e, &CreateCommentOptions{ | ||
Type: refopts.Type, | ||
Doer: refopts.Doer, | ||
Repo: xref.Issue.Repo, | ||
Issue: xref.Issue, | ||
RefIssueID: refopts.OrigIssue.ID, | ||
RefCommentID: refCommentID, | ||
RefAction: xref.Action, | ||
}) | ||
return err | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.