Skip to content

Commit 1d8c41c

Browse files
authored
fix: cache actions for PR jobs (#28)
1 parent 35c4d6a commit 1d8c41c

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

store-cli.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88
"runtime/debug"
99
"strings"
10+
"log"
1011

1112
"github.com/screwdriver-cd/store-cli/sdstore"
1213
"github.com/urfave/cli"
@@ -39,10 +40,27 @@ func finalRecover() {
3940
successExit()
4041
}
4142

43+
// Skip cache action for PR jobs (event, pipeline scope)
44+
func skipCache(storeType, scope, action string) bool {
45+
// if is not cache, or if job is not PR
46+
if (storeType != "cache" || os.Getenv("SD_PULL_REQUEST") == "") {
47+
return false
48+
}
49+
50+
// For PR jobs,
51+
// skip PR event cache to save time, since PR event only consists of 1 job
52+
// skip pipeline scoped unless it's trying to get
53+
if (scope == "event" || (scope == "pipeline" && action != "get")) {
54+
log.Printf("Skipping %s %s-scoped cache for Pull Request", action, scope)
55+
return true
56+
}
57+
58+
return false
59+
}
60+
4261
// makeURL creates the fully-qualified url for a given Store path
4362
func makeURL(storeType, scope, key string) (*url.URL, error) {
4463
storeURL := os.Getenv("SD_STORE_URL")
45-
4664
var scopeEnv string
4765
switch scope {
4866
case "event":
@@ -82,6 +100,10 @@ func makeURL(storeType, scope, key string) (*url.URL, error) {
82100
}
83101

84102
func get(storeType, scope, key string) error {
103+
if skipCache(storeType, scope, "get") {
104+
return nil
105+
}
106+
85107
sdToken := os.Getenv("SD_TOKEN")
86108
fullURL, err := makeURL(storeType, scope, key)
87109

@@ -104,6 +126,9 @@ func get(storeType, scope, key string) error {
104126
}
105127

106128
func set(storeType, scope, filePath string) error {
129+
if skipCache(storeType, scope, "set") {
130+
return nil
131+
}
107132
sdToken := os.Getenv("SD_TOKEN")
108133
fullURL, err := makeURL(storeType, scope, filePath)
109134

@@ -124,6 +149,10 @@ func set(storeType, scope, filePath string) error {
124149
}
125150

126151
func remove(storeType, scope, key string) error {
152+
if skipCache(storeType, scope, "remove") {
153+
return nil
154+
}
155+
127156
sdToken := os.Getenv("SD_TOKEN")
128157
store := sdstore.NewStore(sdToken)
129158

store-cli_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,37 @@ func TestMain(m *testing.M) {
1515
os.Exit(retCode)
1616
}
1717

18+
func TestSkipCache(t *testing.T) {
19+
// Success test cases
20+
testCases := []struct {
21+
storeType string
22+
scope string
23+
action string
24+
prNum string
25+
expected bool
26+
}{
27+
{"cache", "pipeline", "get", "", false},
28+
{"cache", "pipeline", "get", "123", false},
29+
{"cache", "pipeline", "set", "123", true},
30+
{"cache", "pipeline", "remove", "123", true},
31+
{"cache", "event", "get", "", false},
32+
{"cache", "event", "get", "123", true},
33+
{"cache", "event", "set", "123", true},
34+
{"cache", "job", "get", "", false},
35+
{"cache", "job", "set", "123", false},
36+
{"artifact", "event", "get", "", false},
37+
{"log", "build", "set", "123", false},
38+
}
39+
40+
for _, tc := range testCases {
41+
os.Setenv("SD_PULL_REQUEST", tc.prNum)
42+
skip := skipCache(tc.storeType, tc.scope, tc.action)
43+
if skip != tc.expected {
44+
t.Fatalf("%s %s for scope %s, expected '%t' got '%t'", tc.action, tc.storeType, tc.scope, tc.expected, skip)
45+
}
46+
}
47+
}
48+
1849
func TestMakeURL(t *testing.T) {
1950
os.Setenv("SD_STORE_URL", "http://store.screwdriver.cd/v1/")
2051
os.Setenv("SD_BUILD_ID", "10038")

0 commit comments

Comments
 (0)