From b24cfd841cdad405d9817e7b59e1355573eb7a41 Mon Sep 17 00:00:00 2001 From: 6543 <6543@noreply.gitea.io> Date: Thu, 16 Jan 2020 03:52:26 +0000 Subject: [PATCH] Fix ListRepoPullRequests (#219) add ToDo notice add ListRepoPullRequests TEST remove useless drone config emtrys fmt ping CI add new Options from PR #217 use query params Add some PR list options (#217) Empty Commit Add enums Add some PR list options Add test framework (#227) [Extend] StopWatch struct & functions (#211) add StopWatch struct & functions [Add] reaction struct and functions (#213) add struct and functions Co-authored-by: 6543 <6543@obermui.de> Reviewed-by: Andrew Thornton Reviewed-by: techknowlogick [Add] issue Un-/Subscription function (#214) fix lint add issue subscription function Co-authored-by: 6543 <6543@obermui.de> Reviewed-by: Andrew Thornton Reviewed-by: techknowlogick [Add] GetBlob (#212) fix header from PR 206 add GetBlob Co-authored-by: 6543 <6543@obermui.de> Reviewed-by: Lunny Xiao Reviewed-by: Andrew Thornton Co-authored-by: 6543 <6543@obermui.de> Reviewed-by: Andrew Thornton Reviewed-by: techknowlogick Co-authored-by: jolheiser Co-authored-by: techknowlogick Co-authored-by: 6543 <6543@noreply.gitea.io> Reviewed-by: techknowlogick Reviewed-by: 6543 <6543@noreply.gitea.io> Add test framework (#227) Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: John Olheiser Co-authored-by: techknowlogick Reviewed-by: techknowlogick Reviewed-by: Lunny Xiao --- .drone.yml | 5 ++-- gitea/pull.go | 29 +++++++++++++++++------ gitea/pull_test.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 10 deletions(-) create mode 100644 gitea/pull_test.go diff --git a/.drone.yml b/.drone.yml index 1570b56..94f8567 100644 --- a/.drone.yml +++ b/.drone.yml @@ -34,6 +34,7 @@ steps: image: golang:1.13 environment: GOPROXY: https://goproxy.cn + HTTP_PROXY: "" GITEA_SDK_TEST_URL: "http://gitea:3000" GITEA_SDK_TEST_USERNAME: "test01" GITEA_SDK_TEST_PASSWORD: "test01" @@ -41,11 +42,9 @@ steps: commands: - make clean - make vet - #- make lint - make build - - export HTTP_PROXY="" - curl --noproxy "*" http://gitea:3000/api/v1/version # verify connection to instance - - HTTP_PROXY="" http_proxy="" make test + - make test - name: discord pull: always diff --git a/gitea/pull.go b/gitea/pull.go index a7194d7..274ecd5 100644 --- a/gitea/pull.go +++ b/gitea/pull.go @@ -9,6 +9,7 @@ import ( "bytes" "encoding/json" "fmt" + "net/url" "time" ) @@ -58,9 +59,9 @@ type PullRequest struct { // ListPullRequestsOptions options for listing pull requests type ListPullRequestsOptions struct { - Page int `json:"page"` + Page int `json:"page"` // open, closed, all - State string `json:"state"` + State string `json:"state"` // oldest, recentupdate, leastupdate, mostcomment, leastcomment, priority Sort string `json:"sort"` Milestone int64 `json:"milestone"` @@ -68,12 +69,26 @@ type ListPullRequestsOptions struct { // ListRepoPullRequests list PRs of one repository func (c *Client) ListRepoPullRequests(owner, repo string, opt ListPullRequestsOptions) ([]*PullRequest, error) { - body, err := json.Marshal(&opt) - if err != nil { - return nil, err - } + // declare variables + link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/pulls", owner, repo)) prs := make([]*PullRequest, 0, 10) - return prs, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls", owner, repo), jsonHeader, bytes.NewReader(body), &prs) + query := make(url.Values) + // add options to query + 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.Sort) > 0 { + query.Add("sort", opt.Sort) + } + if opt.Milestone > 0 { + query.Add("milestone", fmt.Sprintf("%d", opt.Milestone)) + } + link.RawQuery = query.Encode() + // request + return prs, c.getParsedResponse("GET", link.String(), jsonHeader, nil, &prs) } // GetPullRequest get information of one PR diff --git a/gitea/pull_test.go b/gitea/pull_test.go new file mode 100644 index 0000000..f1f3dcd --- /dev/null +++ b/gitea/pull_test.go @@ -0,0 +1,59 @@ +// 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 ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestPull(t *testing.T) { + c := newTestClient() + user, err := c.GetMyUserInfo() + assert.NoError(t, err) + + var repoName = "repo_pull_test" + repo, err := c.GetRepo(user.UserName, repoName) + 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) + } + + // ListRepoPullRequests list PRs of one repository + pulls, err := c.ListRepoPullRequests(user.UserName, repoName, ListPullRequestsOptions{ + Page: 1, + State: "all", + Sort: "leastupdate", + }) + assert.NoError(t, err) + assert.Len(t, pulls, 0) + + //ToDo add git stuff to have different branches witch can be used to create PRs and test merge etc ... + + // GetPullRequest get information of one PR + //func (c *Client) GetPullRequest(owner, repo string, index int64) (*PullRequest, error) + + // CreatePullRequest create pull request with options + //func (c *Client) CreatePullRequest(owner, repo string, opt CreatePullRequestOption) (*PullRequest, error) + + // EditPullRequest modify pull request with PR id and options + //func (c *Client) EditPullRequest(owner, repo string, index int64, opt EditPullRequestOption) (*PullRequest, error) + + // MergePullRequest merge a PR to repository by PR id + //func (c *Client) MergePullRequest(owner, repo string, index int64, opt MergePullRequestOption) (*MergePullRequestResponse, error) + + // IsPullRequestMerged test if one PR is merged to one repository + //func (c *Client) IsPullRequestMerged(owner, repo string, index int64) (bool, error) +}