Skip to content

Commit ce8de35

Browse files
zeripathtechknowlogick
authored andcommitted
Remove local clones & make hooks run on merge/edit/upload (#6672)
* Add options to git.Clone to make it more capable * Begin the process of removing the local copy and tidy up * Remove Wiki LocalCopy Checkouts * Remove the last LocalRepo helpers * Remove WithTemporaryFile * Enable push-hooks for these routes * Ensure tests cope with hooks Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove Repository.LocalCopyPath() * Move temporary repo to use the standard temporary path * Fix the tests Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove LocalWikiPath * Fix missing remove Signed-off-by: Andrew Thornton <art27@cantab.net> * Use AppURL for Oauth user link (#6894) * Use AppURL for Oauth user link Fix #6843 * Update oauth.go * Update oauth.go * internal/ssh: ignore env command totally (#6825) * ssh: ignore env command totally * Remove commented code Needed fix described in issue #6889 * Escape the commit message on issues update and title in telegram hook (#6901) * update sdk to latest (#6903) * improve description of branch protection (fix #6886) (#6906) The branch protection description text were not quite accurate. * Fix logging documentation (#6904) * ENABLE_MACARON_REDIRECT should be REDIRECT_MACARON_LOG * Allow DISABLE_ROUTER_LOG to be set in the [log] section * [skip ci] Updated translations via Crowdin * Move sdk structs to modules/structs (#6905) * move sdk structs to moduels/structs * fix tests * fix fmt * fix swagger * fix vendor
1 parent 34eee25 commit ce8de35

33 files changed

+1698
-1463
lines changed

contrib/pr/checkout.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ func runPR() {
108108
models.LoadFixtures()
109109
os.RemoveAll(setting.RepoRootPath)
110110
os.RemoveAll(models.LocalCopyPath())
111-
os.RemoveAll(models.LocalWikiPath())
112111
com.CopyDir(path.Join(curDir, "integrations/gitea-repositories-meta"), setting.RepoRootPath)
113112

114113
log.Printf("[PR] Setting up router\n")

integrations/api_repo_file_content_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package integrations
66

77
import (
88
"net/http"
9+
"net/url"
910
"path/filepath"
1011
"testing"
1112

@@ -40,7 +41,10 @@ func getExpectedFileContentResponseForFileContents(branch string) *api.FileConte
4041
}
4142

4243
func TestAPIGetFileContents(t *testing.T) {
43-
prepareTestEnv(t)
44+
onGiteaRun(t, testAPIGetFileContents)
45+
}
46+
47+
func testAPIGetFileContents(t *testing.T, u *url.URL) {
4448
user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16
4549
user3 := models.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org
4650
user4 := models.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos

integrations/api_repo_file_create_test.go

Lines changed: 114 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding/base64"
99
"fmt"
1010
"net/http"
11+
"net/url"
1112
"path/filepath"
1213
"testing"
1314

@@ -91,125 +92,126 @@ func getExpectedFileResponseForCreate(commitID, treePath string) *api.FileRespon
9192
}
9293

9394
func TestAPICreateFile(t *testing.T) {
94-
prepareTestEnv(t)
95-
user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16
96-
user3 := models.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org
97-
user4 := models.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos
98-
repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo
99-
repo3 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo
100-
repo16 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo
101-
fileID := 0
102-
103-
// Get user2's token
104-
session := loginUser(t, user2.Name)
105-
token2 := getTokenForLoggedInUser(t, session)
106-
session = emptyTestSession(t)
107-
// Get user4's token
108-
session = loginUser(t, user4.Name)
109-
token4 := getTokenForLoggedInUser(t, session)
110-
session = emptyTestSession(t)
111-
112-
// Test creating a file in repo1 which user2 owns, try both with branch and empty branch
113-
for _, branch := range [...]string{
114-
"master", // Branch
115-
"", // Empty branch
116-
} {
95+
onGiteaRun(t, func(t *testing.T, u *url.URL) {
96+
user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16
97+
user3 := models.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org
98+
user4 := models.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos
99+
repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo
100+
repo3 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo
101+
repo16 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo
102+
fileID := 0
103+
104+
// Get user2's token
105+
session := loginUser(t, user2.Name)
106+
token2 := getTokenForLoggedInUser(t, session)
107+
session = emptyTestSession(t)
108+
// Get user4's token
109+
session = loginUser(t, user4.Name)
110+
token4 := getTokenForLoggedInUser(t, session)
111+
session = emptyTestSession(t)
112+
113+
// Test creating a file in repo1 which user2 owns, try both with branch and empty branch
114+
for _, branch := range [...]string{
115+
"master", // Branch
116+
"", // Empty branch
117+
} {
118+
createFileOptions := getCreateFileOptions()
119+
createFileOptions.BranchName = branch
120+
fileID++
121+
treePath := fmt.Sprintf("new/file%d.txt", fileID)
122+
url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
123+
req := NewRequestWithJSON(t, "POST", url, &createFileOptions)
124+
resp := session.MakeRequest(t, req, http.StatusCreated)
125+
gitRepo, _ := git.OpenRepository(repo1.RepoPath())
126+
commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName)
127+
expectedFileResponse := getExpectedFileResponseForCreate(commitID, treePath)
128+
var fileResponse api.FileResponse
129+
DecodeJSON(t, resp, &fileResponse)
130+
assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content)
131+
assert.EqualValues(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
132+
assert.EqualValues(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
133+
assert.EqualValues(t, expectedFileResponse.Commit.Author.Email, fileResponse.Commit.Author.Email)
134+
assert.EqualValues(t, expectedFileResponse.Commit.Author.Name, fileResponse.Commit.Author.Name)
135+
}
136+
137+
// Test creating a file in a new branch
117138
createFileOptions := getCreateFileOptions()
118-
createFileOptions.BranchName = branch
139+
createFileOptions.BranchName = repo1.DefaultBranch
140+
createFileOptions.NewBranchName = "new_branch"
119141
fileID++
120142
treePath := fmt.Sprintf("new/file%d.txt", fileID)
121143
url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
122144
req := NewRequestWithJSON(t, "POST", url, &createFileOptions)
123145
resp := session.MakeRequest(t, req, http.StatusCreated)
124-
gitRepo, _ := git.OpenRepository(repo1.RepoPath())
125-
commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName)
126-
expectedFileResponse := getExpectedFileResponseForCreate(commitID, treePath)
127146
var fileResponse api.FileResponse
128147
DecodeJSON(t, resp, &fileResponse)
129-
assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content)
130-
assert.EqualValues(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)
131-
assert.EqualValues(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL)
132-
assert.EqualValues(t, expectedFileResponse.Commit.Author.Email, fileResponse.Commit.Author.Email)
133-
assert.EqualValues(t, expectedFileResponse.Commit.Author.Name, fileResponse.Commit.Author.Name)
134-
}
148+
expectedSHA := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
149+
expectedHTMLURL := fmt.Sprintf("http://localhost:"+setting.HTTPPort+"/user2/repo1/blob/new_branch/new/file%d.txt", fileID)
150+
expectedDownloadURL := fmt.Sprintf("http://localhost:"+setting.HTTPPort+"/user2/repo1/raw/branch/new_branch/new/file%d.txt", fileID)
151+
assert.EqualValues(t, expectedSHA, fileResponse.Content.SHA)
152+
assert.EqualValues(t, expectedHTMLURL, fileResponse.Content.HTMLURL)
153+
assert.EqualValues(t, expectedDownloadURL, fileResponse.Content.DownloadURL)
154+
155+
// Test trying to create a file that already exists, should fail
156+
createFileOptions = getCreateFileOptions()
157+
treePath = "README.md"
158+
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
159+
req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
160+
resp = session.MakeRequest(t, req, http.StatusInternalServerError)
161+
expectedAPIError := context.APIError{
162+
Message: "repository file already exists [path: " + treePath + "]",
163+
URL: base.DocURL,
164+
}
165+
var apiError context.APIError
166+
DecodeJSON(t, resp, &apiError)
167+
assert.Equal(t, expectedAPIError, apiError)
168+
169+
// Test creating a file in repo1 by user4 who does not have write access
170+
createFileOptions = getCreateFileOptions()
171+
fileID++
172+
treePath = fmt.Sprintf("new/file%d.txt", fileID)
173+
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token4)
174+
req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
175+
session.MakeRequest(t, req, http.StatusNotFound)
135176

136-
// Test creating a file in a new branch
137-
createFileOptions := getCreateFileOptions()
138-
createFileOptions.BranchName = repo1.DefaultBranch
139-
createFileOptions.NewBranchName = "new_branch"
140-
fileID++
141-
treePath := fmt.Sprintf("new/file%d.txt", fileID)
142-
url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
143-
req := NewRequestWithJSON(t, "POST", url, &createFileOptions)
144-
resp := session.MakeRequest(t, req, http.StatusCreated)
145-
var fileResponse api.FileResponse
146-
DecodeJSON(t, resp, &fileResponse)
147-
expectedSHA := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
148-
expectedHTMLURL := fmt.Sprintf("http://localhost:"+setting.HTTPPort+"/user2/repo1/blob/new_branch/new/file%d.txt", fileID)
149-
expectedDownloadURL := fmt.Sprintf("http://localhost:"+setting.HTTPPort+"/user2/repo1/raw/branch/new_branch/new/file%d.txt", fileID)
150-
assert.EqualValues(t, expectedSHA, fileResponse.Content.SHA)
151-
assert.EqualValues(t, expectedHTMLURL, fileResponse.Content.HTMLURL)
152-
assert.EqualValues(t, expectedDownloadURL, fileResponse.Content.DownloadURL)
153-
154-
// Test trying to create a file that already exists, should fail
155-
createFileOptions = getCreateFileOptions()
156-
treePath = "README.md"
157-
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
158-
req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
159-
resp = session.MakeRequest(t, req, http.StatusInternalServerError)
160-
expectedAPIError := context.APIError{
161-
Message: "repository file already exists [path: " + treePath + "]",
162-
URL: base.DocURL,
163-
}
164-
var apiError context.APIError
165-
DecodeJSON(t, resp, &apiError)
166-
assert.Equal(t, expectedAPIError, apiError)
167-
168-
// Test creating a file in repo1 by user4 who does not have write access
169-
createFileOptions = getCreateFileOptions()
170-
fileID++
171-
treePath = fmt.Sprintf("new/file%d.txt", fileID)
172-
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token4)
173-
req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
174-
session.MakeRequest(t, req, http.StatusNotFound)
175-
176-
// Tests a repo with no token given so will fail
177-
createFileOptions = getCreateFileOptions()
178-
fileID++
179-
treePath = fmt.Sprintf("new/file%d.txt", fileID)
180-
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user2.Name, repo16.Name, treePath)
181-
req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
182-
session.MakeRequest(t, req, http.StatusNotFound)
183-
184-
// Test using access token for a private repo that the user of the token owns
185-
createFileOptions = getCreateFileOptions()
186-
fileID++
187-
treePath = fmt.Sprintf("new/file%d.txt", fileID)
188-
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token2)
189-
req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
190-
session.MakeRequest(t, req, http.StatusCreated)
191-
192-
// Test using org repo "user3/repo3" where user2 is a collaborator
193-
createFileOptions = getCreateFileOptions()
194-
fileID++
195-
treePath = fmt.Sprintf("new/file%d.txt", fileID)
196-
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user3.Name, repo3.Name, treePath, token2)
197-
req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
198-
session.MakeRequest(t, req, http.StatusCreated)
199-
200-
// Test using org repo "user3/repo3" with no user token
201-
createFileOptions = getCreateFileOptions()
202-
fileID++
203-
treePath = fmt.Sprintf("new/file%d.txt", fileID)
204-
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user3.Name, repo3.Name, treePath)
205-
req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
206-
session.MakeRequest(t, req, http.StatusNotFound)
207-
208-
// Test using repo "user2/repo1" where user4 is a NOT collaborator
209-
createFileOptions = getCreateFileOptions()
210-
fileID++
211-
treePath = fmt.Sprintf("new/file%d.txt", fileID)
212-
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token4)
213-
req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
214-
session.MakeRequest(t, req, http.StatusForbidden)
177+
// Tests a repo with no token given so will fail
178+
createFileOptions = getCreateFileOptions()
179+
fileID++
180+
treePath = fmt.Sprintf("new/file%d.txt", fileID)
181+
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user2.Name, repo16.Name, treePath)
182+
req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
183+
session.MakeRequest(t, req, http.StatusNotFound)
184+
185+
// Test using access token for a private repo that the user of the token owns
186+
createFileOptions = getCreateFileOptions()
187+
fileID++
188+
treePath = fmt.Sprintf("new/file%d.txt", fileID)
189+
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token2)
190+
req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
191+
session.MakeRequest(t, req, http.StatusCreated)
192+
193+
// Test using org repo "user3/repo3" where user2 is a collaborator
194+
createFileOptions = getCreateFileOptions()
195+
fileID++
196+
treePath = fmt.Sprintf("new/file%d.txt", fileID)
197+
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user3.Name, repo3.Name, treePath, token2)
198+
req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
199+
session.MakeRequest(t, req, http.StatusCreated)
200+
201+
// Test using org repo "user3/repo3" with no user token
202+
createFileOptions = getCreateFileOptions()
203+
fileID++
204+
treePath = fmt.Sprintf("new/file%d.txt", fileID)
205+
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user3.Name, repo3.Name, treePath)
206+
req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
207+
session.MakeRequest(t, req, http.StatusNotFound)
208+
209+
// Test using repo "user2/repo1" where user4 is a NOT collaborator
210+
createFileOptions = getCreateFileOptions()
211+
fileID++
212+
treePath = fmt.Sprintf("new/file%d.txt", fileID)
213+
url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token4)
214+
req = NewRequestWithJSON(t, "POST", url, &createFileOptions)
215+
session.MakeRequest(t, req, http.StatusForbidden)
216+
})
215217
}

0 commit comments

Comments
 (0)