Add Helper for Optional Values #448

Merged
6543 merged 4 commits from 6543/go-sdk:add-bool-helper into master 1 year ago
  1. 20
      gitea/helper.go
  2. 5
      gitea/issue_test.go
  3. 9
      gitea/release_test.go
  4. 12
      gitea/repo_branch_test.go
  5. 3
      gitea/user_test.go

20
gitea/helper.go

@ -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
}

5
gitea/issue_test.go

@ -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)
}

9
gitea/release_test.go

@ -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

12
gitea/repo_branch_test.go

@ -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)

3
gitea/user_test.go

@ -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
}

Loading…
Cancel
Save