From eee23415d854e0b5e17b3f16eaf0191996ca0e43 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 9 Jan 2020 22:32:01 +0100 Subject: [PATCH 01/10] * Update Dates * Fix ListIssueOption --- gitea/client.go | 3 ++- gitea/issue.go | 53 +++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/gitea/client.go b/gitea/client.go index 2877f86..3c72db5 100644 --- a/gitea/client.go +++ b/gitea/client.go @@ -1,4 +1,5 @@ // Copyright 2014 The Gogs Authors. All rights reserved. +// 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. @@ -21,7 +22,7 @@ func Version() string { return "0.12.3" } -// Client represents a Gogs API client. +// Client represents a Gitea API client. type Client struct { url string accessToken string diff --git a/gitea/issue.go b/gitea/issue.go index f1711f6..90f04c8 100644 --- a/gitea/issue.go +++ b/gitea/issue.go @@ -9,6 +9,7 @@ import ( "bytes" "encoding/json" "fmt" + "net/url" "time" ) @@ -44,26 +45,66 @@ type Issue struct { // ListIssueOption list issue options type ListIssueOption struct { - Page int - State string + Page int + // open, closed, all + State string + Labels []string + KeyWord string +} + +func (opt *ListIssueOption) QueryEncode() string { + query := make(url.Values) + if opt.Page > 0 { + query.Add("page", fmt.Sprintf("%d", opt.Page)) + } + if len(opt.State) > 0 { + query.Add("state", opt.State) + } + + if opt.Page > 0 { + query.Add("page", fmt.Sprintf("%d", opt.Page)) + } + if len(opt.State) > 0 { + query.Add("state", opt.State) + } + if len(opt.Labels) > 0 { + var lq string + for _, l := range opt.Labels { + if len(lq) > 0 { + lq += "," + } + lq += l + } + query.Add("labels", lq) + } + if len(opt.KeyWord) > 0 { + query.Add("q", opt.KeyWord) + } + + return query.Encode() } // ListIssues returns all issues assigned the authenticated user func (c *Client) ListIssues(opt ListIssueOption) ([]*Issue, error) { + link, _ := url.Parse("​/repos​/issues​/search") issues := make([]*Issue, 0, 10) - return issues, c.getParsedResponse("GET", fmt.Sprintf("/issues?page=%d", opt.Page), nil, nil, &issues) + link.RawQuery = opt.QueryEncode() + return issues, c.getParsedResponse("GET", link.String(), nil, nil, &issues) } // ListUserIssues returns all issues assigned to the authenticated user func (c *Client) ListUserIssues(opt ListIssueOption) ([]*Issue, error) { - issues := make([]*Issue, 0, 10) - return issues, c.getParsedResponse("GET", fmt.Sprintf("/user/issues?page=%d", opt.Page), nil, nil, &issues) + //issues := make([]*Issue, 0, 10) + //return issues, c.getParsedResponse("GET", fmt.Sprintf("/user/issues?page=%d", opt.Page), nil, nil, &issues) + return nil, fmt.Errorf("This API is not implemented jet") } // ListRepoIssues returns all issues for a given repository func (c *Client) ListRepoIssues(owner, repo string, opt ListIssueOption) ([]*Issue, error) { + link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/issues", owner, repo)) issues := make([]*Issue, 0, 10) - return issues, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues?page=%d", owner, repo, opt.Page), nil, nil, &issues) + link.RawQuery = opt.QueryEncode() + return issues, c.getParsedResponse("GET", link.String(), nil, nil, &issues) } // GetIssue returns a single issue for a given repository -- 2.40.1 From 1a1f79d4b6e4fd504059da8c88366c04422d7121 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 16 Jan 2020 20:35:32 +0100 Subject: [PATCH 02/10] add workaround --- gitea/issue.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/gitea/issue.go b/gitea/issue.go index 90f04c8..be1420a 100644 --- a/gitea/issue.go +++ b/gitea/issue.go @@ -94,9 +94,23 @@ func (c *Client) ListIssues(opt ListIssueOption) ([]*Issue, error) { // ListUserIssues returns all issues assigned to the authenticated user func (c *Client) ListUserIssues(opt ListIssueOption) ([]*Issue, error) { - //issues := make([]*Issue, 0, 10) - //return issues, c.getParsedResponse("GET", fmt.Sprintf("/user/issues?page=%d", opt.Page), nil, nil, &issues) - return nil, fmt.Errorf("This API is not implemented jet") + // WARNING: "/user/issues?page=%d" API is not implemented jet! + allIssues, err := c.ListIssues(opt) + if err != nil { + return nil, err + } + user, err := c.GetMyUserInfo() + if err != nil { + return nil, err + } + // Workaround: client sort out non user related issues + issues := make([]*Issue, 0, 10) + for _, i := range allIssues { + if i.ID == user.ID { + issues = append(issues, i) + } + } + return issues, nil } // ListRepoIssues returns all issues for a given repository -- 2.40.1 From 41b115801d7d612c705b7b7147fecdb1c8c63075 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 25 Jan 2020 01:57:43 +0100 Subject: [PATCH 03/10] introduce "createTestRepo" a standad func to create a repo for testing --- gitea/issue.go | 2 +- gitea/main_test.go | 22 ++++++++++++++++++++++ gitea/pull_test.go | 14 ++------------ 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/gitea/issue.go b/gitea/issue.go index be1420a..0626e41 100644 --- a/gitea/issue.go +++ b/gitea/issue.go @@ -99,7 +99,7 @@ func (c *Client) ListUserIssues(opt ListIssueOption) ([]*Issue, error) { if err != nil { return nil, err } - user, err := c.GetMyUserInfo() + user, err := c.GetMyUserInfo() if err != nil { return nil, err } diff --git a/gitea/main_test.go b/gitea/main_test.go index e04fced..35061c9 100644 --- a/gitea/main_test.go +++ b/gitea/main_test.go @@ -16,6 +16,8 @@ import ( "runtime" "strconv" "testing" + + "github.com/stretchr/testify/assert" ) func getGiteaURL() string { @@ -154,3 +156,23 @@ func TestMain(m *testing.M) { exitCode := m.Run() os.Exit(exitCode) } + +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) + if err != nil { + repo, err = c.CreateRepo(CreateRepoOption{ + Name: name, + Description: "A test Repo: " + name, + AutoInit: true, + Gitignores: "C,C++", + License: "MIT", + Readme: "Default", + Private: false, + }) + assert.NoError(t, err) + assert.NotNil(t, repo) + } + return repo, err +} diff --git a/gitea/pull_test.go b/gitea/pull_test.go index f1f3dcd..d1ab7a7 100644 --- a/gitea/pull_test.go +++ b/gitea/pull_test.go @@ -16,19 +16,9 @@ func TestPull(t *testing.T) { assert.NoError(t, err) var repoName = "repo_pull_test" - repo, err := c.GetRepo(user.UserName, repoName) + _, err = createTestRepo(t, repoName, c) if err != nil { - repo, err = c.CreateRepo(CreateRepoOption{ - Name: repoName, - Description: "PullTests", - AutoInit: true, - Gitignores: "C,C++", - License: "MIT", - Readme: "Default", - Private: false, - }) - assert.NoError(t, err) - assert.NotNil(t, repo) + return } // ListRepoPullRequests list PRs of one repository -- 2.40.1 From 9f776565c156333e68ed4e6f4a293df372c73438 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 25 Jan 2020 02:07:53 +0100 Subject: [PATCH 04/10] more Repo Tests and mv createTestRepo --- gitea/main_test.go | 22 ---------------------- gitea/repo_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/gitea/main_test.go b/gitea/main_test.go index 35061c9..e04fced 100644 --- a/gitea/main_test.go +++ b/gitea/main_test.go @@ -16,8 +16,6 @@ import ( "runtime" "strconv" "testing" - - "github.com/stretchr/testify/assert" ) func getGiteaURL() string { @@ -156,23 +154,3 @@ func TestMain(m *testing.M) { exitCode := m.Run() os.Exit(exitCode) } - -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) - if err != nil { - repo, err = c.CreateRepo(CreateRepoOption{ - Name: name, - Description: "A test Repo: " + name, - AutoInit: true, - Gitignores: "C,C++", - License: "MIT", - Readme: "Default", - Private: false, - }) - assert.NoError(t, err) - assert.NotNil(t, repo) - } - return repo, err -} diff --git a/gitea/repo_test.go b/gitea/repo_test.go index bbd8d9e..eb26ca6 100644 --- a/gitea/repo_test.go +++ b/gitea/repo_test.go @@ -5,6 +5,7 @@ package gitea import ( + "log" "testing" "github.com/stretchr/testify/assert" @@ -28,3 +29,31 @@ func TestCreateRepo(t *testing.T) { err = c.DeleteRepo(user.UserName, repoName) assert.NoError(t, err) } + +func TestDeleteRepo(t *testing.T) { + log.Println("== TestDeleteRepo ==") + c := newTestClient() + repo, _ := createTestRepo(t, "TestDeleteRepo", c) + assert.NoError(t, c.DeleteRepo(repo.Owner.UserName, repo.Name)) +} + +// 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) + if err != nil { + repo, err = c.CreateRepo(CreateRepoOption{ + Name: name, + Description: "A test Repo: " + name, + AutoInit: true, + Gitignores: "C,C++", + License: "MIT", + Readme: "Default", + Private: false, + }) + assert.NoError(t, err) + assert.NotNil(t, repo) + } + return repo, err +} -- 2.40.1 From a6ccd5b81d2d368db032009a917b8942aa46e246 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 25 Jan 2020 03:44:29 +0100 Subject: [PATCH 05/10] impruve --- gitea/issue.go | 2 +- gitea/repo_test.go | 29 ++++++++++++++++------------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/gitea/issue.go b/gitea/issue.go index 0626e41..c4a5a4c 100644 --- a/gitea/issue.go +++ b/gitea/issue.go @@ -94,7 +94,7 @@ func (c *Client) ListIssues(opt ListIssueOption) ([]*Issue, error) { // ListUserIssues returns all issues assigned to the authenticated user func (c *Client) ListUserIssues(opt ListIssueOption) ([]*Issue, error) { - // WARNING: "/user/issues?page=%d" API is not implemented jet! + // WARNING: "/user/issues" API is not implemented jet! allIssues, err := c.ListIssues(opt) if err != nil { return nil, err diff --git a/gitea/repo_test.go b/gitea/repo_test.go index eb26ca6..9bca709 100644 --- a/gitea/repo_test.go +++ b/gitea/repo_test.go @@ -41,19 +41,22 @@ func TestDeleteRepo(t *testing.T) { 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) - if err != nil { - repo, err = c.CreateRepo(CreateRepoOption{ - Name: name, - Description: "A test Repo: " + name, - AutoInit: true, - Gitignores: "C,C++", - License: "MIT", - Readme: "Default", - Private: false, - }) - assert.NoError(t, err) - assert.NotNil(t, repo) + _, err := c.GetRepo(user.UserName, name) + if err == nil { + _ = 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 } -- 2.40.1 From d9fd9293c2b90a596167a1c7f2cc17e7329d06e3 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 25 Jan 2020 03:44:41 +0100 Subject: [PATCH 06/10] add Issue Tests --- gitea/issue_test.go | 90 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 gitea/issue_test.go diff --git a/gitea/issue_test.go b/gitea/issue_test.go new file mode 100644 index 0000000..6016d8f --- /dev/null +++ b/gitea/issue_test.go @@ -0,0 +1,90 @@ +// 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) + listIssues(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) + + createOne := func(title, body string, assignees []string, deadline *time.Time, milestone int64, labels []int64, closed, shouldFail bool) { + issue, e := c.CreateIssue(user.UserName, repo.Name, CreateIssueOption{ + Title: title, + Body: body, + Assignees: assignees, + Deadline: deadline, + Milestone: milestone, + Labels: labels, + Closed: closed, + }) + if shouldFail { + assert.Error(t, e) + return + } + 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) + } + } + + 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"}) + + createOne("First Issue", "", nil, nil, 0, nil, false, false) + createOne("Issue 2", "closed isn't it?", nil, nil, 0, nil, true, false) + createOne("Issue 3", "", nil, nil, 0, nil, true, false) + createOne("Feature: spam protect 4", "explain explain explain", []string{user.UserName}, &nowTime, 0, nil, true, false) + createOne("W 123", "", nil, &nowTime, mile.ID, nil, false, false) + createOne("First Issue", "", nil, nil, 0, nil, false, false) + createOne("Do it soon!", "is important!", []string{user.UserName}, &nowTime, mile.ID, []int64{label1.ID, label2.ID}, false, false) + createOne("Job Done", "you never know", nil, nil, mile.ID, []int64{label2.ID}, true, false) + createOne("", "you never know", nil, nil, mile.ID, nil, true, true) +} + +func listIssues(t *testing.T, c *Client) { + log.Println("== TestListIssues ==") + + issues, err := c.ListIssues(ListIssueOption{ + Labels: []string{"Label2"}, + KeyWord: "Done", + State: "all", + }) + assert.NoError(t, err) + assert.Len(t, issues, 1) +} -- 2.40.1 From 1ec8d63d5e52e81e429b1c25671043a8205c5843 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 25 Jan 2020 03:50:52 +0100 Subject: [PATCH 07/10] nice log --- gitea/main_test.go | 2 +- gitea/pull_test.go | 2 ++ gitea/repo_test.go | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/gitea/main_test.go b/gitea/main_test.go index e04fced..79eb98c 100644 --- a/gitea/main_test.go +++ b/gitea/main_test.go @@ -43,7 +43,6 @@ func newTestClient() *Client { token := getGiteaToken() if token == "" { client := NewClientWithHTTP(getGiteaURL(), &http.Client{}) - log.Printf("testing with %v, %v, %v\n", getGiteaURL(), getGiteaUsername(), getGiteaPassword()) client.SetBasicAuth(getGiteaUsername(), getGiteaPassword()) return client } @@ -151,6 +150,7 @@ func TestMain(m *testing.M) { p.Kill() }() } + log.Printf("testing with %v, %v, %v\n", getGiteaURL(), getGiteaUsername(), getGiteaPassword()) exitCode := m.Run() os.Exit(exitCode) } diff --git a/gitea/pull_test.go b/gitea/pull_test.go index d1ab7a7..7c391eb 100644 --- a/gitea/pull_test.go +++ b/gitea/pull_test.go @@ -5,12 +5,14 @@ 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) diff --git a/gitea/repo_test.go b/gitea/repo_test.go index 9bca709..f46e735 100644 --- a/gitea/repo_test.go +++ b/gitea/repo_test.go @@ -12,6 +12,7 @@ import ( ) func TestCreateRepo(t *testing.T) { + log.Println("== TestCreateRepo ==") c := newTestClient() user, err := c.GetMyUserInfo() assert.NoError(t, err) -- 2.40.1 From e772c9266b63659193068930a2ae576ce982f675 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 25 Jan 2020 04:01:30 +0100 Subject: [PATCH 08/10] add more test cases and fix --- gitea/issue.go | 6 +++--- gitea/issue_test.go | 10 +++++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/gitea/issue.go b/gitea/issue.go index c4a5a4c..a6e42e1 100644 --- a/gitea/issue.go +++ b/gitea/issue.go @@ -86,10 +86,10 @@ func (opt *ListIssueOption) QueryEncode() string { // ListIssues returns all issues assigned the authenticated user func (c *Client) ListIssues(opt ListIssueOption) ([]*Issue, error) { - link, _ := url.Parse("​/repos​/issues​/search") + link, _ := url.Parse("/repos/issues/search") issues := make([]*Issue, 0, 10) link.RawQuery = opt.QueryEncode() - return issues, c.getParsedResponse("GET", link.String(), nil, nil, &issues) + return issues, c.getParsedResponse("GET", link.String(), jsonHeader, nil, &issues) } // ListUserIssues returns all issues assigned to the authenticated user @@ -118,7 +118,7 @@ func (c *Client) ListRepoIssues(owner, repo string, opt ListIssueOption) ([]*Iss link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/issues", owner, repo)) issues := make([]*Issue, 0, 10) link.RawQuery = opt.QueryEncode() - return issues, c.getParsedResponse("GET", link.String(), nil, nil, &issues) + return issues, c.getParsedResponse("GET", link.String(), jsonHeader, nil, &issues) } // GetIssue returns a single issue for a given repository diff --git a/gitea/issue_test.go b/gitea/issue_test.go index 6016d8f..22af205 100644 --- a/gitea/issue_test.go +++ b/gitea/issue_test.go @@ -80,7 +80,15 @@ func createIssue(t *testing.T, c *Client) { func listIssues(t *testing.T, c *Client) { log.Println("== TestListIssues ==") - issues, err := c.ListIssues(ListIssueOption{ + issues, err := c.ListRepoIssues("test01", "IssueTestsRepo", ListIssueOption{ + Labels: []string{"Label2"}, + KeyWord: "Done", + State: "all", + }) + assert.NoError(t, err) + assert.Len(t, issues, 1) + + issues, err = c.ListIssues(ListIssueOption{ Labels: []string{"Label2"}, KeyWord: "Done", State: "all", -- 2.40.1 From bd7c533200ea663728412e0dc4c2c8210614df7b Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 25 Jan 2020 04:02:40 +0100 Subject: [PATCH 09/10] add Test --- gitea/issue_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/gitea/issue_test.go b/gitea/issue_test.go index 22af205..6fd154f 100644 --- a/gitea/issue_test.go +++ b/gitea/issue_test.go @@ -95,4 +95,8 @@ func listIssues(t *testing.T, c *Client) { }) assert.NoError(t, err) assert.Len(t, issues, 1) + + issues, err = c.ListRepoIssues("test01", "IssueTestsRepo", ListIssueOption{}) + assert.NoError(t, err) + assert.Len(t, issues, 4) } -- 2.40.1 From f5a352fe50adf52ce264288ac263dadeaff23b80 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 25 Jan 2020 13:17:47 +0100 Subject: [PATCH 10/10] fix test --- gitea/issue_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gitea/issue_test.go b/gitea/issue_test.go index 6fd154f..72607f7 100644 --- a/gitea/issue_test.go +++ b/gitea/issue_test.go @@ -82,16 +82,16 @@ func listIssues(t *testing.T, c *Client) { issues, err := c.ListRepoIssues("test01", "IssueTestsRepo", ListIssueOption{ Labels: []string{"Label2"}, - KeyWord: "Done", + KeyWord: "", State: "all", }) assert.NoError(t, err) - assert.Len(t, issues, 1) + assert.Len(t, issues, 2) issues, err = c.ListIssues(ListIssueOption{ Labels: []string{"Label2"}, KeyWord: "Done", - State: "all", + State: "", }) assert.NoError(t, err) assert.Len(t, issues, 1) -- 2.40.1