Add Helper for Optional Values (#448)

Add helper for optional bool, string & int64

Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/448
Reviewed-by: Andrew Thornton <art27@cantab.net>
Reviewed-by: John Olheiser <john.olheiser@gmail.com>
Co-Authored-By: 6543 <6543@obermui.de>
Co-Committed-By: 6543 <6543@obermui.de>
This commit is contained in:
6543 2020-11-13 05:27:28 +08:00
parent b00ea89ffe
commit aa13606bc6
5 changed files with 31 additions and 18 deletions

20
gitea/helper.go Normal file
View File

@ -0,0 +1,20 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
// OptionalBool convert a bool to a bool reference
func OptionalBool(v bool) *bool {
return &v
}
// OptionalString convert a string to a string reference
func OptionalString(v string) *string {
return &v
}
// OptionalInt64 convert a int64 to a int64 reference
func OptionalInt64(v int64) *int64 {
return &v
}

View File

@ -54,16 +54,15 @@ func editIssues(t *testing.T, c *Client) {
issue, _, err := c.GetIssue(il[0].Poster.UserName, il[0].Repository.Name, il[0].Index)
assert.NoError(t, err)
body := "123 test and go"
state := StateClosed
issueNew, _, err := c.EditIssue(issue.Poster.UserName, issue.Repository.Name, issue.Index, EditIssueOption{
Title: "Edited",
Body: &body,
Body: OptionalString("123 test and go"),
State: &state,
})
assert.NoError(t, err)
assert.EqualValues(t, issue.ID, issueNew.ID)
assert.EqualValues(t, body, issueNew.Body)
assert.EqualValues(t, "123 test and go", issueNew.Body)
assert.EqualValues(t, "Edited", issueNew.Title)
}

View File

@ -57,17 +57,16 @@ func TestRelease(t *testing.T) {
assert.EqualValues(t, r, r2)
// EditRelease
bFalse := false
r2, _, err = c.EditRelease(repo.Owner.UserName, repo.Name, r.ID, EditReleaseOption{
Title: "Release Awesome",
Note: "",
IsDraft: &bFalse,
IsPrerelease: &bFalse,
IsDraft: OptionalBool(false),
IsPrerelease: OptionalBool(false),
})
assert.NoError(t, err)
assert.EqualValues(t, r.Target, r2.Target)
assert.EqualValues(t, bFalse, r2.IsDraft)
assert.EqualValues(t, bFalse, r2.IsPrerelease)
assert.EqualValues(t, false, r2.IsDraft)
assert.EqualValues(t, false, r2.IsPrerelease)
assert.EqualValues(t, r.Note, r2.Note)
// DeleteRelease

View File

@ -107,17 +107,13 @@ func TestRepoBranchProtection(t *testing.T) {
assert.NoError(t, err)
assert.EqualValues(t, bpl[0], bp)
optTrue := true
optFalse := false
one := int64(1)
// EditBranchProtection
bp, _, err = c.EditBranchProtection(repo.Owner.UserName, repo.Name, bpl[0].BranchName, EditBranchProtectionOption{
EnablePush: &optFalse,
EnablePushWhitelist: &optFalse,
EnablePush: OptionalBool(false),
EnablePushWhitelist: OptionalBool(false),
PushWhitelistUsernames: nil,
RequiredApprovals: &one,
EnableApprovalsWhitelist: &optTrue,
RequiredApprovals: OptionalInt64(1),
EnableApprovalsWhitelist: OptionalBool(true),
ApprovalsWhitelistUsernames: []string{"test01"},
})
assert.NoError(t, err)

View File

@ -172,12 +172,11 @@ func TestUserEmail(t *testing.T) {
}
func createTestUser(t *testing.T, username string, client *Client) *User {
bFalse := false
user, _, _ := client.GetUserInfo(username)
if user.ID != 0 {
return user
}
user, _, err := client.AdminCreateUser(CreateUserOption{Username: username, Password: username + "!1234", Email: username + "@gitea.io", MustChangePassword: &bFalse, SendNotify: bFalse})
user, _, err := client.AdminCreateUser(CreateUserOption{Username: username, Password: username + "!1234", Email: username + "@gitea.io", MustChangePassword: OptionalBool(false), SendNotify: false})
assert.NoError(t, err)
return user
}