Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Commit

Permalink
feat: add --repo flag to issue view
Browse files Browse the repository at this point in the history
  • Loading branch information
profclems committed Aug 12, 2020
1 parent e3c5f8f commit 77a15fe
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 76 deletions.
26 changes: 12 additions & 14 deletions commands/issue.go
Expand Up @@ -3,40 +3,38 @@ package commands
import (
"fmt"
"github.com/gookit/color"
"os"
"strings"
"text/tabwriter"

"github.com/gosuri/uitable"
"github.com/spf13/cobra"
"github.com/xanzy/go-gitlab"
"glab/internal/git"
"glab/internal/manip"
"strings"
)

func displayAllIssues(m []*gitlab.Issue) {
if len(m) > 0 {
fmt.Printf("\nShowing issues %d of %d on %s\n\n", len(m), len(m), git.GetRepo())

// initialize tabwriter
w := new(tabwriter.Writer)

// minwidth, tabwidth, padding, padchar, flags
w.Init(os.Stdout, 8, 8, 0, '\t', 0)

defer w.Flush()
table := uitable.New()
table.MaxColWidth = 70
for _, issue := range m {
var labels string
for _, l := range issue.Labels {
labels += " " + l + ","
}
labels = strings.Trim(labels, ", ")
if labels != ""{
labels = "(" + labels + ")"
}
var issueID string
duration := manip.TimeAgo(*issue.CreatedAt)
if issue.State == "opened" {
_, _ = fmt.Fprintln(w, color.Sprintf("<green>#%d</>\t%s\t<cyan>(%s)</>\t<gray>%s</>", issue.IID, issue.Title, labels, duration))
issueID = color.Sprintf("<green>#%d</>", issue.IID)
} else {
_, _ = fmt.Fprintln(w, color.Sprintf("<red>#%d</>\t%s\t<cyan>(%s)</>\t<gray>%s</>", issue.IID, issue.Title, labels, duration))
issueID = color.Sprintf("<red>#%d</>", issue.IID)
}
table.AddRow(issueID, issue.Title, color.Cyan.Sprintf(labels), color.Gray.Sprintf(duration))
}
fmt.Println(table)
} else {
fmt.Println("No Issues available on " + git.GetRepo())
}
Expand Down
159 changes: 159 additions & 0 deletions commands/issue_view.go
@@ -0,0 +1,159 @@
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 issueViewCmd = &cobra.Command{
Use: "view <id> [flags]",
Short: `Display the title, body, and other information about an issue.`,
Long: ``,
Aliases: []string{"show"},
Args: cobra.MaximumNArgs(3),
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
}

issue, _, err := gitlabClient.Issues.GetIssue(repo, pid)
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(issue.WebURL)
if err != nil {
er(err)
}
if err:= a.Run(); err != nil {
er(err)
}
return
}
var issueState string
if issue.State == "opened" {
issueState = color.Green.Sprint(issue.State)
} else {
issueState = color.Red.Sprint(issue.State)
}
now := time.Now()
ago := now.Sub(*issue.CreatedAt)
color.Printf("\n%s <gray>#%d</>\n", issue.Title, issue.IID)
color.Printf("(%s)<gray> • opened by %s (%s) %s</>\n", issueState,
issue.Author.Username,
issue.Author.Name,
utils.PrettyTimeAgo(ago),
)
if issue.Description != "" {
issue.Description, _ = utils.RenderMarkdown(issue.Description)
fmt.Println(issue.Description)
}
color.Printf("\n<gray>%d upvotes • %d downvotes • %d comments</>\n\n",
issue.Upvotes, issue.Downvotes, issue.UserNotesCount)
var labels string
for _, l := range issue.Labels {
labels += " " + l + ","
}
labels = strings.Trim(labels, ", ")

var assignees string
for _, a := range issue.Assignees {
assignees += " " + a.Username + "(" + a.Name + "),"
}
assignees = strings.Trim(assignees, ", ")
table := uitable.New()
table.MaxColWidth = 50
table.Wrap = true
table.AddRow("Project ID:", issue.ProjectID)
table.AddRow("Labels:", prettifyNilEmptyValues(labels, "None"))
table.AddRow("Milestone:", prettifyNilEmptyValues(issue.Milestone, "None"))
table.AddRow("Assignees:", prettifyNilEmptyValues(assignees, "None"))
table.AddRow("Due date:", prettifyNilEmptyValues(issue.DueDate, "None"))
table.AddRow("Weight:", prettifyNilEmptyValues(issue.Weight, "None"))
table.AddRow("Confidential:", prettifyNilEmptyValues(issue.Confidential, "None"))
table.AddRow("Discussion Locked:", prettifyNilEmptyValues(issue.DiscussionLocked, "false"))
table.AddRow("Subscribed:", prettifyNilEmptyValues(issue.Subscribed, "false"))

if issue.State == "closed" {
now := time.Now()
ago := now.Sub(*issue.ClosedAt)
table.AddRow("Closed By:",
fmt.Sprintf("%s (%s) %s", issue.ClosedBy.Username, issue.ClosedBy.Name, utils.PrettyTimeAgo(ago)))
}
table.AddRow("Reference:", issue.References.Full)
table.AddRow("Web URL:", issue.WebURL)
fmt.Println(table)
fmt.Println() // Empty Space

if c, _ := cmd.Flags().GetBool("comments"); c { //open in browser if --web flag is specified
l := &gitlab.ListIssueNotesOptions{}
notes, _, err := gitlabClient.Notes.ListIssueNotes(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 {
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 issue")
}
}
},
}

