Merge api on Create or update file contents

This commit is contained in:
seokcheon.ju 2024-02-26 21:41:30 +09:00
parent bf71ec2fd9
commit ea47b16c61
6 changed files with 43 additions and 16 deletions

View File

@ -199,7 +199,7 @@ func preparePullReviewTest(t *testing.T, c *Client, repoName string) (*Repositor
c.SetSudo("pull_submitter")
newFile, _, err := c.CreateFile(repo.Owner.UserName, repo.Name, "WOW-file", CreateFileOptions{
newFile, _, err := c.CreateOrUpdateFile(repo.Owner.UserName, repo.Name, "WOW-file", CreateOrUpdateFileOptions{
Content: "QSBuZXcgRmlsZQoKYW5kIHNvbWUgbGluZXMK",
FileOptions: FileOptions{
Message: "creat a new file",

View File

@ -161,7 +161,7 @@ func preparePullTest(t *testing.T, c *Client, repoName, forkOrg string) bool {
return false
}
updatedFile, _, err := c.UpdateFile(forkRepo.Owner.UserName, forkRepo.Name, "LICENSE", UpdateFileOptions{
updatedFile, _, err := c.CreateOrUpdateFile(forkRepo.Owner.UserName, forkRepo.Name, "LICENSE", CreateOrUpdateFileOptions{
FileOptions: FileOptions{
Message: "Overwrite",
BranchName: "main",
@ -174,7 +174,7 @@ func preparePullTest(t *testing.T, c *Client, repoName, forkOrg string) bool {
return false
}
newFile, _, err := c.CreateFile(forkRepo.Owner.UserName, forkRepo.Name, "WOW-file", CreateFileOptions{
newFile, _, err := c.CreateOrUpdateFile(forkRepo.Owner.UserName, forkRepo.Name, "WOW-file", CreateOrUpdateFileOptions{
Content: "QSBuZXcgRmlsZQo=",
FileOptions: FileOptions{
Message: "creat a new file",
@ -186,7 +186,7 @@ func preparePullTest(t *testing.T, c *Client, repoName, forkOrg string) bool {
return false
}
conflictFile1, _, err := c.CreateFile(origRepo.Owner.UserName, origRepo.Name, "bad-file", CreateFileOptions{
conflictFile1, _, err := c.CreateOrUpdateFile(origRepo.Owner.UserName, origRepo.Name, "bad-file", CreateOrUpdateFileOptions{
Content: "U3RhcnQgQ29uZmxpY3QK",
FileOptions: FileOptions{
Message: "Start Conflict",
@ -197,7 +197,7 @@ func preparePullTest(t *testing.T, c *Client, repoName, forkOrg string) bool {
return false
}
conflictFile2, _, err := c.CreateFile(forkRepo.Owner.UserName, forkRepo.Name, "bad-file", CreateFileOptions{
conflictFile2, _, err := c.CreateOrUpdateFile(forkRepo.Owner.UserName, forkRepo.Name, "bad-file", CreateOrUpdateFileOptions{
Content: "V2lsbEhhdmUgQ29uZmxpY3QK",
FileOptions: FileOptions{
Message: "creat a new file witch will conflict",

View File

@ -147,7 +147,7 @@ func prepareBranchTest(t *testing.T, c *Client, repoName string) *Repository {
return nil
}
updatedFile, _, err := c.UpdateFile(origRepo.Owner.UserName, origRepo.Name, "README.md", UpdateFileOptions{
updatedFile, _, err := c.CreateOrUpdateFile(origRepo.Owner.UserName, origRepo.Name, "README.md", CreateOrUpdateFileOptions{
FileOptions: FileOptions{
Message: "update it",
BranchName: "main",
@ -160,7 +160,7 @@ func prepareBranchTest(t *testing.T, c *Client, repoName string) *Repository {
return nil
}
newFile, _, err := c.CreateFile(origRepo.Owner.UserName, origRepo.Name, "WOW-file", CreateFileOptions{
newFile, _, err := c.CreateOrUpdateFile(origRepo.Owner.UserName, origRepo.Name, "WOW-file", CreateOrUpdateFileOptions{
Content: "QSBuZXcgRmlsZQo=",
FileOptions: FileOptions{
Message: "creat a new file",

View File

@ -35,7 +35,7 @@ func TestGetCommitDiffOrPatch(t *testing.T) {
assert.NoError(t, err)
// Add a new simple small commit to the repository.
fileResponse, _, err := c.CreateFile(repo.Owner.UserName, repo.Name, "NOT_A_LICENSE", CreateFileOptions{
fileResponse, _, err := c.CreateOrUpdateFile(repo.Owner.UserName, repo.Name, "NOT_A_LICENSE", CreateOrUpdateFileOptions{
Content: base64.StdEncoding.EncodeToString([]byte("But is it?\n")),
FileOptions: FileOptions{
Message: "Ensure people know it's not a license!",

View File

@ -31,6 +31,7 @@ type FileOptions struct {
}
// CreateFileOptions options for creating files
// Deprecated: Use CreateOrUpdateFileOptions instead
// Note: `author` and `committer` are optional (if only one is given, it will be used for the other, otherwise the authenticated user will be used)
type CreateFileOptions struct {
FileOptions
@ -48,12 +49,12 @@ type DeleteFileOptions struct {
SHA string `json:"sha"`
}
// UpdateFileOptions options for updating files
// CreateOrUpdateFileOptions options for creating or updating files
// Note: `author` and `committer` are optional (if only one is given, it will be used for the other, otherwise the authenticated user will be used)
type UpdateFileOptions struct {
type CreateOrUpdateFileOptions struct {
FileOptions
// sha is the SHA for the file that already exists
// required: true
// required only for updating files
SHA string `json:"sha"`
// content must be base64 encoded
// required: true
@ -197,6 +198,7 @@ func (c *Client) getDirOrFileContents(owner, repo, ref, filepath string) ([]byte
}
// CreateFile create a file in a repository
// Deprecated: use CreateOrUpdateFile instead
func (c *Client) CreateFile(owner, repo, filepath string, opt CreateFileOptions) (*FileResponse, *Response, error) {
var err error
if opt.BranchName, err = c.setDefaultBranchForOldVersions(owner, repo, opt.BranchName); err != nil {
@ -216,8 +218,8 @@ func (c *Client) CreateFile(owner, repo, filepath string, opt CreateFileOptions)
return fr, resp, err
}
// UpdateFile update a file in a repository
func (c *Client) UpdateFile(owner, repo, filepath string, opt UpdateFileOptions) (*FileResponse, *Response, error) {
// CreateOrUpdateFile update a file in a repository
func (c *Client) CreateOrUpdateFile(owner, repo, filepath string, opt CreateOrUpdateFileOptions) (*FileResponse, *Response, error) {
var err error
if opt.BranchName, err = c.setDefaultBranchForOldVersions(owner, repo, opt.BranchName); err != nil {
return nil, nil, err

View File

@ -26,7 +26,7 @@ func TestFileCreateUpdateGet(t *testing.T) {
assert.EqualValues(t, "IyBDaGFuZ2VGaWxlcwoKQSB0ZXN0IFJlcG86IENoYW5nZUZpbGVz", base64.StdEncoding.EncodeToString(raw))
testFileName := "A+#&ä"
newFile, _, err := c.CreateFile(repo.Owner.UserName, repo.Name, testFileName, CreateFileOptions{
newFile, _, err := c.CreateOrUpdateFile(repo.Owner.UserName, repo.Name, testFileName, CreateOrUpdateFileOptions{
FileOptions: FileOptions{
Message: "create file " + testFileName,
},
@ -36,7 +36,7 @@ func TestFileCreateUpdateGet(t *testing.T) {
raw, _, _ = c.GetFile(repo.Owner.UserName, repo.Name, "main", testFileName)
assert.EqualValues(t, "ZmlsZUEK", base64.StdEncoding.EncodeToString(raw))
updatedFile, _, err := c.UpdateFile(repo.Owner.UserName, repo.Name, testFileName, UpdateFileOptions{
updatedFile, _, err := c.CreateOrUpdateFile(repo.Owner.UserName, repo.Name, testFileName, CreateOrUpdateFileOptions{
FileOptions: FileOptions{
Message: "add a new line",
},
@ -68,7 +68,7 @@ func TestFileCreateUpdateGet(t *testing.T) {
licenceRaw, _, err := c.GetFile(repo.Owner.UserName, repo.Name, "", "LICENSE")
assert.NoError(t, err)
testContent := "Tk9USElORyBJUyBIRVJFIEFOWU1PUkUKSUYgWU9VIExJS0UgVE8gRklORCBTT01FVEhJTkcKV0FJVCBGT1IgVEhFIEZVVFVSRQo="
updatedFile, _, err = c.UpdateFile(repo.Owner.UserName, repo.Name, "LICENSE", UpdateFileOptions{
updatedFile, _, err = c.CreateOrUpdateFile(repo.Owner.UserName, repo.Name, "LICENSE", CreateOrUpdateFileOptions{
FileOptions: FileOptions{
Message: "Overwrite",
BranchName: "main",
@ -116,3 +116,28 @@ func TestFileCreateUpdateGet(t *testing.T) {
assert.Nil(t, file)
assert.EqualValues(t, 200, resp.StatusCode)
}
func TestFileDeprecatedCreate(t *testing.T) {
log.Println("== TestFileDeprecatedCreate ==")
c := newTestClient()
repo, err := createTestRepo(t, "ChangeFiles", c)
assert.NoError(t, err)
assert.NotNil(t, repo)
raw, _, err := c.GetFile(repo.Owner.UserName, repo.Name, "main", "README.md")
assert.NoError(t, err)
assert.EqualValues(t, "IyBDaGFuZ2VGaWxlcwoKQSB0ZXN0IFJlcG86IENoYW5nZUZpbGVz", base64.StdEncoding.EncodeToString(raw))
testFileName := "A+#&ä"
_, _, err = c.CreateOrUpdateFile(repo.Owner.UserName, repo.Name, testFileName, CreateOrUpdateFileOptions{
FileOptions: FileOptions{
Message: "create file " + testFileName,
},
Content: "ZmlsZUEK",
})
assert.NoError(t, err)
raw, _, _ = c.GetFile(repo.Owner.UserName, repo.Name, "main", testFileName)
assert.EqualValues(t, "ZmlsZUEK", base64.StdEncoding.EncodeToString(raw))
}