Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run: add support environment #20

Merged
merged 3 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions cmd/testops/result/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/qase-tms/qasectl/internal/parsers/qase"
"github.com/qase-tms/qasectl/internal/parsers/xctest"
"github.com/qase-tms/qasectl/internal/service/result"
"github.com/qase-tms/qasectl/internal/service/run"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log/slog"
Expand Down Expand Up @@ -65,12 +66,13 @@ func Command() *cobra.Command {
}

c := client.NewClientV1(token)
s := result.NewService(c, p)
rs := run.NewService(c)
s := result.NewService(c, p, rs)

param := result.UploadParams{
RunID: runID,
Title: title,
Description: &description,
Description: description,
Batch: batch,
Project: project,
}
Expand Down
7 changes: 5 additions & 2 deletions cmd/testops/run/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,29 @@ import (
const (
titleFlag = "title"
descriptionFlag = "description"
environmentFlag = "environment"
)

// Command returns a new cobra command for create runs
func Command() *cobra.Command {
var (
title string
description string
environment string
)

cmd := &cobra.Command{
Use: "create",
Short: "Create a new test run",
Example: "qli run create --title 'My test run' --description 'This is a test run' --project 'PRJ' --token 'TOKEN'",
Example: "qli run create --title 'My test run' --description 'This is a test run' --environment local --project 'PRJ' --token 'TOKEN'",
RunE: func(cmd *cobra.Command, args []string) error {
token := viper.GetString(flags.TokenFlag)
project := viper.GetString(flags.ProjectFlag)

c := client.NewClientV1(token)
s := run.NewService(c)

id, err := s.CreateRun(cmd.Context(), project, title, &description)
id, err := s.CreateRun(cmd.Context(), project, title, description, environment)
if err != nil {
return err
}
Expand All @@ -49,6 +51,7 @@ func Command() *cobra.Command {
fmt.Println(err)
}
cmd.Flags().StringVarP(&description, descriptionFlag, "d", "", "description of the test run")
cmd.Flags().StringVarP(&environment, environmentFlag, "e", "", "environment of the test run")

return cmd
}
56 changes: 49 additions & 7 deletions internal/client/clientv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
apiV1Client "github.com/qase-tms/qase-go/qase-api-client"
models "github.com/qase-tms/qasectl/internal/models/result"
"github.com/qase-tms/qasectl/internal/models/run"
"log/slog"
"os"
)
Expand All @@ -22,21 +23,62 @@ func NewClientV1(token string) *ClientV1 {
}
}

// GetEnvironments returns environments
func (c *ClientV1) GetEnvironments(ctx context.Context, projectCode string) ([]run.Environment, error) {
const op = "client.clientv1.getenvironments"
logger := slog.With("op", op)

logger.Debug("getting environments", "projectCode", projectCode)

ctx, client := c.getApiV1Client(ctx)

resp, r, err := client.EnvironmentsAPI.
GetEnvironments(ctx, projectCode).
Execute()

if err != nil {
logger.Debug("failed to get environments", "response", r)
return nil, fmt.Errorf("failed to get environments: %w", err)
}

environments := make([]run.Environment, 0, len(resp.Result.Entities))
for _, env := range resp.Result.Entities {
environments = append(environments, run.Environment{
Title: env.GetTitle(),
ID: env.GetId(),
Slug: env.GetSlug(),
})
}

logger.Debug("got environments", "environments", environments)

return environments, nil
}

// CreateRun creates a new run
func (c *ClientV1) CreateRun(ctx context.Context, projectCode, title string, description *string) (int64, error) {
func (c *ClientV1) CreateRun(ctx context.Context, projectCode, title string, description string, envID int64) (int64, error) {
const op = "client.clientv1.createrun"
logger := slog.With("op", op)

logger.Debug("creating run", "projectCode", projectCode, "title", title, "description", description)

ctx, client := c.getApiV1Client(ctx)

m := apiV1Client.RunCreate{
Title: title,
}

if description != "" {
m.SetDescription(description)
}

if envID != 0 {
m.SetEnvironmentId(envID)
}

logger.Debug("creating run", "projectCode", projectCode, "model", m)

resp, r, err := client.RunsAPI.
CreateRun(ctx, projectCode).
RunCreate(apiV1Client.RunCreate{
Title: title,
Description: description,
}).
RunCreate(m).
Execute()

if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions internal/models/run/environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package run

type Environment struct {
Title string `json:"title"`
ID int64 `json:"id"`
Slug string `json:"slug"`
}
2 changes: 1 addition & 1 deletion internal/service/result/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package result
type UploadParams struct {
RunID int64
Title string
Description *string
Description string
Batch int64
Project string
}
17 changes: 10 additions & 7 deletions internal/service/result/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,27 @@ import (

type client interface {
UploadData(ctx context.Context, project string, runID int64, results []models.Result) error
CreateRun(ctx context.Context, projectCode, title string, description *string) (int64, error)
CompleteRun(ctx context.Context, projectCode string, runId int64) error
}

type Parser interface {
Parse() ([]models.Result, error)
}

type runService interface {
CreateRun(ctx context.Context, p, t string, d, e string) (int64, error)
CompleteRun(ctx context.Context, projectCode string, runId int64) error
}

// Service is a service for importing data
type Service struct {
client client
parser Parser
rs runService
}

// NewService creates a new service
func NewService(client client, parser Parser) *Service {
return &Service{client: client, parser: parser}
func NewService(client client, parser Parser, rs runService) *Service {
return &Service{client: client, parser: parser, rs: rs}
}

// Upload imports the data
Expand All @@ -47,12 +51,11 @@ func (s *Service) Upload(ctx context.Context, p UploadParams) {

isTestRunCreated := false
if p.RunID == 0 {
runID, err := s.client.CreateRun(ctx, p.Project, p.Title, p.Description)
runID, err := s.rs.CreateRun(ctx, p.Project, p.Title, p.Description, "")
if err != nil {
logger.Error("failed to create run", "error", err)
return
}

p.RunID = runID
isTestRunCreated = true
}
Expand All @@ -72,7 +75,7 @@ func (s *Service) Upload(ctx context.Context, p UploadParams) {
}

if isTestRunCreated {
err := s.client.CompleteRun(ctx, p.Project, p.RunID)
err := s.rs.CompleteRun(ctx, p.Project, p.RunID)
if err != nil {
logger.Error("failed to complete run", "error", err)
}
Expand Down
26 changes: 22 additions & 4 deletions internal/service/run/run.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package run

import "context"
import (
"context"
"fmt"
"github.com/qase-tms/qasectl/internal/models/run"
)

// client is a client for run
type client interface {
CreateRun(ctx context.Context, projectCode, title string, description *string) (int64, error)
GetEnvironments(ctx context.Context, projectCode string) ([]run.Environment, error)
CreateRun(ctx context.Context, projectCode, title string, description string, envID int64) (int64, error)
CompleteRun(ctx context.Context, projectCode string, runId int64) error
}

Expand All @@ -19,8 +24,21 @@ func NewService(client client) *Service {
}

// CreateRun creates a new run
func (s *Service) CreateRun(ctx context.Context, projectCode, title string, description *string) (int64, error) {
return s.client.CreateRun(ctx, projectCode, title, description)
func (s *Service) CreateRun(ctx context.Context, p, t string, d, e string) (int64, error) {
var envID int64 = 0
if e != "" {
es, err := s.client.GetEnvironments(ctx, p)
if err != nil {
return 0, fmt.Errorf("failed to get environments: %w", err)
}
for _, env := range es {
if env.Slug == e {
envID = env.ID
}
}
}

return s.client.CreateRun(ctx, p, t, d, envID)
}

// CompleteRun completes a run
Expand Down