diff --git a/models/issue.go b/models/issue.go index 19f00d5f3..3a7097977 100755 --- a/models/issue.go +++ b/models/issue.go @@ -775,6 +775,38 @@ func (issue *Issue) ChangeContent(doer *User, content string) (err error) { return sess.Commit() } +// ChangeRef changes issue ref, as the given user. +func (issue *Issue) ChangeRef(doer *User, newRef string) (err error) { + oldRef := issue.Ref + issue.Ref = newRef + + sess := x.NewSession() + defer sess.Close() + if err = sess.Begin(); err != nil { + return err + } + + if err = updateIssueCols(sess, issue, "ref"); err != nil { + sess.Rollback() + return fmt.Errorf("UpdateIssueCols: %v", err) + } + + var opts = &CreateCommentOptions{ + Type: CommentTypeRef, + Doer: doer, + Repo: issue.Repo, + Issue: issue, + OldRef: oldRef, + NewRef: newRef, + } + if _, err = createComment(sess, opts); err != nil { + sess.Rollback() + return err + } + + return sess.Commit() +} + // GetTasks returns the amount of tasks in the issues content func (issue *Issue) GetTasks() int { return len(issueTasksPat.FindAllStringIndex(issue.Content, -1)) diff --git a/models/issue_comment.go b/models/issue_comment.go index 60d38452c..ec9441bde 100755 --- a/models/issue_comment.go +++ b/models/issue_comment.go @@ -48,6 +48,8 @@ const ( CommentTypePullRef // Labels changed CommentTypeLabel + // Ref changed + CommentTypeRef // Milestone changed CommentTypeMilestone // Assignees changed diff --git a/routers/repo/issue.go b/routers/repo/issue.go index 42a6b9609..875c02024 100755 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -432,7 +432,7 @@ func RetrieveRepoMetas(ctx *context.Context, repo *models.Repository, isPull boo return nil } - brs, _, err := ctx.Repo.GitRepo.GetBranches(0,0) + brs, _, err := ctx.Repo.GitRepo.GetBranches(0, 0) if err != nil { ctx.ServerError("GetBranches", err) return nil @@ -1302,6 +1302,29 @@ func UpdateIssueContent(ctx *context.Context) { }) } +// UpdateIssueRef change issue's code reference +func UpdateIssueRef(ctx *context.Context) { + issue := GetActionIssue(ctx) + if ctx.Written() { + return + } + + if !ctx.IsSigned || (ctx.User.ID != issue.PosterID && !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull)) { + ctx.Error(403) + return + } + + ref := ctx.Query("ref") + if err := issue_service.ChangeRef(issue, ctx.User, ref); err != nil { + ctx.ServerError("ChangeRef", err) + return + } + + ctx.JSON(200, map[string]interface{}{ + "ref": issue.Ref, + }) +} + // UpdateIssueMilestone change issue's milestone func UpdateIssueMilestone(ctx *context.Context) { issues := getActionIssues(ctx) diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 9df429e8b..7af42867c 100755 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -868,6 +868,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Group("/:index", func() { m.Post("/title", repo.UpdateIssueTitle) m.Post("/content", repo.UpdateIssueContent) + m.Post("/ref", repo.UpdateIssueRef) m.Post("/watch", repo.IssueWatch) m.Group("/dependency", func() { m.Post("/add", repo.AddDependency) diff --git a/services/issue/content.go b/services/issue/content.go index 1081e30b5..387930449 100644 --- a/services/issue/content.go +++ b/services/issue/content.go @@ -21,3 +21,12 @@ func ChangeContent(issue *models.Issue, doer *models.User, content string) (err return nil } + +// ChangeRef changes issue ref, as the given user. +func ChangeRef(issue *models.Issue, doer *models.User, ref string) (err error) { + if err := issue.ChangeRef(doer, ref); err != nil { + return err + } + + return nil +}