-
Notifications
You must be signed in to change notification settings - Fork 6
/
releases.go
119 lines (100 loc) · 4.67 KB
/
releases.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) 2019, NewReleases Go client AUTHORS.
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package newreleases
import (
"context"
"net/http"
"net/url"
"strconv"
"time"
)
// ReleasesService provides information about releases for every project that is
// tracked.
type ReleasesService service
// Release holds information about a specific released version.
type Release struct {
Version string `json:"version"`
Date time.Time `json:"date"`
CVE []string `json:"cve,omitempty"`
IsPrerelease bool `json:"is_prerelease,omitempty"`
IsUpdated bool `json:"is_updated,omitempty"`
IsExcluded bool `json:"is_excluded,omitempty"`
HasNote bool `json:"has_note,omitempty"`
}
// ListByProjectID returns a paginated list of project releases and the number
// of the last page. The project is referenced by its ID.
func (s *ReleasesService) ListByProjectID(ctx context.Context, projectID string, page int) (releases []Release, lastPage int, err error) {
return s.list(ctx, projectID, page)
}
// ListByProjectName returns a paginated list of project releases and the number
// of the last page. The project is referenced by its provider and name.
func (s *ReleasesService) ListByProjectName(ctx context.Context, provider, projectName string, page int) (releases []Release, lastPage int, err error) {
return s.list(ctx, provider+"/"+projectName, page)
}
func (s *ReleasesService) list(ctx context.Context, projectRef string, page int) (releases []Release, lastPage int, err error) {
type releasesResponse struct {
Releases []Release `json:"releases"`
TotalPages int `json:"total_pages"`
}
var r releasesResponse
path := "v1/projects/" + projectRef + "/releases"
if page <= 0 {
return nil, 0, errInvalidPageNumber
}
if page > 1 {
path += "?page=" + strconv.Itoa(page)
}
err = s.client.request(ctx, http.MethodGet, path, nil, &r)
return r.Releases, r.TotalPages, err
}
// GetByProjectID returns a specific version release for a project referenced by
// its ID.
func (s *ReleasesService) GetByProjectID(ctx context.Context, projectID, version string) (release *Release, err error) {
return s.get(ctx, projectID, version)
}
// GetByProjectName returns a specific version release for a project referenced
// by its provider and name.
func (s *ReleasesService) GetByProjectName(ctx context.Context, provider, projectName, version string) (release *Release, err error) {
return s.get(ctx, provider+"/"+projectName, version)
}
func (s *ReleasesService) get(ctx context.Context, projectRef, version string) (release *Release, err error) {
err = s.client.request(ctx, http.MethodGet, "v1/projects/"+projectRef+"/releases/"+url.PathEscape(version), nil, &release)
return release, err
}
// GetLatestByProjectID returns the latest non-excluded version release for a project referenced by
// its ID.
func (s *ReleasesService) GetLatestByProjectID(ctx context.Context, projectID string) (release *Release, err error) {
return s.getLatest(ctx, projectID)
}
// GetLatestByProjectName returns the latest non-excluded version release for a project referenced
// by its provider and name.
func (s *ReleasesService) GetLatestByProjectName(ctx context.Context, provider, projectName string) (release *Release, err error) {
return s.getLatest(ctx, provider+"/"+projectName)
}
func (s *ReleasesService) getLatest(ctx context.Context, projectRef string) (release *Release, err error) {
err = s.client.request(ctx, http.MethodGet, "v1/projects/"+projectRef+"/latest-release", nil, &release)
return release, err
}
// ReleaseNote holds information about an additional note for a specific
// version.
type ReleaseNote struct {
Title string `json:"title,omitempty"`
Message string `json:"message,omitempty"`
URL string `json:"url,omitempty"`
}
// GetNoteByProjectID returns a specific release note for a project referenced
// by its ID.
func (s *ReleasesService) GetNoteByProjectID(ctx context.Context, projectID string, version string) (release *ReleaseNote, err error) {
return s.getNote(ctx, projectID, version)
}
// GetNoteByProjectName returns a specific release note for a project referenced
// by its provider and name.
func (s *ReleasesService) GetNoteByProjectName(ctx context.Context, provider, projectName string, version string) (release *ReleaseNote, err error) {
return s.getNote(ctx, provider+"/"+projectName, version)
}
func (s *ReleasesService) getNote(ctx context.Context, projectRef, version string) (note *ReleaseNote, err error) {
err = s.client.request(ctx, http.MethodGet, "v1/projects/"+projectRef+"/releases/"+url.PathEscape(version)+"/note", nil, ¬e)
return note, err
}