Skip to content

Commit

Permalink
fix: use SEMAPHORE_GIT_PR_BRANCH if available (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaspin authored May 5, 2022
1 parent 0054f18 commit e6c484e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cache-cli/cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func RunRestore(cmd *cobra.Command, args []string) {

if len(args) == 0 {
lookupResults := files.Lookup(files.LookupOptions{
GitBranch: os.Getenv("SEMAPHORE_GIT_BRANCH"),
GitBranch: FindGitBranch(),
Restore: true,
})

Expand Down
30 changes: 29 additions & 1 deletion cache-cli/cmd/restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,12 @@ func Test__AutomaticRestore(t *testing.T) {
assert.Contains(t, output, "Nothing to restore from cache")
})

t.Run(fmt.Sprintf("%s detects and restores", backend), func(t *testing.T) {
t.Run(fmt.Sprintf("%s detects and restores using SEMAPHORE_GIT_BRANCH", backend), func(t *testing.T) {
storage.Clear()

os.Chdir(fmt.Sprintf("%s/test/autocache/gems", rootPath))
os.Setenv("SEMAPHORE_GIT_BRANCH", "master")
os.Setenv("SEMAPHORE_GIT_PR_BRANCH", "")
os.MkdirAll("vendor/bundle", os.ModePerm)

// storing
Expand All @@ -208,5 +209,32 @@ func Test__AutomaticRestore(t *testing.T) {
os.RemoveAll("vendor")
os.Remove(compressedFile)
})

t.Run(fmt.Sprintf("%s detects and restores using SEMAPHORE_GIT_PR_BRANCH", backend), func(t *testing.T) {
storage.Clear()

os.Chdir(fmt.Sprintf("%s/test/autocache/gems", rootPath))
os.Setenv("SEMAPHORE_GIT_BRANCH", "master")
os.Setenv("SEMAPHORE_GIT_PR_BRANCH", "some-development-branch")
os.MkdirAll("vendor/bundle", os.ModePerm)

// storing
checksum, _ := files.GenerateChecksum("Gemfile.lock")
key := fmt.Sprintf("gems-some-development-branch-%s", checksum)
compressedFile, _ := files.Compress(key, "vendor/bundle")
storage.Store(key, compressedFile)

// restoring
capturer := utils.CreateOutputCapturer()
RunRestore(restoreCmd, []string{})
output := capturer.Done()

assert.Contains(t, output, "Detected Gemfile.lock")
assert.Contains(t, output, fmt.Sprintf("Downloading key '%s'", key))
assert.Contains(t, output, fmt.Sprintf("Restored: %s", filepath.FromSlash("vendor/bundle")))

os.RemoveAll("vendor")
os.Remove(compressedFile)
})
})
}
11 changes: 10 additions & 1 deletion cache-cli/cmd/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func RunStore(cmd *cobra.Command, args []string) {

if len(args) == 0 {
lookupResults := files.Lookup(files.LookupOptions{
GitBranch: os.Getenv("SEMAPHORE_GIT_BRANCH"),
GitBranch: FindGitBranch(),
Restore: false,
})

Expand Down Expand Up @@ -121,6 +121,15 @@ func NormalizeKey(key string) string {
return normalizedKey
}

func FindGitBranch() string {
gitPrBranch := os.Getenv("SEMAPHORE_GIT_PR_BRANCH")
if gitPrBranch != "" {
return gitPrBranch
}

return os.Getenv("SEMAPHORE_GIT_BRANCH")
}

func init() {
RootCmd.AddCommand(storeCmd)
}
28 changes: 27 additions & 1 deletion cache-cli/cmd/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,12 @@ func Test__AutomaticStore(t *testing.T) {
assert.Contains(t, output, fmt.Sprintf("'%s' doesn't exist locally.", filepath.FromSlash("vendor/bundle")))
})

t.Run(fmt.Sprintf("%s detects and stores", backend), func(t *testing.T) {
t.Run(fmt.Sprintf("%s detects and stores using SEMAPHORE_GIT_BRANCH", backend), func(t *testing.T) {
storage.Clear()

os.Chdir(fmt.Sprintf("%s/test/autocache/gems", rootPath))
os.Setenv("SEMAPHORE_GIT_BRANCH", "master")
os.Setenv("SEMAPHORE_GIT_PR_BRANCH", "")
os.MkdirAll("vendor/bundle", os.ModePerm)

checksum, _ := files.GenerateChecksum("Gemfile.lock")
Expand All @@ -129,11 +130,36 @@ func Test__AutomaticStore(t *testing.T) {
os.RemoveAll("vendor")
})

t.Run(fmt.Sprintf("%s detects and stores using SEMAPHORE_GIT_PR_BRANCH", backend), func(t *testing.T) {
storage.Clear()

os.Chdir(fmt.Sprintf("%s/test/autocache/gems", rootPath))
os.Setenv("SEMAPHORE_GIT_BRANCH", "master")
os.Setenv("SEMAPHORE_GIT_PR_BRANCH", "some-development-branch")
os.MkdirAll("vendor/bundle", os.ModePerm)

checksum, _ := files.GenerateChecksum("Gemfile.lock")

key := fmt.Sprintf("gems-some-development-branch-%s", checksum)

capturer := utils.CreateOutputCapturer()
RunStore(storeCmd, []string{})
output := capturer.Done()

assert.Contains(t, output, "Detected Gemfile.lock")
assert.Contains(t, output, fmt.Sprintf("Compressing %s", filepath.FromSlash("vendor/bundle")))
assert.Contains(t, output, fmt.Sprintf("Uploading '%s' with cache key '%s'", filepath.FromSlash("vendor/bundle"), key))
assert.Contains(t, output, "Upload complete")

os.RemoveAll("vendor")
})

t.Run(fmt.Sprintf("%s does not store if key already exist", backend), func(t *testing.T) {
storage.Clear()

os.Chdir(fmt.Sprintf("%s/test/autocache/gems", rootPath))
os.Setenv("SEMAPHORE_GIT_BRANCH", "master")
os.Setenv("SEMAPHORE_GIT_PR_BRANCH", "")
os.MkdirAll("vendor/bundle", os.ModePerm)

checksum, _ := files.GenerateChecksum("Gemfile.lock")
Expand Down

0 comments on commit e6c484e

Please sign in to comment.