-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathpush_test.go
238 lines (224 loc) · 11.6 KB
/
push_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package push
import (
"context"
"encoding/json"
"io/ioutil"
"math/rand"
"net/http"
"os"
"path"
"strconv"
"testing"
"github.com/github/codeql-action-sync/internal/cachedirectory"
"github.com/github/codeql-action-sync/test"
"github.com/go-git/go-git/v5"
"github.com/gorilla/mux"
"github.com/stretchr/testify/require"
"golang.org/x/oauth2"
"github.com/google/go-github/v32/github"
)
func getTestPushService(t *testing.T, cacheDirectoryString string, githubEnterpriseURL string) pushService {
err := os.MkdirAll(path.Join(cacheDirectoryString, "refs"), 0777)
require.NoError(t, err)
cacheDirectory := cachedirectory.NewCacheDirectory(cacheDirectoryString)
var githubEnterpriseClient *github.Client
if githubEnterpriseURL != "" {
client, err := github.NewEnterpriseClient(githubEnterpriseURL+"/api/v3", githubEnterpriseURL+"/api/uploads", &http.Client{})
githubEnterpriseClient = client
require.NoError(t, err)
} else {
githubEnterpriseClient = nil
}
token := oauth2.Token{AccessToken: "token"}
return pushService{
ctx: context.Background(),
cacheDirectory: cacheDirectory,
githubEnterpriseClient: githubEnterpriseClient,
destinationRepositoryOwner: "destination-repository-owner",
destinationRepositoryName: "destination-repository-name",
destinationToken: &token,
}
}
func TestCreateRepositoryWhenUserIsOwner(t *testing.T) {
temporaryDirectory := test.CreateTemporaryDirectory(t)
githubTestServer, githubEnterpriseURL := test.GetTestHTTPServer(t)
pushService := getTestPushService(t, temporaryDirectory, githubEnterpriseURL)
githubTestServer.HandleFunc("/api/v3/user", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromObject(t, github.User{Login: github.String("destination-repository-owner")}, response)
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/repos/destination-repository-owner/destination-repository-name", func(response http.ResponseWriter, request *http.Request) {
response.WriteHeader(http.StatusNotFound)
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/user/repos", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromObject(t, github.Repository{}, response)
}).Methods("POST")
_, err := pushService.createRepository()
require.NoError(t, err)
}
func TestUpdateRepositoryWhenUserIsOwner(t *testing.T) {
temporaryDirectory := test.CreateTemporaryDirectory(t)
githubTestServer, githubEnterpriseURL := test.GetTestHTTPServer(t)
pushService := getTestPushService(t, temporaryDirectory, githubEnterpriseURL)
githubTestServer.HandleFunc("/api/v3/user", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromObject(t, github.User{Login: github.String("destination-repository-owner")}, response)
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/repos/destination-repository-owner/destination-repository-name", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromObject(t, github.Repository{Homepage: github.String(repositoryHomepage)}, response)
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/repos/destination-repository-owner/destination-repository-name", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromObject(t, github.Repository{}, response)
}).Methods("PATCH")
_, err := pushService.createRepository()
require.NoError(t, err)
}
func TestUpdateRepositoryWhenUserIsOwnerForced(t *testing.T) {
temporaryDirectory := test.CreateTemporaryDirectory(t)
githubTestServer, githubEnterpriseURL := test.GetTestHTTPServer(t)
pushService := getTestPushService(t, temporaryDirectory, githubEnterpriseURL)
githubTestServer.HandleFunc("/api/v3/user", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromObject(t, github.User{Login: github.String("destination-repository-owner")}, response)
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/repos/destination-repository-owner/destination-repository-name", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromObject(t, github.Repository{}, response)
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/repos/destination-repository-owner/destination-repository-name", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromObject(t, github.Repository{}, response)
}).Methods("PATCH")
_, err := pushService.createRepository()
require.EqualError(t, err, errorAlreadyExists)
pushService.force = true
_, err = pushService.createRepository()
require.NoError(t, err)
}
func TestCreateOrganizationAndRepositoryWhenOrganizationIsOwner(t *testing.T) {
temporaryDirectory := test.CreateTemporaryDirectory(t)
githubTestServer, githubEnterpriseURL := test.GetTestHTTPServer(t)
pushService := getTestPushService(t, temporaryDirectory, githubEnterpriseURL)
organizationCreated := false
githubTestServer.HandleFunc("/api/v3/user", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromObject(t, github.User{Login: github.String("user")}, response)
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/orgs/destination-repository-owner", func(response http.ResponseWriter, request *http.Request) {
if organizationCreated {
test.ServeHTTPResponseFromObject(t, github.Organization{}, response)
} else {
response.WriteHeader(http.StatusNotFound)
}
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/admin/organizations", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromObject(t, github.Organization{}, response)
organizationCreated = true
}).Methods("POST")
githubTestServer.HandleFunc("/api/v3/repos/destination-repository-owner/destination-repository-name", func(response http.ResponseWriter, request *http.Request) {
response.WriteHeader(http.StatusNotFound)
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/orgs/destination-repository-owner/repos", func(response http.ResponseWriter, request *http.Request) {
test.ServeHTTPResponseFromObject(t, github.Repository{}, response)
}).Methods("POST")
_, err := pushService.createRepository()
require.NoError(t, err)
}
func TestPushGit(t *testing.T) {
temporaryDirectory := test.CreateTemporaryDirectory(t)
destinationPath := path.Join(temporaryDirectory, "target")
_, err := git.PlainInit(destinationPath, true)
require.NoError(t, err)
pushService := getTestPushService(t, "./push_test/action-cache-initial/", "")
repository := github.Repository{
CloneURL: github.String(destinationPath),
}
err = pushService.pushGit(&repository, true)
require.NoError(t, err)
test.CheckExpectedReferencesInRepository(t, destinationPath, []string{
"26936381e619a01122ea33993e3cebc474496805 refs/tags/codeql-bundle-20200101",
"26936381e619a01122ea33993e3cebc474496805 refs/tags/codeql-bundle-20200630",
})
err = pushService.pushGit(&repository, false)
require.NoError(t, err)
test.CheckExpectedReferencesInRepository(t, destinationPath, []string{
"26936381e619a01122ea33993e3cebc474496805 refs/tags/codeql-bundle-20200101",
"26936381e619a01122ea33993e3cebc474496805 refs/tags/codeql-bundle-20200630",
"b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/heads/main",
"26936381e619a01122ea33993e3cebc474496805 refs/heads/v1",
"e529a54fad10a936308b2220e05f7f00757f8e7c refs/heads/v3",
"bd82b85707bc13904e3526517677039d4da4a9bb refs/heads/very-ignored-branch",
"bd82b85707bc13904e3526517677039d4da4a9bb refs/tags/an-ignored-tag-too",
"26936381e619a01122ea33993e3cebc474496805 refs/tags/v2",
"26936381e619a01122ea33993e3cebc474496805 refs/heads/a-ref-that-will-need-pruning",
})
pushService = getTestPushService(t, "./push_test/action-cache-modified/", "")
err = pushService.pushGit(&repository, true)
require.NoError(t, err)
test.CheckExpectedReferencesInRepository(t, destinationPath, []string{
"26936381e619a01122ea33993e3cebc474496805 refs/tags/codeql-bundle-20200101",
"26936381e619a01122ea33993e3cebc474496805 refs/tags/codeql-bundle-20200630",
"b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/heads/main",
"26936381e619a01122ea33993e3cebc474496805 refs/heads/v1",
"e529a54fad10a936308b2220e05f7f00757f8e7c refs/heads/v3",
"bd82b85707bc13904e3526517677039d4da4a9bb refs/heads/very-ignored-branch",
"bd82b85707bc13904e3526517677039d4da4a9bb refs/tags/an-ignored-tag-too",
"26936381e619a01122ea33993e3cebc474496805 refs/tags/v2",
})
err = pushService.pushGit(&repository, false)
require.NoError(t, err)
test.CheckExpectedReferencesInRepository(t, destinationPath, []string{
"26936381e619a01122ea33993e3cebc474496805 refs/tags/codeql-bundle-20200101",
"26936381e619a01122ea33993e3cebc474496805 refs/tags/codeql-bundle-20200630",
"b9f01aa2c50f49898d4c7845a66be8824499fe9d refs/heads/main",
"26936381e619a01122ea33993e3cebc474496805 refs/heads/v1",
"e529a54fad10a936308b2220e05f7f00757f8e7c refs/heads/v3",
"bd82b85707bc13904e3526517677039d4da4a9bb refs/heads/very-ignored-branch",
"bd82b85707bc13904e3526517677039d4da4a9bb refs/tags/an-ignored-tag-too",
"26936381e619a01122ea33993e3cebc474496805 refs/tags/v2",
"26936381e619a01122ea33993e3cebc474496805 refs/heads/a-ref-that-will-need-pruning/because-it-now-has-this-extra-bit",
})
}
func TestPushReleases(t *testing.T) {
githubTestServer, githubEnterpriseURL := test.GetTestHTTPServer(t)
pushService := getTestPushService(t, "./push_test/action-cache-initial/", githubEnterpriseURL)
existingReleases := map[string]github.RepositoryRelease{}
existingAssets := map[int][]github.ReleaseAsset{}
existingAssetBodys := map[int]map[string][]byte{}
githubTestServer.HandleFunc("/api/v3/repos/destination-repository-owner/destination-repository-name/releases/tags/{tag}", func(response http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
if value, ok := existingReleases[vars["tag"]]; ok {
test.ServeHTTPResponseFromObject(t, value, response)
} else {
response.WriteHeader(http.StatusNotFound)
}
}).Methods("GET")
githubTestServer.HandleFunc("/api/v3/repos/destination-repository-owner/destination-repository-name/releases", func(response http.ResponseWriter, request *http.Request) {
body, err := ioutil.ReadAll(request.Body)
require.NoError(t, err)
var release *github.RepositoryRelease
err = json.Unmarshal(body, &release)
require.NoError(t, err)
release.ID = github.Int64(rand.Int63())
existingReleases[release.GetTagName()] = *release
test.ServeHTTPResponseFromObject(t, release, response)
}).Methods("POST")
githubTestServer.HandleFunc("/api/v3/repos/destination-repository-owner/destination-repository-name/releases/{id:[0-9]+}/assets", func(response http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
releaseID, err := strconv.Atoi(vars["id"])
require.NoError(t, err)
test.ServeHTTPResponseFromObject(t, existingAssets[releaseID], response)
}).Methods("GET")
githubTestServer.HandleFunc("/api/uploads/repos/destination-repository-owner/destination-repository-name/releases/{id:[0-9]+}/assets", func(response http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
releaseID, err := strconv.Atoi(vars["id"])
require.NoError(t, err)
assetName := request.URL.Query().Get("name")
asset := github.ReleaseAsset{
Name: github.String(assetName),
}
existingAssets[releaseID] = append(existingAssets[releaseID], asset)
if existingAssetBodys[releaseID] == nil {
existingAssetBodys[releaseID] = map[string][]byte{}
}
existingAssetBodys[releaseID][assetName], err = ioutil.ReadAll(request.Body)
require.NoError(t, err)
test.ServeHTTPResponseFromObject(t, asset, response)
}).Methods("POST")
err := pushService.pushReleases()
require.NoError(t, err)
}