Skip to content

Commit

Permalink
Add selection for Github source on card creation
Browse files Browse the repository at this point in the history
Fixes #13
  • Loading branch information
Daniils Petrovs committed Jul 8, 2022
1 parent efc1f9a commit 5740abb
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 13 deletions.
20 changes: 16 additions & 4 deletions cmd/card_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var cardCreateCmd = &cobra.Command{

projects := client.FetchProjects(&zube.Query{})
workspaces := client.FetchWorkspaces(&zube.Query{})
sources := client.FetchSources()

// We need to get the project ID before any other question, since the other prompt option fetchers
// rely on it
Expand Down Expand Up @@ -98,6 +99,14 @@ var cardCreateCmd = &cobra.Command{
Default: "None",
},
},
{
Name: "source",
Prompt: &survey.Select{
Message: "Github source:",
Options: append(zube.SourceNames(&sources), "None"),
Default: "None",
},
},
{
Name: "priority",
Prompt: &survey.Select{
Expand All @@ -109,9 +118,9 @@ var cardCreateCmd = &cobra.Command{
}

answers := struct {
Workspace, Epic, Priority, Title, Description string
Labels []int
Assignees []string
Workspace, Epic, Priority, Title, Description, Source string
Labels []int
Assignees []string
}{}

err = survey.Ask(qs, &answers)
Expand All @@ -124,6 +133,8 @@ var cardCreateCmd = &cobra.Command{

epic := zube.GetEpicByTitle(answers.Epic, &epics)

source := zube.GetSourceByName(answers.Source, &sources)

priority := zube.ParsePriority(answers.Priority)

labels = zube.GetLabelsByIndexes(answers.Labels, labels)
Expand All @@ -138,7 +149,8 @@ var cardCreateCmd = &cobra.Command{
Priority: priority,
Body: answers.Description,
LabelIds: zube.LabelIds(&labels),
AssigneeIds: zube.MemberIds(&assignees)}
AssigneeIds: zube.MemberIds(&assignees),
GithubIssue: models.GithubIssue{SourceId: source.Id}}

newCard := client.CreateCard(&card)
account := client.FetchAccounts(
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/spf13/viper"
)

const Version = "0.2.0"
const Version = "0.2.1"

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand Down
14 changes: 14 additions & 0 deletions zube/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,20 @@ func (client *Client) FetchAccounts(query *Query) []models.Account {
return response.Data
}

// Fetch and return an array of Github `Source`s
func (client *Client) FetchSources() []models.Source {
var response models.PaginatedResponse[models.Source]

url := zubeURL("/api/sources", Query{})

body, err := client.performAPIRequestURLNoBody(http.MethodGet, &url)

Check(err, "failed to fetch sources")

json.Unmarshal(body, &response)
return response.Data
}

func (client *Client) FetchProjects(query *Query) []models.Project {
var response models.PaginatedResponse[models.Project]

Expand Down
27 changes: 27 additions & 0 deletions zube/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ package zube

import (
"fmt"
"log"
"strconv"

"github.com/markphelps/optional"
"github.com/platogo/zube-cli/zube/models"
)

func Check(err error, msg string) {
if err != nil {
log.Fatal(msg)
}
}

// Convert projects to a slice of project names
func ProjectNames(projects *[]models.Project) []string {
var names []string
Expand Down Expand Up @@ -136,6 +143,26 @@ func MemberIds(members *[]models.Member) []int {
return ids
}

func SourceNames(sources *[]models.Source) []string {
var names []string

for _, source := range *sources {
names = append(names, source.Name)
}

return names
}

func GetSourceByName(name string, sources *[]models.Source) models.Source {
for _, source := range *sources {
if source.Name == name {
return source
}
}

return models.Source{}
}

// Check if `coll` contains an element `e` of type `T`
func Contains[T comparable](coll []T, e T) bool {
for _, item := range coll {
Expand Down
17 changes: 9 additions & 8 deletions zube/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,15 @@ type Card struct {
Title string `json:"title"`
UpvotesCount int `json:"upvotes_count"`
Timestamps
EpicId int `json:"epic_id"`
CloserId int `json:"closer_id"`
Assignees []Assignee `json:"assignees"`
AssigneeIds []int `json:"assignee_ids"`
Creator []Person `json:"creator"`
Epic Epic `json:"epic"`
Labels []Label `json:"labels"`
LabelIds []int `json:"label_ids"`
GithubIssue GithubIssue `json:"github_issue"`
EpicId int `json:"epic_id"`
CloserId int `json:"closer_id"`
Assignees []Assignee `json:"assignees"`
AssigneeIds []int `json:"assignee_ids"`
Creator []Person `json:"creator"`
Epic Epic `json:"epic"`
Labels []Label `json:"labels"`
LabelIds []int `json:"label_ids"`
}

type Comment struct {
Expand Down

0 comments on commit 5740abb

Please sign in to comment.