Skip to content

Commit 2ed89af

Browse files
author
Aaron Schlesinger
committed
ref(*): make git.SHA and storage.SlugBuilderInfo fields immutable
… and provide read-only funcs for them
1 parent 72d8a64 commit 2ed89af

5 files changed

Lines changed: 43 additions & 35 deletions

File tree

pkg/gitreceive/build.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func build(conf *Config, s3Client *s3.S3, builderKey, rawGitSha string) error {
5353
repoDir := filepath.Join(conf.GitHome, repo)
5454
buildDir := filepath.Join(repoDir, "build")
5555

56-
slugName := fmt.Sprintf("%s:git-%s", appName, gitSha.Short)
56+
slugName := fmt.Sprintf("%s:git-%s", appName, gitSha.Short())
5757
imageName := strings.Replace(slugName, ":", "-", -1)
5858
if err := os.MkdirAll(buildDir, os.ModeDir); err != nil {
5959
return fmt.Errorf("making the build directory %s (%s)", buildDir, err)
@@ -78,7 +78,7 @@ func build(conf *Config, s3Client *s3.S3, builderKey, rawGitSha string) error {
7878

7979
// build a tarball from the new objects
8080
appTgz := fmt.Sprintf("%s.tar.gz", appName)
81-
gitArchiveCmd := repoCmd(repoDir, "git", "archive", "--format=tar.gz", fmt.Sprintf("--output=%s", appTgz), gitSha.Full)
81+
gitArchiveCmd := repoCmd(repoDir, "git", "archive", "--format=tar.gz", fmt.Sprintf("--output=%s", appTgz), gitSha.Full())
8282
gitArchiveCmd.Stdout = os.Stdout
8383
gitArchiveCmd.Stderr = os.Stderr
8484
if err := run(gitArchiveCmd); err != nil {
@@ -137,15 +137,15 @@ func build(conf *Config, s3Client *s3.S3, builderKey, rawGitSha string) error {
137137
var finalManifest string
138138
uid := uuid.New()[:8]
139139
if usingDockerfile {
140-
buildPodName = fmt.Sprintf("dockerbuild-%s-%s-%s", appName, gitSha.Short, uid)
140+
buildPodName = fmt.Sprintf("dockerbuild-%s-%s-%s", appName, gitSha.Short(), uid)
141141
finalManifest = strings.Replace(string(fileBytes), "repo_name", buildPodName, -1)
142-
finalManifest = strings.Replace(finalManifest, "puturl", slugBuilderInfo.PushURL, -1)
143-
finalManifest = strings.Replace(finalManifest, "tar-url", slugBuilderInfo.TarURL, -1)
142+
finalManifest = strings.Replace(finalManifest, "puturl", slugBuilderInfo.PushURL(), -1)
143+
finalManifest = strings.Replace(finalManifest, "tar-url", slugBuilderInfo.TarURL(), -1)
144144
} else {
145-
buildPodName = fmt.Sprintf("slugbuild-%s-%s-%s", appName, gitSha.Short, uid)
145+
buildPodName = fmt.Sprintf("slugbuild-%s-%s-%s", appName, gitSha.Short(), uid)
146146
finalManifest = strings.Replace(string(fileBytes), "repo_name", buildPodName, -1)
147-
finalManifest = strings.Replace(finalManifest, "puturl", slugBuilderInfo.PushURL, -1)
148-
finalManifest = strings.Replace(finalManifest, "tar-url", slugBuilderInfo.TarURL, -1)
147+
finalManifest = strings.Replace(finalManifest, "puturl", slugBuilderInfo.PushURL(), -1)
148+
finalManifest = strings.Replace(finalManifest, "tar-url", slugBuilderInfo.TarURL(), -1)
149149
finalManifest = strings.Replace(finalManifest, "buildurl", buildPackURL, -1)
150150
}
151151

@@ -164,9 +164,9 @@ func build(conf *Config, s3Client *s3.S3, builderKey, rawGitSha string) error {
164164
return fmt.Errorf("opening %s for read (%s)", appTgz, err)
165165
}
166166

167-
log.Debug("Uploading tar to %s/%s/%s", s3Client.Endpoint, bucketName, slugBuilderInfo.TarKey)
168-
if err := storage.UploadObject(s3Client, bucketName, slugBuilderInfo.TarKey, appTgzReader); err != nil {
169-
return fmt.Errorf("uploading %s to %s/%s (%v)", absAppTgz, bucketName, slugBuilderInfo.TarKey, err)
167+
log.Debug("Uploading tar to %s/%s/%s", s3Client.Endpoint, bucketName, slugBuilderInfo.TarKey())
168+
if err := storage.UploadObject(s3Client, bucketName, slugBuilderInfo.TarKey(), appTgzReader); err != nil {
169+
return fmt.Errorf("uploading %s to %s/%s (%v)", absAppTgz, bucketName, slugBuilderInfo.TarKey(), err)
170170
}
171171

172172
log.Info("Starting build... but first, coffee!")
@@ -219,9 +219,9 @@ func build(conf *Config, s3Client *s3.S3, builderKey, rawGitSha string) error {
219219
// poll the s3 server to ensure the slug exists
220220
// TODO: time out looking
221221
for {
222-
exists, err := storage.ObjectExists(s3Client, bucketName, slugBuilderInfo.PushKey)
222+
exists, err := storage.ObjectExists(s3Client, bucketName, slugBuilderInfo.PushKey())
223223
if err != nil {
224-
return fmt.Errorf("Checking if object %s/%s exists (%s)", bucketName, slugBuilderInfo.PushKey, err)
224+
return fmt.Errorf("Checking if object %s/%s exists (%s)", bucketName, slugBuilderInfo.PushKey(), err)
225225
}
226226
if exists {
227227
break
@@ -233,7 +233,7 @@ func build(conf *Config, s3Client *s3.S3, builderKey, rawGitSha string) error {
233233
log.Info("Launching...")
234234

235235
buildHook := &pkg.BuildHook{
236-
Sha: gitSha.Full,
236+
Sha: gitSha.Full(),
237237
ReceiveUser: conf.Username,
238238
ReceiveRepo: appName,
239239
Image: appName,

pkg/gitreceive/git/sha.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ func (e ErrInvalidGitSha) Error() string {
2222
}
2323

2424
type SHA struct {
25-
Full string
26-
Short string
25+
full string
26+
short string
2727
}
2828

2929
func NewSha(rawSha string) (*SHA, error) {
3030
if !shaRegex.Match([]byte(rawSha)) {
3131
return nil, ErrInvalidGitSha{sha: rawSha}
3232
}
33-
return &SHA{Full: rawSha, Short: rawSha[0:8]}, nil
33+
return &SHA{full: rawSha, short: rawSha[0:8]}, nil
3434
}
35+
36+
func (s SHA) Full() string { return s.full }
37+
func (s SHA) Short() string { return s.short }

pkg/gitreceive/git/sha_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ func TestNewSha(t *testing.T) {
3434
t.Errorf("expected no error for sha %s (#%d), got %s", shaStr, i, err)
3535
continue
3636
}
37-
if sha.Full != shaStr {
38-
t.Errorf("expected full sha string to be %s, got %s", shaStr, sha.Full)
37+
if sha.Full() != shaStr {
38+
t.Errorf("expected full sha string to be %s, got %s", shaStr, sha.Full())
3939
}
40-
if sha.Short != shaStr[0:8] {
41-
t.Errorf("expected short sha to be first 8 characters of %s, got %s", shaStr, sha.Short)
40+
if sha.Short() != shaStr[0:8] {
41+
t.Errorf("expected short sha to be first 8 characters of %s, got %s", shaStr, sha.Short())
4242
}
4343
}
4444
}

pkg/gitreceive/storage/slug_builder_info.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88

99
// SlugBuilderInfo contains all of the object storage related information needed to pass to a slug builder
1010
type SlugBuilderInfo struct {
11-
PushKey string
12-
PushURL string
13-
TarKey string
14-
TarURL string
11+
pushKey string
12+
pushURL string
13+
tarKey string
14+
tarURL string
1515
}
1616

1717
// NewSlugBuilderInfo creates and populates a new SlugBuilderInfo based on the given data
@@ -21,9 +21,14 @@ func NewSlugBuilderInfo(s3Endpoint, appName, slugName string, gitSha *git.SHA) *
2121
pushKey := fmt.Sprintf("home/%s:git-%s/push", appName, gitSha.Full)
2222

2323
return &SlugBuilderInfo{
24-
PushKey: pushKey,
25-
PushURL: fmt.Sprintf("%s/git/%s", s3Endpoint, pushKey),
26-
TarKey: tarKey,
27-
TarURL: fmt.Sprintf("%s/git/%s", s3Endpoint, tarKey),
24+
pushKey: pushKey,
25+
pushURL: fmt.Sprintf("%s/git/%s", s3Endpoint, pushKey),
26+
tarKey: tarKey,
27+
tarURL: fmt.Sprintf("%s/git/%s", s3Endpoint, tarKey),
2828
}
2929
}
30+
31+
func (s SlugBuilderInfo) PushKey() string { return s.pushKey }
32+
func (s SlugBuilderInfo) PushURL() string { return s.pushURL }
33+
func (s SlugBuilderInfo) TarKey() string { return s.tarKey }
34+
func (s SlugBuilderInfo) TarURL() string { return s.tarURL }

pkg/gitreceive/storage/slug_builder_info_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ func TestS3Endpoint(t *testing.T) {
2020
}
2121
sbi := NewSlugBuilderInfo(s3Endpoint, appName, slugName, sha)
2222

23-
expectedPushURL := s3Endpoint + "/git/" + sbi.PushKey
24-
if sbi.PushURL != expectedPushURL {
25-
t.Errorf("push URL %s didn't match expected %s", sbi.PushURL, expectedPushURL)
23+
expectedPushURL := s3Endpoint + "/git/" + sbi.PushKey()
24+
if sbi.PushURL() != expectedPushURL {
25+
t.Errorf("push URL %s didn't match expected %s", sbi.PushURL(), expectedPushURL)
2626
}
27-
expectedTarURL := s3Endpoint + "/git/" + sbi.TarKey
28-
if sbi.TarURL != expectedTarURL {
29-
t.Errorf("tar URL %s didn't match expected %s", sbi.TarURL, expectedTarURL)
27+
expectedTarURL := s3Endpoint + "/git/" + sbi.TarKey()
28+
if sbi.TarURL() != expectedTarURL {
29+
t.Errorf("tar URL %s didn't match expected %s", sbi.TarURL(), expectedTarURL)
3030
}
3131
}

0 commit comments

Comments
 (0)