Reference issues from pull requests and other issues (#8137)
* Update ref comment * Generate comment on simple ref * Make fmt + remove unneeded repo load * Add TODO comments * Add ref-check in issue creation; re-arrange template * Make unit tests pass; rearrange code * Make fmt * Filter out xref comment if user can't see the referencing issue * Add TODOs * Add cross reference * Rearrange code; add cross-repository references * Striketrhough obsolete references * Remove unnecesary TODO * Add "not supported" note * Support for edits and deletes, and issue title * Revert changes to go.mod * Fix fmt * Add support for xref from API * Add first integration test * Add integration tests * Correct formatting * Fix add comment test * Add migration * Remove outdated comments; fix typo * Some code refactoring and rearranging * Rename findCrossReferences to createCrossReferences * Delete xrefs when repository is deleted * Corrections as suggested by @lafriks * Prepare for merge * Fix log for errors
This commit is contained in:
parent
8a0379d68a
commit
2a2b46c62e
10 changed files with 613 additions and 19 deletions
|
@ -595,8 +595,9 @@ func (issue *Issue) ClearLabels(doer *User) (err error) {
|
|||
if err = sess.Commit(); err != nil {
|
||||
return fmt.Errorf("Commit: %v", err)
|
||||
}
|
||||
sess.Close()
|
||||
|
||||
if err = issue.loadPoster(x); err != nil {
|
||||
if err = issue.LoadPoster(); err != nil {
|
||||
return fmt.Errorf("loadPoster: %v", err)
|
||||
}
|
||||
|
||||
|
@ -870,9 +871,18 @@ func (issue *Issue) ChangeTitle(doer *User, title string) (err error) {
|
|||
return fmt.Errorf("createChangeTitleComment: %v", err)
|
||||
}
|
||||
|
||||
if err = issue.neuterCrossReferences(sess); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = issue.addCrossReferences(sess, doer); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = sess.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
sess.Close()
|
||||
|
||||
mode, _ := AccessLevel(issue.Poster, issue.Repo)
|
||||
if issue.IsPull {
|
||||
|
@ -939,9 +949,26 @@ func (issue *Issue) ChangeContent(doer *User, content string) (err error) {
|
|||
oldContent := issue.Content
|
||||
issue.Content = content
|
||||
|
||||
if err = UpdateIssueCols(issue, "content"); err != nil {
|
||||
sess := x.NewSession()
|
||||
defer sess.Close()
|
||||
if err = sess.Begin(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = updateIssueCols(sess, issue, "content"); err != nil {
|
||||
return fmt.Errorf("UpdateIssueCols: %v", err)
|
||||
}
|
||||
if err = issue.neuterCrossReferences(sess); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = issue.addCrossReferences(sess, doer); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = sess.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
sess.Close()
|
||||
|
||||
mode, _ := AccessLevel(issue.Poster, issue.Repo)
|
||||
if issue.IsPull {
|
||||
|
@ -1171,8 +1198,10 @@ func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
return opts.Issue.loadAttributes(e)
|
||||
if err = opts.Issue.loadAttributes(e); err != nil {
|
||||
return err
|
||||
}
|
||||
return opts.Issue.addCrossReferences(e, doer)
|
||||
}
|
||||
|
||||
// NewIssue creates new issue with labels for repository.
|
||||
|
@ -1199,6 +1228,7 @@ func NewIssue(repo *Repository, issue *Issue, labelIDs []int64, assigneeIDs []in
|
|||
if err = sess.Commit(); err != nil {
|
||||
return fmt.Errorf("Commit: %v", err)
|
||||
}
|
||||
sess.Close()
|
||||
|
||||
if err = NotifyWatchers(&Action{
|
||||
ActUserID: issue.Poster.ID,
|
||||
|
@ -1808,7 +1838,24 @@ func updateIssue(e Engine, issue *Issue) error {
|
|||
|
||||
// UpdateIssue updates all fields of given issue.
|
||||
func UpdateIssue(issue *Issue) error {
|
||||
return updateIssue(x, issue)
|
||||
sess := x.NewSession()
|
||||
defer sess.Close()
|
||||
if err := sess.Begin(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := updateIssue(sess, issue); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := issue.neuterCrossReferences(sess); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := issue.loadPoster(sess); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := issue.addCrossReferences(sess, issue.Poster); err != nil {
|
||||
return err
|
||||
}
|
||||
return sess.Commit()
|
||||
}
|
||||
|
||||
// UpdateIssueDeadline updates an issue deadline and adds comments. Setting a deadline to 0 means deleting it.
|
||||
|
|
|
@ -142,14 +142,30 @@ type Comment struct {
|
|||
Review *Review `xorm:"-"`
|
||||
ReviewID int64 `xorm:"index"`
|
||||
Invalidated bool
|
||||
|
||||
// Reference an issue or pull from another comment, issue or PR
|
||||
// All information is about the origin of the reference
|
||||
RefRepoID int64 `xorm:"index"` // Repo where the referencing
|
||||
RefIssueID int64 `xorm:"index"`
|
||||
RefCommentID int64 `xorm:"index"` // 0 if origin is Issue title or content (or PR's)
|
||||
RefAction XRefAction `xorm:"SMALLINT"` // What hapens if RefIssueID resolves
|
||||
RefIsPull bool
|
||||
|
||||
RefRepo *Repository `xorm:"-"`
|
||||
RefIssue *Issue `xorm:"-"`
|
||||
RefComment *Comment `xorm:"-"`
|
||||
}
|
||||
|
||||
// LoadIssue loads issue from database
|
||||
func (c *Comment) LoadIssue() (err error) {
|
||||
return c.loadIssue(x)
|
||||
}
|
||||
|
||||
func (c *Comment) loadIssue(e Engine) (err error) {
|
||||
if c.Issue != nil {
|
||||
return nil
|
||||
}
|
||||
c.Issue, err = GetIssueByID(c.IssueID)
|
||||
c.Issue, err = getIssueByID(e, c.IssueID)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -527,6 +543,11 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err
|
|||
TreePath: opts.TreePath,
|
||||
ReviewID: opts.ReviewID,
|
||||
Patch: opts.Patch,
|
||||
RefRepoID: opts.RefRepoID,
|
||||
RefIssueID: opts.RefIssueID,
|
||||
RefCommentID: opts.RefCommentID,
|
||||
RefAction: opts.RefAction,
|
||||
RefIsPull: opts.RefIsPull,
|
||||
}
|
||||
if _, err = e.Insert(comment); err != nil {
|
||||
return nil, err
|
||||
|
@ -540,6 +561,10 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if err = comment.addCrossReferences(e, opts.Doer); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return comment, nil
|
||||
}
|
||||
|
||||
|
@ -794,6 +819,11 @@ type CreateCommentOptions struct {
|
|||
ReviewID int64
|
||||
Content string
|
||||
Attachments []string // UUIDs of attachments
|
||||
RefRepoID int64
|
||||
RefIssueID int64
|
||||
RefCommentID int64
|
||||
RefAction XRefAction
|
||||
RefIsPull bool
|
||||
}
|
||||
|
||||
// CreateComment creates comment of issue or commit.
|
||||
|
@ -934,23 +964,35 @@ func FindComments(opts FindCommentsOptions) ([]*Comment, error) {
|
|||
|
||||
// UpdateComment updates information of comment.
|
||||
func UpdateComment(doer *User, c *Comment, oldContent string) error {
|
||||
if _, err := x.ID(c.ID).AllCols().Update(c); err != nil {
|
||||
sess := x.NewSession()
|
||||
defer sess.Close()
|
||||
if err := sess.Begin(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := sess.ID(c.ID).AllCols().Update(c); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.loadIssue(sess); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.neuterCrossReferences(sess); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.addCrossReferences(sess, doer); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := sess.Commit(); err != nil {
|
||||
return fmt.Errorf("Commit: %v", err)
|
||||
}
|
||||
sess.Close()
|
||||
|
||||
if err := c.LoadPoster(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.LoadIssue(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := c.Issue.LoadAttributes(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.loadPoster(x); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mode, _ := AccessLevel(doer, c.Issue.Repo)
|
||||
if err := PrepareWebhooks(c.Issue.Repo, HookEventIssueComment, &api.IssueCommentPayload{
|
||||
|
@ -996,6 +1038,10 @@ func DeleteComment(doer *User, comment *Comment) error {
|
|||
return err
|
||||
}
|
||||
|
||||
if err := comment.neuterCrossReferences(sess); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := sess.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -1014,6 +1060,9 @@ func DeleteComment(doer *User, comment *Comment) error {
|
|||
if err := comment.loadPoster(x); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := comment.neuterCrossReferences(x); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mode, _ := AccessLevel(doer, comment.Issue.Repo)
|
||||
|
||||
|
|
308
models/issue_xref.go
Normal file
308
models/issue_xref.go
Normal file
|
@ -0,0 +1,308 @@
|
|||
// 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"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
|
||||
"github.com/go-xorm/xorm"
|
||||
"github.com/unknwon/com"
|
||||
)
|
||||
|
||||
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 // 0
|
||||
// XRefActionCloses means the cross-reference should close an issue if it is resolved
|
||||
XRefActionCloses // 1 - not implemented yet
|
||||
// XRefActionReopens means the cross-reference should reopen an issue if it is resolved
|
||||
XRefActionReopens // 2 - Not implemented yet
|
||||
// XRefActionNeutered means the cross-reference will no longer affect the source
|
||||
XRefActionNeutered // 3
|
||||
)
|
||||
|
||||
type crossReference struct {
|
||||
Issue *Issue
|
||||
Action XRefAction
|
||||
}
|
||||
|
||||
// crossReferencesContext is context to pass along findCrossReference functions
|
||||
type crossReferencesContext struct {
|
||||
Type CommentType
|
||||
Doer *User
|
||||
OrigIssue *Issue
|
||||
OrigComment *Comment
|
||||
}
|
||||
|
||||
func newCrossReference(e *xorm.Session, ctx *crossReferencesContext, xref *crossReference) error {
|
||||
var refCommentID int64
|
||||
if ctx.OrigComment != nil {
|
||||
refCommentID = ctx.OrigComment.ID
|
||||
}
|
||||
_, err := createComment(e, &CreateCommentOptions{
|
||||
Type: ctx.Type,
|
||||
Doer: ctx.Doer,
|
||||
Repo: xref.Issue.Repo,
|
||||
Issue: xref.Issue,
|
||||
RefRepoID: ctx.OrigIssue.RepoID,
|
||||
RefIssueID: ctx.OrigIssue.ID,
|
||||
RefCommentID: refCommentID,
|
||||
RefAction: xref.Action,
|
||||
RefIsPull: xref.Issue.IsPull,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func neuterCrossReferences(e Engine, issueID int64, commentID int64) error {
|
||||
active := make([]*Comment, 0, 10)
|
||||
sess := e.Where("`ref_action` IN (?, ?, ?)", XRefActionNone, XRefActionCloses, XRefActionReopens)
|
||||
if issueID != 0 {
|
||||
sess = sess.And("`ref_issue_id` = ?", issueID)
|
||||
}
|
||||
if commentID != 0 {
|
||||
sess = sess.And("`ref_comment_id` = ?", commentID)
|
||||
}
|
||||
if err := sess.Find(&active); err != nil || len(active) == 0 {
|
||||
return err
|
||||
}
|
||||
ids := make([]int64, len(active))
|
||||
for i, c := range active {
|
||||
ids[i] = c.ID
|
||||
}
|
||||
_, err := e.In("id", ids).Cols("`ref_action`").Update(&Comment{RefAction: XRefActionNeutered})
|
||||
return err
|
||||
}
|
||||
|
||||
// .___
|
||||
// | | ______ ________ __ ____
|
||||
// | |/ ___// ___/ | \_/ __ \
|
||||
// | |\___ \ \___ \| | /\ ___/
|
||||
// |___/____ >____ >____/ \___ >
|
||||
// \/ \/ \/
|
||||
//
|
||||
|
||||
func (issue *Issue) addCrossReferences(e *xorm.Session, doer *User) error {
|
||||
var commentType CommentType
|
||||
if issue.IsPull {
|
||||
commentType = CommentTypePullRef
|
||||
} else {
|
||||
commentType = CommentTypeIssueRef
|
||||
}
|
||||
ctx := &crossReferencesContext{
|
||||
Type: commentType,
|
||||
Doer: doer,
|
||||
OrigIssue: issue,
|
||||
}
|
||||
return issue.createCrossReferences(e, ctx, issue.Title+"\n"+issue.Content)
|
||||
}
|
||||
|
||||
func (issue *Issue) createCrossReferences(e *xorm.Session, ctx *crossReferencesContext, content string) error {
|
||||
xreflist, err := ctx.OrigIssue.getCrossReferences(e, ctx, content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, xref := range xreflist {
|
||||
if err = newCrossReference(e, ctx, xref); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (issue *Issue) getCrossReferences(e *xorm.Session, ctx *crossReferencesContext, 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 index, err := strconv.ParseInt(match[1], 10, 64); err == nil {
|
||||
if err = ctx.OrigIssue.loadRepo(e); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if xref, err = ctx.OrigIssue.isValidCommentReference(e, ctx, issue.Repo, index); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if xref != nil {
|
||||
xreflist = ctx.OrigIssue.updateCrossReferenceList(xreflist, xref)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Issues in other repositories
|
||||
matches = crossReferenceIssueNumericPattern.FindAllStringSubmatch(content, -1)
|
||||
for _, match := range matches {
|
||||
if index, err := strconv.ParseInt(match[3], 10, 64); err == nil {
|
||||
repo, err := getRepositoryByOwnerAndName(e, match[1], match[2])
|
||||
if err != nil {
|
||||
if IsErrRepoNotExist(err) {
|
||||
continue
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if err = ctx.OrigIssue.loadRepo(e); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if xref, err = issue.isValidCommentReference(e, ctx, repo, index); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if xref != nil {
|
||||
xreflist = issue.updateCrossReferenceList(xreflist, xref)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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) isValidCommentReference(e Engine, ctx *crossReferencesContext, 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 != ctx.OrigIssue.Repo.ID {
|
||||
perm, err := getUserRepoPermission(e, refIssue.Repo, ctx.Doer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !perm.CanReadIssuesOrPulls(refIssue.IsPull) {
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
return &crossReference{
|
||||
Issue: refIssue,
|
||||
Action: XRefActionNone,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (issue *Issue) neuterCrossReferences(e Engine) error {
|
||||
return neuterCrossReferences(e, issue.ID, 0)
|
||||
}
|
||||
|
||||
// _________ __
|
||||
// \_ ___ \ ____ _____ _____ ____ _____/ |_
|
||||
// / \ \/ / _ \ / \ / \_/ __ \ / \ __\
|
||||
// \ \___( <_> ) Y Y \ Y Y \ ___/| | \ |
|
||||
// \______ /\____/|__|_| /__|_| /\___ >___| /__|
|
||||
// \/ \/ \/ \/ \/
|
||||
//
|
||||
|
||||
func (comment *Comment) addCrossReferences(e *xorm.Session, doer *User) error {
|
||||
if comment.Type != CommentTypeCode && comment.Type != CommentTypeComment {
|
||||
return nil
|
||||
}
|
||||
if err := comment.loadIssue(e); err != nil {
|
||||
return err
|
||||
}
|
||||
ctx := &crossReferencesContext{
|
||||
Type: CommentTypeCommentRef,
|
||||
Doer: doer,
|
||||
OrigIssue: comment.Issue,
|
||||
OrigComment: comment,
|
||||
}
|
||||
return comment.Issue.createCrossReferences(e, ctx, comment.Content)
|
||||
}
|
||||
|
||||
func (comment *Comment) neuterCrossReferences(e Engine) error {
|
||||
return neuterCrossReferences(e, 0, comment.ID)
|
||||
}
|
||||
|
||||
// LoadRefComment loads comment that created this reference from database
|
||||
func (comment *Comment) LoadRefComment() (err error) {
|
||||
if comment.RefComment != nil {
|
||||
return nil
|
||||
}
|
||||
comment.RefComment, err = GetCommentByID(comment.RefCommentID)
|
||||
return
|
||||
}
|
||||
|
||||
// LoadRefIssue loads comment that created this reference from database
|
||||
func (comment *Comment) LoadRefIssue() (err error) {
|
||||
if comment.RefIssue != nil {
|
||||
return nil
|
||||
}
|
||||
comment.RefIssue, err = GetIssueByID(comment.RefIssueID)
|
||||
if err == nil {
|
||||
err = comment.RefIssue.loadRepo(x)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// CommentTypeIsRef returns true if CommentType is a reference from another issue
|
||||
func CommentTypeIsRef(t CommentType) bool {
|
||||
return t == CommentTypeCommentRef || t == CommentTypePullRef || t == CommentTypeIssueRef
|
||||
}
|
||||
|
||||
// RefCommentHTMLURL returns the HTML URL for the comment that created this reference
|
||||
func (comment *Comment) RefCommentHTMLURL() string {
|
||||
if err := comment.LoadRefComment(); err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("LoadRefComment(%d): %v", comment.RefCommentID, err)
|
||||
return ""
|
||||
}
|
||||
return comment.RefComment.HTMLURL()
|
||||
}
|
||||
|
||||
// RefIssueHTMLURL returns the HTML URL of the issue where this reference was created
|
||||
func (comment *Comment) RefIssueHTMLURL() string {
|
||||
if err := comment.LoadRefIssue(); err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("LoadRefIssue(%d): %v", comment.RefCommentID, err)
|
||||
return ""
|
||||
}
|
||||
return comment.RefIssue.HTMLURL()
|
||||
}
|
||||
|
||||
// RefIssueTitle returns the title of the issue where this reference was created
|
||||
func (comment *Comment) RefIssueTitle() string {
|
||||
if err := comment.LoadRefIssue(); err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("LoadRefIssue(%d): %v", comment.RefCommentID, err)
|
||||
return ""
|
||||
}
|
||||
return comment.RefIssue.Title
|
||||
}
|
||||
|
||||
// RefIssueIdent returns the user friendly identity (e.g. "#1234") of the issue where this reference was created
|
||||
func (comment *Comment) RefIssueIdent() string {
|
||||
if err := comment.LoadRefIssue(); err != nil { // Silently dropping errors :unamused:
|
||||
log.Error("LoadRefIssue(%d): %v", comment.RefCommentID, err)
|
||||
return ""
|
||||
}
|
||||
// FIXME: check this name for cross-repository references (#7901 if it gets merged)
|
||||
return "#" + com.ToStr(comment.RefIssue.Index)
|
||||
}
|
|
@ -244,6 +244,8 @@ var migrations = []Migration{
|
|||
NewMigration("add email notification enabled preference to user", addEmailNotificationEnabledToUser),
|
||||
// v94 -> v95
|
||||
NewMigration("add enable_status_check, status_check_contexts to protected_branch", addStatusCheckColumnsForProtectedBranches),
|
||||
// v95 -> v96
|
||||
NewMigration("add table columns for cross referencing issues", addCrossReferenceColumns),
|
||||
}
|
||||
|
||||
// Migrate database to current version
|
||||
|
|
20
models/migrations/v95.go
Normal file
20
models/migrations/v95.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
// 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 migrations
|
||||
|
||||
import "github.com/go-xorm/xorm"
|
||||
|
||||
func addCrossReferenceColumns(x *xorm.Engine) error {
|
||||
// Comment see models/comment.go
|
||||
type Comment struct {
|
||||
RefRepoID int64 `xorm:"index"`
|
||||
RefIssueID int64 `xorm:"index"`
|
||||
RefCommentID int64 `xorm:"index"`
|
||||
RefAction int64 `xorm:"SMALLINT"`
|
||||
RefIsPull bool
|
||||
}
|
||||
|
||||
return x.Sync2(new(Comment))
|
||||
}
|
|
@ -1810,6 +1810,7 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
|
|||
&Notification{RepoID: repoID},
|
||||
&CommitStatus{RepoID: repoID},
|
||||
&RepoIndexerStatus{RepoID: repoID},
|
||||
&Comment{RefRepoID: repoID},
|
||||
); err != nil {
|
||||
return fmt.Errorf("deleteBeans: %v", err)
|
||||
}
|
||||
|
@ -1956,8 +1957,12 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
|
|||
|
||||
// GetRepositoryByOwnerAndName returns the repository by given ownername and reponame.
|
||||
func GetRepositoryByOwnerAndName(ownerName, repoName string) (*Repository, error) {
|
||||
return getRepositoryByOwnerAndName(x, ownerName, repoName)
|
||||
}
|
||||
|
||||
func getRepositoryByOwnerAndName(e Engine, ownerName, repoName string) (*Repository, error) {
|
||||
var repo Repository
|
||||
has, err := x.Select("repository.*").
|
||||
has, err := e.Table("repository").Select("repository.*").
|
||||
Join("INNER", "`user`", "`user`.id = repository.owner_id").
|
||||
Where("repository.lower_name = ?", strings.ToLower(repoName)).
|
||||
And("`user`.lower_name = ?", strings.ToLower(ownerName)).
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue