-
Notifications
You must be signed in to change notification settings - Fork 234
/
util.go
103 lines (92 loc) · 2.51 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2017 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gitea
import (
"fmt"
"net/url"
"strconv"
"strings"
"github.com/drone/go-scm/scm"
)
func encodeListOptions(opts scm.ListOptions) string {
params := url.Values{}
if opts.Page != 0 {
params.Set("page", strconv.Itoa(opts.Page))
}
if opts.Size != 0 {
params.Set("limit", strconv.Itoa(opts.Size))
}
return params.Encode()
}
func encodeIssueListOptions(opts scm.IssueListOptions) string {
params := url.Values{}
if opts.Page != 0 {
params.Set("page", strconv.Itoa(opts.Page))
}
if opts.Size != 0 {
params.Set("limit", strconv.Itoa(opts.Size))
}
if opts.Open && opts.Closed {
params.Set("state", "all")
} else if opts.Closed {
params.Set("state", "closed")
}
return params.Encode()
}
func encodePullRequestListOptions(opts scm.PullRequestListOptions) string {
params := url.Values{}
if opts.Page != 0 {
params.Set("page", strconv.Itoa(opts.Page))
}
if opts.Size != 0 {
params.Set("limit", strconv.Itoa(opts.Size))
}
if opts.Open && opts.Closed {
params.Set("state", "all")
} else if opts.Closed {
params.Set("state", "closed")
}
return params.Encode()
}
// convertAPIURLToHTMLURL converts an release API endpoint into a html endpoint
func convertAPIURLToHTMLURL(apiURL string, tagName string) string {
// "url": "https://try.gitea.com/api/v1/repos/octocat/Hello-World/123",
// "html_url": "https://try.gitea.com/octocat/Hello-World/releases/tag/v1.0.0",
// the url field is the API url, not the html url, so until go-sdk v0.13.3, build it ourselves
link, err := url.Parse(apiURL)
if err != nil {
return ""
}
pathParts := strings.Split(link.Path, "/")
if len(pathParts) != 7 {
return ""
}
link.Path = fmt.Sprintf("/%s/%s/releases/tag/%s", pathParts[4], pathParts[5], tagName)
return link.String()
}
func encodeMilestoneListOptions(opts scm.MilestoneListOptions) string {
params := url.Values{}
if opts.Page != 0 {
params.Set("page", strconv.Itoa(opts.Page))
}
if opts.Size != 0 {
params.Set("per_page", strconv.Itoa(opts.Size))
}
if opts.Open && opts.Closed {
params.Set("state", "all")
} else if opts.Closed {
params.Set("state", "closed")
}
return params.Encode()
}
type ListOptions struct {
Page int
PageSize int
}
func encodeReleaseListOptions(o ListOptions) string {
query := make(url.Values)
query.Add("page", fmt.Sprintf("%d", o.Page))
query.Add("limit", fmt.Sprintf("%d", o.PageSize))
return query.Encode()
}