Add Helper for Optional Values #448

Merged
6543 merged 4 commits from 6543/go-sdk:add-bool-helper into master 2020-11-12 21:27:29 +00:00
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 {
6543 marked this conversation as resolved Outdated

I'm not sure I like the abstraction for a few lines of code...
Also, small nit, but why not OptionalBool(b bool) for consistency with the other methods?

I'm not sure I like the abstraction for a few lines of code... Also, small nit, but why not `OptionalBool(b bool)` for consistency with the other methods?
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
} }