Skip to content

Commit

Permalink
fix: allow app_name placeholder in schema validation (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenruizdegauna authored Feb 19, 2025
1 parent c8c99d6 commit 38da340
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
4 changes: 4 additions & 0 deletions publisher/config/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"errors"
"fmt"
"github.com/newrelic/infrastructure-publish-action/publisher/utils"
"gopkg.in/yaml.v2"
"io/ioutil"
"strings"
Expand Down Expand Up @@ -115,6 +116,9 @@ func validateName(appName string, src string) error {
if appName == "" {
return fmt.Errorf("%w: appName cannot be empty", ErrInvalidAppName)
}
if strings.HasPrefix(src, utils.PlaceholderForAppName) {
return nil
}
if !strings.HasPrefix(src, appName) {
return fmt.Errorf("%w: %s should prefix %s", ErrInvalidAppName, appName, src)
}
Expand Down
1 change: 1 addition & 0 deletions publisher/config/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func TestValidateSchemas(t *testing.T) {
schemas UploadArtifactSchemas
expectedError error
}{
{name: "valid app name with placeholder", appName: "some-app-name", schemas: UploadArtifactSchemas{{Src: "{app_name}_linux_{version}_{arch}.deb", Uploads: []Upload{{Type: TypeApt}}}}, expectedError: nil},
{name: "valid app name with pkg suffix", appName: "some-app-name", schemas: UploadArtifactSchemas{{Src: "some-app-name_some_suffix_0.0.1_amd64.deb", Uploads: []Upload{{Type: TypeApt}}}}, expectedError: nil},
{name: "valid app name exactly as pkg name", appName: "some-app-name", schemas: UploadArtifactSchemas{{Src: "some-app-name_0.0.1_amd64.deb", Uploads: []Upload{{Type: TypeApt}}}}, expectedError: nil},
{name: "invalid app name", appName: "some-app-name", schemas: UploadArtifactSchemas{{Src: "some-other-name_0.0.1_amd64.deb", Uploads: []Upload{{Type: TypeFile}}}}, expectedError: ErrInvalidAppName},
Expand Down
18 changes: 9 additions & 9 deletions publisher/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ const (
placeholderForOsVersion = "{os_version}"
placeholderForDestPrefix = "{dest_prefix}"
placeholderForRepoName = "{repo_name}"
placeholderForAppName = "{app_name}"
PlaceholderForAppName = "{app_name}"
placeholderForArch = "{arch}"
placeholderForTag = "{tag}"
placeholderForVersion = "{version}"
PlaceholderForSrc = "{src}"
PlaceholderForAccessPointHost = "{access_point_host}"

s3RetrySleepTimeout = 3 * time.Second
s3Retries = 10
s3Retries = 10
)

var (
Logger = log.New(log.Writer(), "", 0)
Logger = log.New(log.Writer(), "", 0)
)

func ReadFileContent(filePath string) ([]byte, error) {
Expand All @@ -41,7 +41,7 @@ func ReadFileContent(filePath string) ([]byte, error) {

func ReplacePlaceholders(template, repoName, appName, arch, tag, version, destPrefix, osVersion string) (str string) {
str = strings.Replace(template, placeholderForRepoName, repoName, -1)
str = strings.Replace(str, placeholderForAppName, appName, -1)
str = strings.Replace(str, PlaceholderForAppName, appName, -1)
str = strings.Replace(str, placeholderForArch, arch, -1)
str = strings.Replace(str, placeholderForTag, tag, -1)
str = strings.Replace(str, placeholderForVersion, version, -1)
Expand Down Expand Up @@ -111,7 +111,7 @@ func CopyFile(srcPath string, destPath string, override bool, commandTimeout tim

Logger.Println("[ ] Create " + destDirectory)

if err = ExecWithRetries(s3Retries, S3RemountFn, Logger, "mkdir", commandTimeout,"-p", destDirectory); err != nil {
if err = ExecWithRetries(s3Retries, S3RemountFn, Logger, "mkdir", commandTimeout, "-p", destDirectory); err != nil {
return err
}

Expand All @@ -120,10 +120,10 @@ func CopyFile(srcPath string, destPath string, override bool, commandTimeout tim
Logger.Println("[ ] Copy " + srcPath + " into " + destPath)

if override {
if err = ExecWithRetries(s3Retries, S3RemountFn, Logger, "cp", commandTimeout,"-f", srcPath, destPath); err != nil {
if err = ExecWithRetries(s3Retries, S3RemountFn, Logger, "cp", commandTimeout, "-f", srcPath, destPath); err != nil {
return err
}
}else{
} else {
// Note: we are not doing retries here as this command is not
// idempotent. If one copy fails, retry will skip and leave corrupted
// file in the repo
Expand Down Expand Up @@ -154,7 +154,7 @@ func ExecWithRetries(retries int, s3Remount RetryCallback, l *log.Logger, cmdNam
type RetryCallback func(l *log.Logger, commandTimeout time.Duration)

func S3RemountFn(l *log.Logger, commandTimeout time.Duration) {
err := ExecLogOutput(l, "make", commandTimeout ,"unmount-s3")
err := ExecLogOutput(l, "make", commandTimeout, "unmount-s3")
if err != nil {
l.Printf("unmounting s3 failed %v", err)
}
Expand All @@ -163,4 +163,4 @@ func S3RemountFn(l *log.Logger, commandTimeout time.Duration) {
if err != nil {
l.Printf("mounting s3 failed %v", err)
}
}
}

0 comments on commit 38da340

Please sign in to comment.