Skip to content

Commit b5a909d

Browse files
authored
Build backup new endpoints (#1179)
Merge part 2 PRD
1 parent 4ef9ef4 commit b5a909d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+559
-25
lines changed

lib/travis/api/app/endpoint.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def endpoint(link, query_values = {})
120120
require 'travis/api/app/endpoint/branches'
121121
require 'travis/api/app/endpoint/broadcasts'
122122
require 'travis/api/app/endpoint/builds'
123+
require 'travis/api/app/endpoint/build_backups'
123124
require 'travis/api/app/endpoint/documentation'
124125
require 'travis/api/app/endpoint/endpoints'
125126
require 'travis/api/app/endpoint/env_vars'
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require 'travis/api/app'
2+
require 'travis/api/app/responders/base'
3+
4+
class Travis::Api::App
5+
class Endpoint
6+
class BuildBackups < Endpoint
7+
include Helpers::Accept
8+
9+
before { authenticate_by_mode! }
10+
11+
get '/' do
12+
prefer_follower do
13+
respond_with service(:find_build_backups, params)
14+
end
15+
end
16+
end
17+
end
18+
end

lib/travis/api/v3/model.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,11 @@ class Model < ActiveRecord::Base
88
def self.===(other)
99
super or (self == Model and other.class.parent == Models)
1010
end
11+
12+
def ro_mode?
13+
return false unless Travis.config.org? && Travis.config.read_only?
14+
15+
!Travis::Features.owner_active?(:read_only_disabled, self)
16+
end
1117
end
1218
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Travis::API::V3
2+
class Models::BuildBackup < Model
3+
attr_accessor :content
4+
5+
belongs_to :repository
6+
end
7+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module Travis::API::V3
2+
class Queries::BuildBackup < RemoteQuery
3+
params :id
4+
5+
def find
6+
raise WrongParams, 'missing build_backup.id'.freeze unless id
7+
build_backup = Models::BuildBackup.find_by_id(id)
8+
content = get(build_backup.file_name)
9+
raise EntityMissing, 'could not retrieve content'.freeze if content.nil?
10+
build_backup.content = content.force_encoding('UTF-8') if content.present?
11+
12+
build_backup
13+
end
14+
15+
private
16+
17+
def main_type
18+
'build_backup'
19+
end
20+
end
21+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Travis::API::V3
2+
class Queries::BuildBackups < Query
3+
params :repository_id
4+
5+
def all
6+
return Models::BuildBackup.where(repository_id: repository_id) if repository_id
7+
raise WrongParams, 'missing build_backups.repository_id'.freeze
8+
end
9+
end
10+
end

lib/travis/api/v3/remote_query.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ def fetch
1818
storage_objects
1919
end
2020

21+
def get(key)
22+
io = StringIO.new
23+
gcs_connection.get_object(gcs_config[:bucket_name], key, download_dest: io)
24+
io.rewind
25+
io.read
26+
end
27+
2128
def remove(objects)
2229
objects.each do |object|
2330
raise SourceUnknown "#{object.source} is an unknown source." unless ['s3', 'gcs'].include? object.source
@@ -91,7 +98,7 @@ def s3_objects
9198

9299
def gcs_connection
93100
gcs = ::Google::Apis::StorageV1::StorageService.new
94-
json_key_io = StringIO.new(gcs_config[:json_key])
101+
json_key_io = StringIO.new(JSON.dump(gcs_config[:json_key]))
95102

96103
gcs.authorization = ::Google::Auth::ServiceAccountCredentials.make_creds(
97104
json_key_io: json_key_io,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Travis::API::V3
2+
class Renderer::BuildBackup < ModelRenderer
3+
representation(:minimal, :file_name, :created_at)
4+
representation(:standard, *representations[:minimal])
5+
6+
def self.render(model, representation = :standard, **options)
7+
return super unless options[:accept] == 'text/plain'.freeze
8+
9+
model.content
10+
end
11+
end
12+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Travis::API::V3
2+
class Renderer::BuildBackups < CollectionRenderer
3+
type :build_backups
4+
collection_key :build_backups
5+
end
6+
end

lib/travis/api/v3/renderer/owner.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ module Travis::API::V3
44
class Renderer::Owner < ModelRenderer
55
include Renderer::AvatarURL
66

7-
representation(:minimal, :id, :login, :name, :vcs_type)
7+
representation(:minimal, :id, :login, :name, :vcs_type, :ro_mode)
88
representation(:standard, :id, :login, :name, :github_id, :vcs_id, :vcs_type, :avatar_url, :education,
9-
:allow_migration, :allowance)
9+
:allow_migration, :allowance, :ro_mode)
1010
representation(:additional, :repositories, :installation)
1111

1212
def initialize(*)
@@ -33,12 +33,18 @@ def allow_migration
3333
def allowance
3434
return BillingClient.default_allowance_response(id) if Travis.config.org?
3535
return BillingClient.default_allowance_response(id) unless access_control.user
36-
36+
3737
BillingClient.minimal_allowance_response(id)
3838
end
3939

4040
def owner_type
4141
vcs_type.match(/User$/) ? 'User' : 'Organization'
4242
end
43+
44+
def ro_mode
45+
return false unless Travis.config.org? && Travis.config.read_only?
46+
47+
!Travis::Features.owner_active?(:read_only_disabled, model)
48+
end
4349
end
4450
end

0 commit comments

Comments
 (0)