Class: Travis::Client::Repository

Inherits:
Entity
  • Object
show all
Includes:
States
Defined in:
lib/travis/client/repository.rb

Defined Under Namespace

Classes: Key

Constant Summary

Constants included from States

States::STATES

Constants inherited from Entity

Entity::MAP

Instance Attribute Summary collapse

Attributes inherited from Entity

#attributes, #curry, #id, #session

Instance Method Summary collapse

Methods included from States

#canceled?, #color, #created?, #errored?, #failed?, #finished?, #green?, #passed?, #pending?, #queued?, #ready?, #red?, #running?, #started?, #unsuccessful?, #yellow?

Methods inherited from Entity

#[], #[]=, aka, #attribute_names, attributes, base_path, #cancelable?, cast_id, #complete?, has, has_singleton, id?, id_field, #include?, #inspect, inspect_info, #load, many, #missing?, one, preloadable, preloadable?, relations, #relations, #reload, #restartable?, subclass_for, subclasses, time, #to_h, #update_attributes, weak?

Constructor Details

#initializeRepository

Returns a new instance of Repository.


48
49
50
51
# File 'lib/travis/client/repository.rb', line 48

def initialize(*)
  super
  attributes['active'] = attributes['private'] = attributes['admin'] = nil
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description


1
2
3
# File 'lib/travis/client/repository.rb', line 1

def description
  @description
end

#last_buildObject (readonly)


76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/travis/client/repository.rb', line 76

def last_build
  return unless last_build_id
  attributes['last_build'] ||= begin
    last_build               = session.find_one(Build, last_build_id)
    last_build.number        = last_build_number
    last_build.state         = last_build_state
    last_build.duration      = last_build_duration
    last_build.started_at    = last_build_started_at
    last_build.finished_at   = last_build_finished_at
    last_build.repository_id = id
    last_build
  end
end

#slugObject (readonly)

Returns the value of attribute slug


1
2
3
# File 'lib/travis/client/repository.rb', line 1

def slug
  @slug
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)

195
196
197
198
199
# File 'lib/travis/client/repository.rb', line 195

def active?
  # TODO remove once active is properly synced and exposed by api
  return active unless active.nil?
  last_build_id?
end

#admin?(user = session.user) ⇒ Boolean

Returns:

  • (Boolean)

201
202
203
# File 'lib/travis/client/repository.rb', line 201

def admin?(user = session.user)
  user.admin_access? self
end

#branch(name) ⇒ Object


112
113
114
115
116
117
118
# File 'lib/travis/client/repository.rb', line 112

def branch(name)
  attributes['branches']       ||= {}
  attributes['branches'][name] ||= begin
    build = attributes['last_on_branch'].detect { |b| b.commit.branch == name.to_s } if attributes['last_on_branch']
    build || session.get("/repos/#{id}/branches/#{name}")['branch']
  end
end

#branchesObject


108
109
110
# File 'lib/travis/client/repository.rb', line 108

def branches
  last_on_branch.map { |b| { b.commit.branch => b } }.inject(:merge)
end

#build(number) ⇒ Object


95
96
97
# File 'lib/travis/client/repository.rb', line 95

def build(number)
  builds(:number => number.to_s).first
end

#builds(params = nil) ⇒ Object


90
91
92
93
# File 'lib/travis/client/repository.rb', line 90

def builds(params = nil)
  return each_build unless params
  session.find_many(Build, params.merge(:repository_id => id))
end

#caches(params = {}) ⇒ Object


187
188
189
# File 'lib/travis/client/repository.rb', line 187

def caches(params = {})
  session.get("/repos/#{id}/caches", params)['caches']
end

#delete_caches(params = {}) ⇒ Object


191
192
193
# File 'lib/travis/client/repository.rb', line 191

def delete_caches(params = {})
  session.delete("/repos/#{id}/caches", params)['caches']
end

#disableObject


145
146
147
# File 'lib/travis/client/repository.rb', line 145

def disable
  set_hook(false)
end

#each_build(params = nil, &block) ⇒ Object


120
121
122
123
124
125
126
127
128
129
130
# File 'lib/travis/client/repository.rb', line 120

