use the local copy of gitea-sdk, verbatim

This commit is contained in:
Earl Warren
2023-12-17 12:12:20 +01:00
parent a70c43680d
commit ba1629ea52
116 changed files with 82 additions and 3429 deletions

View File

@@ -30,9 +30,7 @@ run:
go: 1.21
timeout: 10m
skip-dirs:
- node_modules
- public
- web_src
- forges/forgejo/sdk
linters-settings:
stylecheck:

View File

@@ -10,8 +10,8 @@ import (
f3_tree "lab.forgefriends.org/friendlyforgeformat/gof3/tree/f3"
"lab.forgefriends.org/friendlyforgeformat/gof3/tree/generic"
forgejo_sdk "code.gitea.io/sdk/gitea"
"github.com/hashicorp/go-version"
forgejo_sdk "lab.forgefriends.org/friendlyforgeformat/gof3/forges/forgejo/sdk"
)
type common struct {

View File

@@ -13,7 +13,7 @@ import (
"lab.forgefriends.org/friendlyforgeformat/gof3/tree/generic"
"lab.forgefriends.org/friendlyforgeformat/gof3/util"
forgejo_sdk "code.gitea.io/sdk/gitea"
forgejo_sdk "lab.forgefriends.org/friendlyforgeformat/gof3/forges/forgejo/sdk"
)
type issue struct {

View File

@@ -11,7 +11,7 @@ import (
f3_tree "lab.forgefriends.org/friendlyforgeformat/gof3/tree/f3"
"lab.forgefriends.org/friendlyforgeformat/gof3/tree/generic"
forgejo_sdk "code.gitea.io/sdk/gitea"
forgejo_sdk "lab.forgefriends.org/friendlyforgeformat/gof3/forges/forgejo/sdk"
)
type issues struct {

View File

@@ -13,7 +13,7 @@ import (
"lab.forgefriends.org/friendlyforgeformat/gof3/tree/generic"
"lab.forgefriends.org/friendlyforgeformat/gof3/util"
forgejo_sdk "code.gitea.io/sdk/gitea"
forgejo_sdk "lab.forgefriends.org/friendlyforgeformat/gof3/forges/forgejo/sdk"
)
type label struct {

View File

@@ -11,7 +11,7 @@ import (
f3_tree "lab.forgefriends.org/friendlyforgeformat/gof3/tree/f3"
"lab.forgefriends.org/friendlyforgeformat/gof3/tree/generic"
forgejo_sdk "code.gitea.io/sdk/gitea"
forgejo_sdk "lab.forgefriends.org/friendlyforgeformat/gof3/forges/forgejo/sdk"
)
type labels struct {

View File

@@ -14,7 +14,7 @@ import (
"lab.forgefriends.org/friendlyforgeformat/gof3/tree/generic"
"lab.forgefriends.org/friendlyforgeformat/gof3/util"
forgejo_sdk "code.gitea.io/sdk/gitea"
forgejo_sdk "lab.forgefriends.org/friendlyforgeformat/gof3/forges/forgejo/sdk"
)
type milestone struct {

View File

@@ -11,7 +11,7 @@ import (
f3_tree "lab.forgefriends.org/friendlyforgeformat/gof3/tree/f3"
"lab.forgefriends.org/friendlyforgeformat/gof3/tree/generic"
forgejo_sdk "code.gitea.io/sdk/gitea"
forgejo_sdk "lab.forgefriends.org/friendlyforgeformat/gof3/forges/forgejo/sdk"
)
type milestones struct {

View File

@@ -14,7 +14,7 @@ import (
"lab.forgefriends.org/friendlyforgeformat/gof3/tree/generic"
"lab.forgefriends.org/friendlyforgeformat/gof3/util"
forgejo_sdk "code.gitea.io/sdk/gitea"
forgejo_sdk "lab.forgefriends.org/friendlyforgeformat/gof3/forges/forgejo/sdk"
)
type project struct {

View File

@@ -12,7 +12,7 @@ import (
f3_tree "lab.forgefriends.org/friendlyforgeformat/gof3/tree/f3"
"lab.forgefriends.org/friendlyforgeformat/gof3/tree/generic"
forgejo_sdk "code.gitea.io/sdk/gitea"
forgejo_sdk "lab.forgefriends.org/friendlyforgeformat/gof3/forges/forgejo/sdk"
)
type projects struct {

View File

@@ -15,7 +15,7 @@ import (
"lab.forgefriends.org/friendlyforgeformat/gof3/tree/generic"
"lab.forgefriends.org/friendlyforgeformat/gof3/util"
forgejo_sdk "code.gitea.io/sdk/gitea"
forgejo_sdk "lab.forgefriends.org/friendlyforgeformat/gof3/forges/forgejo/sdk"
)
type pullRequest struct {

View File

@@ -11,7 +11,7 @@ import (
f3_tree "lab.forgefriends.org/friendlyforgeformat/gof3/tree/f3"
"lab.forgefriends.org/friendlyforgeformat/gof3/tree/generic"
forgejo_sdk "code.gitea.io/sdk/gitea"
forgejo_sdk "lab.forgefriends.org/friendlyforgeformat/gof3/forges/forgejo/sdk"
)
type pullRequests struct {

View File

@@ -14,7 +14,7 @@ import (
"lab.forgefriends.org/friendlyforgeformat/gof3/tree/generic"
"lab.forgefriends.org/friendlyforgeformat/gof3/util"
forgejo_sdk "code.gitea.io/sdk/gitea"
forgejo_sdk "lab.forgefriends.org/friendlyforgeformat/gof3/forges/forgejo/sdk"
)
type review struct {

View File

@@ -12,7 +12,7 @@ import (
"lab.forgefriends.org/friendlyforgeformat/gof3/tree/generic"
"lab.forgefriends.org/friendlyforgeformat/gof3/util"
forgejo_sdk "code.gitea.io/sdk/gitea"
forgejo_sdk "lab.forgefriends.org/friendlyforgeformat/gof3/forges/forgejo/sdk"
)
type reviews struct {

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"fmt"

View File

@@ -3,7 +3,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -1,51 +0,0 @@
// 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
import (
"log"
"testing"
"github.com/stretchr/testify/assert"
)
func TestAdminOrg(t *testing.T) {
log.Println("== TestAdminOrg ==")
c := newTestClient()
user, _, err := c.GetMyUserInfo()
assert.NoError(t, err)
orgName := "NewTestOrg"
newOrg, _, err := c.AdminCreateOrg(user.UserName, CreateOrgOption{
Name: orgName,
FullName: orgName + " FullName",
Description: "test adminCreateOrg",
Visibility: VisibleTypePublic,
})
assert.NoError(t, err)
assert.NotEmpty(t, newOrg)
assert.EqualValues(t, orgName, newOrg.UserName)
orgs, _, err := c.AdminListOrgs(AdminListOrgsOptions{})
assert.NoError(t, err)
if assert.True(t, len(orgs) >= 1) {
orgs = orgs[len(orgs)-1:]
assert.EqualValues(t, newOrg.ID, orgs[0].ID)
}
_, err = c.DeleteOrg(orgName)
assert.NoError(t, err)
}
func TestAdminCronTasks(t *testing.T) {
log.Println("== TestAdminCronTasks ==")
c := newTestClient()
tasks, _, err := c.ListCronTasks(ListCronTaskOptions{})
assert.NoError(t, err)
assert.True(t, len(tasks) > 15)
_, err = c.RunCronTasks(tasks[0].Name)
assert.NoError(t, err)
}

View File

@@ -3,7 +3,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -4,7 +4,7 @@
//go:build !windows
package gitea
package sdk
import (
"fmt"

View File

@@ -4,7 +4,7 @@
//go:build windows
package gitea
package sdk
import (
"fmt"

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea // import "code.gitea.io/sdk/gitea"
package sdk // import "code.gitea.io/sdk/gitea"
import (
"bytes"
"encoding/json"

View File

@@ -3,7 +3,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -6,4 +6,4 @@
// The version corresponds to the highest supported version
// of the gitea API, but backwards-compatibility is mostly
// given.
package gitea // import "code.gitea.io/sdk/gitea"
package sdk // import "code.gitea.io/sdk/gitea"

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"fmt"

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
// OptionalBool convert a bool to a bool reference
func OptionalBool(v bool) *bool {

View File

@@ -3,7 +3,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -1,125 +0,0 @@
// Copyright 2022 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
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
// Hashers are based on https://github.com/go-gitea/gitea/blob/0dfc2e55ea258d2b1a3cd86e2b6f27a481e495ff/services/webhook/deliver.go#L105-L116
func TestVerifyWebhookSignature(t *testing.T) {
secret := "s3cr3t"
payload := []byte(`{"foo": "bar", "baz": true}`)
hasher := hmac.New(sha256.New, []byte(secret))
hasher.Write(payload)
sig := hex.EncodeToString(hasher.Sum(nil))
tt := []struct {
Name string
Secret string
Payload string
Succeed bool
}{
{
Name: "Correct secret and payload",
Secret: "s3cr3t",
Payload: `{"foo": "bar", "baz": true}`,
Succeed: true,
},
{
Name: "Correct secret bad payload",
Secret: "s3cr3t",
Payload: "{}",
Succeed: false,
},
{
Name: "Incorrect secret good payload",
Secret: "secret",
Payload: `{"foo": "bar", "baz": true}`,
Succeed: false,
},
}
for _, tc := range tt {
t.Run(tc.Name, func(t *testing.T) {
ok, err := VerifyWebhookSignature(tc.Secret, sig, []byte(tc.Payload))
assert.NoError(t, err, "verification should not error")
assert.True(t, ok == tc.Succeed, "verification should be %t", tc.Succeed)
})
}
}
func TestVerifyWebhookSignatureHandler(t *testing.T) {
secret := "s3cr3t"
payload := []byte(`{"foo": "bar", "baz": true}`)
hasher := hmac.New(sha256.New, []byte(secret))
hasher.Write(payload)
sig := hex.EncodeToString(hasher.Sum(nil))
tt := []struct {
Name string
Secret string
Payload string
Signature string
Status int
}{
{
Name: "Correct secret and payload",
Secret: "s3cr3t",
Payload: `{"foo": "bar", "baz": true}`,
Signature: sig,
Status: http.StatusOK,
},
{
Name: "Correct secret bad payload",
Secret: "s3cr3t",
Payload: "{}",
Signature: sig,
Status: http.StatusUnauthorized,
},
{
Name: "Incorrect secret good payload",
Secret: "secret",
Payload: `{"foo": "bar", "baz": true}`,
Signature: sig,
Status: http.StatusUnauthorized,
},
{
Name: "No signature",
Status: http.StatusBadRequest,
},
}
for _, tc := range tt {
t.Run(tc.Name, func(t *testing.T) {
server := httptest.NewServer(VerifyWebhookSignatureMiddleware(tc.Secret)(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write(nil)
})))
defer server.Close()
req, err := http.NewRequest(http.MethodPost, server.URL, strings.NewReader(tc.Payload))
assert.NoError(t, err, "should create request")
if tc.Signature != "" {
req.Header.Set("X-Gitea-Signature", tc.Signature)
}
resp, err := http.DefaultClient.Do(req)
assert.NoError(t, err, "request should be delivered")
assert.True(t, resp.StatusCode == tc.Status, "status should be %d, but got %d", tc.Status, resp.StatusCode)
})
}
}

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"crypto"

View File

@@ -3,7 +3,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -1,85 +0,0 @@
// 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
import (
"log"
"testing"
"github.com/stretchr/testify/assert"
)
// TestIssueComment creat a issue and test comment creation/edit/deletion on it
func TestIssueComment(t *testing.T) {
log.Println("== TestIssueComment ==")
c := newTestClient()
user, _, err := c.GetMyUserInfo()
assert.NoError(t, err)
repo, err := createTestRepo(t, "TestIssueCommentRepo", c)
assert.NoError(t, err)
issue1, _, err := c.CreateIssue(user.UserName, repo.Name, CreateIssueOption{Title: "issue1", Body: "body", Closed: false})
assert.NoError(t, err)
assert.EqualValues(t, 1, issue1.Index)
issue2, _, err := c.CreateIssue(user.UserName, repo.Name, CreateIssueOption{Title: "issue1", Body: "body", Closed: false})
assert.EqualValues(t, 2, issue2.Index)
assert.NoError(t, err)
tUser2 := createTestUser(t, "Commenter2", c)
tUser3 := createTestUser(t, "Commenter3", c)
createOne := func(u *User, issue int64, text string) {
c.sudo = u.UserName
comment, _, e := c.CreateIssueComment(user.UserName, repo.Name, issue, CreateIssueCommentOption{Body: text})
c.sudo = ""
assert.NoError(t, e)
assert.NotEmpty(t, comment)
assert.EqualValues(t, text, comment.Body)
assert.EqualValues(t, u.ID, comment.Poster.ID)
}
// CreateIssue
createOne(user, 1, "what a nice issue")
createOne(tUser2, 1, "dont think so")
createOne(tUser3, 1, "weow weow")
createOne(user, 1, "spam isn't it?")
createOne(tUser3, 2, "hehe first commit")
createOne(tUser2, 2, "second")
createOne(user, 2, "3")
_, err = c.AdminDeleteUser(tUser3.UserName)
assert.NoError(t, err)
// ListRepoIssueComments
comments, _, err := c.ListRepoIssueComments(user.UserName, repo.Name, ListIssueCommentOptions{})
assert.NoError(t, err)
assert.Len(t, comments, 7)
// ListIssueComments
comments, _, err = c.ListIssueComments(user.UserName, repo.Name, 2, ListIssueCommentOptions{})
assert.NoError(t, err)
assert.Len(t, comments, 3)
// GetIssueComment
comment, _, err := c.GetIssueComment(user.UserName, repo.Name, comments[1].ID)
assert.NoError(t, err)
assert.EqualValues(t, comment.Poster.ID, comments[1].Poster.ID)
assert.EqualValues(t, comment.Body, comments[1].Body)
assert.EqualValues(t, comment.Updated.Unix(), comments[1].Updated.Unix())
// EditIssueComment
comment, _, err = c.EditIssueComment(user.UserName, repo.Name, comments[1].ID, EditIssueCommentOption{
Body: "changed my mind",
})
assert.NoError(t, err)
assert.EqualValues(t, "changed my mind", comment.Body)
// DeleteIssueComment
_, err = c.DeleteIssueComment(user.UserName, repo.Name, comments[1].ID)
assert.NoError(t, err)
_, _, err = c.GetIssueComment(user.UserName, repo.Name, comments[1].ID)
assert.Error(t, err)
}

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -1,121 +0,0 @@
// 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
import (
"log"
"testing"
"github.com/stretchr/testify/assert"
)
// TestLabels test label related func
func TestLabels(t *testing.T) {
log.Println("== TestLabels ==")
c := newTestClient()
repo, err := createTestRepo(t, "LabelTestsRepo", c)
assert.NoError(t, err)
createOpts := CreateLabelOption{
Name: " ",
Description: "",
Color: "",
}
err = createOpts.Validate()
assert.Error(t, err)
assert.EqualValues(t, "invalid color format", err.Error())
createOpts.Color = "12345f"
err = createOpts.Validate()
assert.Error(t, err)
assert.EqualValues(t, "empty name not allowed", err.Error())
createOpts.Name = "label one"
labelOne, _, err := c.CreateLabel(repo.Owner.UserName, repo.Name, createOpts)
assert.NoError(t, err)
assert.EqualValues(t, createOpts.Name, labelOne.Name)
assert.EqualValues(t, createOpts.Color, labelOne.Color)
labelTwo, _, err := c.CreateLabel(repo.Owner.UserName, repo.Name, CreateLabelOption{
Name: "blue",
Color: "#0000FF",
Description: "CMYB(100%, 100%, 0%, 0%)",
})
assert.NoError(t, err)
_, _, err = c.CreateLabel(repo.Owner.UserName, repo.Name, CreateLabelOption{
Name: "gray",
Color: "808080",
Description: "CMYB(0%, 0%, 0%, 50%)",
})
assert.NoError(t, err)
_, _, err = c.CreateLabel(repo.Owner.UserName, repo.Name, CreateLabelOption{
Name: "green",
Color: "#98F76C",
Description: "CMYB(38%, 0%, 56%, 3%)",
})
assert.NoError(t, err)
labels, resp, err := c.ListRepoLabels(repo.Owner.UserName, repo.Name, ListLabelsOptions{ListOptions: ListOptions{PageSize: 3}})
assert.NoError(t, err)
assert.Len(t, labels, 3)
assert.NotNil(t, resp)
assert.Contains(t, labels, labelTwo)
assert.NotContains(t, labels, labelOne)
label, _, err := c.GetRepoLabel(repo.Owner.UserName, repo.Name, labelTwo.ID)
assert.NoError(t, err)
assert.EqualValues(t, labelTwo, label)
label, _, err = c.EditLabel(repo.Owner.UserName, repo.Name, labelTwo.ID, EditLabelOption{
Color: OptionalString("#0E0175"),
Description: OptionalString("blueish"),
})
assert.NoError(t, err)
assert.EqualValues(t, &Label{
ID: labelTwo.ID,
Name: labelTwo.Name,
Color: "0E0175",
Description: "blueish",
URL: labelTwo.URL,
}, label)
labels, _, _ = c.ListRepoLabels(repo.Owner.UserName, repo.Name, ListLabelsOptions{ListOptions: ListOptions{PageSize: 3}})
createTestIssue(t, c, repo.Name, "test-issue", "", nil, nil, 0, []int64{label.ID}, false, false)
issueIndex := int64(1)
issueLabels, _, err := c.GetIssueLabels(repo.Owner.UserName, repo.Name, issueIndex, ListLabelsOptions{})
assert.NoError(t, err)
assert.Len(t, issueLabels, 1)
assert.EqualValues(t, label, issueLabels[0])
_, _, err = c.AddIssueLabels(repo.Owner.UserName, repo.Name, issueIndex, IssueLabelsOption{Labels: []int64{labels[0].ID}})
assert.NoError(t, err)
issueLabels, _, err = c.AddIssueLabels(repo.Owner.UserName, repo.Name, issueIndex, IssueLabelsOption{Labels: []int64{labels[1].ID, labels[2].ID}})
assert.NoError(t, err)
assert.Len(t, issueLabels, 3)
assert.EqualValues(t, labels, issueLabels)
labels, _, _ = c.ListRepoLabels(repo.Owner.UserName, repo.Name, ListLabelsOptions{})
assert.Len(t, labels, 11)
issueLabels, _, err = c.ReplaceIssueLabels(repo.Owner.UserName, repo.Name, issueIndex, IssueLabelsOption{Labels: []int64{labels[0].ID, labels[1].ID}})
assert.NoError(t, err)
assert.Len(t, issueLabels, 2)
_, err = c.DeleteIssueLabel(repo.Owner.UserName, repo.Name, issueIndex, labels[0].ID)
assert.NoError(t, err)
issueLabels, _, _ = c.GetIssueLabels(repo.Owner.UserName, repo.Name, issueIndex, ListLabelsOptions{})
assert.Len(t, issueLabels, 1)
_, err = c.ClearIssueLabels(repo.Owner.UserName, repo.Name, issueIndex)
assert.NoError(t, err)
issueLabels, _, _ = c.GetIssueLabels(repo.Owner.UserName, repo.Name, issueIndex, ListLabelsOptions{})
assert.Len(t, issueLabels, 0)
_, err = c.DeleteLabel(repo.Owner.UserName, repo.Name, labelTwo.ID)
assert.NoError(t, err)
labels, _, _ = c.ListRepoLabels(repo.Owner.UserName, repo.Name, ListLabelsOptions{})
assert.Len(t, labels, 10)
}

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -1,75 +0,0 @@
// 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
import (
"log"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestMilestones(t *testing.T) {
log.Println("== TestMilestones ==")
c := newTestClient()
repo, _ := createTestRepo(t, "TestMilestones", c)
now := time.Now()
future := time.Unix(1896134400, 0) // 2030-02-01
closed := "closed"
sClosed := StateClosed
// CreateMilestone 4x
m1, _, err := c.CreateMilestone(repo.Owner.UserName, repo.Name, CreateMilestoneOption{Title: "v1.0", Description: "First Version", Deadline: &now})
assert.NoError(t, err)
_, _, err = c.CreateMilestone(repo.Owner.UserName, repo.Name, CreateMilestoneOption{Title: "v2.0", Description: "Second Version", Deadline: &future})
assert.NoError(t, err)
_, _, err = c.CreateMilestone(repo.Owner.UserName, repo.Name, CreateMilestoneOption{Title: "v3.0", Description: "Third Version", Deadline: nil})
assert.NoError(t, err)
m4, _, err := c.CreateMilestone(repo.Owner.UserName, repo.Name, CreateMilestoneOption{Title: "temp", Description: "part time milestone"})
assert.NoError(t, err)
// EditMilestone
m1, _, err = c.EditMilestone(repo.Owner.UserName, repo.Name, m1.ID, EditMilestoneOption{Description: &closed, State: &sClosed})
assert.NoError(t, err)
// DeleteMilestone
_, err = c.DeleteMilestone(repo.Owner.UserName, repo.Name, m4.ID)
assert.NoError(t, err)
// ListRepoMilestones
ml, _, err := c.ListRepoMilestones(repo.Owner.UserName, repo.Name, ListMilestoneOption{})
assert.NoError(t, err)
assert.Len(t, ml, 2)
ml, _, err = c.ListRepoMilestones(repo.Owner.UserName, repo.Name, ListMilestoneOption{State: StateClosed})
assert.NoError(t, err)
assert.Len(t, ml, 1)
ml, _, err = c.ListRepoMilestones(repo.Owner.UserName, repo.Name, ListMilestoneOption{State: StateAll})
assert.NoError(t, err)
assert.Len(t, ml, 3)
ml, _, err = c.ListRepoMilestones(repo.Owner.UserName, repo.Name, ListMilestoneOption{State: StateAll, Name: "V3.0"})
assert.NoError(t, err)
assert.Len(t, ml, 1)
assert.EqualValues(t, "v3.0", ml[0].Title)
// test fallback resolveMilestoneByName
m, _, err := c.resolveMilestoneByName(repo.Owner.UserName, repo.Name, "V3.0")
assert.NoError(t, err)
assert.EqualValues(t, ml[0].ID, m.ID)
_, _, err = c.resolveMilestoneByName(repo.Owner.UserName, repo.Name, "NoEvidenceOfExist")
assert.Error(t, err)
assert.EqualValues(t, "milestone 'NoEvidenceOfExist' do not exist", err.Error())
// GetMilestone
_, _, err = c.GetMilestone(repo.Owner.UserName, repo.Name, m4.ID)
assert.Error(t, err)
m, _, err = c.GetMilestone(repo.Owner.UserName, repo.Name, m1.ID)
assert.NoError(t, err)
assert.EqualValues(t, m1, m)
m2, _, err := c.GetMilestoneByName(repo.Owner.UserName, repo.Name, m.Title)
assert.NoError(t, err)
assert.EqualValues(t, m, m2)
}

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"fmt"

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"fmt"

View File

@@ -1,52 +0,0 @@
// 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
import (
"log"
"testing"
"github.com/stretchr/testify/assert"
)
// TestIssue is main func witch call all Tests for Issue API
// (to make sure they are on correct order)
func TestIssueSubscription(t *testing.T) {
log.Println("== TestIssueSubscription ==")
c := newTestClient()
repo, _ := createTestRepo(t, "IssueWatch", c)
createTestIssue(t, c, repo.Name, "First Issue", "", nil, nil, 0, nil, false, false)
wi, _, err := c.CheckIssueSubscription(repo.Owner.UserName, repo.Name, 1)
assert.NoError(t, err)
assert.True(t, wi.Subscribed)
_, err = c.UnWatchRepo(repo.Owner.UserName, repo.Name)
assert.NoError(t, err)
wi, _, err = c.CheckIssueSubscription(repo.Owner.UserName, repo.Name, 1)
assert.NoError(t, err)
assert.True(t, wi.Subscribed)
_, err = c.IssueSubscribe(repo.Owner.UserName, repo.Name, 1)
if assert.Error(t, err) {
assert.EqualValues(t, "already subscribed", err.Error())
}
wi, _, err = c.CheckIssueSubscription(repo.Owner.UserName, repo.Name, 1)
assert.NoError(t, err)
assert.True(t, wi.Subscribed)
_, err = c.IssueUnSubscribe(repo.Owner.UserName, repo.Name, 1)
assert.NoError(t, err)
wi, _, err = c.CheckIssueSubscription(repo.Owner.UserName, repo.Name, 1)
assert.NoError(t, err)
assert.False(t, wi.Subscribed)
_, err = c.WatchRepo(repo.Owner.UserName, repo.Name)
assert.NoError(t, err)
wi, _, err = c.CheckIssueSubscription(repo.Owner.UserName, repo.Name, 1)
assert.NoError(t, err)
assert.False(t, wi.Subscribed)
}

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"fmt"

View File

@@ -1,154 +0,0 @@
// 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
import (
"log"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// TestIssue is main func witch call all Tests for Issue API
// (to make sure they are on correct order)
func TestIssue(t *testing.T) {
c := newTestClient()
createIssue(t, c)
// Little sleep in order to give some time for gitea to properly store all information on database. Without this sleep, CI is a bit unstable
time.Sleep(100 * time.Millisecond)
editIssues(t, c)
listIssues(t, c)
deleteIssue(t, c)
}
func createIssue(t *testing.T, c *Client) {
log.Println("== TestCreateIssues ==")
user, _, err := c.GetMyUserInfo()
assert.NoError(t, err)
repo, _ := createTestRepo(t, "IssueTestsRepo", c)
nowTime := time.Now()
mile, _, _ := c.CreateMilestone(user.UserName, repo.Name, CreateMilestoneOption{Title: "mile1"})
label1, _, _ := c.CreateLabel(user.UserName, repo.Name, CreateLabelOption{Name: "Label1", Description: "a", Color: "#ee0701"})
label2, _, _ := c.CreateLabel(user.UserName, repo.Name, CreateLabelOption{Name: "Label2", Description: "b", Color: "#128a0c"})
createTestIssue(t, c, repo.Name, "First Issue", "", nil, nil, 0, nil, false, false)
createTestIssue(t, c, repo.Name, "Issue 2", "closed isn't it?", nil, nil, 0, nil, true, false)
createTestIssue(t, c, repo.Name, "Issue 3", "", nil, nil, 0, nil, true, false)
createTestIssue(t, c, repo.Name, "Feature: spam protect 4", "explain explain explain", []string{user.UserName}, &nowTime, 0, nil, true, false)
createTestIssue(t, c, repo.Name, "W 123", "", nil, &nowTime, mile.ID, nil, false, false)
createTestIssue(t, c, repo.Name, "First Issue", "", nil, nil, 0, nil, false, false)
createTestIssue(t, c, repo.Name, "Do it soon!", "is important!", []string{user.UserName}, &nowTime, mile.ID, []int64{label1.ID, label2.ID}, false, false)
createTestIssue(t, c, repo.Name, "Job Done", "you never know", nil, nil, mile.ID, []int64{label2.ID}, true, false)
createTestIssue(t, c, repo.Name, "", "you never know", nil, nil, mile.ID, nil, true, true)
}
func deleteIssue(t *testing.T, c *Client) {
log.Println("== TestDeleteIssues ==")
user, _, err := c.GetMyUserInfo()
assert.NoError(t, err)
repo, _ := createTestRepo(t, "IssueTestsRepo", c)
issue := createTestIssue(t, c, repo.Name, "Deleteable Issue", "", nil, nil, 0, nil, false, false)
_, err = c.DeleteIssue(user.UserName, repo.Name, issue.Index)
assert.NoError(t, err)
}
func editIssues(t *testing.T, c *Client) {
log.Println("== TestEditIssues ==")
il, _, err := c.ListIssues(ListIssueOption{KeyWord: "soon"})
assert.NoError(t, err)
issue, _, err := c.GetIssue(il[0].Poster.UserName, il[0].Repository.Name, il[0].Index)
assert.NoError(t, err)
state := StateClosed
issueNew, _, err := c.EditIssue(issue.Poster.UserName, issue.Repository.Name, issue.Index, EditIssueOption{
Title: "Edited",
Body: OptionalString("123 test and go"),
State: &state,
Ref: OptionalString("main"),
})
assert.NoError(t, err)
assert.EqualValues(t, issue.ID, issueNew.ID)
assert.EqualValues(t, "123 test and go", issueNew.Body)
assert.EqualValues(t, "Edited", issueNew.Title)
assert.EqualValues(t, "main", issueNew.Ref)
}
func listIssues(t *testing.T, c *Client) {
log.Println("== TestListIssues ==")
issues, _, err := c.ListRepoIssues("test01", "IssueTestsRepo", ListIssueOption{
Labels: []string{"Label1", "Label2"},
KeyWord: "",
State: "all",
})
assert.NoError(t, err)
assert.Len(t, issues, 1)
issues, _, err = c.ListIssues(ListIssueOption{
Labels: []string{"Label2"},
KeyWord: "Done",
State: "all",
})
assert.NoError(t, err)
assert.Len(t, issues, 1)
issues, _, err = c.ListRepoIssues("test01", "IssueTestsRepo", ListIssueOption{
Milestones: []string{"mile1"},
State: "all",
})
assert.NoError(t, err)
assert.Len(t, issues, 3)
for i := range issues {
if assert.NotNil(t, issues[i].Milestone) {
assert.EqualValues(t, "mile1", issues[i].Milestone.Title)
}
}
issues, _, err = c.ListRepoIssues("test01", "IssueTestsRepo", ListIssueOption{})
assert.NoError(t, err)
assert.Len(t, issues, 3)
}
func createTestIssue(t *testing.T, c *Client, repoName, title, body string, assignees []string, deadline *time.Time, milestone int64, labels []int64, closed, shouldFail bool) *Issue {
user, _, err := c.GetMyUserInfo()
assert.NoError(t, err)
issue, _, e := c.CreateIssue(user.UserName, repoName, CreateIssueOption{
Title: title,
Body: body,
Assignees: assignees,
Deadline: deadline,
Milestone: milestone,
Labels: labels,
Closed: closed,
})
if shouldFail {
assert.Error(t, e)
return nil
}
assert.NoError(t, e)
assert.NotEmpty(t, issue)
assert.EqualValues(t, title, issue.Title)
assert.EqualValues(t, body, issue.Body)
assert.EqualValues(t, len(assignees), len(issue.Assignees))
for i, a := range issue.Assignees {
assert.EqualValues(t, assignees[i], a.UserName)
}
if milestone > 0 {
assert.EqualValues(t, milestone, issue.Milestone.ID)
}
assert.EqualValues(t, len(labels), len(issue.Labels))
if closed {
assert.False(t, issue.Closed.IsZero())
} else {
assert.Empty(t, issue.Closed)
}
return issue
}

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"fmt"

View File

@@ -1,164 +0,0 @@
// 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
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"testing"
)
func getGiteaURL() string {
return os.Getenv("GITEA_SDK_TEST_URL")
}
func getGiteaToken() string {
return os.Getenv("GITEA_SDK_TEST_TOKEN")
}
func getGiteaUsername() string {
return os.Getenv("GITEA_SDK_TEST_USERNAME")
}
func getGiteaPassword() string {
return os.Getenv("GITEA_SDK_TEST_PASSWORD")
}
func enableRunGitea() bool {
r, _ := strconv.ParseBool(os.Getenv("GITEA_SDK_TEST_RUN_GITEA"))
return r
}
func newTestClient() *Client {
c, _ := NewClient(getGiteaURL(), newTestClientAuth())
return c
}
func newTestClientAuth() ClientOption {
token := getGiteaToken()
if token == "" {
return SetBasicAuth(getGiteaUsername(), getGiteaPassword())
}
return SetToken(getGiteaToken())
}
func giteaMasterPath() string {
switch runtime.GOOS {
case "darwin":
return fmt.Sprintf("https://dl.gitea.io/gitea/master/gitea-master-darwin-10.6-%s", runtime.GOARCH)
case "linux":
return fmt.Sprintf("https://dl.gitea.io/gitea/master/gitea-master-linux-%s", runtime.GOARCH)
case "windows":
return fmt.Sprintf("https://dl.gitea.io/gitea/master/gitea-master-windows-4.0-%s.exe", runtime.GOARCH)
}
return ""
}
func downGitea() (string, error) {
for i := 3; i > 0; i-- {
resp, err := http.Get(giteaMasterPath())
if err != nil {
continue
}
defer resp.Body.Close()
f, err := ioutil.TempFile(os.TempDir(), "gitea")
if err != nil {
continue
}
_, err = io.Copy(f, resp.Body)
f.Close()
if err != nil {
continue
}
if err = os.Chmod(f.Name(), 0o700); err != nil {
return "", err
}
return f.Name(), nil
}
return "", fmt.Errorf("Download gitea from %v failed", giteaMasterPath())
}
func runGitea() (*os.Process, error) {
log.Println("Downloading Gitea from", giteaMasterPath())
p, err := downGitea()
if err != nil {
log.Fatal(err)
}
giteaDir := filepath.Dir(p)
cfgDir := filepath.Join(giteaDir, "custom", "conf")
err = os.MkdirAll(cfgDir, os.ModePerm)
if err != nil {
log.Fatal(err)
}
cfg, err := os.Create(filepath.Join(cfgDir, "app.ini"))
if err != nil {
log.Fatal(err)
}
_, err = cfg.WriteString(`[security]
INTERNAL_TOKEN = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE1NTg4MzY4ODB9.LoKQyK5TN_0kMJFVHWUW0uDAyoGjDP6Mkup4ps2VJN4
INSTALL_LOCK = true
SECRET_KEY = 2crAW4UANgvLipDS6U5obRcFosjSJHQANll6MNfX7P0G3se3fKcCwwK3szPyGcbo
[database]
DB_TYPE = sqlite3
[log]
MODE = console
LEVEL = Trace
REDIRECT_MACARON_LOG = true
MACARON = ,
ROUTER = ,`)
cfg.Close()
if err != nil {
log.Fatal(err)
}
log.Println("Run gitea migrate", p)
err = exec.Command(p, "migrate").Run()
if err != nil {
log.Fatal(err)
}
log.Println("Run gitea admin", p)
err = exec.Command(p, "admin", "create-user", "--username=test01", "--password=test01", "--email=test01@gitea.io", "--admin=true", "--must-change-password=false", "--access-token").Run()
if err != nil {
log.Fatal(err)
}
log.Println("Start Gitea", p)
return os.StartProcess(filepath.Base(p), []string{}, &os.ProcAttr{
Dir: giteaDir,
})
}
func TestMain(m *testing.M) {
if enableRunGitea() {
p, err := runGitea()
if err != nil {
log.Fatal(err)
return
}
defer func() {
if err := p.Kill(); err != nil {
log.Fatal(err)
}
}()
}
log.Printf("testing with %v, %v, %v\n", getGiteaURL(), getGiteaUsername(), getGiteaPassword())
exitCode := m.Run()
os.Exit(exitCode)
}

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"fmt"

View File

@@ -1,141 +0,0 @@
// 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
import (
"fmt"
"log"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestNotifications(t *testing.T) {
log.Println("== TestNotifications ==")
// init user2
c := newTestClient()
user1, _, err := c.GetMyUserInfo()
assert.NoError(t, err)
user2 := createTestUser(t, "notify2", c)
// create 2 repos
repoA, err := createTestRepo(t, "TestNotifications_A", c)
assert.NoError(t, err)
c.sudo = user2.UserName
repoB, err := createTestRepo(t, "TestNotifications_B", c)
assert.NoError(t, err)
_, err = c.WatchRepo(user1.UserName, repoA.Name)
c.sudo = ""
assert.NoError(t, err)
c.sudo = user2.UserName
notifications, _, err := c.ReadNotifications(MarkNotificationOptions{})
assert.NoError(t, err)
assert.Len(t, notifications, 0)
count, _, err := c.CheckNotifications()
assert.EqualValues(t, 0, count)
assert.NoError(t, err)
c.sudo = ""
_, _, err = c.CreateIssue(repoA.Owner.UserName, repoA.Name, CreateIssueOption{Title: "A Issue", Closed: false})
assert.NoError(t, err)
issue, _, err := c.CreateIssue(repoB.Owner.UserName, repoB.Name, CreateIssueOption{Title: "B Issue", Closed: false})
assert.NoError(t, err)
time.Sleep(time.Second * 1)
// CheckNotifications of user2
c.sudo = user2.UserName
count, _, err = c.CheckNotifications()
assert.NoError(t, err)
assert.EqualValues(t, 2, count)
// ListNotifications
nList, _, err := c.ListNotifications(ListNotificationOptions{})
assert.NoError(t, err)
assert.Len(t, nList, 2)
for _, n := range nList {
assert.EqualValues(t, true, n.Unread)
assert.EqualValues(t, "Issue", n.Subject.Type)
assert.EqualValues(t, NotifySubjectOpen, nList[0].Subject.State)
assert.EqualValues(t, NotifySubjectOpen, nList[1].Subject.State)
if n.Subject.Title == "A Issue" {
assert.EqualValues(t, repoA.Name, n.Repository.Name)
} else if n.Subject.Title == "B Issue" {
assert.EqualValues(t, repoB.Name, n.Repository.Name)
} else {
assert.Error(t, fmt.Errorf("ListNotifications returned a Issue witch should not"))
}
}
// ListRepoNotifications
nList, _, err = c.ListRepoNotifications(repoA.Owner.UserName, repoA.Name, ListNotificationOptions{})
assert.NoError(t, err)
assert.Len(t, nList, 1)
assert.EqualValues(t, "A Issue", nList[0].Subject.Title)
// ReadRepoNotifications
notifications, _, err = c.ReadRepoNotifications(repoA.Owner.UserName, repoA.Name, MarkNotificationOptions{})
assert.NoError(t, err)
assert.Len(t, notifications, 1)
// GetThread
n, _, err := c.GetNotification(nList[0].ID)
assert.NoError(t, err)
assert.EqualValues(t, false, n.Unread)
assert.EqualValues(t, "A Issue", n.Subject.Title)
// ReadNotifications
notifications, _, err = c.ReadNotifications(MarkNotificationOptions{})
assert.NoError(t, err)
assert.Len(t, notifications, 1)
nList, _, err = c.ListNotifications(ListNotificationOptions{})
assert.NoError(t, err)
assert.Len(t, nList, 0)
// ReadThread
iState := StateClosed
c.sudo = ""
_, _, err = c.EditIssue(repoB.Owner.UserName, repoB.Name, issue.Index, EditIssueOption{State: &iState})
assert.NoError(t, err)
time.Sleep(time.Second * 1)
c.sudo = user2.UserName
nList, _, err = c.ListNotifications(ListNotificationOptions{})
assert.NoError(t, err)
count, _, err = c.CheckNotifications()
assert.NoError(t, err)
assert.EqualValues(t, 1, count)
if assert.Len(t, nList, 1) {
assert.EqualValues(t, NotifySubjectClosed, nList[0].Subject.State)
notification, _, err := c.ReadNotification(nList[0].ID)
assert.NoError(t, err)
assert.EqualValues(t, notification.ID, nList[0].ID)
}
c.sudo = ""
notifications, _, err = c.ReadNotifications(MarkNotificationOptions{})
assert.NoError(t, err)
assert.Len(t, notifications, 2)
_, _ = c.DeleteRepo("test01", "Reviews")
nList, _, err = c.ListNotifications(ListNotificationOptions{Status: []NotifyStatus{NotifyStatusRead}})
assert.NoError(t, err)
assert.Len(t, nList, 2)
notification, _, err := c.ReadNotification(nList[0].ID, NotifyStatusPinned)
assert.EqualValues(t, notification.ID, nList[0].ID)
assert.NoError(t, err)
notification, _, err = c.ReadNotification(nList[1].ID, NotifyStatusUnread)
assert.EqualValues(t, notification.ID, nList[1].ID)
assert.NoError(t, err)
nList, _, err = c.ListNotifications(ListNotificationOptions{Status: []NotifyStatus{NotifyStatusPinned, NotifyStatusUnread}})
assert.NoError(t, err)
if assert.Len(t, nList, 2) {
assert.EqualValues(t, NotifySubjectOpen, nList[0].Subject.State)
assert.EqualValues(t, NotifySubjectOpen, nList[1].Subject.State)
}
}

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -1,79 +0,0 @@
// 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
import (
"log"
"testing"
"github.com/stretchr/testify/assert"
)
func TestOauth2(t *testing.T) {
log.Println("== TestOauth2Application ==")
c := newTestClient()
user := createTestUser(t, "oauth2_user", c)
c.SetSudo(user.UserName)
type test struct {
name string
confidentialClient *bool
}
boolTrue := true
boolFalse := false
testCases := []test{
{"ConfidentialClient unset should fallback to false", nil},
{"ConfidentialClient true", &boolTrue},
{"ConfidentialClient false", &boolFalse},
}
for _, testCase := range testCases {
createOptions := CreateOauth2Option{
Name: "test",
RedirectURIs: []string{"http://test/test"},
}
if testCase.confidentialClient != nil {
createOptions.ConfidentialClient = *testCase.confidentialClient
}
newApp, _, err := c.CreateOauth2(createOptions)
assert.NoError(t, err, testCase.name)
assert.NotNil(t, newApp, testCase.name)
assert.EqualValues(t, "test", newApp.Name, testCase.name)
if testCase.confidentialClient != nil {
assert.EqualValues(t, *testCase.confidentialClient, newApp.ConfidentialClient, testCase.name)
} else {
assert.EqualValues(t, false, newApp.ConfidentialClient, testCase.name)
}
a, _, err := c.ListOauth2(ListOauth2Option{})
assert.NoError(t, err, testCase.name)
assert.Len(t, a, 1, testCase.name)
assert.EqualValues(t, newApp.Name, a[0].Name, testCase.name)
assert.EqualValues(t, newApp.ConfidentialClient, a[0].ConfidentialClient, testCase.name)
b, _, err := c.GetOauth2(newApp.ID)
assert.NoError(t, err, testCase.name)
assert.EqualValues(t, newApp.Name, b.Name, testCase.name)
assert.EqualValues(t, newApp.ConfidentialClient, b.ConfidentialClient, testCase.name)
b, _, err = c.UpdateOauth2(newApp.ID, CreateOauth2Option{
Name: newApp.Name,
ConfidentialClient: !newApp.ConfidentialClient,
RedirectURIs: []string{"https://test/login"},
})
assert.NoError(t, err, testCase.name)
assert.EqualValues(t, newApp.Name, b.Name, testCase.name)
assert.EqualValues(t, "https://test/login", b.RedirectURIs[0], testCase.name)
assert.EqualValues(t, newApp.ID, b.ID, testCase.name)
assert.NotEqual(t, newApp.ClientSecret, b.ClientSecret, testCase.name)
assert.NotEqual(t, newApp.ConfidentialClient, b.ConfidentialClient, testCase.name)
_, err = c.DeleteOauth2(newApp.ID)
assert.NoError(t, err, testCase.name)
}
}

View File

@@ -3,7 +3,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"fmt"

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"fmt"

View File

@@ -1,66 +0,0 @@
// 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
import (
"log"
"testing"
"github.com/stretchr/testify/assert"
)
/*
// DeleteOrgMembership remove a member from an organization
func (c *Client) DeleteOrgMembership(org, user string) error {}
*/
func TestOrgMembership(t *testing.T) {
log.Println("== TestOrgMembership ==")
c := newTestClient()
user := createTestUser(t, "org_mem_user", c)
c.SetSudo(user.UserName)
newOrg, _, err := c.CreateOrg(CreateOrgOption{Name: "MemberOrg"})
assert.NoError(t, err)
assert.NotNil(t, newOrg)
// Check func
check, _, err := c.CheckPublicOrgMembership(newOrg.UserName, user.UserName)
assert.NoError(t, err)
assert.False(t, check)
check, _, err = c.CheckOrgMembership(newOrg.UserName, user.UserName)
assert.NoError(t, err)
assert.True(t, check)
perm, _, err := c.GetOrgPermissions(newOrg.UserName, user.UserName)
assert.NoError(t, err)
assert.NotNil(t, perm)
assert.True(t, perm.IsOwner)
_, err = c.SetPublicOrgMembership(newOrg.UserName, user.UserName, true)
assert.NoError(t, err)
check, _, err = c.CheckPublicOrgMembership(newOrg.UserName, user.UserName)
assert.NoError(t, err)
assert.True(t, check)
u, _, err := c.ListOrgMembership(newOrg.UserName, ListOrgMembershipOption{})
assert.NoError(t, err)
assert.Len(t, u, 1)
assert.EqualValues(t, user.UserName, u[0].UserName)
u, _, err = c.ListPublicOrgMembership(newOrg.UserName, ListOrgMembershipOption{})
assert.NoError(t, err)
assert.Len(t, u, 1)
assert.EqualValues(t, user.UserName, u[0].UserName)
_, err = c.DeleteOrgMembership(newOrg.UserName, user.UserName)
assert.Error(t, err)
c.sudo = ""
_, err = c.AdminDeleteUser(user.UserName)
assert.Error(t, err)
_, err = c.DeleteOrg(newOrg.UserName)
assert.NoError(t, err)
_, err = c.AdminDeleteUser(user.UserName)
assert.NoError(t, err)
}

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -1,56 +0,0 @@
// Copyright 2021 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
import (
"log"
"testing"
"github.com/stretchr/testify/assert"
)
func createTestOrgTeams(t *testing.T, c *Client, org, name string, accessMode AccessMode, units []RepoUnitType) (*Team, error) {
team, _, e := c.CreateTeam(org, CreateTeamOption{
Name: name,
Description: name + "'s team desc",
Permission: accessMode,
CanCreateOrgRepo: false,
IncludesAllRepositories: false,
Units: units,
})
assert.NoError(t, e)
assert.NotNil(t, team)
return team, e
}
func TestTeamSearch(t *testing.T) {
log.Println("== TestTeamSearch ==")
c := newTestClient()
orgName := "TestTeamsOrg"
// prepare for test
_, _, err := c.CreateOrg(CreateOrgOption{
Name: orgName,
Visibility: VisibleTypePublic,
RepoAdminChangeTeamAccess: true,
})
defer func() {
_, _ = c.DeleteOrg(orgName)
}()
assert.NoError(t, err)
if _, err = createTestOrgTeams(t, c, orgName, "Admins", AccessModeAdmin, []RepoUnitType{RepoUnitCode, RepoUnitIssues, RepoUnitPulls, RepoUnitReleases}); err != nil {
return
}
teams, _, err := c.SearchOrgTeams(orgName, &SearchTeamsOptions{
Query: "Admins",
})
assert.NoError(t, err)
if assert.Len(t, teams, 1) {
assert.Equal(t, "Admins", teams[0].Name)
}
}

View File

@@ -1,49 +0,0 @@
// Copyright 2021 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
import (
"testing"
"github.com/stretchr/testify/assert"
)
func createTestOrgRepo(t *testing.T, c *Client, name string) (func(), *Repository, error) {
_, _, err := c.GetOrg(name)
if err == nil {
_, _ = c.DeleteOrg(name)
}
_, _, err = c.CreateOrg(CreateOrgOption{
Name: name,
Visibility: VisibleTypePublic,
RepoAdminChangeTeamAccess: true,
})
if !assert.NoError(t, err) {
return nil, nil, err
}
_, _, err = c.GetRepo(name, name)
if err == nil {
_, _ = c.DeleteRepo(name, name)
}
repo, _, err := c.CreateOrgRepo(name, CreateRepoOption{
Name: name,
Description: "A test Repo: " + name,
AutoInit: true,
Gitignores: "C,C++",
License: "MIT",
Readme: "Default",
IssueLabels: "Default",
Private: false,
})
assert.NoError(t, err)
assert.NotNil(t, repo)
return func() {
_, _ = c.DeleteRepo(name, name)
_, _ = c.DeleteOrg(name)
}, repo, err
}

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"fmt"

View File

@@ -1,108 +0,0 @@
// Copyright 2023 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
import (
"bytes"
"fmt"
"log"
"net/http"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// create an org with a single package for testing purposes
func createTestPackage(t *testing.T, c *Client) error {
_, _ = c.DeletePackage("PackageOrg", "generic", "MyPackage", "v1")
_, _ = c.DeleteOrg("PackageOrg")
_, _, _ = c.CreateOrg(CreateOrgOption{Name: "PackageOrg"})
client := &http.Client{
Timeout: time.Second * 10,
}
reader := bytes.NewReader([]byte("Hello world!"))
url := fmt.Sprintf("%s/api/packages/PackageOrg/generic/MyPackage/v1/file1.txt", os.Getenv("GITEA_SDK_TEST_URL"))
req, err := http.NewRequest(http.MethodPut, url, reader)
if err != nil {
log.Println(err)
return err
}
req.SetBasicAuth(os.Getenv("GITEA_SDK_TEST_USERNAME"), os.Getenv("GITEA_SDK_TEST_PASSWORD"))
response, err := client.Do(req)
if err != nil {
return err
}
defer response.Body.Close()
return nil
}
func TestListPackages(t *testing.T) {
log.Println("== TestListPackages ==")
c := newTestClient()
err := createTestPackage(t, c)
assert.NoError(t, err)
packagesList, _, err := c.ListPackages("PackageOrg", ListPackagesOptions{
ListOptions{
Page: 1,
PageSize: 1000,
},
})
assert.NoError(t, err)
assert.Len(t, packagesList, 1)
}
func TestGetPackage(t *testing.T) {
log.Println("== TestGetPackage ==")
c := newTestClient()
err := createTestPackage(t, c)
assert.NoError(t, err)
pkg, _, err := c.GetPackage("PackageOrg", "generic", "MyPackage", "v1")
assert.NoError(t, err)
assert.NotNil(t, pkg)
assert.True(t, pkg.Name == "MyPackage")
assert.True(t, pkg.Version == "v1")
assert.NotEmpty(t, pkg.CreatedAt)
}
func TestDeletePackage(t *testing.T) {
log.Println("== TestDeletePackage ==")
c := newTestClient()
err := createTestPackage(t, c)
assert.NoError(t, err)
_, err = c.DeletePackage("PackageOrg", "generic", "MyPackage", "v1")
assert.NoError(t, err)
// no packages should be listed following deletion
packagesList, _, err := c.ListPackages("PackageOrg", ListPackagesOptions{
ListOptions{
Page: 1,
PageSize: 1000,
},
})
assert.NoError(t, err)
assert.Len(t, packagesList, 0)
}
func TestListPackageFiles(t *testing.T) {
log.Println("== TestListPackageFiles ==")
c := newTestClient()
err := createTestPackage(t, c)
assert.NoError(t, err)
packageFiles, _, err := c.ListPackageFiles("PackageOrg", "generic", "MyPackage", "v1")
assert.NoError(t, err)
assert.Len(t, packageFiles, 1)
assert.True(t, packageFiles[0].Name == "file1.txt")
}

View File

@@ -3,7 +3,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -1,233 +0,0 @@
// 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
import (
"log"
"testing"
"github.com/stretchr/testify/assert"
)
func TestPullReview(t *testing.T) {
log.Println("== TestPullReview ==")
c := newTestClient()
repoName := "Reviews"
repo, pull, submitter, reviewer, success := preparePullReviewTest(t, c, repoName)
if !success {
return
}
// CreatePullReview
r1, _, err := c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
State: ReviewStateComment,
Body: "I'll have a look at it later",
})
assert.NoError(t, err)
if assert.NotNil(t, r1) {
assert.EqualValues(t, ReviewStateComment, r1.State)
assert.EqualValues(t, 1, r1.Reviewer.ID)
}
c.SetSudo(submitter.UserName)
_, _, err = c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
State: ReviewStateApproved,
Body: "lgtm it myself",
})
assert.Error(t, err)
r2, _, err := c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
State: ReviewStateComment,
Body: "no seriously please have a look at it",
})
assert.NoError(t, err)
assert.NotNil(t, r2)
c.SetSudo(reviewer.UserName)
r3, _, err := c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
State: ReviewStateApproved,
Body: "lgtm",
Comments: []CreatePullReviewComment{
{
Path: "WOW-file",
Body: "no better name - really?",
NewLineNum: 1,
},
},
})
assert.NoError(t, err)
assert.NotNil(t, r3)
// ListPullReviews
c.SetSudo("")
rl, _, err := c.ListPullReviews(repo.Owner.UserName, repo.Name, pull.Index, ListPullReviewsOptions{})
assert.NoError(t, err)
assert.Len(t, rl, 3)
for i := range rl {
assert.EqualValues(t, pull.HTMLURL, rl[i].HTMLPullURL)
if rl[i].CodeCommentsCount == 1 {
assert.EqualValues(t, reviewer.ID, rl[i].Reviewer.ID)
}
}
// GetPullReview
rNew, _, err := c.GetPullReview(repo.Owner.UserName, repo.Name, pull.Index, r3.ID)
assert.NoError(t, err)
assert.EqualValues(t, r3, rNew)
// DeletePullReview
c.SetSudo(submitter.UserName)
_, err = c.DeletePullReview(repo.Owner.UserName, repo.Name, pull.Index, r2.ID)
assert.NoError(t, err)
_, err = c.DeletePullReview(repo.Owner.UserName, repo.Name, pull.Index, r3.ID)
assert.Error(t, err)
// SubmitPullReview
c.SetSudo("")
r4, resp, err := c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
Body: "...",
Comments: []CreatePullReviewComment{
{
Path: "WOW-file",
Body: "its ok",
NewLineNum: 1,
},
},
})
assert.NoError(t, err)
assert.NotNil(t, resp)
r5, _, err := c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
Body: "...",
Comments: []CreatePullReviewComment{
{
Path: "WOW-file",
Body: "hehe and here it is",
NewLineNum: 3,
},
},
})
assert.NoError(t, err)
assert.EqualValues(t, r4.ID, r5.ID)
r, _, err := c.SubmitPullReview(repo.Owner.UserName, repo.Name, pull.Index, r4.ID, SubmitPullReviewOptions{
State: ReviewStateRequestChanges,
Body: "one nit",
})
assert.NoError(t, err)
assert.EqualValues(t, r4.ID, r.ID)
assert.EqualValues(t, ReviewStateRequestChanges, r.State)
// ListPullReviewComments
rcl, _, err := c.ListPullReviewComments(repo.Owner.UserName, repo.Name, pull.Index, r.ID)
assert.NoError(t, err)
assert.EqualValues(t, r.CodeCommentsCount, len(rcl))
for _, rc := range rcl {
assert.EqualValues(t, pull.HTMLURL, rc.HTMLPullURL)
if rc.LineNum == 3 {
assert.EqualValues(t, "hehe and here it is", rc.Body)
} else {
assert.EqualValues(t, 1, rc.LineNum)
assert.EqualValues(t, "its ok", rc.Body)
}
}
r, _, err = c.GetPullReview(repo.Owner.UserName, repo.Name, pull.Index, r.ID)
assert.NoError(t, err)
assert.False(t, r.Dismissed)
// DismissPullReview
resp, err = c.DismissPullReview(repo.Owner.UserName, repo.Name, pull.Index, r.ID, DismissPullReviewOptions{Message: "stale"})
assert.NoError(t, err)
if assert.NotNil(t, resp) {
assert.EqualValues(t, 200, resp.StatusCode)
}
r, _, _ = c.GetPullReview(repo.Owner.UserName, repo.Name, pull.Index, r.ID)
assert.True(t, r.Dismissed)
// UnDismissPullReview
resp, err = c.UnDismissPullReview(repo.Owner.UserName, repo.Name, pull.Index, r.ID)
assert.NoError(t, err)
if assert.NotNil(t, resp) {
assert.EqualValues(t, 200, resp.StatusCode)
}
r, _, _ = c.GetPullReview(repo.Owner.UserName, repo.Name, pull.Index, r.ID)
assert.False(t, r.Dismissed)
rl, _, err = c.ListPullReviews(repo.Owner.UserName, repo.Name, pull.Index, ListPullReviewsOptions{})
assert.NoError(t, err)
assert.Len(t, rl, 3)
c.SetSudo(submitter.UserName)
resp, err = c.CreateReviewRequests(repo.Owner.UserName, repo.Name, pull.Index, PullReviewRequestOptions{Reviewers: []string{reviewer.UserName}})
assert.NoError(t, err)
assert.NotNil(t, resp)
rl, _, _ = c.ListPullReviews(repo.Owner.UserName, repo.Name, pull.Index, ListPullReviewsOptions{})
if assert.Len(t, rl, 4) {
assert.EqualValues(t, ReviewStateRequestReview, rl[3].State)
}
c.SetSudo(reviewer.UserName)
resp, err = c.DeleteReviewRequests(repo.Owner.UserName, repo.Name, pull.Index, PullReviewRequestOptions{Reviewers: []string{reviewer.UserName}})
assert.NoError(t, err)
assert.NotNil(t, resp)
rl, _, _ = c.ListPullReviews(repo.Owner.UserName, repo.Name, pull.Index, ListPullReviewsOptions{})
assert.Len(t, rl, 3)
c.SetSudo("")
_, err = c.AdminDeleteUser(reviewer.UserName)
assert.NoError(t, err)
_, err = c.AdminDeleteUser(submitter.UserName)
assert.NoError(t, err)
}
func preparePullReviewTest(t *testing.T, c *Client, repoName string) (*Repository, *PullRequest, *User, *User, bool) {
repo, err := createTestRepo(t, repoName, c)
if !assert.NoError(t, err) {
return nil, nil, nil, nil, false
}
pullSubmitter := createTestUser(t, "pull_submitter", c)
write := AccessModeWrite
_, err = c.AddCollaborator(repo.Owner.UserName, repo.Name, pullSubmitter.UserName, AddCollaboratorOption{
Permission: &write,
})
assert.NoError(t, err)
c.SetSudo("pull_submitter")
newFile, _, err := c.CreateFile(repo.Owner.UserName, repo.Name, "WOW-file", CreateFileOptions{
Content: "QSBuZXcgRmlsZQoKYW5kIHNvbWUgbGluZXMK",
FileOptions: FileOptions{
Message: "creat a new file",
BranchName: "main",
NewBranchName: "new_file",
},
})
if !assert.NoError(t, err) || !assert.NotNil(t, newFile) {
return nil, nil, nil, nil, false
}
pull, _, err := c.CreatePullRequest(c.username, repoName, CreatePullRequestOption{
Base: "main",
Head: "new_file",
Title: "Creat a NewFile",
})
assert.NoError(t, err)
assert.NotNil(t, pull)
c.SetSudo("")
reviewer := createTestUser(t, "pull_reviewer", c)
admin := AccessModeAdmin
_, err = c.AddCollaborator(repo.Owner.UserName, repo.Name, pullSubmitter.UserName, AddCollaboratorOption{
Permission: &admin,
})
assert.NoError(t, err)
return repo, pull, pullSubmitter, reviewer, pull.Poster.ID == pullSubmitter.ID
}

View File

@@ -1,213 +0,0 @@
// 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
import (
"log"
"testing"
"github.com/stretchr/testify/assert"
)
func TestPull(t *testing.T) {
log.Println("== TestPull ==")
c := newTestClient()
user, _, err := c.GetMyUserInfo()
assert.NoError(t, err)
repoName := "repo_pull_test"
forkOrg := "ForkOrg"
if !preparePullTest(t, c, repoName, forkOrg) {
return
}
// ListRepoPullRequests list PRs of one repository
pulls, _, err := c.ListRepoPullRequests(user.UserName, repoName, ListPullRequestsOptions{})
assert.NoError(t, err)
assert.Len(t, pulls, 0)
pullUpdateFile, _, err := c.CreatePullRequest(c.username, repoName, CreatePullRequestOption{
Base: "main",
Head: forkOrg + ":overwrite_licence",
Title: "overwrite a file",
})
assert.NoError(t, err)
assert.NotNil(t, pullUpdateFile)
pullNewFile, _, err := c.CreatePullRequest(c.username, repoName, CreatePullRequestOption{
Base: "main",
Head: forkOrg + ":new_file",
Title: "create a file",
})
assert.NoError(t, err)
assert.NotNil(t, pullNewFile)
pullConflict, _, err := c.CreatePullRequest(c.username, repoName, CreatePullRequestOption{
Base: "main",
Head: forkOrg + ":will_conflict",
Title: "this pull will conflict",
})
assert.NoError(t, err)
assert.NotNil(t, pullConflict)
pulls, _, err = c.ListRepoPullRequests(user.UserName, repoName, ListPullRequestsOptions{})
assert.NoError(t, err)
assert.Len(t, pulls, 3)
diff, _, err := c.GetPullRequestDiff(c.username, repoName, pullUpdateFile.Index, PullRequestDiffOptions{
Binary: true,
})
assert.NoError(t, err)
assert.True(t, len(diff) > 1100 && len(diff) < 1300)
patch, _, err := c.GetPullRequestPatch(c.username, repoName, pullUpdateFile.Index)
assert.NoError(t, err)
assert.True(t, len(patch) > len(diff))
commits, _, err := c.ListPullRequestCommits(c.username, repoName, pullUpdateFile.Index, ListPullRequestCommitsOptions{})
assert.NoError(t, err)
if assert.Len(t, commits, 1) && assert.Len(t, commits[0].Files, 1) {
assert.EqualValues(t, "LICENSE", commits[0].Files[0].Filename)
}
files, _, err := c.ListPullRequestFiles(c.username, repoName, pullUpdateFile.Index, ListPullRequestFilesOptions{})
assert.NoError(t, err)
assert.Len(t, files, 1)
file := files[0]
assert.EqualValues(t, "LICENSE", file.Filename)
assert.EqualValues(t, "changed", file.Status)
assert.EqualValues(t, 3, file.Additions)
assert.EqualValues(t, 9, file.Deletions)
assert.EqualValues(t, 12, file.Changes)
// test Update pull
pr, _, err := c.GetPullRequest(user.UserName, repoName, pullUpdateFile.Index)
assert.NoError(t, err)
assert.NotNil(t, pr)
assert.False(t, pullUpdateFile.HasMerged)
assert.True(t, pullUpdateFile.Mergeable)
merged, _, err := c.MergePullRequest(user.UserName, repoName, pullUpdateFile.Index, MergePullRequestOption{
Style: MergeStyleSquash,
Title: pullUpdateFile.Title,
Message: "squash: " + pullUpdateFile.Title,
})
assert.NoError(t, err)
assert.True(t, merged)
merged, _, err = c.IsPullRequestMerged(user.UserName, repoName, pullUpdateFile.Index)
assert.NoError(t, err)
assert.True(t, merged)
pr, _, err = c.GetPullRequest(user.UserName, repoName, pullUpdateFile.Index)
assert.NoError(t, err)
assert.EqualValues(t, pullUpdateFile.Head.Name, pr.Head.Name)
assert.EqualValues(t, pullUpdateFile.Base.Name, pr.Base.Name)
assert.NotEqual(t, pullUpdateFile.Base.Sha, pr.Base.Sha)
assert.Len(t, *pr.MergedCommitID, 40)
assert.True(t, pr.HasMerged)
// test conflict pull
pr, _, err = c.GetPullRequest(user.UserName, repoName, pullConflict.Index)
assert.NoError(t, err)
assert.False(t, pr.HasMerged)
assert.False(t, pr.Mergeable)
merged, _, err = c.MergePullRequest(user.UserName, repoName, pullConflict.Index, MergePullRequestOption{
Style: MergeStyleMerge,
Title: "pullConflict",
Message: "pullConflict Msg",
})
assert.NoError(t, err)
assert.False(t, merged)
merged, _, err = c.IsPullRequestMerged(user.UserName, repoName, pullConflict.Index)
assert.NoError(t, err)
assert.False(t, merged)
pr, _, err = c.GetPullRequest(user.UserName, repoName, pullConflict.Index)
assert.NoError(t, err)
assert.Nil(t, pr.MergedCommitID)
assert.False(t, pr.HasMerged)
state := StateClosed
pr, _, err = c.EditPullRequest(user.UserName, repoName, pullConflict.Index, EditPullRequestOption{
Title: "confl",
State: &state,
})
assert.NoError(t, err)
assert.EqualValues(t, state, pr.State)
pulls, _, err = c.ListRepoPullRequests(user.UserName, repoName, ListPullRequestsOptions{
State: StateClosed,
Sort: "leastupdate",
})
assert.NoError(t, err)
assert.Len(t, pulls, 2)
}
func preparePullTest(t *testing.T, c *Client, repoName, forkOrg string) bool {
_, _ = c.DeleteRepo(forkOrg, repoName)
_, _ = c.DeleteRepo(c.username, repoName)
_, _ = c.DeleteOrg(forkOrg)
origRepo, err := createTestRepo(t, repoName, c)
if !assert.NoError(t, err) {
return false
}
org, _, err := c.CreateOrg(CreateOrgOption{Name: forkOrg})
assert.NoError(t, err)
forkRepo, _, err := c.CreateFork(origRepo.Owner.UserName, origRepo.Name, CreateForkOption{Organization: &org.UserName})
assert.NoError(t, err)
assert.NotNil(t, forkRepo)
mainLicense, _, err := c.GetContents(forkRepo.Owner.UserName, forkRepo.Name, "main", "LICENSE")
if !assert.NoError(t, err) || !assert.NotNil(t, mainLicense) {
return false
}
updatedFile, _, err := c.UpdateFile(forkRepo.Owner.UserName, forkRepo.Name, "LICENSE", UpdateFileOptions{
FileOptions: FileOptions{
Message: "Overwrite",
BranchName: "main",
NewBranchName: "overwrite_licence",
},
SHA: mainLicense.SHA,
Content: "Tk9USElORyBJUyBIRVJFIEFOWU1PUkUKSUYgWU9VIExJS0UgVE8gRklORCBTT01FVEhJTkcKV0FJVCBGT1IgVEhFIEZVVFVSRQo=",
})
if !assert.NoError(t, err) || !assert.NotNil(t, updatedFile) {
return false
}
newFile, _, err := c.CreateFile(forkRepo.Owner.UserName, forkRepo.Name, "WOW-file", CreateFileOptions{
Content: "QSBuZXcgRmlsZQo=",
FileOptions: FileOptions{
Message: "creat a new file",
BranchName: "main",
NewBranchName: "new_file",
},
})
if !assert.NoError(t, err) || !assert.NotNil(t, newFile) {
return false
}
conflictFile1, _, err := c.CreateFile(origRepo.Owner.UserName, origRepo.Name, "bad-file", CreateFileOptions{
Content: "U3RhcnQgQ29uZmxpY3QK",
FileOptions: FileOptions{
Message: "Start Conflict",
BranchName: "main",
},
})
if !assert.NoError(t, err) || !assert.NotNil(t, conflictFile1) {
return false
}
conflictFile2, _, err := c.CreateFile(forkRepo.Owner.UserName, forkRepo.Name, "bad-file", CreateFileOptions{
Content: "V2lsbEhhdmUgQ29uZmxpY3QK",
FileOptions: FileOptions{
Message: "creat a new file witch will conflict",
BranchName: "main",
NewBranchName: "will_conflict",
},
})
if !assert.NoError(t, err) || !assert.NotNil(t, conflictFile2) {
return false
}
return true
}

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -1,117 +0,0 @@
// 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
import (
"fmt"
"log"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRelease(t *testing.T) {
log.Println("== TestRelease ==")
c := newTestClient()
repo, _ := createTestRepo(t, "ReleaseTests", c)
// ListReleases
rl, _, err := c.ListReleases(repo.Owner.UserName, repo.Name, ListReleasesOptions{})
assert.NoError(t, err)
assert.Len(t, rl, 0)
// CreateRelease
r, _, err := c.CreateRelease(repo.Owner.UserName, repo.Name, CreateReleaseOption{
TagName: "awesome",
Target: "main",
Title: "Release 1",
Note: "yes it's awesome",
IsDraft: true,
IsPrerelease: true,
})
assert.NoError(t, err)
assert.EqualValues(t, "awesome", r.TagName)
assert.EqualValues(t, true, r.IsPrerelease)
assert.EqualValues(t, true, r.IsDraft)
assert.EqualValues(t, "Release 1", r.Title)
assert.EqualValues(t, fmt.Sprintf("%s/api/v1/repos/%s/releases/%d", c.url, repo.FullName, r.ID), r.URL)
assert.EqualValues(t, "main", r.Target)
assert.EqualValues(t, "yes it's awesome", r.Note)
assert.EqualValues(t, c.username, r.Publisher.UserName)
rl, _, _ = c.ListReleases(repo.Owner.UserName, repo.Name, ListReleasesOptions{})
assert.Len(t, rl, 1)
// GetRelease
r2, _, err := c.GetRelease(repo.Owner.UserName, repo.Name, r.ID)
assert.NoError(t, err)
assert.EqualValues(t, r, r2)
r2, _, err = c.GetReleaseByTag(repo.Owner.UserName, repo.Name, r.TagName)
assert.NoError(t, err)
assert.EqualValues(t, r, r2)
// ListRelease without pre-releases
tr := true
rl, _, err = c.ListReleases(repo.Owner.UserName, repo.Name, ListReleasesOptions{
IsPreRelease: &tr,
})
assert.NoError(t, err)
assert.Len(t, rl, 1) // created release is a pre-release
// test fallback
r2, _, err = c.fallbackGetReleaseByTag(repo.Owner.UserName, repo.Name, r.TagName)
assert.NoError(t, err)
assert.EqualValues(t, r, r2)
// EditRelease
r2, _, err = c.EditRelease(repo.Owner.UserName, repo.Name, r.ID, EditReleaseOption{
Title: "Release Awesome",
Note: "",
IsDraft: OptionalBool(false),
IsPrerelease: OptionalBool(false),
})
assert.NoError(t, err)
assert.EqualValues(t, r.Target, r2.Target)
assert.EqualValues(t, false, r2.IsDraft)
assert.EqualValues(t, false, r2.IsPrerelease)
assert.EqualValues(t, r.Note, r2.Note)
// DeleteRelease
_, err = c.DeleteRelease(repo.Owner.UserName, repo.Name, r.ID)
assert.NoError(t, err)
rl, _, _ = c.ListReleases(repo.Owner.UserName, repo.Name, ListReleasesOptions{})
assert.Len(t, rl, 0)
// CreateRelease
_, _, err = c.CreateRelease(repo.Owner.UserName, repo.Name, CreateReleaseOption{
TagName: "aNewReleaseTag",
Target: "main",
Title: "Title of aNewReleaseTag",
})
assert.NoError(t, err)
// DeleteReleaseByTag
_, err = c.DeleteReleaseByTag(repo.Owner.UserName, repo.Name, "aNewReleaseTag")
assert.NoError(t, err)
rl, _, _ = c.ListReleases(repo.Owner.UserName, repo.Name, ListReleasesOptions{})
assert.Len(t, rl, 0)
_, err = c.DeleteReleaseByTag(repo.Owner.UserName, repo.Name, "aNewReleaseTag")
assert.Error(t, err)
// Test Response if try to get not existing release
_, resp, err := c.GetRelease(repo.Owner.UserName, repo.Name, 1234)
assert.Error(t, err)
if assert.NotNil(t, resp) {
assert.EqualValues(t, 404, resp.StatusCode)
}
_, resp, err = c.GetReleaseByTag(repo.Owner.UserName, repo.Name, "not_here")
assert.Error(t, err)
if assert.NotNil(t, resp) {
assert.EqualValues(t, 404, resp.StatusCode)
}
_, resp, err = c.fallbackGetReleaseByTag(repo.Owner.UserName, repo.Name, "not_here")
assert.Error(t, err)
if assert.NotNil(t, resp) {
assert.EqualValues(t, 404, resp.StatusCode)
}
}

View File

@@ -3,7 +3,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -3,7 +3,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -1,175 +0,0 @@
// 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
import (
"log"
"sort"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRepoBranches(t *testing.T) {
log.Println("== TestRepoBranches ==")
c := newTestClient()
repoName := "branches"
repo := prepareBranchTest(t, c, repoName)
if repo == nil {
return
}
bl, _, err := c.ListRepoBranches(repo.Owner.UserName, repo.Name, ListRepoBranchesOptions{})
assert.NoError(t, err)
assert.Len(t, bl, 3)
sort.Slice(bl, func(i, j int) bool {
return bl[i].Name < bl[j].Name
})
assert.EqualValues(t, "feature", bl[0].Name)
assert.EqualValues(t, "main", bl[1].Name)
assert.EqualValues(t, "update", bl[2].Name)
b, _, err := c.GetRepoBranch(repo.Owner.UserName, repo.Name, "update")
assert.NoError(t, err)
assert.EqualValues(t, bl[2].Commit.ID, b.Commit.ID)
assert.EqualValues(t, bl[2].Commit.Added, b.Commit.Added)
s, _, err := c.DeleteRepoBranch(repo.Owner.UserName, repo.Name, "main")
assert.NoError(t, err)
assert.False(t, s)
s, _, err = c.DeleteRepoBranch(repo.Owner.UserName, repo.Name, "feature")
assert.NoError(t, err)
assert.True(t, s)
bl, _, err = c.ListRepoBranches(repo.Owner.UserName, repo.Name, ListRepoBranchesOptions{})
assert.NoError(t, err)
assert.Len(t, bl, 2)
b, _, err = c.GetRepoBranch(repo.Owner.UserName, repo.Name, "feature")
assert.Error(t, err)
assert.Nil(t, b)
bNew, _, err := c.CreateBranch(repo.Owner.UserName, repo.Name, CreateBranchOption{BranchName: "NewBranch"})
assert.NoError(t, err)
b, _, err = c.GetRepoBranch(repo.Owner.UserName, repo.Name, bNew.Name)
assert.NoError(t, err)
assert.EqualValues(t, bNew, b)
}
func TestRepoBranchProtection(t *testing.T) {
log.Println("== TestRepoBranchProtection ==")
c := newTestClient()
repoName := "BranchProtection"
repo := prepareBranchTest(t, c, repoName)
if repo == nil {
return
}
assert.NotNil(t, repo)
// ListBranchProtections
bpl, _, err := c.ListBranchProtections(repo.Owner.UserName, repo.Name, ListBranchProtectionsOptions{})
assert.NoError(t, err)
assert.Len(t, bpl, 0)
// CreateBranchProtection
bp, _, err := c.CreateBranchProtection(repo.Owner.UserName, repo.Name, CreateBranchProtectionOption{
BranchName: "main",
EnablePush: true,
EnablePushWhitelist: true,
PushWhitelistUsernames: []string{"test01"},
EnableMergeWhitelist: true,
MergeWhitelistUsernames: []string{"test01"},
BlockOnOutdatedBranch: true,
})
assert.NoError(t, err)
assert.EqualValues(t, "main", bp.BranchName)
assert.EqualValues(t, false, bp.EnableStatusCheck)
assert.EqualValues(t, true, bp.EnablePush)
assert.EqualValues(t, true, bp.EnablePushWhitelist)
assert.EqualValues(t, []string{"test01"}, bp.PushWhitelistUsernames)
bp, _, err = c.CreateBranchProtection(repo.Owner.UserName, repo.Name, CreateBranchProtectionOption{
BranchName: "update",
EnablePush: false,
EnableMergeWhitelist: true,
MergeWhitelistUsernames: []string{"test01"},
})
assert.NoError(t, err)
assert.NotNil(t, bp)
bpl, _, err = c.ListBranchProtections(repo.Owner.UserName, repo.Name, ListBranchProtectionsOptions{})
assert.NoError(t, err)
assert.Len(t, bpl, 2)
// GetBranchProtection
bp, _, err = c.GetBranchProtection(repo.Owner.UserName, repo.Name, bpl[0].BranchName)
assert.NoError(t, err)
assert.EqualValues(t, bpl[0], bp)
// EditBranchProtection
bp, _, err = c.EditBranchProtection(repo.Owner.UserName, repo.Name, bpl[0].BranchName, EditBranchProtectionOption{
EnablePush: OptionalBool(false),
EnablePushWhitelist: OptionalBool(false),
PushWhitelistUsernames: nil,
RequiredApprovals: OptionalInt64(1),
EnableApprovalsWhitelist: OptionalBool(true),
ApprovalsWhitelistUsernames: []string{"test01"},
})
assert.NoError(t, err)
assert.NotEqual(t, bpl[0], bp)
assert.EqualValues(t, bpl[0].BranchName, bp.BranchName)
assert.EqualValues(t, bpl[0].EnableMergeWhitelist, bp.EnableMergeWhitelist)
assert.EqualValues(t, bpl[0].Created, bp.Created)
// DeleteBranchProtection
_, err = c.DeleteBranchProtection(repo.Owner.UserName, repo.Name, bpl[1].BranchName)
assert.NoError(t, err)
bpl, _, err = c.ListBranchProtections(repo.Owner.UserName, repo.Name, ListBranchProtectionsOptions{})
assert.NoError(t, err)
assert.Len(t, bpl, 1)
}
func prepareBranchTest(t *testing.T, c *Client, repoName string) *Repository {
origRepo, err := createTestRepo(t, repoName, c)
if !assert.NoError(t, err) {
return nil
}
mainLicense, _, err := c.GetContents(origRepo.Owner.UserName, origRepo.Name, "main", "README.md")
if !assert.NoError(t, err) || !assert.NotNil(t, mainLicense) {
return nil
}
updatedFile, _, err := c.UpdateFile(origRepo.Owner.UserName, origRepo.Name, "README.md", UpdateFileOptions{
FileOptions: FileOptions{
Message: "update it",
BranchName: "main",
NewBranchName: "update",
},
SHA: mainLicense.SHA,
Content: "Tk9USElORyBJUyBIRVJFIEFOWU1PUkUKSUYgWU9VIExJS0UgVE8gRklORCBTT01FVEhJTkcKV0FJVCBGT1IgVEhFIEZVVFVSRQo=",
})
if !assert.NoError(t, err) || !assert.NotNil(t, updatedFile) {
return nil
}
newFile, _, err := c.CreateFile(origRepo.Owner.UserName, origRepo.Name, "WOW-file", CreateFileOptions{
Content: "QSBuZXcgRmlsZQo=",
FileOptions: FileOptions{
Message: "creat a new file",
BranchName: "main",
NewBranchName: "feature",
},
})
if !assert.NoError(t, err) || !assert.NotNil(t, newFile) {
return nil
}
return origRepo
}

View File

@@ -3,7 +3,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -1,80 +0,0 @@
// Copyright 2021 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
import (
"log"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRepoCollaborator(t *testing.T) {
log.Println("== TestRepoCollaborator ==")
c := newTestClient()
repo, _ := createTestRepo(t, "RepoCollaborators", c)
createTestUser(t, "ping", c)
createTestUser(t, "pong", c)
defer func() {
_, err := c.AdminDeleteUser("ping")
assert.NoError(t, err)
_, err = c.AdminDeleteUser("pong")
assert.NoError(t, err)
}()
collaborators, _, err := c.ListCollaborators(repo.Owner.UserName, repo.Name, ListCollaboratorsOptions{})
assert.NoError(t, err)
assert.Len(t, collaborators, 0)
mode := AccessModeAdmin
resp, err := c.AddCollaborator(repo.Owner.UserName, repo.Name, "ping", AddCollaboratorOption{Permission: &mode})
assert.NoError(t, err)
assert.EqualValues(t, 204, resp.StatusCode)
permissonPing, resp, err := c.CollaboratorPermission(repo.Owner.UserName, repo.Name, "ping")
assert.NoError(t, err)
assert.EqualValues(t, 200, resp.StatusCode)
assert.EqualValues(t, AccessModeAdmin, permissonPing.Permission)
assert.EqualValues(t, "ping", permissonPing.User.UserName)
mode = AccessModeRead
_, err = c.AddCollaborator(repo.Owner.UserName, repo.Name, "pong", AddCollaboratorOption{Permission: &mode})
assert.NoError(t, err)
permissonPong, resp, err := c.CollaboratorPermission(repo.Owner.UserName, repo.Name, "pong")
assert.NoError(t, err)
assert.EqualValues(t, 200, resp.StatusCode)
assert.EqualValues(t, AccessModeRead, permissonPong.Permission)
assert.EqualValues(t, "pong", permissonPong.User.UserName)
collaborators, _, err = c.ListCollaborators(repo.Owner.UserName, repo.Name, ListCollaboratorsOptions{})
assert.NoError(t, err)
assert.Len(t, collaborators, 2)
assert.EqualValues(t, []string{"ping", "pong"}, userToStringSlice(collaborators))
reviewers, _, err := c.GetReviewers(repo.Owner.UserName, repo.Name)
assert.NoError(t, err)
assert.Len(t, reviewers, 3)
assert.EqualValues(t, []string{"ping", "pong", "test01"}, userToStringSlice(reviewers))
assignees, _, err := c.GetAssignees(repo.Owner.UserName, repo.Name)
assert.NoError(t, err)
assert.Len(t, assignees, 2)
assert.EqualValues(t, []string{"ping", "test01"}, userToStringSlice(assignees))
resp, err = c.DeleteCollaborator(repo.Owner.UserName, repo.Name, "ping")
assert.NoError(t, err)
assert.EqualValues(t, 204, resp.StatusCode)
collaborators, _, err = c.ListCollaborators(repo.Owner.UserName, repo.Name, ListCollaboratorsOptions{})
assert.NoError(t, err)
assert.Len(t, collaborators, 1)
permissonNotExists, resp, err := c.CollaboratorPermission(repo.Owner.UserName, repo.Name, "user_that_not_exists")
assert.Error(t, err)
assert.EqualValues(t, 404, resp.StatusCode)
assert.Nil(t, permissonNotExists)
}

View File

@@ -3,7 +3,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"fmt"

View File

@@ -1,60 +0,0 @@
// 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
import (
"encoding/base64"
"log"
"testing"
"github.com/stretchr/testify/assert"
)
func TestListRepoCommits(t *testing.T) {
log.Println("== TestListRepoCommits ==")
c := newTestClient()
repo, err := createTestRepo(t, "ListRepoCommits", c)
assert.NoError(t, err)
l, _, err := c.ListRepoCommits(repo.Owner.UserName, repo.Name, ListCommitOptions{})
assert.NoError(t, err)
assert.Len(t, l, 1)
assert.EqualValues(t, "Initial commit\n", l[0].RepoCommit.Message)
assert.EqualValues(t, "gpg.error.not_signed_commit", l[0].RepoCommit.Verification.Reason)
assert.EqualValues(t, 100, l[0].Stats.Additions)
}
func TestGetCommitDiffOrPatch(t *testing.T) {
log.Println("== TestGetCommitDiffOrPatch ==")
c := newTestClient()
repo, err := createTestRepo(t, "TestGetCommitDiffOrPatch", c)
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{
Content: base64.StdEncoding.EncodeToString([]byte("But is it?\n")),
FileOptions: FileOptions{
Message: "Ensure people know it's not a license!",
Committer: Identity{
Name: "Sup3rCookie",
Email: "Sup3rCookie@example.com",
},
},
})
assert.NoError(t, err)
// Test the diff output.
diffOutput, _, err := c.GetCommitDiff(repo.Owner.UserName, repo.Name, fileResponse.Commit.SHA)
assert.NoError(t, err)
assert.EqualValues(t, "diff --git a/NOT_A_LICENSE b/NOT_A_LICENSE\nnew file mode 100644\nindex 0000000..f27a20a\n--- /dev/null\n+++ b/NOT_A_LICENSE\n@@ -0,0 +1 @@\n+But is it?\n", string(diffOutput))
// Test the patch output.
patchOutput, _, err := c.GetCommitPatch(repo.Owner.UserName, repo.Name, fileResponse.Commit.SHA)
assert.NoError(t, err)
// Use contains, because we cannot include the first part, because of dates + non-static CommitID..
assert.Contains(t, string(patchOutput), "Subject: [PATCH] Ensure people know it's not a license!\n\n---\n NOT_A_LICENSE | 1 +\n 1 file changed, 1 insertion(+)\n create mode 100644 NOT_A_LICENSE\n\ndiff --git a/NOT_A_LICENSE b/NOT_A_LICENSE\nnew file mode 100644\nindex 0000000..f27a20a\n--- /dev/null\n+++ b/NOT_A_LICENSE\n@@ -0,0 +1 @@\n+But is it?\n")
}

View File

@@ -3,7 +3,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -1,118 +0,0 @@
// 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
import (
"bytes"
"encoding/base64"
"log"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFileCreateUpdateGet(t *testing.T) {
log.Println("== TestFileCRUD ==")
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+#&ä"
newFile, _, err := c.CreateFile(repo.Owner.UserName, repo.Name, testFileName, CreateFileOptions{
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))
updatedFile, _, err := c.UpdateFile(repo.Owner.UserName, repo.Name, testFileName, UpdateFileOptions{
FileOptions: FileOptions{
Message: "add a new line",
},
SHA: newFile.Content.SHA,
Content: "ZmlsZUEKCmFuZCBhIG5ldyBsaW5lCg==",
})
assert.NoError(t, err)
assert.NotNil(t, updatedFile)
file, _, err := c.GetContents(repo.Owner.UserName, repo.Name, "main", testFileName)
assert.NoError(t, err)
assert.EqualValues(t, updatedFile.Content.SHA, file.SHA)
assert.EqualValues(t, &updatedFile.Content.Content, &file.Content)
_, err = c.DeleteFile(repo.Owner.UserName, repo.Name, testFileName, DeleteFileOptions{
FileOptions: FileOptions{
Message: "Delete File " + testFileName,
},
SHA: updatedFile.Content.SHA,
})
assert.NoError(t, err)
_, resp, err := c.GetFile(repo.Owner.UserName, repo.Name, "main", testFileName)
assert.Error(t, err)
assert.EqualValues(t, "The target couldn't be found.", err.Error())
assert.EqualValues(t, 404, resp.StatusCode)
licence, _, err := c.GetContents(repo.Owner.UserName, repo.Name, "", "LICENSE")
assert.NoError(t, err)
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{
FileOptions: FileOptions{
Message: "Overwrite",
BranchName: "main",
NewBranchName: "overwrite-a+/&licence",
},
SHA: licence.SHA,
Content: testContent,
})
assert.NoError(t, err)
assert.NotNil(t, updatedFile)
licenceRawNew, _, err := c.GetFile(repo.Owner.UserName, repo.Name, "overwrite-a+/&licence", "LICENSE")
assert.NoError(t, err)
assert.NotNil(t, licence)
assert.False(t, bytes.Equal(licenceRaw, licenceRawNew))
assert.EqualValues(t, testContent, base64.StdEncoding.EncodeToString(licenceRawNew))
// ListContents in root dir of default branch
dir, resp, err := c.ListContents(repo.Owner.UserName, repo.Name, "", "")
assert.NoError(t, err)
assert.Len(t, dir, 3)
assert.NotNil(t, resp)
// ListContents in not existing dir of default branch
_, resp, err = c.ListContents(repo.Owner.UserName, repo.Name, "", "/hehe/")
assert.Error(t, err)
assert.EqualValues(t, 404, resp.StatusCode)
// ListContents in root dir of not existing branch
_, resp, err = c.ListContents(repo.Owner.UserName, repo.Name, "no-ref-at-all", "")
assert.Error(t, err)
assert.EqualValues(t, 404, resp.StatusCode)
// ListContents try to get file as dir
dir, resp, err = c.ListContents(repo.Owner.UserName, repo.Name, "", "LICENSE")
if assert.Error(t, err) {
assert.EqualValues(t, "expect directory, got file", err.Error())
}
assert.Nil(t, dir)
assert.EqualValues(t, 200, resp.StatusCode)
// GetContents try to get dir as file
file, resp, err = c.GetContents(repo.Owner.UserName, repo.Name, "", "")
if assert.Error(t, err) {
assert.EqualValues(t, "expect file, got directory", err.Error())
}
assert.Nil(t, file)
assert.EqualValues(t, 200, resp.StatusCode)
}

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"encoding/json"

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"fmt"

View File

@@ -1,72 +0,0 @@
// Copyright 2021 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
import (
"log"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRepoStaring(t *testing.T) {
log.Println("== TestRepoStaring ==")
// init user2
c := newTestClient()
user1, _, err := c.GetMyUserInfo()
assert.NoError(t, err)
userA := createTestUser(t, "stargazer_a", c)
userB := createTestUser(t, "stargazer_b", c)
repo, _ := createTestRepo(t, "toStar", c)
if repo == nil {
t.Skip()
}
is, _, err := c.IsRepoStarring(repo.Owner.UserName, repo.Name)
assert.NoError(t, err)
assert.False(t, is)
repos, _, err := c.GetMyStarredRepos()
assert.NoError(t, err)
assert.Len(t, repos, 0)
_, err = c.StarRepo(repo.Owner.UserName, repo.Name)
assert.NoError(t, err)
c.SetSudo(userA.UserName)
_, err = c.StarRepo(repo.Owner.UserName, repo.Name)
assert.NoError(t, err)
c.SetSudo(userB.UserName)
_, err = c.StarRepo(repo.Owner.UserName, repo.Name)
assert.NoError(t, err)
users, _, err := c.ListRepoStargazers(repo.Owner.UserName, repo.Name, ListStargazersOptions{})
assert.NoError(t, err)
assert.Len(t, users, 3)
assert.EqualValues(t, user1.UserName, users[0].UserName)
_, err = c.UnStarRepo(repo.Owner.UserName, repo.Name)
assert.NoError(t, err)
_, err = c.UnStarRepo(repo.Owner.UserName, repo.Name)
assert.NoError(t, err)
c.SetSudo("")
users, _, err = c.ListRepoStargazers(repo.Owner.UserName, repo.Name, ListStargazersOptions{})
assert.NoError(t, err)
assert.Len(t, users, 2)
repos, _, err = c.GetMyStarredRepos()
assert.NoError(t, err)
assert.Len(t, repos, 1)
reposNew, _, err := c.GetStarredRepos(user1.UserName)
assert.NoError(t, err)
assert.Len(t, repos, 1)
assert.EqualValues(t, repos, reposNew)
}

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -1,58 +0,0 @@
// 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
import (
"fmt"
"log"
"testing"
"github.com/stretchr/testify/assert"
)
func TestTags(t *testing.T) {
log.Println("== TestTags ==")
c := newTestClient()
repo, _ := createTestRepo(t, "TestTags", c)
// Create Tags
cTagMSG := "A tag message.\n\n:)"
cTag, resp, err := c.CreateTag(repo.Owner.UserName, repo.Name, CreateTagOption{
TagName: "tag1",
Message: cTagMSG,
Target: "main",
})
assert.NoError(t, err)
assert.EqualValues(t, 201, resp.StatusCode)
assert.EqualValues(t, cTagMSG, cTag.Message)
assert.EqualValues(t, fmt.Sprintf("%s/%s/TestTags/archive/tag1.zip", c.url, c.username), cTag.ZipballURL)
tags, _, err := c.ListRepoTags(repo.Owner.UserName, repo.Name, ListRepoTagsOptions{})
assert.NoError(t, err)
assert.Len(t, tags, 1)
assert.EqualValues(t, cTag, tags[0])
// get tag
gTag, _, err := c.GetTag(repo.Owner.UserName, repo.Name, cTag.Name)
assert.NoError(t, err)
assert.EqualValues(t, cTag, gTag)
aTag, _, err := c.GetAnnotatedTag(repo.Owner.UserName, repo.Name, cTag.ID)
assert.NoError(t, err)
assert.EqualValues(t, cTag.Name, aTag.Tag)
assert.EqualValues(t, cTag.ID, aTag.SHA)
assert.EqualValues(t, fmt.Sprintf("%s/api/v1/repos/%s/TestTags/git/tags/%s", c.url, c.username, cTag.ID), aTag.URL)
assert.EqualValues(t, cTag.Message+"\n", aTag.Message)
assert.EqualValues(t, "commit", aTag.Object.Type)
// DeleteReleaseTag
resp, err = c.DeleteTag(repo.Owner.UserName, repo.Name, "tag1")
assert.NoError(t, err)
assert.EqualValues(t, 204, resp.StatusCode)
tags, _, err = c.ListRepoTags(repo.Owner.UserName, repo.Name, ListRepoTagsOptions{})
assert.NoError(t, err)
assert.Len(t, tags, 0)
}

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"fmt"

View File

@@ -1,74 +0,0 @@
// Copyright 2021 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
import (
"log"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRepoTeamManagement(t *testing.T) {
log.Println("== TestRepoTeamManagement ==")
c := newTestClient()
// prepare for test
clean, repo, err := createTestOrgRepo(t, c, "RepoTeamManagement")
if err != nil {
return
}
defer clean()
if _, err = createTestOrgTeams(t, c, repo.Owner.UserName, "Admins", AccessModeAdmin, []RepoUnitType{RepoUnitCode, RepoUnitIssues, RepoUnitPulls, RepoUnitReleases}); err != nil {
return
}
if _, err = createTestOrgTeams(t, c, repo.Owner.UserName, "CodeManager", AccessModeWrite, []RepoUnitType{RepoUnitCode}); err != nil {
return
}
if _, err = createTestOrgTeams(t, c, repo.Owner.UserName, "IssueManager", AccessModeWrite, []RepoUnitType{RepoUnitIssues, RepoUnitPulls}); err != nil {
return
}
// test
teams, _, err := c.GetRepoTeams(repo.Owner.UserName, repo.Name)
assert.NoError(t, err)
if !assert.Len(t, teams, 1) {
return
}
assert.EqualValues(t, AccessModeOwner, teams[0].Permission)
team, _, err := c.CheckRepoTeam(repo.Owner.UserName, repo.Name, "Admins")
assert.NoError(t, err)
assert.Nil(t, team)
resp, err := c.AddRepoTeam(repo.Owner.UserName, repo.Name, "Admins")
assert.NoError(t, err)
assert.EqualValues(t, 204, resp.StatusCode)
resp, err = c.AddRepoTeam(repo.Owner.UserName, repo.Name, "CodeManager")
assert.NoError(t, err)
assert.EqualValues(t, 204, resp.StatusCode)
resp, err = c.AddRepoTeam(repo.Owner.UserName, repo.Name, "IssueManager")
assert.NoError(t, err)
assert.EqualValues(t, 204, resp.StatusCode)
team, _, err = c.CheckRepoTeam(repo.Owner.UserName, repo.Name, "Admins")
assert.NoError(t, err)
if assert.NotNil(t, team) {
assert.EqualValues(t, "Admins", team.Name)
assert.EqualValues(t, AccessModeAdmin, team.Permission)
}
teams, _, err = c.GetRepoTeams(repo.Owner.UserName, repo.Name)
assert.NoError(t, err)
assert.Len(t, teams, 4)
resp, err = c.RemoveRepoTeam(repo.Owner.UserName, repo.Name, "IssueManager")
assert.NoError(t, err)
assert.EqualValues(t, 204, resp.StatusCode)
team, _, err = c.CheckRepoTeam(repo.Owner.UserName, repo.Name, "IssueManager")
assert.NoError(t, err)
assert.Nil(t, team)
}

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -1,44 +0,0 @@
// Copyright 2021 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
import (
"log"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRepoFromTemplate(t *testing.T) {
log.Println("== TestRepoFromTemplate ==")
c := newTestClient()
repo, err := createTestRepo(t, "TemplateRepo", c)
assert.NoError(t, err)
repo, _, err = c.EditRepo(repo.Owner.UserName, repo.Name, EditRepoOption{Template: OptionalBool(true)})
assert.NoError(t, err)
_, err = c.SetRepoTopics(repo.Owner.UserName, repo.Name, []string{"abc", "def", "ghi"})
assert.NoError(t, err)
newRepo, resp, err := c.CreateRepoFromTemplate(repo.Owner.UserName, repo.Name, CreateRepoFromTemplateOption{
Owner: repo.Owner.UserName,
Name: "repoFromTemplate",
Description: "",
Topics: true,
Labels: true,
})
assert.NoError(t, err)
assert.EqualValues(t, 201, resp.StatusCode)
assert.False(t, newRepo.Template)
labels, _, err := c.ListRepoLabels(repo.Owner.UserName, repo.Name, ListLabelsOptions{})
assert.NoError(t, err)
assert.Len(t, labels, 7)
topics, _, _ := c.ListRepoTopics(repo.Owner.UserName, repo.Name, ListRepoTopicsOptions{})
assert.EqualValues(t, []string{"abc", "def", "ghi"}, topics)
_, err = c.DeleteRepo(repo.Owner.UserName, "repoFromTemplate")
assert.NoError(t, err)
}

View File

@@ -1,202 +0,0 @@
// 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
import (
"bytes"
"io"
"log"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestCreateRepo(t *testing.T) {
log.Println("== TestCreateRepo ==")
c := newTestClient()
user, _, err := c.GetMyUserInfo()
assert.NoError(t, err)
repoName := "test1"
_, _, err = c.GetRepo(user.UserName, repoName)
if err != nil {
repo, _, err := c.CreateRepo(CreateRepoOption{
Name: repoName,
})
assert.NoError(t, err)
assert.NotNil(t, repo)
}
_, err = c.DeleteRepo(user.UserName, repoName)
assert.NoError(t, err)
}
func TestRepoMigrateAndLanguages(t *testing.T) {
log.Println("== TestMigrateRepo ==")
c := newTestClient()
user, _, uErr := c.GetMyUserInfo()
assert.NoError(t, uErr)
_, _, err := c.GetRepo(user.UserName, "sdk-mirror")
if err == nil {
_, _ = c.DeleteRepo(user.UserName, "sdk-mirror")
}
repoM, _, err := c.MigrateRepo(MigrateRepoOption{
CloneAddr: "https://gitea.com/gitea/go-sdk.git",
RepoName: "sdk-mirror",
RepoOwner: user.UserName,
Mirror: true,
Private: false,
Description: "mirror sdk",
})
assert.NoError(t, err)
repoG, _, err := c.GetRepo(repoM.Owner.UserName, repoM.Name)
assert.NoError(t, err)
assert.EqualValues(t, repoM.ID, repoG.ID)
assert.EqualValues(t, "main", repoG.DefaultBranch)
assert.True(t, repoG.Mirror)
assert.False(t, repoG.Empty)
assert.EqualValues(t, 1, repoG.Watchers)
var zeroTime time.Time
assert.NotEqual(t, zeroTime, repoG.MirrorUpdated)
log.Println("== TestRepoLanguages ==")
time.Sleep(time.Second)
lang, _, err := c.GetRepoLanguages(repoM.Owner.UserName, repoM.Name)
assert.NoError(t, err)
assert.Len(t, lang, 2)
assert.True(t, 217441 < lang["Go"])
assert.True(t, 3614 < lang["Makefile"] && 6000 > lang["Makefile"])
}
func TestSearchRepo(t *testing.T) {
log.Println("== TestSearchRepo ==")
c := newTestClient()
repo, err := createTestRepo(t, "RepoSearch1", c)
assert.NoError(t, err)
_, err = c.AddRepoTopic(repo.Owner.UserName, repo.Name, "TestTopic1")
assert.NoError(t, err)
_, err = c.AddRepoTopic(repo.Owner.UserName, repo.Name, "TestTopic2")
assert.NoError(t, err)
repo, err = createTestRepo(t, "RepoSearch2", c)
assert.NoError(t, err)
_, err = c.AddRepoTopic(repo.Owner.UserName, repo.Name, "TestTopic1")
assert.NoError(t, err)
repos, _, err := c.SearchRepos(SearchRepoOptions{
Keyword: "Search1",
KeywordInDescription: true,
})
assert.NoError(t, err)
assert.NotNil(t, repos)
assert.Len(t, repos, 1)
repos, _, err = c.SearchRepos(SearchRepoOptions{
Keyword: "Search",
KeywordInDescription: true,
})
assert.NoError(t, err)
assert.NotNil(t, repos)
assert.Len(t, repos, 2)
repos, _, err = c.SearchRepos(SearchRepoOptions{
Keyword: "TestTopic1",
KeywordInDescription: true,
})
assert.NoError(t, err)
assert.NotNil(t, repos)
assert.Len(t, repos, 2)
repos, _, err = c.SearchRepos(SearchRepoOptions{
Keyword: "TestTopic2",
KeywordInDescription: true,
})
assert.NoError(t, err)
assert.NotNil(t, repos)
assert.Len(t, repos, 1)
_, err = c.DeleteRepo(repo.Owner.UserName, repo.Name)
assert.NoError(t, err)
}
func TestDeleteRepo(t *testing.T) {
log.Println("== TestDeleteRepo ==")
c := newTestClient()
repo, _ := createTestRepo(t, "TestDeleteRepo", c)
_, err := c.DeleteRepo(repo.Owner.UserName, repo.Name)
assert.NoError(t, err)
}
func TestGetArchive(t *testing.T) {
log.Println("== TestGetArchive ==")
c := newTestClient()
repo, _ := createTestRepo(t, "ToDownload", c)
time.Sleep(time.Second / 2)
archive, _, err := c.GetArchive(repo.Owner.UserName, repo.Name, "main", ZipArchive)
assert.NoError(t, err)
assert.True(t, len(archive) > 1500 && len(archive) < 1700)
}
func TestGetArchiveReader(t *testing.T) {
log.Println("== TestGetArchiveReader ==")
c := newTestClient()
repo, _ := createTestRepo(t, "ToDownload", c)
time.Sleep(time.Second / 2)
r, _, err := c.GetArchiveReader(repo.Owner.UserName, repo.Name, "main", ZipArchive)
assert.NoError(t, err)
defer r.Close()
archive := bytes.NewBuffer(nil)
nBytes, err := io.Copy(archive, r)
assert.NoError(t, err)
assert.True(t, nBytes > 1500)
assert.EqualValues(t, nBytes, len(archive.Bytes()))
}
func TestGetRepoByID(t *testing.T) {
log.Println("== TestGetRepoByID ==")
c := newTestClient()
testrepo, _ := createTestRepo(t, "TestGetRepoByID", c)
repo, _, err := c.GetRepoByID(testrepo.ID)
assert.NoError(t, err)
assert.NotNil(t, repo)
assert.EqualValues(t, testrepo.ID, repo.ID)
_, err = c.DeleteRepo(repo.Owner.UserName, repo.Name)
assert.NoError(t, err)
}
// standard func to create a init repo for test routines
func createTestRepo(t *testing.T, name string, c *Client) (*Repository, error) {
user, _, uErr := c.GetMyUserInfo()
assert.NoError(t, uErr)
repo, _, err := c.GetRepo(user.UserName, name)
// We need to check that the received repo is not a
// redirected one, it could be the case that gitea redirect us
// to a new repo(because it e.g. was transferred or renamed).
if err == nil && repo.Owner.UserName == user.UserName {
_, _ = c.DeleteRepo(user.UserName, name)
}
repo, _, err = c.CreateRepo(CreateRepoOption{
Name: name,
Description: "A test Repo: " + name,
AutoInit: true,
Gitignores: "C,C++",
License: "MIT",
Readme: "Default",
IssueLabels: "Default",
Private: false,
})
assert.NoError(t, err)
assert.NotNil(t, repo)
return repo, err
}

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -1,55 +0,0 @@
// 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
import (
"log"
"sort"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRepoTopics(t *testing.T) {
log.Println("== TestRepoTopics ==")
c := newTestClient()
repo, err := createTestRepo(t, "RandomTopic", c)
assert.NoError(t, err)
// Add
_, err = c.AddRepoTopic(repo.Owner.UserName, repo.Name, "best")
assert.NoError(t, err)
_, err = c.AddRepoTopic(repo.Owner.UserName, repo.Name, "git")
assert.NoError(t, err)
_, err = c.AddRepoTopic(repo.Owner.UserName, repo.Name, "gitea")
assert.NoError(t, err)
_, err = c.AddRepoTopic(repo.Owner.UserName, repo.Name, "drone")
assert.NoError(t, err)
// Get List
tl, _, err := c.ListRepoTopics(repo.Owner.UserName, repo.Name, ListRepoTopicsOptions{})
assert.NoError(t, err)
assert.Len(t, tl, 4)
// Del
_, err = c.DeleteRepoTopic(repo.Owner.UserName, repo.Name, "drone")
assert.NoError(t, err)
_, err = c.DeleteRepoTopic(repo.Owner.UserName, repo.Name, "best")
assert.NoError(t, err)
tl, _, err = c.ListRepoTopics(repo.Owner.UserName, repo.Name, ListRepoTopicsOptions{})
assert.NoError(t, err)
assert.Len(t, tl, 2)
// Set List
newTopics := []string{"analog", "digital", "cat"}
_, err = c.SetRepoTopics(repo.Owner.UserName, repo.Name, newTopics)
assert.NoError(t, err)
tl, _, _ = c.ListRepoTopics(repo.Owner.UserName, repo.Name, ListRepoTopicsOptions{})
assert.Len(t, tl, 3)
sort.Strings(tl)
sort.Strings(newTopics)
assert.EqualValues(t, newTopics, tl)
}

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

View File

@@ -1,40 +0,0 @@
// 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
import (
"log"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRepoTransfer(t *testing.T) {
log.Printf("== TestRepoTransfer ==")
c := newTestClient()
org, _, err := c.AdminCreateOrg(c.username, CreateOrgOption{Name: "TransferOrg"})
assert.NoError(t, err)
repo, err := createTestRepo(t, "ToMove", c)
assert.NoError(t, err)
newRepo, _, err := c.TransferRepo(c.username, repo.Name, TransferRepoOption{NewOwner: org.UserName})
assert.NoError(t, err) // admin transfer repository will execute immediately but not set as pendding.
assert.NotNil(t, newRepo)
assert.EqualValues(t, "ToMove", newRepo.Name)
repo, err = createTestRepo(t, "ToMove", c)
assert.NoError(t, err)
_, resp, err := c.TransferRepo(c.username, repo.Name, TransferRepoOption{NewOwner: org.UserName})
assert.EqualValues(t, 422, resp.StatusCode)
assert.Error(t, err)
_, err = c.DeleteRepo(repo.Owner.UserName, repo.Name)
assert.NoError(t, err)
_, err = c.DeleteRepo(newRepo.Owner.UserName, newRepo.Name)
assert.NoError(t, err)
_, err = c.DeleteOrg(org.UserName)
assert.NoError(t, err)
}

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"fmt"

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"fmt"

View File

@@ -1,52 +0,0 @@
// 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
import (
"log"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRepoWatch(t *testing.T) {
log.Printf("== TestRepoWatch ==")
c := newTestClient()
rawVersion, _, err := c.ServerVersion()
assert.NoError(t, err)
assert.True(t, true, rawVersion != "")
repo1, _ := createTestRepo(t, "TestRepoWatch_1", c)
repo2, _ := createTestRepo(t, "TestRepoWatch_2", c)
assert.NotEqual(t, repo1, repo2)
// GetWatchedRepos
wl, _, err := c.GetWatchedRepos("test01")
assert.NoError(t, err)
assert.NotNil(t, wl)
maxcount := len(wl)
// GetMyWatchedRepos
wl, _, err = c.GetMyWatchedRepos()
assert.NoError(t, err)
assert.Len(t, wl, maxcount)
// CheckRepoWatch
isWatching, _, err := c.CheckRepoWatch(repo1.Owner.UserName, repo1.Name)
assert.NoError(t, err)
assert.True(t, isWatching)
// UnWatchRepo
_, err = c.UnWatchRepo(repo1.Owner.UserName, repo1.Name)
assert.NoError(t, err)
isWatching, _, _ = c.CheckRepoWatch(repo1.Owner.UserName, repo1.Name)
assert.False(t, isWatching)
// WatchRepo
_, err = c.WatchRepo(repo1.Owner.UserName, repo1.Name)
assert.NoError(t, err)
isWatching, _, _ = c.CheckRepoWatch(repo1.Owner.UserName, repo1.Name)
assert.True(t, isWatching)
}

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import "time"

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
// GlobalUISettings represent the global ui settings of a gitea instance witch is exposed by API
type GlobalUISettings struct {

View File

@@ -1,50 +0,0 @@
// 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
import (
"log"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetGlobalSettings(t *testing.T) {
log.Println("== TestGetGlobalSettings ==")
c := newTestClient()
uiSettings, _, err := c.GetGlobalUISettings()
assert.NoError(t, err)
expectedAllowedReactions := []string{"+1", "-1", "laugh", "hooray", "confused", "heart", "rocket", "eyes"}
assert.ElementsMatch(t, expectedAllowedReactions, uiSettings.AllowedReactions)
repoSettings, _, err := c.GetGlobalRepoSettings()
assert.NoError(t, err)
assert.EqualValues(t, &GlobalRepoSettings{
HTTPGitDisabled: false,
MirrorsDisabled: false,
LFSDisabled: true,
}, repoSettings)
apiSettings, _, err := c.GetGlobalAPISettings()
assert.NoError(t, err)
assert.EqualValues(t, &GlobalAPISettings{
MaxResponseItems: 50,
DefaultPagingNum: 30,
DefaultGitTreesPerPage: 1000,
DefaultMaxBlobSize: 10485760,
}, apiSettings)
attachSettings, _, err := c.GetGlobalAttachmentSettings()
assert.NoError(t, err)
if assert.NotEmpty(t, attachSettings.AllowedTypes) {
attachSettings.AllowedTypes = ""
}
assert.EqualValues(t, &GlobalAttachmentSettings{
Enabled: true,
MaxSize: 4,
MaxFiles: 5,
}, attachSettings)
}

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitea
package sdk
import (
"bytes"

Some files were not shown because too many files have changed in this diff Show More