func prettifyNilEmptyValues(value interface{}, defVal string) interface{} {
if value == nil || value == "" {
return defVal
}
if value == false {
return false
}
return value
}

func init() {
issueViewCmd.Flags().StringP("repo", "r", "", "Select another repository using the OWNER/REPO format. Supports group namespaces")
issueViewCmd.Flags().BoolP("comments", "c", false, "Show issue comments and activities")
issueViewCmd.Flags().BoolP("web", "w", false, "Open issue in a browser. Uses default browser or browser specified in BROWSER variable")
issueCmd.AddCommand(issueViewCmd)
}
25 changes: 10 additions & 15 deletions commands/mr.go
Expand Up @@ -3,13 +3,11 @@ package commands
import (
"fmt"
"github.com/gookit/color"
"github.com/gosuri/uitable"
"github.com/spf13/cobra"
"github.com/xanzy/go-gitlab"
"glab/internal/git"
"glab/internal/manip"
"os"
"text/tabwriter"

"github.com/xanzy/go-gitlab"
)

func displayMergeRequest(hm *gitlab.MergeRequest) {
Expand All @@ -23,24 +21,21 @@ func displayMergeRequest(hm *gitlab.MergeRequest) {
}

func displayAllMergeRequests(m []*gitlab.MergeRequest) {
// initialize tabwriter
w := new(tabwriter.Writer)

// minwidth, tabwidth, padding, padchar, flags
w.Init(os.Stdout, 8, 8, 0, '\t', 0)

defer w.Flush()
if len(m) > 0 {
table := uitable.New()
table.MaxColWidth = 70
fmt.Println()
fmt.Printf("Showing mergeRequests %d of %d on %s\n", len(m), len(m), git.GetRepo())
fmt.Printf("Showing mergeRequests %d of %d on %s\n\n", len(m), len(m), git.GetRepo())
for _, mr := range m {
var mrID string
if mr.State == "opened" {
_, _ = fmt.Fprintln(w, color.Sprintf("<green>#%d</>\t%s\t\t<cyan>(%s) ← (%s)</>", mr.IID, mr.Title, mr.TargetBranch, mr.SourceBranch))
mrID = color.Sprintf("<green>#%d</>", mr.IID)
} else {
_, _ = fmt.Fprintln(w, color.Sprintf("<green>#%d</>\t%s\t\t<cyan>(%s) ← (%s)</>", mr.IID, mr.Title, mr.TargetBranch, mr.SourceBranch))
mrID = color.Sprintf("<red>#%d</>", mr.IID)
}
table.AddRow(mrID, mr.Title, color.Sprintf("<cyan>(%s) ← (%s)</>",mr.TargetBranch, mr.SourceBranch))
}
fmt.Println()
fmt.Println(table)
} else {
fmt.Println("No Merge Requests available on " + git.GetRepo())
}
Expand Down
3 changes: 2 additions & 1 deletion internal/browser/browser.go
Expand Up @@ -7,11 +7,12 @@ import (
"strings"

"github.com/google/shlex"
"glab/internal/config"
)

// Command produces an exec.Cmd respecting runtime.GOOS and $BROWSER environment variable
func Command(url string) (*exec.Cmd, error) {
launcher := os.Getenv("BROWSER")
launcher := config.GetEnv("BROWSER")
if launcher != "" {
return FromLauncher(launcher, url)
}
Expand Down
8 changes: 4 additions & 4 deletions internal/config/config.go
Expand Up @@ -40,9 +40,9 @@ func GetEnv(key string) string {
env := os.Getenv(key) //first get user defined variable via export

if env == "" {
env = manip.GetKeyValueInFile(configFile, key) //Find variable in local env
env = GetKeyValueInFile(configFile, key) //Find variable in local env
if env == "NOTFOUND" || env == "OK" {
env = manip.GetKeyValueInFile(globalConfigFile, key) //Find variable in global env
env = GetKeyValueInFile(globalConfigFile, key) //Find variable in global env
if env == "NOTFOUND" || env == "OK" {
//log.Fatal("Configuration not set for ", key)
return ""
Expand Down Expand Up @@ -93,8 +93,8 @@ func SetEnv(key, value string) {
w := bufio.NewWriter(f)
_, _ = w.WriteString(strings.Trim(newData, "\n"))
_ = w.Flush()
if manip.GetKeyValueInFile(".gitignore", configFileFileParentDir) == "NOTFOUND" {
manip.ReadAndAppend(".gitignore", configFileFileParentDir)
if GetKeyValueInFile(".gitignore", configFileFileParentDir) == "NOTFOUND" {
ReadAndAppend(".gitignore", configFileFileParentDir)
}
}

Expand Down
43 changes: 43 additions & 0 deletions internal/config/file.go
@@ -0,0 +1,43 @@
package config

import (
"io/ioutil"
"log"
"os"
"strings"
)

// ReadAndAppend : appends string to file
func ReadAndAppend(file, text string) {
// If the file doesn't exist, create it, or append to the file
f, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
if _, err := f.Write([]byte("\n" + text)); err != nil {
log.Fatal(err)
}
if err := f.Close(); err != nil {
log.Fatal(err)
}
}
// GetKeyValueInFile : returns env variable value
func GetKeyValueInFile(filePath, key string) string {
data, _ := ioutil.ReadFile(filePath)

file := string(data)
line := 0
temp := strings.Split(file, "\n")
for _, item := range temp {
//fmt.Println("[",line,"]",item)
env := strings.Split(item, "=")
if env[0] == key {
if len(env) > 1 {
return env[1]
}
return "OK"
}
line++
}
return "NOTFOUND"
}

0 comments on commit 77a15fe

Please sign in to comment.