Skip to content
This repository was archived by the owner on Mar 24, 2023. It is now read-only.

Commit 2200e0d

Browse files
authored
Merge pull request #551 from laverya/add-filepath-trim-option-to-github-asset
Add filepath trim option to GitHub asset
2 parents cc6f0fb + c185e4d commit 2200e0d

File tree

12 files changed

+309
-10
lines changed

12 files changed

+309
-10
lines changed

hack/docs/mutations.json

+13-4
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@
219219
"path": "properties.assets.properties.v1.items.properties.helm.properties.github.properties",
220220
"delete": [
221221
"dest",
222-
"mode"
222+
"mode",
223+
"strip_path"
223224
]
224225
},
225226
{
@@ -352,15 +353,17 @@
352353
"ref": "8fcaebe55af67fe6789fa678faaa76fa867fbc",
353354
"path": "k8s-yamls/",
354355
"dest": "./k8s/",
355-
"source": "private"
356+
"source": "private",
357+
"strip_path": ""
356358
},
357359
{
358360
"repo": "github.com/replicatedhq/ship",
359361
"ref": "master",
360362
"path": "hack/docs/",
361-
"dest": "./docs/",
363+
"dest": "./docs{{repl Add 1 1}}/",
362364
"source": "public",
363-
"mode": 644
365+
"mode": 644,
366+
"strip_path": "{{repl ParseBool \"true\"}}"
364367
}
365368
]
366369
},
@@ -409,6 +412,12 @@
409412
"description": "One of `public` or `private`, if `private`, access to the repo can be validated on release creation"
410413
}
411414
},
415+
{
416+
"path": "properties.assets.properties.v1.items.properties.github.properties.strip_path",
417+
"merge": {
418+
"description": "If true, the github directory will not be included in the filepath of the generated files. For instance, when outputting all files within 'source/' in the repository to the 'dest/' directory, the file 'source/a/file.txt' would be placed at 'dest/source/a/file.txt' when this is false and 'dest/a/file.txt' when this is true."
419+
}
420+
},
412421
{
413422
"path": "properties.assets.properties.v1.items.properties.github.properties.when",
414423
"merge": {

hack/docs/schema.json

+9-3
Original file line numberDiff line numberDiff line change
@@ -308,15 +308,17 @@
308308
"ref": "8fcaebe55af67fe6789fa678faaa76fa867fbc",
309309
"path": "k8s-yamls/",
310310
"dest": "./k8s/",
311-
"source": "private"
311+
"source": "private",
312+
"strip_path": ""
312313
},
313314
{
314315
"repo": "github.com/replicatedhq/ship",
315316
"ref": "master",
316317
"path": "hack/docs/",
317-
"dest": "./docs/",
318+
"dest": "./docs{{repl Add 1 1}}/",
318319
"source": "public",
319-
"mode": 644
320+
"mode": 644,
321+
"strip_path": "{{repl ParseBool \"true\"}}"
320322
}
321323
],
322324
"type": "object",
@@ -348,6 +350,10 @@
348350
"description": "One of `public` or `private`, if `private`, access to the repo can be validated on release creation",
349351
"type": "string"
350352
},
353+
"strip_path": {
354+
"description": "If true, the github directory will not be included in the filepath of the generated files. For instance, when outputting all files within 'source/' in the repository to the 'dest/' directory, the file 'source/a/file.txt' would be placed at 'dest/source/a/file.txt' when this is false and 'dest/a/file.txt' when this is true.",
355+
"type": "string"
356+
},
351357
"when": {
352358
"description": "This asset will be included when 'when' is omitted or true",
353359
"type": "string"

integration/init_app/github-template-func/expected/.ship/release.yml

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ assets:
2020
dest: ./github-noslash
2121
source: public
2222
ref: ad1e78d13c33fae7a7ce22ed19920945ceea23e9
23+
- github:
24+
repo: replicatedhq/test-charts
25+
path: template-functions
26+
dest: ./github-stripped
27+
strip_path: "{{repl ParseBool \"true\"}}"
28+
source: public
29+
ref: ad1e78d13c33fae7a7ce22ed19920945ceea23e9
2330
config:
2431
v1:
2532
- name: option_group
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#This file tests a part of the Config suite of template functions in Ship
2+
3+
Config option: abc123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#This file tests a part of the Integration suite of template functions in Ship
2+
3+
Release semver: 1.0.1-SNAPSHOT
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#This file tests a part of the Static suite of template functions in Ship
2+
3+
TwoPlusTwo: 4
4+
UPPERCASE: UPPERCASE

integration/init_app/github-template-func/input/.ship/release.yml

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ assets:
2020
dest: ./github-noslash
2121
source: public
2222
ref: ad1e78d13c33fae7a7ce22ed19920945ceea23e9
23+
- github:
24+
repo: replicatedhq/test-charts
25+
path: template-functions
26+
dest: ./github-stripped
27+
strip_path: "{{repl ParseBool \"true\"}}"
28+
source: public
29+
ref: ad1e78d13c33fae7a7ce22ed19920945ceea23e9
2330
config:
2431
v1:
2532
- name: option_group

pkg/api/asset.go

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type GitHubAsset struct {
6262
Ref string `json:"ref" yaml:"ref" hcl:"ref"`
6363
Path string `json:"path" yaml:"path" hcl:"path"`
6464
Source string `json:"source" yaml:"source" hcl:"source"`
65+
StripPath string `json:"strip_path" yaml:"strip_path" hcl:"strip_path"`
6566
}
6667

6768
// WebAsset is an asset whose contents are specified by the HTML at the corresponding URL

pkg/lifecycle/render/amazoneks/render_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ func TestBuildAsset(t *testing.T) {
10031003
req.Error(err)
10041004
}
10051005

1006-
req.Equal(got, tt.want)
1006+
req.Equal(tt.want, got)
10071007
})
10081008
}
10091009
}

