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) issue, _, err := c.GetIssue(il[0].Poster.UserName, il[0].Repository.Name, il[0].Index)
assert.NoError(t, err) assert.NoError(t, err)
body := "123 test and go"
state := StateClosed state := StateClosed
issueNew, _, err := c.EditIssue(issue.Poster.UserName, issue.Repository.Name, issue.Index, EditIssueOption{ issueNew, _, err := c.EditIssue(issue.Poster.UserName, issue.Repository.Name, issue.Index, EditIssueOption{
Title: "Edited", Title: "Edited",
Body: &body, Body: OptionalString("123 test and go"),
State: &state, State: &state,
}) })
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, issue.ID, issueNew.ID) 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) assert.EqualValues(t, "Edited", issueNew.Title)
} }

View file

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

View file

@ -107,17 +107,13 @@ func TestRepoBranchProtection(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, bpl[0], bp) assert.EqualValues(t, bpl[0], bp)
optTrue := true
optFalse := false
one := int64(1)
// EditBranchProtection // EditBranchProtection
bp, _, err = c.EditBranchProtection(repo.Owner.UserName, repo.Name, bpl[0].BranchName, EditBranchProtectionOption{ bp, _, err = c.EditBranchProtection(repo.Owner.UserName, repo.Name, bpl[0].BranchName, EditBranchProtectionOption{
EnablePush: &optFalse, EnablePush: OptionalBool(false),
EnablePushWhitelist: &optFalse, EnablePushWhitelist: OptionalBool(false),
PushWhitelistUsernames: nil, PushWhitelistUsernames: nil,
RequiredApprovals: &one, RequiredApprovals: OptionalInt64(1),
EnableApprovalsWhitelist: &optTrue, EnableApprovalsWhitelist: OptionalBool(true),
ApprovalsWhitelistUsernames: []string{"test01"}, ApprovalsWhitelistUsernames: []string{"test01"},
}) })
assert.NoError(t, err) 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 { func createTestUser(t *testing.T, username string, client *Client) *User {
bFalse := false
user, _, _ := client.GetUserInfo(username) user, _, _ := client.GetUserInfo(username)
if user.ID != 0 { if user.ID != 0 {
return user 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) assert.NoError(t, err)
return user return user
} }