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

support host static html resources of test report #3862

Merged
merged 2 commits into from
Nov 29, 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
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/koderover/zadig/v2/pkg/cli/zadig-agent/internal/common/types"
"github.com/koderover/zadig/v2/pkg/tool/s3"
"github.com/koderover/zadig/v2/pkg/types/step"
"github.com/koderover/zadig/v2/pkg/util/fs"
)

type TarArchiveStep struct {
Expand Down Expand Up @@ -78,18 +77,18 @@ func (s *TarArchiveStep) Run(ctx context.Context) error {
cmdAndArtifactFullPaths := make([]string, 0)
cmdAndArtifactFullPaths = append(cmdAndArtifactFullPaths, "-czf")
cmdAndArtifactFullPaths = append(cmdAndArtifactFullPaths, tarName)
if s.spec.ChangeTarDir {
cmdAndArtifactFullPaths = append(cmdAndArtifactFullPaths, "--exclude", tarName, "-C", s.spec.TarDir)
}

for _, artifactPath := range s.spec.ResultDirs {
if len(artifactPath) == 0 {
continue
}
artifactPath = helper.ReplaceEnvWithValue(artifactPath, envMap)
artifactPath = strings.TrimPrefix(artifactPath, "/")

artifactPath := filepath.Join(s.workspace, artifactPath)
isDir, err := fs.IsDir(artifactPath)
if err != nil || !isDir {
s.logger.Errorf("artifactPath is not exist %s or is not dir, err: %s", artifactPath, err)
continue
if !s.spec.AbsResultDir {
artifactPath = strings.TrimPrefix(artifactPath, "/")
artifactPath = filepath.Join(s.workspace, artifactPath)
}
cmdAndArtifactFullPaths = append(cmdAndArtifactFullPaths, artifactPath)
}
Expand All @@ -111,8 +110,11 @@ func (s *TarArchiveStep) Run(ctx context.Context) error {
if err != nil {
return fmt.Errorf("failed to close %s err: %s", tarName, err)
}

cmd := exec.Command("tar", cmdAndArtifactFullPaths...)
cmd.Stderr = os.Stderr

log.Debugf("tar cmd: %s", cmd.String())
if err = cmd.Run(); err != nil {
if s.spec.IgnoreErr {
s.logger.Errorf("failed to compress %s, cmd: %s, err: %s", tarName, cmd.String(), err)
Expand Down
7 changes: 3 additions & 4 deletions pkg/cli/zadig-agent/internal/agent/step/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,9 @@ func RunStep(ctx context.Context, jobCtx *jobctl.JobContext, step *commonmodels.
case "debug_after":
return nil
default:
//err := fmt.Errorf("step type: %s does not match any known type", step.StepType)
//log.Error(err)
//return err
logger.Infof(fmt.Sprintf("step type: %s does not match any known type", step.StepType))
err := fmt.Errorf("step type: %s does not match any known type", step.StepType)
log.Error(err)
return err
}
if err := stepInstance.Run(ctx); err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ func LocalChartTemplatePath(name string) string {
return LocalTemplatePath(name, setting.ChartTemplatesPath)
}

// if this path is changed, must also change cleanCacheFiles in pkg/microservice/aslan/core/clean_cache_files.go
func LocalHtmlReportPath(projectName, workflowName, jobTaskName string, taskID int64) string {
return filepath.Join(DataPath(), "project", projectName, "workflow", workflowName, "jobTask", jobTaskName, "task", fmt.Sprintf("%d", taskID), "html-report") + "/"
}

func MongoURI() string {
return viper.GetString(setting.ENVMongoDBConnectionString)
}
Expand Down
106 changes: 106 additions & 0 deletions pkg/microservice/aslan/core/clean_cache_files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
Copyright 2024 The KodeRover Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package core

import (
"fmt"
"os"
"path/filepath"
"time"

commonconfig "github.com/koderover/zadig/v2/pkg/config"
"github.com/koderover/zadig/v2/pkg/tool/log"
)

func cleanCacheFiles() {
projectPath := filepath.Join(commonconfig.DataPath(), "project")
err := cleanProjectFiles(projectPath)
if err != nil {
log.Errorf("[cleanCacheFiles] failed to clean project cache files: %v", err)
}
}

func cleanProjectFiles(path string) error {
return traverseDir(path, cleanWorkflowFiles)
}

func cleanWorkflowFiles(path string, info os.FileInfo) error {
workflowPath := filepath.Join(path, "workflow")
return traverseDir(workflowPath, cleanTaskFiles)
}

func cleanjobTaskFiles(path string, info os.FileInfo) error {
jobTaskPath := filepath.Join(path, "jobTask")
return traverseDir(jobTaskPath, cleanTaskFiles)
}

func cleanTaskFiles(path string, info os.FileInfo) error {
taskPath := filepath.Join(path, "task")
return traverseDir(taskPath, cleanHtmlReportFiles)
}

func cleanHtmlReportFiles(path string, info os.FileInfo) error {
htmlReportPath := filepath.Join(path, "html-report")
htmlReportInfo, err := os.Stat(htmlReportPath)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return fmt.Errorf("failed to stat htmlReportPath: %s, err: %v", htmlReportPath, err)
}

if time.Since(htmlReportInfo.ModTime()) > 7*24*time.Hour {
err = os.RemoveAll(htmlReportPath)
if err != nil {
return fmt.Errorf("failed to remove htmlReportPath: %s, err: %v", htmlReportPath, err)
}
}
return nil
}

func traverseDir(path string, fn func(string, os.FileInfo) error) error {
pathInfo, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return fmt.Errorf("failed to stat path: %s, err: %v", path, err)
}

if !pathInfo.IsDir() {
return fmt.Errorf("path is not a directory: %s", path)
}

files, err := os.ReadDir(path)
if err != nil {
return fmt.Errorf("failed to read directory: %s, err: %v", path, err)
}

for _, file := range files {
info, err := file.Info()
if err != nil {
return fmt.Errorf("failed to get file info: %s, err: %v", file.Name(), err)
}
if info.IsDir() {
err = fn(filepath.Join(path, file.Name()), info)
if err != nil {
return err
}
}
}
return nil
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ func (w *WorkflowV4) CalculateHash() [md5.Size]byte {
return md5.Sum(jsonBytes)
}

type ParameterSettingType string

const (
StringType ParameterSettingType = "string"
ChoiceType ParameterSettingType = "choice"
ImageType ParameterSettingType = "image"
Script ParameterSettingType = "script"
// Deprecated
ExternalType ParameterSettingType = "external"
)

type WorkflowStage struct {
Name string `bson:"name" yaml:"name" json:"name"`
Parallel bool `bson:"parallel" yaml:"parallel" json:"parallel"`
Expand Down
Loading