Fix ListRepoPullRequests #219

Merged
lunny merged 7 commits from 6543/go-sdk:fix-ListRepoPullRequests into master 2020-01-16 03:52:30 +00:00
3 changed files with 83 additions and 10 deletions

View File

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

View File

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

59
gitea/pull_test.go Normal file
View File

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