From 68fb62347ff93517015eab8036d45625e63de8d2 Mon Sep 17 00:00:00 2001
From: Unknown <joe2010xtmf@163.com>
Date: Sun, 11 May 2014 13:46:36 -0400
Subject: [PATCH] Finish issue assignee

---
 cmd/serve.go              |  1 -
 cmd/web.go                |  1 +
 models/issue.go           |  6 +++-
 public/js/app.js          |  2 +-
 routers/repo/issue.go     | 66 ++++++++++++++++++++++++++++-----------
 templates/issue/view.tmpl |  3 +-
 6 files changed, 56 insertions(+), 23 deletions(-)

diff --git a/cmd/serve.go b/cmd/serve.go
index e19b5d78796..5eac51f5e47 100644
--- a/cmd/serve.go
+++ b/cmd/serve.go
@@ -177,7 +177,6 @@ func runServ(k *cli.Context) {
 	models.SetRepoEnvs(user.Id, user.Name, repoName, repoUserName)
 
 	gitcmd := exec.Command(verb, repoPath)
-	println(base.RepoRootPath)
 	gitcmd.Dir = base.RepoRootPath
 	gitcmd.Stdout = os.Stdout
 	gitcmd.Stdin = os.Stdin
diff --git a/cmd/web.go b/cmd/web.go
index 6ff819f62be..9a42b27a113 100644
--- a/cmd/web.go
+++ b/cmd/web.go
@@ -184,6 +184,7 @@ func runWeb(*cli.Context) {
 		r.Get("/issues/new", repo.CreateIssue)
 		r.Post("/issues/new", bindIgnErr(auth.CreateIssueForm{}), repo.CreateIssuePost)
 		r.Post("/issues/:index", bindIgnErr(auth.CreateIssueForm{}), repo.UpdateIssue)
+		r.Post("/issues/:index/assignee", repo.UpdateAssignee)
 		r.Get("/issues/milestones", repo.Milestones)
 		r.Get("/issues/milestones/new", repo.NewMilestones)
 		r.Post("/comment/:action", repo.Comment)
diff --git a/models/issue.go b/models/issue.go
index f856ca63b64..40d3bab04de 100644
--- a/models/issue.go
+++ b/models/issue.go
@@ -349,8 +349,12 @@ func UpdateIssueUserPairByAssignee(aid, iid int64) error {
 		return err
 	}
 
+	// Assignee ID equals to 0 means clear assignee.
+	if aid == 0 {
+		return nil
+	}
 	rawSql = "UPDATE `issue_user` SET is_assigned = true WHERE uid = ? AND issue_id = ?"
-	_, err := orm.Exec(rawSql, true, aid, iid)
+	_, err := orm.Exec(rawSql, aid, iid)
 	return err
 }
 
diff --git a/public/js/app.js b/public/js/app.js
index ebb05d2d6e5..f59442ee9c6 100644
--- a/public/js/app.js
+++ b/public/js/app.js
@@ -547,7 +547,7 @@ function initIssue() {
             if(uid != assignee){
                 $.post($a.data("ajax"), {
                     issue: $('#issue').data("id"),
-                    assign: assignee
+                    assigneeid: uid
                 }, function (json) {
                     if (json.ok) {
                         window.location.reload();
diff --git a/routers/repo/issue.go b/routers/repo/issue.go
index 7aa747351fd..62189595855 100644
--- a/routers/repo/issue.go
+++ b/routers/repo/issue.go
@@ -239,20 +239,12 @@ func ViewIssue(ctx *middleware.Context, params martini.Params) {
 		return
 	}
 
-	// Update assignee.
-	if ctx.Repo.IsOwner {
-		aid, _ := base.StrTo(ctx.Query("assignneid")).Int64()
-		if aid > 0 {
-			// Not check for invalid assignne id and give responsibility to owners.
-			issue.AssigneeId = aid
-			if err = models.UpdateIssueUserPairByAssignee(aid, issue.Id); err != nil {
-				ctx.Handle(500, "issue.ViewIssue(UpdateIssueUserPairByAssignee): %v", err)
-				return
-			}
-			ctx.Redirect(fmt.Sprintf("%s/issues/%d", ctx.Repo.RepoLink, issue.Index))
-			return
-		}
+	us, err := models.GetCollaborators(strings.TrimPrefix(ctx.Repo.RepoLink, "/"))
+	if err != nil {
+		ctx.Handle(500, "issue.CreateIssue(GetCollaborators)", err)
+		return
 	}
+	ctx.Data["Collaborators"] = us
 
 	if ctx.IsSigned {
 		// Update issue-user.
@@ -300,18 +292,18 @@ func ViewIssue(ctx *middleware.Context, params martini.Params) {
 }
 
 func UpdateIssue(ctx *middleware.Context, params martini.Params, form auth.CreateIssueForm) {
-	index, err := base.StrTo(params["index"]).Int()
+	idx, err := base.StrTo(params["index"]).Int()
 	if err != nil {
-		ctx.Handle(404, "issue.UpdateIssue", err)
+		ctx.Error(404)
 		return
 	}
 
-	issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, int64(index))
+	issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, int64(idx))
 	if err != nil {
 		if err == models.ErrIssueNotExist {
 			ctx.Handle(404, "issue.UpdateIssue", err)
 		} else {
-			ctx.Handle(200, "issue.UpdateIssue(get issue)", err)
+			ctx.Handle(500, "issue.UpdateIssue(GetIssueByIndex)", err)
 		}
 		return
 	}
@@ -327,7 +319,7 @@ func UpdateIssue(ctx *middleware.Context, params martini.Params, form auth.Creat
 	issue.Labels = form.Labels
 	issue.Content = form.Content
 	if err = models.UpdateIssue(issue); err != nil {
-		ctx.Handle(200, "issue.UpdateIssue(update issue)", err)
+		ctx.Handle(500, "issue.UpdateIssue(UpdateIssue)", err)
 		return
 	}
 
@@ -338,6 +330,44 @@ func UpdateIssue(ctx *middleware.Context, params martini.Params, form auth.Creat
 	})
 }
 
+func UpdateAssignee(ctx *middleware.Context) {
+	if !ctx.Repo.IsOwner {
+		ctx.Error(403)
+		return
+	}
+
+	idx, err := base.StrTo(ctx.Query("issue")).Int64()
+	if err != nil {
+		ctx.Error(404)
+		return
+	}
+
+	issue, err := models.GetIssueByIndex(ctx.Repo.Repository.Id, idx)
+	if err != nil {
+		if err == models.ErrIssueNotExist {
+			ctx.Handle(404, "issue.UpdateAssignee", err)
+		} else {
+			ctx.Handle(500, "issue.UpdateAssignee(GetIssueByIndex)", err)
+		}
+		return
+	}
+
+	aid, _ := base.StrTo(ctx.Query("assigneeid")).Int64()
+	// Not check for invalid assignne id and give responsibility to owners.
+	issue.AssigneeId = aid
+	if err = models.UpdateIssueUserPairByAssignee(aid, issue.Id); err != nil {
+		ctx.Handle(500, "issue.UpdateAssignee(UpdateIssueUserPairByAssignee): %v", err)
+		return
+	} else if err = models.UpdateIssue(issue); err != nil {
+		ctx.Handle(500, "issue.UpdateAssignee(UpdateIssue)", err)
+		return
+	}
+
+	ctx.JSON(200, map[string]interface{}{
+		"ok": true,
+	})
+}
+
 func Comment(ctx *middleware.Context, params martini.Params) {
 	index, err := base.StrTo(ctx.Query("issueIndex")).Int64()
 	if err != nil {
diff --git a/templates/issue/view.tmpl b/templates/issue/view.tmpl
index 9134a9802a3..941f8e2f866 100644
--- a/templates/issue/view.tmpl
+++ b/templates/issue/view.tmpl
@@ -100,7 +100,7 @@
             </div>
 
             <div class="issue-bar col-md-2">
-                <div class="assignee" data-assigned="{{if .Issue.Assignee}}{{.Issue.Assignee.Id}}{{else}}0{{end}}" data-ajax="{url}">{{if .IsRepositoryOwner}}
+                <div class="assignee" data-assigned="{{if .Issue.Assignee}}{{.Issue.Assignee.Id}}{{else}}0{{end}}" data-ajax="{{.Issue.Index}}/assignee">{{if .IsRepositoryOwner}}
                     <div class="pull-right action">
                         <button type="button" class="dropdown-toggle btn btn-default btn-sm" data-toggle="dropdown">
                             <i class="fa fa-group"></i>
@@ -112,7 +112,6 @@
                                 {{range .Collaborators}}
                                 <li data-uid="{{.Id}}"><img src="{{.AvatarLink}}"><strong>{{.Name}}</strong> {{.FullName}}</li>
                                 {{end}}
-                                <li data-uid="1"><img src="//1.gravatar.com/avatar/f72f7454ce9d710baa506394f68f4132"><strong>fuxiaohei</strong></li>
                             </ul>
                         </div>
                     </div>{{end}}