From 5ffbf10c8a6ffa84d43a30041269ba842980c59b Mon Sep 17 00:00:00 2001 From: Tulir Asokan <tulir@maunium.net> Date: Sat, 30 Sep 2017 18:56:35 +0300 Subject: [PATCH] Add issue creating/closing/reopening --- README.md | 2 +- commands.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9b1a9b9..dfc2b29 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ A Gitlab bot for Matrix. It uses * [x] View commit history * [ ] Issue management * [x] Read issues - * [ ] Create/close/reopen issues + * [x] Create/close/reopen issues * [ ] Read comments on issues * [ ] Comment on issues diff --git a/commands.go b/commands.go index 4d81359..823620b 100644 --- a/commands.go +++ b/commands.go @@ -225,20 +225,31 @@ func commandIssue(git *gitlab.Client, room *mautrix.Room, sender string, args [] return } - switch args[0] { + subcommand := args[0] + if len(args) > 1 { + args = args[1:] + } else { + args = []string{} + } + + switch subcommand { + case "show": + fallthrough + case "view": + fallthrough case "read": - if len(args) < 3 { + if len(args) < 2 { room.Send("Usage: !gitlab issue read <repo> <issue id>") return } - id, err := strconv.Atoi(args[2]) + id, err := strconv.Atoi(args[1]) if err != nil { room.Send("Usage: !gitlab issue read <repo> <issue id>") return } - issue, _, err := git.Issues.GetIssue(args[1], id) + issue, _, err := git.Issues.GetIssue(args[0], id) if err != nil { room.Sendf("An error occurred: %s", err) return @@ -251,8 +262,44 @@ func commandIssue(git *gitlab.Client, room *mautrix.Room, sender string, args [] } fmt.Fprintf(&buf, "<br/>\n<blockquote>%s</blockquote><br/>\n", strings.Replace(issue.Description, "\n", "<br/>\n", -1)) room.SendHTML(buf.String()) + case "create": + fallthrough case "open": + if len(args) < 2 { + room.Send("Usage: !gitlab issue open <repo> <title> \\n <body>") + return + } + + title := strings.Join(args[1:], " ") + description := strings.Join(lines, "\n") + + _, _, err := git.Issues.CreateIssue(args[0], &gitlab.CreateIssueOptions{ + Title: &title, + Description: &description, + }) + if err != nil { + room.Sendf("Failed to create issue: %s", err) + } case "close": + fallthrough + case "reopen": + if len(args) < 2 { + room.Sendf("Usage: !gitlab issue %s <repo> <issue number>", subcommand) + return + } + + issueID, err := strconv.Atoi(args[1]) + if err != nil { + room.Sendf("Invalid issue ID: %s", args[1]) + } + _, resp, err := git.Issues.UpdateIssue(args[0], issueID, &gitlab.UpdateIssueOptions{ + StateEvent: &subcommand, + }) + if resp.StatusCode == 404 { + room.Sendf("Issue #%d or repository %s not found.", issueID, args[0]) + } else if err != nil { + room.Sendf("Failed to %s issue: %s", subcommand, err) + } case "comment": case "read-comments": default: -- GitLab