Skip to content

Commit c41a8a9

Browse files
author
Gitea
committed
Use UpdateIssueTitle model to change ref in backend
1 parent c99c280 commit c41a8a9

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

models/issue.go

+39
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,45 @@ func (issue *Issue) ChangeTitle(doer *User, oldTitle string) (err error) {
733733
return sess.Commit()
734734
}
735735

736+
// ChangeRef changes the branch of this issue, as the given user.
737+
func (issue *Issue) ChangeRef(doer *User, oldRef string) (err error) {
738+
sess := x.NewSession()
739+
defer sess.Close()
740+
741+
if err = sess.Begin(); err != nil {
742+
return err
743+
}
744+
745+
if err = updateIssueCols(sess, issue, "ref"); err != nil {
746+
return fmt.Errorf("updateIssueCols: %v", err)
747+
}
748+
749+
if err = issue.loadRepo(sess); err != nil {
750+
return fmt.Errorf("loadRepo: %v", err)
751+
}
752+
753+
if _, err = createComment(sess, &CreateCommentOptions{
754+
Type: CommentTypeChangeTitle,
755+
Doer: doer,
756+
Repo: issue.Repo,
757+
Issue: issue,
758+
OldTitle: oldRef,
759+
NewTitle: issue.Ref,
760+
}); err != nil {
761+
return fmt.Errorf("createComment: %v", err)
762+
}
763+
764+
if err = issue.neuterCrossReferences(sess); err != nil {
765+
return err
766+
}
767+
768+
if err = issue.addCrossReferences(sess, doer); err != nil {
769+
return err
770+
}
771+
772+
return sess.Commit()
773+
}
774+
736775
// AddDeletePRBranchComment adds delete branch comment for pull request issue
737776
func AddDeletePRBranchComment(doer *User, repo *Repository, issueID int64, branchName string) error {
738777
issue, err := getIssueByID(x, issueID)

routers/repo/issue.go

+28
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,34 @@ func UpdateIssueTitle(ctx *context.Context) {
10551055
})
10561056
}
10571057

1058+
// UpdateIssueRef change issue's ref (branch)
1059+
func UpdateIssueRef(ctx *context.Context) {
1060+
issue := GetActionIssue(ctx)
1061+
if ctx.Written() {
1062+
return
1063+
}
1064+
1065+
if !ctx.IsSigned || (!issue.IsPoster(ctx.User.ID) && !ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull)) {
1066+
ctx.Error(403)
1067+
return
1068+
}
1069+
1070+
ref := ctx.QueryTrim("ref")
1071+
if len(ref) == 0 {
1072+
ctx.Error(204)
1073+
return
1074+
}
1075+
1076+
if err := issue_service.ChangeIssueRef(issue, ctx.User, ref); err != nil {
1077+
ctx.ServerError("ChangeRef", err)
1078+
return
1079+
}
1080+
1081+
ctx.JSON(200, map[string]interface{}{
1082+
"ref": ref,
1083+
})
1084+
}
1085+
10581086
// UpdateIssueContent change issue's content
10591087
func UpdateIssueContent(ctx *context.Context) {
10601088
issue := GetActionIssue(ctx)

services/issue/issue.go

+14
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ func ChangeTitle(issue *models.Issue, doer *models.User, title string) (err erro
4040
return nil
4141
}
4242

43+
// ChangeIssueRef changes the branch of this issue, as the given user.
44+
func ChangeIssueRef(issue *models.Issue, doer *models.User, ref string) (err error) {
45+
oldRef := issue.Ref
46+
issue.Ref = ref
47+
48+
if err = issue.ChangeRef(doer, oldRef); err != nil {
49+
return
50+
}
51+
// TODO: implement notifications
52+
//notification.NotifyIssueChangeTitle(doer, issue, oldRef)
53+
54+
return nil
55+
}
56+
4357
// UpdateAssignees is a helper function to add or delete one or multiple issue assignee(s)
4458
// Deleting is done the GitHub way (quote from their api documentation):
4559
// https://developer.github.com/v3/issues/#edit-an-issue

0 commit comments

Comments
 (0)