This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
298 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package commands | ||
|
||
|
||
import ( | ||
"fmt" | ||
"github.com/AlecAivazis/survey/v2" | ||
"glab/internal/git" | ||
"glab/internal/manip" | ||
"log" | ||
|
||
"github.com/spf13/cobra" | ||
gitlab "github.com/xanzy/go-gitlab" | ||
) | ||
|
||
var issueCreateNoteCmd = &cobra.Command{ | ||
Use: "note <issue-id>", | ||
Aliases: []string{"comment"}, | ||
Short: "Add a comment to issue", | ||
Long: ``, | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
gitlabClient, repo := git.InitGitlabClient() | ||
mID := args[0] | ||
body, err := cmd.Flags().GetString("message") | ||
if r, _ := cmd.Flags().GetString("repo"); r != "" { | ||
repo = r | ||
} | ||
if err != nil { | ||
er(err) | ||
return | ||
} | ||
mr, _, err := gitlabClient.Issues.GetIssue(repo, manip.StringToInt(mID)) | ||
if err != nil { | ||
er(err) | ||
return | ||
} | ||
if body == "" { | ||
prompt := &survey.Editor{ | ||
Renderer: survey.Renderer{}, | ||
Message: "Note Message: ", | ||
Help: "Enter the note message for issue. Uses the editor defined by the $VISUAL or $EDITOR environment variables). If neither of those are present, notepad (on Windows) or vim (Linux or Mac) is used", | ||
FileName: "*.md", | ||
} | ||
err = survey.AskOne(prompt, &body) | ||
} | ||
|
||
if err != nil { | ||
er(err) | ||
return | ||
} | ||
if body == "" { | ||
log.Fatal("Aborted... Note has an empty message") | ||
} | ||
|
||
noteInfo,_, err := gitlabClient.Notes.CreateIssueNote(repo, manip.StringToInt(mID), &gitlab.CreateIssueNoteOptions{ | ||
Body: &body, | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Printf("%s#note_%d\n",mr.WebURL, noteInfo.ID) | ||
}, | ||
} | ||
|
||
func init() { | ||
issueCreateNoteCmd.Flags().StringP("message", "m", "", "Enter note message") | ||
issueCmd.AddCommand(issueCreateNoteCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package commands | ||
|
||
|
||
import ( | ||
"fmt" | ||
"github.com/AlecAivazis/survey/v2" | ||
"glab/internal/git" | ||
"glab/internal/manip" | ||
"log" | ||
|
||
"github.com/spf13/cobra" | ||
gitlab "github.com/xanzy/go-gitlab" | ||
) | ||
|
||
var mrCreateNoteCmd = &cobra.Command{ | ||
Use: "note <merge-request-id>", | ||
Aliases: []string{"comment"}, | ||
Short: "Add a comment to merge request", | ||
Long: ``, | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
gitlabClient, repo := git.InitGitlabClient() | ||
mID := args[0] | ||
body, err := cmd.Flags().GetString("message") | ||
if r, _ := cmd.Flags().GetString("repo"); r != "" { | ||
repo = r | ||
} | ||
if err != nil { | ||
er(err) | ||
return | ||
} | ||
mr, _, err := gitlabClient.MergeRequests.GetMergeRequest(repo, manip.StringToInt(mID), &gitlab.GetMergeRequestsOptions{}) | ||
if err != nil { | ||
er(err) | ||
return | ||
} | ||
if body == "" { | ||
prompt := &survey.Editor{ | ||
Renderer: survey.Renderer{}, | ||
Message: "Note Message: ", | ||
Help: "Enter the note message for the merge request. Uses the editor defined by the $VISUAL or $EDITOR environment variables). If neither of those are present, notepad (on Windows) or vim (Linux or Mac) is used", | ||
FileName: "*.md", | ||
} | ||
err = survey.AskOne(prompt, &body) | ||
} | ||
|
||
if err != nil { | ||
er(err) | ||
return | ||
} | ||
if body == "" { | ||
log.Fatal("Aborted... Note has an empty message") | ||
} | ||
|
||
noteInfo,_, err := gitlabClient.Notes.CreateMergeRequestNote(repo, manip.StringToInt(mID), &gitlab.CreateMergeRequestNoteOptions{ | ||
Body: &body, | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Printf("%s#note_%d\n",mr.WebURL, noteInfo.ID) | ||
}, | ||
} | ||
|
||
func init() { | ||
mrCreateNoteCmd.Flags().StringP("message", "m", "", "Use the given <msg>; multiple -m are concatenated as separate paragraphs") | ||
mrCmd.AddCommand(mrCreateNoteCmd) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
"github.com/MakeNowJust/heredoc" | ||
"github.com/gookit/color" | ||
"github.com/gosuri/uitable" | ||
"github.com/spf13/cobra" | ||
"github.com/xanzy/go-gitlab" | ||
"glab/internal/browser" | ||
"glab/internal/git" | ||
"glab/internal/manip" | ||
"glab/internal/utils" | ||
"log" | ||
"strings" | ||
"time" | ||
) | ||
|
||
var mrViewCmd = &cobra.Command{ | ||
Use: "view <id>", | ||
Short: `Display the title, body, and other information about a merge request.`, | ||
Long: ``, | ||
Aliases: []string{"show"}, | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) == 0 || len(args) > 1 { | ||
cmdErr(cmd, args) | ||
return | ||
} | ||
pid := manip.StringToInt(args[0]) | ||
|
||
gitlabClient, repo := git.InitGitlabClient() | ||
|
||
if r, _ := cmd.Flags().GetString("repo"); r != "" { | ||
repo = r | ||
} | ||
opts := &gitlab.GetMergeRequestsOptions{} | ||
opts.IncludeDivergedCommitsCount = gitlab.Bool(true) | ||
opts.RenderHTML = gitlab.Bool(true) | ||
opts.IncludeRebaseInProgress = gitlab.Bool(true) | ||
|
||
mr, _, err := gitlabClient.MergeRequests.GetMergeRequest(repo, pid, opts) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if lb, _ := cmd.Flags().GetBool("web"); lb { //open in browser if --web flag is specified | ||
a, err := browser.Command(mr.WebURL) | ||
if err != nil { | ||
er(err) | ||
} | ||
if err := a.Run(); err != nil { | ||
er(err) | ||
} | ||
return | ||
} | ||
showSystemLog, _ := cmd.Flags().GetBool("system-logs") | ||
var mrState string | ||
if mr.State == "opened" { | ||
mrState = color.Green.Sprint(mr.State) | ||
} else { | ||
mrState = color.Red.Sprint(mr.State) | ||
} | ||
now := time.Now() | ||
ago := now.Sub(*mr.CreatedAt) | ||
color.Printf("\n%s <gray>#%d</>\n", mr.Title, mr.IID) | ||
color.Printf("(%s)<gray> • opened by %s (%s) %s</>\n", mrState, | ||
mr.Author.Username, | ||
mr.Author.Name, | ||
utils.PrettyTimeAgo(ago), | ||
) | ||
if mr.Description != "" { | ||
mr.Description, _ = utils.RenderMarkdown(mr.Description) | ||
fmt.Println(mr.Description) | ||
} | ||
color.Printf("\n<gray>%d upvotes • %d downvotes • %d comments</>\n\n", | ||
mr.Upvotes, mr.Downvotes, mr.UserNotesCount) | ||
var labels string | ||
for _, l := range mr.Labels { | ||
labels += " " + l + "," | ||
} | ||
labels = strings.Trim(labels, ", ") | ||
|
||
var assignees string | ||
for _, a := range mr.Assignees { | ||
assignees += " " + a.Username + "(" + a.Name + ")," | ||
} | ||
assignees = strings.Trim(assignees, ", ") | ||
table := uitable.New() | ||
table.MaxColWidth = 50 | ||
table.Wrap = true | ||
table.AddRow("Project ID:", mr.ProjectID) | ||
table.AddRow("Labels:", prettifyNilEmptyValues(labels, "None")) | ||
table.AddRow("Milestone:", prettifyNilEmptyValues(mr.Milestone, "None")) | ||
table.AddRow("Assignees:", prettifyNilEmptyValues(assignees, "None")) | ||
table.AddRow("Discussion Locked:", prettifyNilEmptyValues(mr.DiscussionLocked, "false")) | ||
table.AddRow("Subscribed:", prettifyNilEmptyValues(mr.Subscribed, "false")) | ||
|
||
if mr.State == "closed" { | ||
now := time.Now() | ||
ago := now.Sub(*mr.ClosedAt) | ||
table.AddRow("Closed By:", | ||
fmt.Sprintf("%s (%s) %s", mr.ClosedBy.Username, mr.ClosedBy.Name, utils.PrettyTimeAgo(ago))) | ||
} | ||
table.AddRow("Web URL:", mr.WebURL) | ||
fmt.Println(table) | ||
fmt.Println() // Empty Space | ||
|
||
if c, _ := cmd.Flags().GetBool("comments"); c { | ||
l := &gitlab.ListMergeRequestNotesOptions{} | ||
notes, _, err := gitlabClient.Notes.ListMergeRequestNotes(repo, pid, l) | ||
if err != nil { | ||
er(err) | ||
} | ||
|
||
table := uitable.New() | ||
table.MaxColWidth = 100 | ||
table.Wrap = true | ||
fmt.Println(heredoc.Doc(` | ||
-------------------------------------------- | ||
Comments / Notes | ||
-------------------------------------------- | ||
`)) | ||
if len(notes) > 0 { | ||
for _, note := range notes { | ||
if note.System && !showSystemLog { | ||
continue | ||
} | ||
//body, _ := utils.RenderMarkdown(note.Body) | ||
table.AddRow(note.Author.Username+":", | ||
fmt.Sprintf("%s\n%s", | ||
note.Body, | ||
color.Gray.Sprint(utils.TimeToPrettyTimeAgo(*note.CreatedAt)), | ||
), | ||
) | ||
table.AddRow("") | ||
} | ||
fmt.Println(table) | ||
} else { | ||
fmt.Println("There are no comments on this mr") | ||
} | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
mrViewCmd.Flags().StringP("repo", "r", "", "Select another repository using the OWNER/REPO format. Supports group namespaces") | ||
mrViewCmd.Flags().BoolP("comments", "c", false, "Show mr comments and activities") | ||
mrViewCmd.Flags().BoolP("system-logs", "s", false, "Show system activities / logs") | ||
mrViewCmd.Flags().BoolP("web", "w", false, "Open mr in a browser. Uses default browser or browser specified in BROWSER variable") | ||
mrCmd.AddCommand(mrViewCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Resolves #78