/
methods.rb
133 lines (103 loc) · 3.1 KB
/
methods.rb
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
129
130
131
132
133
# frozen_string_literal: true
require 'travis/client'
require 'yaml'
module Travis
module Client
module Methods
def access_token
session.access_token
end
def access_token=(token)
session.access_token = token
end
def api_endpoint
session.uri
end
def github_auth(github_token)
reply = session.post_raw('/auth/github', github_token:)
unless reply.respond_to?(:key?) && reply.key?('access_token')
raise InvalidTokenError,
'token is invalid, or does not have sufficient scope; see https://docs.travis-ci.com/user/github-oauth-scopes/ for more information on scope'
end
session.access_token = reply['access_token']
end
def explicit_api_endpoint?
@explicit_api_endpoint ||= false
end
def api_endpoint=(uri)
@explicit_api_endpoint = true
session.uri = uri
end
def repos(params = {})
session.find_many(Repository, params)
end
def repo(id_or_slug)
session.find_one(Repository, id_or_slug)
end
def build(id)
session.find_one(Build, id)
end
def job(id)
session.find_one(Job, id)
end
def artifact(id)
session.find_one(Artifact, id)
end
alias log artifact
def user
session.find_one(User)
rescue NotFound
raise NotLoggedIn, 'currently not logged in'
end
def account(name)
session.find_one(Account, name)
end
def accounts
session.find_many(Account, all: true)
end
def broadcasts
session.find_many(Broadcast)
end
def restart(entity)
# btw, internally we call this reset, not restart, as it resets the state machine
# but we thought that would be too confusing
raise Error, "cannot restart a #{entity.class.one}" unless entity.restartable?
session.post_raw("/#{entity.class.many}/#{entity.id}/restart")
entity.reload
end
def cancel(entity)
raise Error, "cannot cancel a #{entity.class.one}" unless entity.cancelable?
session.post_raw("/#{entity.class.many}/#{entity.id}/cancel")
entity.reload
end
def logout
session.get_raw('/logout')
end
def regenerate_token
session.headers['Travis-Api-Version'] = '3'
token = session.patch_raw('/access_token')
session.headers.delete('Travis-Api-Version')
token
end
def remove_token
session.headers['Travis-Api-Version'] = '3'
resp = session.delete_raw('/access_token')
session.headers.delete('Travis-Api-Version')
resp
end
def listen(*entities, &block)
listener = Listener.new(session)
listener.subscribe(*entities, &block)
listener.listen
end
def lint(body)
body = body.to_yaml unless body.is_a? String
result = session.post_raw('/lint', 'content' => body)
LintResult.new(result)
end
def hooks
session.get('hooks')['hooks']
end
end
end
end