def each_build(params = nil, &block)
  return enum_for(__method__, params) unless block_given?
  params ||= {}
  chunk = builds(params)
  until chunk.empty?
    chunk.each(&block)
    number = chunk.last.number
    chunk  = number == '1' ? [] : builds(params.merge(:after_number => number))
  end
  self
end

#enableObject


149
150
151
# File 'lib/travis/client/repository.rb', line 149

def enable
  set_hook(true)
end

#encrypt(value) ⇒ Object


71
72
73
# File 'lib/travis/client/repository.rb', line 71

def encrypt(value)
  key.encrypt(value)
end

#env_varsObject


213
214
215
# File 'lib/travis/client/repository.rb', line 213

def env_vars
  attributes['env_vars'] ||= EnvVar::List.new(self)
end

#job(number) ⇒ Object


132
133
134
135
136
137
138
# File 'lib/travis/client/repository.rb', line 132

def job(number)
  build_number = number.to_s[/^\d+/] or return nil
  build        = build(build_number) or return nil
  job          = build.jobs.detect { |j| j.number == number } if number != build_number
  job        ||= build.jobs.first if build and build.jobs.size == 1
  job
end

#last_on_branch(name = nil) ⇒ Object


103
104
105
106
# File 'lib/travis/client/repository.rb', line 103

def last_on_branch(name = nil)
  return branch(name) if name
  attributes['last_on_branch'] ||= session.get('branches', :repository_id => id)['branches']
end

#member?Boolean

Returns:

  • (Boolean)

161
162
163
# File 'lib/travis/client/repository.rb', line 161

def member?
  session.user.repositories.include? self
end

#nameObject


60
61
62
# File 'lib/travis/client/repository.rb', line 60

def name
  slug[/[^\/]+$/]
end

#ownerObject


169
170
171
# File 'lib/travis/client/repository.rb', line 169

def owner
  session.(owner_name)
end

#owner_nameObject


165
166
167
# File 'lib/travis/client/repository.rb', line 165

def owner_name
  slug[/^[^\/]+/]
end

#public_keyObject Also known as: key


53
54
55
56
57
58
# File 'lib/travis/client/repository.rb', line 53

def public_key
  attributes["public_key"] ||= begin
    payload = session.get_raw("/repos/#{id}/key")
    Key.new(payload.fetch('key'), payload['fingerprint'])
  end
end

#public_key=(key) ⇒ Object Also known as: key=


64
65
66
# File 'lib/travis/client/repository.rb', line 64

def public_key=(key)
  # ignored
end

#pull?(user = session.user) ⇒ Boolean

Returns:

  • (Boolean)

209
210
211
# File 'lib/travis/client/repository.rb', line 209

def pull?(user = session.user)
  user.pull_access? self
end

#push?(user = session.user) ⇒ Boolean

Returns:

  • (Boolean)

205
206
207
# File 'lib/travis/client/repository.rb', line 205

def push?(user = session.user)
  user.push_access? self
end

#pusher_channelsObject


153
154
155
156
157
158
159
# File 'lib/travis/client/repository.rb', line 153

def pusher_channels
  attributes['pusher_channels'] ||= if session.private_channels?
    ["user-#{session.user.id}", "repo-#{id}"]
  else
    ["common"]
  end
end

#recent_buildsObject


99
100
101
# File 'lib/travis/client/repository.rb', line 99

def recent_builds
  builds({})
end

#requestsObject


173
174
175
# File 'lib/travis/client/repository.rb', line 173

def requests
  attributes['requests'] ||= session.find_many(Request, :repository_id => id)
end

#set_hook(flag) ⇒ Object


140
141
142
143
# File 'lib/travis/client/repository.rb', line 140

def set_hook(flag)
  result = session.put_raw('/hooks/', :hook => { :id => id, :active => flag })
  result['result']
end

#settingsObject


177
178
179
180
181
182
183
184
185
# File 'lib/travis/client/repository.rb', line 177

def settings
  attributes['settings'] ||= begin
    settings = session.get("/repos/#{id}/settings")['settings']
    settings.repository = self
    settings
  end
rescue Travis::Client::NotFound
  raise Travis::Client::Error, "not allowed to access settings"
end