Skip to content

Commit

Permalink
feat: add tasks schema, fix up object owned mixin
Browse files Browse the repository at this point in the history
Signed-off-by: Sarah Funkhouser <147884153+golanglemonade@users.noreply.github.com>
  • Loading branch information
golanglemonade committed Nov 8, 2024
1 parent e4d5efa commit a01741e
Show file tree
Hide file tree
Showing 141 changed files with 55,983 additions and 12,332 deletions.
25 changes: 23 additions & 2 deletions cmd/cli/Taskfile.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
version: "3"

env:
DEFAULT_USER_EMAIL: "mitb@theopenlane.io"
SECOND_USER_EMAIL: "funk@theopenlane.io"

tasks:
generate:all:history:
desc: generates a new cli cmd for all missing history commands from the query/ directory
Expand Down Expand Up @@ -44,22 +48,39 @@ tasks:
- curl http://localhost:17608/v1/verify?token={{.VERIFY_TOKEN}}
vars:
VERIFY_TOKEN:
sh: go run main.go register --email="mitb@theopenlane.io" --first-name="matt" --last-name="anderson" --password="mattisthebest1234" | jq -r .token
sh: go run main.go register --email="{{ .EMAIL_ADDRESS | default .DEFAULT_USER_EMAIL }}" --first-name="matt" --last-name="anderson" --password="mattisthebest1234" | jq -r .token

login:creds:
desc: a task to login the verified user
aliases: [login]
env:
CORE_PASSWORD: mattisthebest1234
cmds:
- go run main.go login -u mitb@theopenlane.io
- go run main.go login -u {{ .EMAIL_ADDRESS | default .DEFAULT_USER_EMAIL }}

user:all:
desc: a task to register, verify, and login a new user
cmds:
- task: verifyuser
- task: login

user:all:another:
desc: a task to register, verify, and login another user
cmds:
- task: verifyuser
vars:
EMAIL_ADDRESS: "{{ .SECOND_USER_EMAIL }}"
- task: login
vars:
EMAIL_ADDRESS: "{{ .SECOND_USER_EMAIL }}"

login:another:
desc: a task to login another user
cmds:
- task: login
vars:
EMAIL_ADDRESS: "{{ .SECOND_USER_EMAIL }}"

login:google:
desc: a task to login with google oauth
aliases: [google]
Expand Down
93 changes: 93 additions & 0 deletions cmd/cli/cmd/task/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package task

import (
"context"
"time"

"github.com/spf13/cobra"

"github.com/theopenlane/core/cmd/cli/cmd"
"github.com/theopenlane/core/pkg/enums"
"github.com/theopenlane/core/pkg/openlaneclient"
)

var createCmd = &cobra.Command{
Use: "create",
Short: "create a new task",
Run: func(cmd *cobra.Command, args []string) {
err := create(cmd.Context())
cobra.CheckErr(err)
},
}

func init() {
command.AddCommand(createCmd)

// command line flags for the create command
createCmd.Flags().StringP("title", "t", "", "title of the task")
createCmd.Flags().StringP("description", "d", "", "description of the task")
createCmd.Flags().StringP("status", "s", "", "status of the task")
createCmd.Flags().StringP("assignee", "a", "", "assignee (user ID) of the task")
createCmd.Flags().Duration("due", 0, "time until due date of the task")
createCmd.Flags().StringP("organization", "o", "", "organization ID of the task to own the task, this will give the organization access to the task")
createCmd.Flags().StringP("group", "g", "", "group ID of the task to own the task, this will give the group access to the task")
}

// createValidation validates the required fields for the command
func createValidation() (input openlaneclient.CreateTaskInput, err error) {
// validation of required fields for the create command
// output the input struct with the required fields and optional fields based on the command line flags
input.Title = cmd.Config.String("title")
if input.Title == "" {
return input, cmd.NewRequiredFieldMissingError("task title")
}

description := cmd.Config.String("description")
if description != "" {
input.Description = &description
}

status := cmd.Config.String("status")
if status != "" {
input.Status = enums.ToTaskStatus(status)
}

assignee := cmd.Config.String("assignee")
if assignee != "" {
input.Assignee = &assignee
}

due := cmd.Config.Duration("due")
if due != 0 {
dueDate := time.Now().Add(due)
input.Due = &dueDate
}

organization := cmd.Config.String("organization")
if organization != "" {
input.OrganizationIDs = []string{organization}
}

group := cmd.Config.String("group")
if group != "" {
input.GroupIDs = []string{group}
}

return input, nil
}

// create a new task
func create(ctx context.Context) error {
// setup http client
client, err := cmd.SetupClientWithAuth(ctx)
cobra.CheckErr(err)
defer cmd.StoreSessionCookies(client)

input, err := createValidation()
cobra.CheckErr(err)

o, err := client.CreateTask(ctx, input)
cobra.CheckErr(err)

return consoleOutput(o)
}
50 changes: 50 additions & 0 deletions cmd/cli/cmd/task/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package task

import (
"context"

"github.com/spf13/cobra"

"github.com/theopenlane/core/cmd/cli/cmd"
)

