Skip to content

Commit

Permalink
implement create update remove cards and create comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cnam committed Sep 24, 2015
1 parent bb0fbef commit 1f8a626
Show file tree
Hide file tree
Showing 15 changed files with 396 additions and 62 deletions.
40 changes: 26 additions & 14 deletions cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,32 @@ func daemon(c *cli.Context) {

m.Get("/assets/html/user/views/oauth.html", user.OauthHandler)

m.Combo("/api/oauth").
Get(user.OauthUrl).
Post(binding.Json(auth.Oauth2{}), user.OauthLogin)

m.Post("/api/login", binding.Json(auth.SignIn{}), user.SignIn)
m.Post("/api/register", binding.Json(auth.SignUp{}), user.SignUp)

m.Get("/api/boards", board.ListBoards)
m.Get("/api/board", board.ItemBoard)
m.Get("/api/labels", board.ListLabels)
m.Get("/api/cards", board.ListCards)
m.Get("/api/milestones", board.ListMilestones)
m.Get("/api/users", board.ListMembers)
m.Get("/api/comments", board.ListComments)
m.Group("/api", func() {
m.Combo("/oauth").
Get(user.OauthUrl).
Post(binding.Json(auth.Oauth2{}), user.OauthLogin)

m.Post("/login", binding.Json(auth.SignIn{}), user.SignIn)
m.Post("/register", binding.Json(auth.SignUp{}), user.SignUp)

m.Get("/boards", board.ListBoards)
m.Get("/board", board.ItemBoard)
m.Get("/labels", board.ListLabels)
m.Get("/cards", board.ListCards)
m.Get("/milestones", board.ListMilestones)
m.Get("/users", board.ListMembers)
m.Combo("/comments").
Get(board.ListComments).
Post(binding.Json(models.CommentRequest{}), board.CreateComment)

m.Combo("/card").
Post(binding.Json(models.CardRequest{}), board.CreateCard).
Put(binding.Json(models.CardRequest{}), board.UpdateCard).
Delete(binding.Json(models.CardRequest{}), board.DeleteCard)

m.Put("/card/move", binding.Json(models.CardRequest{}), board.MoveToCard)

})

m.Get("/*", routers.Home)

Expand Down
118 changes: 118 additions & 0 deletions models/card.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import (
"encoding/json"
"gitlab.com/kanban/kanban/modules/gitlab"
"regexp"
"fmt"
"strings"
"strconv"
)

type Card struct {
Id int64 `json:"id"`
Iid int64 `json:"iid"`
Assignee *User `json:"assignee"`
Milestone *Milestone `json:"milestone"`
Author *User `json:"author"`
Description string `json:"description"`
Labels []string `json:"labels"`
Expand All @@ -20,6 +24,19 @@ type Card struct {
Todo []*Todo `json:"todo"`
}

type CardRequest struct {
CardId int64 `json:"issue_id"`
ProjectId int64 `json:"project_id"`
Title string `json:"title"`
Description string `json:"description"`
AssigneeId int64 `json:"assignee_id"`
MilestoneId int64 `json:"milestone_id"`
Labels string `json:"labels"`
Properties *Properties `json:"properties"`
Stage map[string]string `json:"stage"`
Todo []*Todo `json:"todo"`
}

type Properties struct {
Andon string `json:"andon"`
}
Expand Down Expand Up @@ -60,6 +77,104 @@ func ListCards(u *User, provider, board_id string) ([]*Card, error) {
return b, nil
}

// CreateCard create new card on board
func CreateCard(u *User, provider string, form *CardRequest) (*Card, int, error) {
var cr *Card
var code int
switch provider {
case "gitlab":
c := gitlab.NewContext(u.Credential["gitlab"].Token)
r, code, err := c.CreateIssue(strconv.FormatInt(form.ProjectId, 10), mapCardRequestToGitlab(form))
if err != nil {
return nil, code, err
}

cr = mapCardFromGitlab(r)
}

return cr, code, nil
}

// UpdateCard updates existing card on board
func UpdateCard(u *User, provider string, form *CardRequest) (*Card, int, error) {
var cr *Card
var code int
switch provider {
case "gitlab":
c := gitlab.NewContext(u.Credential["gitlab"].Token)
r, code, err := c.UpdateIssue(
strconv.FormatInt(form.ProjectId, 10),
strconv.FormatInt(form.CardId, 10),
mapCardRequestToGitlab(form),
)
if err != nil {
return nil, code, err
}

cr = mapCardFromGitlab(r)
}

return cr, code, nil
}

// DeleteCard removes card from board
func DeleteCard(u *User, provider string, form *CardRequest) (*Card, int, error) {
var cr *Card
var code int
switch provider {
case "gitlab":
c := gitlab.NewContext(u.Credential["gitlab"].Token)
foru := mapCardRequestToGitlab(form)
foru.StateEvent = "close"
r, code, err := c.UpdateIssue(
strconv.FormatInt(form.ProjectId, 10),
strconv.FormatInt(form.CardId, 10),
foru,
)
if err != nil {
return nil, code, err
}

cr = mapCardFromGitlab(r)
}

return cr, code, nil
}

// mapCardRequestToGitlab transforms card to gitlab issue request
func mapCardRequestToGitlab(c *CardRequest) *gitlab.IssueRequest {
return &gitlab.IssueRequest{
Title: c.Title,
Description: mapCardDescriptionToGitlab(c.Description, c.Todo, c.Properties),
AssigneeId: c.AssigneeId,
MilestoneId: c.MilestoneId,
Labels: c.Labels,
}
}

// mapCardDescriptionToGitlab Transforms card description to gitlab description
func mapCardDescriptionToGitlab(desc string, t []*Todo, p *Properties) string {
var d string
var chek string
d = desc
for _, v := range t {
if v.Checked {
chek = "x"
} else {
chek = " "
}
d = fmt.Sprintf("%s\n- [%s] %s", d, chek, v.Body)
}

pr, err := json.Marshal(p)

if err == nil {
d = fmt.Sprintf("%s\n<!-- @KB:%s -->", d, string(pr))
}

return strings.TrimSpace(d)
}

// mapCardFromGitlab mapped gitlab issue to kanban card
func mapCardFromGitlab(c *gitlab.Issue) *Card {
return &Card{
Expand All @@ -70,6 +185,7 @@ func mapCardFromGitlab(c *gitlab.Issue) *Card {
Assignee: mapUserFromGitlab(c.Assignee),
Author: mapUserFromGitlab(c.Author),
Description: mapCardDescriptionFromGitlab(c.Description),
Milestone: mapMilestoneFromGitlab(c.Milestone),
Labels: c.Labels,
ProjectId: c.ProjectId,
Properties: mapCardPropertiesFromGitlab(c.Description),
Expand Down Expand Up @@ -103,6 +219,8 @@ func mapCardTodoFromGitlab(d string) []*Todo {

i = append(i, t)
}
} else {
i = make([]*Todo, 0)
}

return i
Expand Down
43 changes: 36 additions & 7 deletions models/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"regexp"
"strings"
"time"
"strconv"
)

type Comment struct {
Expand All @@ -16,6 +17,12 @@ type Comment struct {
IsInfo bool `json:"is_info"`
}

type CommentRequest struct {
CardId int64 `json:"issue_id"`
ProjectId int64 `json:"project_id"`
Body string `json:"body"`
}

var (
reg = string(strings.Join([]string{
`Reassigned to .*?`,
Expand Down Expand Up @@ -52,20 +59,42 @@ func ListComments(u *User, provider, project_id, card_id string) ([]*Comment, er
return nil, err
}

b = mapCommentCollectionFromGitlab(r)
for _, co := range r {
b = append(b, mapCommentFromGitlab(co))
}
}

return b, nil
}

// mapCommentCollectionFromGitlab transforms gitlab coments to kanban comments
func mapCommentCollectionFromGitlab(c []*gitlab.Comment) []*Comment {
var b []*Comment
for _, co := range c {
b = append(b, mapCommentFromGitlab(co))
// CreateComment creates new comment
func CreateComment(u *User, provider string, form *CommentRequest) (*Comment, int, error) {
var b *Comment
var code int
switch provider {
case "gitlab":
c := gitlab.NewContext(u.Credential["gitlab"].Token)
r, code, err := c.CreateComment(
strconv.FormatInt(form.ProjectId, 10),
strconv.FormatInt(form.CardId, 10),
mapCommentRequestToGitlab(form),
)

if err != nil {
return nil, code, err
}

b = mapCommentFromGitlab(r)
}

return b
return b, code, nil
}

// mapCommentRequestToGitlab transforms kanban comment request to gitlab comment request
func mapCommentRequestToGitlab(c *CommentRequest) *gitlab.CommentRequest {
return &gitlab.CommentRequest{
Body: c.Body,
}
}

// mapCommentFromGitlab transform gitlab comment to kanban comment
Expand Down
4 changes: 4 additions & 0 deletions models/milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func ListMilestones(u *User, provider, board_id string) ([]*Milestone, error) {

// mapMilestoneFromGitlab returns map from gitlab milestone to gitlab milestone
func mapMilestoneFromGitlab(m *gitlab.Milestone) *Milestone {
if m == nil {
return nil
}

return &Milestone{
Id: m.Id,
State: m.State,
Expand Down
20 changes: 18 additions & 2 deletions modules/gitlab/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ type Comment struct {
CreatedAt time.Time `json:"created_at"`
}

type CommentRequest struct {
Body string `json:"body"`
}

// ListComments returns list comments for gitlab issue
func (g *GitlabContext) ListComments(project_id, issue_id string, o *ListOptions) ([]*Comment, error) {
cl := g.client
path := getUrl([]string{"projects", url.QueryEscape(project_id), "issues", issue_id, "notes"})
u, err := addOptions(path, o)
if err != nil {
Expand All @@ -26,11 +29,24 @@ func (g *GitlabContext) ListComments(project_id, issue_id string, o *ListOptions
req, _ := http.NewRequest("GET", u, nil)

var ret []*Comment
if err := g.Do(cl, req, &ret); err != nil {
if _, err := g.Do(req, &ret); err != nil {
return nil, err
}

sortutil.AscByField(ret, "CreatedAt")

return ret, nil
}

// CreateComment creates new comment
func (g *GitlabContext) CreateComment(project_id, issue_id string, com *CommentRequest) (*Comment, int, error) {
path := []string{"projects", url.QueryEscape(project_id), "issues", issue_id, "notes"}
req, _ := g.NewRequest("POST", path, com)

var ret *Comment
if res, err := g.Do(req, &ret); err != nil {
return nil, res.StatusCode, err
}

return ret, 0, nil
}
Loading

0 comments on commit 1f8a626

Please sign in to comment.