summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2023-04-27 09:36:26 +0800
committerGitHub <noreply@github.com>2023-04-26 21:36:26 -0400
commitcf465b472166ccf6d3e001e3043e4bf43e16e6b3 (patch)
tree071db9815eb7ec3e6d5b01c81a03f60dcf84afa9
parentde265f377113766124e9846e120e2864bf01c60c (diff)
downloadgitea-cf465b472166ccf6d3e001e3043e4bf43e16e6b3.tar.gz
gitea-cf465b472166ccf6d3e001e3043e4bf43e16e6b3.zip
Support uploading file to empty repo by API (#24357)
The uploading API already works (the only nit is the the IsEmpty flag is out-of-sync, this PR also fixes it) Close #14633
-rw-r--r--services/repository/files/update.go5
-rw-r--r--tests/integration/empty_repo_test.go33
2 files changed, 38 insertions, 0 deletions
diff --git a/services/repository/files/update.go b/services/repository/files/update.go
index 45a4692396..25014f4418 100644
--- a/services/repository/files/update.go
+++ b/services/repository/files/update.go
@@ -458,6 +458,11 @@ func CreateOrUpdateRepoFile(ctx context.Context, repo *repo_model.Repository, do
if err != nil {
return nil, err
}
+
+ if repo.IsEmpty {
+ _ = repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: repo.ID, IsEmpty: false}, "is_empty")
+ }
+
return file, nil
}
diff --git a/tests/integration/empty_repo_test.go b/tests/integration/empty_repo_test.go
index e6e5f7b18c..73331bc933 100644
--- a/tests/integration/empty_repo_test.go
+++ b/tests/integration/empty_repo_test.go
@@ -5,17 +5,21 @@ package integration
import (
"bytes"
+ "encoding/base64"
+ "fmt"
"io"
"mime/multipart"
"net/http"
"testing"
+ auth_model "code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/setting"
+ api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/tests"
@@ -105,3 +109,32 @@ func TestEmptyRepoUploadFile(t *testing.T) {
resp = session.MakeRequest(t, req, http.StatusOK)
assert.Contains(t, resp.Body.String(), "uploaded-file.txt")
}
+
+func TestEmptyRepoAddFileByAPI(t *testing.T) {
+ defer tests.PrepareTestEnv(t)()
+
+ err := user_model.UpdateUserCols(db.DefaultContext, &user_model.User{ID: 30, ProhibitLogin: false}, "prohibit_login")
+ assert.NoError(t, err)
+
+ session := loginUser(t, "user30")
+ token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeRepo)
+
+ url := fmt.Sprintf("/api/v1/repos/user30/empty/contents/new-file.txt?token=%s", token)
+ req := NewRequestWithJSON(t, "POST", url, &api.CreateFileOptions{
+ FileOptions: api.FileOptions{
+ NewBranchName: "new_branch",
+ Message: "init",
+ },
+ Content: base64.StdEncoding.EncodeToString([]byte("newly-added-api-file")),
+ })
+
+ resp := MakeRequest(t, req, http.StatusCreated)
+ var fileResponse api.FileResponse
+ DecodeJSON(t, resp, &fileResponse)
+ expectedHTMLURL := setting.AppURL + "user30/empty/src/branch/new_branch/new-file.txt"
+ assert.EqualValues(t, expectedHTMLURL, *fileResponse.Content.HTMLURL)
+
+ req = NewRequest(t, "GET", "/user30/empty/src/branch/new_branch/new-file.txt")
+ resp = session.MakeRequest(t, req, http.StatusOK)
+ assert.Contains(t, resp.Body.String(), "newly-added-api-file")
+}