var deleteCmd = &cobra.Command{
Use: "delete",
Short: "delete an existing task",
Run: func(cmd *cobra.Command, args []string) {
err := delete(cmd.Context())
cobra.CheckErr(err)
},
}

func init() {
command.AddCommand(deleteCmd)

deleteCmd.Flags().StringP("id", "i", "", "task id to delete")
}

// deleteValidation validates the required fields for the command
func deleteValidation() (string, error) {
id := cmd.Config.String("id")
if id == "" {
return "", cmd.NewRequiredFieldMissingError("task id")
}

return id, nil
}

// delete an existing task in the platform
func delete(ctx context.Context) error {
// setup http client
client, err := cmd.SetupClientWithAuth(ctx)
cobra.CheckErr(err)
defer cmd.StoreSessionCookies(client)

id, err := deleteValidation()
cobra.CheckErr(err)

o, err := client.DeleteTask(ctx, id)
cobra.CheckErr(err)

return consoleOutput(o)
}
2 changes: 2 additions & 0 deletions cmd/cli/cmd/task/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package task is our cobra cli for task endpoints
package task
48 changes: 48 additions & 0 deletions cmd/cli/cmd/task/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package task

import (
"context"

"github.com/spf13/cobra"

"github.com/theopenlane/core/cmd/cli/cmd"
)

var getCmd = &cobra.Command{
Use: "get",
Short: "get an existing task",
Run: func(cmd *cobra.Command, args []string) {
err := get(cmd.Context())
cobra.CheckErr(err)
},
}

func init() {
command.AddCommand(getCmd)

getCmd.Flags().StringP("id", "i", "", "task id to query")
}

// get an existing task in the platform
func get(ctx context.Context) error {
// setup http client
client, err := cmd.SetupClientWithAuth(ctx)
cobra.CheckErr(err)
defer cmd.StoreSessionCookies(client)
// filter options
id := cmd.Config.String("id")

// if an task ID is provided, filter on that task, otherwise get all
if id != "" {
o, err := client.GetTaskByID(ctx, id)
cobra.CheckErr(err)

return consoleOutput(o)
}

// get all will be filtered for the authorized organization(s)
o, err := client.GetAllTasks(ctx)
cobra.CheckErr(err)

return consoleOutput(o)
}
106 changes: 106 additions & 0 deletions cmd/cli/cmd/task/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package task

import (
"encoding/json"
"strings"

"github.com/spf13/cobra"

"github.com/theopenlane/utils/cli/tables"

"github.com/theopenlane/core/cmd/cli/cmd"
"github.com/theopenlane/core/pkg/openlaneclient"
)

// command represents the base task command when called without any subcommands
var command = &cobra.Command{
Use: "task",
Short: "the subcommands for working with tasks",
}

func init() {
cmd.RootCmd.AddCommand(command)
}

// consoleOutput prints the output in the console
func consoleOutput(e any) error {
// check if the output format is JSON and print the tasks in JSON format
if strings.EqualFold(cmd.OutputFormat, cmd.JSONOutput) {
return jsonOutput(e)
}

// check the type of the tasks and print them in a table format
switch v := e.(type) {
case *openlaneclient.GetAllTasks:
var nodes []*openlaneclient.GetAllTasks_Tasks_Edges_Node

for _, i := range v.Tasks.Edges {
nodes = append(nodes, i.Node)
}

e = nodes
case *openlaneclient.GetTasks:
var nodes []*openlaneclient.GetTasks_Tasks_Edges_Node

for _, i := range v.Tasks.Edges {
nodes = append(nodes, i.Node)
}

e = nodes
case *openlaneclient.GetTaskByID:
e = v.Task
case *openlaneclient.CreateTask:
e = v.CreateTask.Task
case *openlaneclient.UpdateTask:
e = v.UpdateTask.Task
case *openlaneclient.DeleteTask:
deletedTableOutput(v)
return nil
}

s, err := json.Marshal(e)
cobra.CheckErr(err)

var list []openlaneclient.Task

err = json.Unmarshal(s, &list)
if err != nil {
var in openlaneclient.Task
err = json.Unmarshal(s, &in)
cobra.CheckErr(err)

list = append(list, in)
}

tableOutput(list)

return nil
}

// jsonOutput prints the output in a JSON format
func jsonOutput(out any) error {
s, err := json.Marshal(out)
cobra.CheckErr(err)

return cmd.JSONPrint(s)
}

// tableOutput prints the output in a table format
func tableOutput(out []openlaneclient.Task) {
// create a table writer
writer := tables.NewTableWriter(command.OutOrStdout(), "ID", "Title", "Description", "Details", "Assignee", "Assigner", "Status", "Due")
for _, i := range out {
writer.AddRow(i.ID, i.Title, *i.Description, i.Details, *i.Assignee, i.Assigner, i.Status, i.Due)
}

writer.Render()
}

// deleteTableOutput prints the deleted id in a table format
func deletedTableOutput(e *openlaneclient.DeleteTask) {
writer := tables.NewTableWriter(command.OutOrStdout(), "DeletedID")

writer.AddRow(e.DeleteTask.DeletedID)

writer.Render()
}
Loading

0 comments on commit a01741e

Please sign in to comment.