Skip to content

Commit cf465b4

Browse files
authored
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
1 parent de265f3 commit cf465b4

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

services/repository/files/update.go

+5
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,11 @@ func CreateOrUpdateRepoFile(ctx context.Context, repo *repo_model.Repository, do
458458
if err != nil {
459459
return nil, err
460460
}
461+
462+
if repo.IsEmpty {
463+
_ = repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: repo.ID, IsEmpty: false}, "is_empty")
464+
}
465+
461466
return file, nil
462467
}
463468

tests/integration/empty_repo_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@ package integration
55

66
import (
77
"bytes"
8+
"encoding/base64"
9+
"fmt"
810
"io"
911
"mime/multipart"
1012
"net/http"
1113
"testing"
1214

15+
auth_model "code.gitea.io/gitea/models/auth"
1316
"code.gitea.io/gitea/models/db"
1417
repo_model "code.gitea.io/gitea/models/repo"
1518
"code.gitea.io/gitea/models/unittest"
1619
user_model "code.gitea.io/gitea/models/user"
1720
"code.gitea.io/gitea/modules/json"
1821
"code.gitea.io/gitea/modules/setting"
22+
api "code.gitea.io/gitea/modules/structs"
1923
"code.gitea.io/gitea/modules/test"
2024
"code.gitea.io/gitea/tests"
2125

@@ -105,3 +109,32 @@ func TestEmptyRepoUploadFile(t *testing.T) {
105109
resp = session.MakeRequest(t, req, http.StatusOK)
106110
assert.Contains(t, resp.Body.String(), "uploaded-file.txt")
107111
}
112+
113+
func TestEmptyRepoAddFileByAPI(t *testing.T) {
114+
defer tests.PrepareTestEnv(t)()
115+
116+
err := user_model.UpdateUserCols(db.DefaultContext, &user_model.User{ID: 30, ProhibitLogin: false}, "prohibit_login")
117+
assert.NoError(t, err)
118+
119+
session := loginUser(t, "user30")
120+
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeRepo)
121+
122+
url := fmt.Sprintf("/api/v1/repos/user30/empty/contents/new-file.txt?token=%s", token)
123+
req := NewRequestWithJSON(t, "POST", url, &api.CreateFileOptions{
124+
FileOptions: api.FileOptions{
125+
NewBranchName: "new_branch",
126+
Message: "init",
127+
},
128+
Content: base64.StdEncoding.EncodeToString([]byte("newly-added-api-file")),
129+
})
130+
131+
resp := MakeRequest(t, req, http.StatusCreated)
132+
var fileResponse api.FileResponse
133+
DecodeJSON(t, resp, &fileResponse)
134+
expectedHTMLURL := setting.AppURL + "user30/empty/src/branch/new_branch/new-file.txt"
135+
assert.EqualValues(t, expectedHTMLURL, *fileResponse.Content.HTMLURL)
136+
137+
req = NewRequest(t, "GET", "/user30/empty/src/branch/new_branch/new-file.txt")
138+
resp = session.MakeRequest(t, req, http.StatusOK)
139+
assert.Contains(t, resp.Body.String(), "newly-added-api-file")
140+
}

0 commit comments

Comments
 (0)