This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
caches.go
128 lines (107 loc) · 3.46 KB
/
caches.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
120
121
122
123
124
125
126
127
128
// Copyright (c) 2015 Ableton AG, Berlin. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package travis
import (
"context"
"fmt"
"net/http"
"net/url"
)
// CachesService handles communication with the caches endpoints
// of Travis CI API
type CachesService struct {
client *Client
}
// Cache is a standard representation of a cache on Travis CI
//
// Travis CI API docs: https://developer.travis-ci.com/resource/caches#attributes
type Cache struct {
// The branch the cache belongs to
Branch *string `json:"branch,omitempty"`
// The string to match against the cache name
Match *string `json:"match,omitempty"`
*Metadata
}
// cachesResponse represents the response of a call
// to the Travis CI caches endpoint.
type cachesResponse struct {
Caches []*Cache `json:"caches"`
}
// ListByRepoId fetches caches based on the given repository id
//
// Travis CI API docs: https://developer.travis-ci.com/resource/caches#find
func (cs *CachesService) ListByRepoId(ctx context.Context, repoId uint) ([]*Cache, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("repo/%d/caches", repoId), nil)
if err != nil {
return nil, nil, err
}
req, err := cs.client.NewRequest(http.MethodGet, u, nil, nil)
if err != nil {
return nil, nil, err
}
var cachesResponse cachesResponse
resp, err := cs.client.Do(ctx, req, &cachesResponse)
if err != nil {
return nil, resp, err
}
return cachesResponse.Caches, resp, err
}
// ListByRepoSlug fetches caches based on the given repository slug
//
// Travis CI API docs: https://developer.travis-ci.com/resource/caches#find
func (cs *CachesService) ListByRepoSlug(ctx context.Context, repoSlug string) ([]*Cache, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("repo/%s/caches", url.QueryEscape(repoSlug)), nil)
if err != nil {
return nil, nil, err
}
req, err := cs.client.NewRequest(http.MethodGet, u, nil, nil)
if err != nil {
return nil, nil, err
}
var cachesResponse cachesResponse
resp, err := cs.client.Do(ctx, req, &cachesResponse)
if err != nil {
return nil, resp, err
}
return cachesResponse.Caches, resp, err
}
// DeleteByRepoId deletes caches based on the given repository id
//
// Travis CI API docs: https://developer.travis-ci.com/resource/caches#delete
func (cs *CachesService) DeleteByRepoId(ctx context.Context, repoId uint) ([]*Cache, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("repo/%d/caches", repoId), nil)
if err != nil {
return nil, nil, err
}
req, err := cs.client.NewRequest(http.MethodDelete, u, nil, nil)
if err != nil {
return nil, nil, err
}
var cachesResponse cachesResponse
resp, err := cs.client.Do(ctx, req, &cachesResponse)
if err != nil {
return nil, resp, err
}
return cachesResponse.Caches, resp, err
}
// DeleteByRepoSlug deletes caches based on the given repository slug
//
// Travis CI API docs: https://developer.travis-ci.com/resource/caches#delete
func (cs *CachesService) DeleteByRepoSlug(ctx context.Context, repoSlug string) ([]*Cache, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("repo/%s/caches", url.QueryEscape(repoSlug)), nil)
if err != nil {
return nil, nil, err
}
req, err := cs.client.NewRequest(http.MethodDelete, u, nil, nil)
if err != nil {
return nil, nil, err
}
var cachesResponse cachesResponse
resp, err := cs.client.Do(ctx, req, &cachesResponse)
if err != nil {
return nil, resp, err
}
return cachesResponse.Caches, resp, err
}