pkg/lifecycle/render/github/render.go

+31-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/base64"
66
"os"
77
"path/filepath"
8+
"strings"
89

910
"github.com/go-kit/kit/log"
1011
"github.com/go-kit/kit/log/level"
@@ -100,7 +101,10 @@ func (r *LocalRenderer) Execute(
100101
return errors.Wrapf(err, "building %s", file.Path)
101102
}
102103

103-
filePath := filepath.Join(asset.Dest, file.Path)
104+
filePath, err := getDestPath(file.Path, asset, builder)
105+
if err != nil {
106+
return errors.Wrapf(err, "determining destination for %s", file.Path)
107+
}
104108

105109
basePath := filepath.Dir(filePath)
106110
debug.Log("event", "mkdirall.attempt", "root", rootFs.RootPath, "dest", filePath, "basePath", basePath)
@@ -123,3 +127,29 @@ func (r *LocalRenderer) Execute(
123127
return nil
124128
}
125129
}
130+
131+
func getDestPath(githubPath string, asset api.GitHubAsset, builder *templates.Builder) (string, error) {
132+
stripPath, err := builder.Bool(asset.StripPath, false)
133+
if err != nil {
134+
return "", errors.Wrapf(err, "parse boolean from %q", asset.StripPath)
135+
}
136+
137+
destDir, err := builder.String(asset.Dest)
138+
if err != nil {
139+
return "", errors.Wrapf(err, "get destination directory from %q", asset.Dest)
140+
}
141+
142+
if stripPath {
143+
// remove asset.Path's directory from the beginning of githubPath
144+
sourcePathDir := filepath.ToSlash(filepath.Dir(asset.Path)) + "/"
145+
githubPath = strings.TrimPrefix(githubPath, sourcePathDir)
146+
147+
// handle cases where the source path was a dir but a trailing slash was not included
148+
if !strings.HasSuffix(asset.Path, "/") {
149+
sourcePathBase := filepath.Base(asset.Path) + "/"
150+
githubPath = strings.TrimPrefix(githubPath, sourcePathBase)
151+
}
152+
}
153+
154+
return filepath.Join(destDir, githubPath), nil
155+
}

0 commit comments

Comments
 (0)