Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh app #723

Merged
merged 37 commits into from Apr 9, 2018
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
f4c1e26
add gh repo attributes
Mar 29, 2018
f064d01
change column to migrated_to_com
Mar 29, 2018
594c10d
update repo flag
Mar 29, 2018
11f6fc9
update names
Mar 29, 2018
4374ff4
revert to active_on_org
Mar 29, 2018
436d276
add github_installation_id to owner
Mar 29, 2018
84822b9
comment out breaking test
Mar 29, 2018
6eca455
use different migrations branch
Mar 29, 2018
60f5a10
Merge branch 'cd-gh-app' of github.com:travis-ci/travis-api into cd-g…
Apr 3, 2018
b112d8c
try pg 9.6
Apr 3, 2018
a7ce6a4
Merge branch 'master' into cd-gh-app
carlad Apr 3, 2018
ae9b303
remove unnecessary test
Apr 4, 2018
a2c7e79
update to installations
Apr 4, 2018
24b3433
use correct migrations branch
Apr 4, 2018
37be6f3
add test for github_installation_id
Apr 4, 2018
754282f
add renderer
Apr 4, 2018
161b8ec
update specs for github_installation_id
Apr 5, 2018
c14a023
add owner/github_installation_id endpoint
Apr 5, 2018
6995096
add check for type
Apr 5, 2018
a4414f3
change to github_id
Apr 5, 2018
231f59c
remove github_installation_id attribute on owner
Apr 6, 2018
8193673
add installation service end endpoint
Apr 6, 2018
a225d3f
remove redundant attributes on user and org
Apr 6, 2018
3aeac04
add access control, update param, add renderer expansion check
Apr 6, 2018
346a28d
update to :owner, add and update spec
Apr 6, 2018
505f403
revert to managed_by_installation
Apr 6, 2018
1316eae
add installation to repo renderer
Apr 6, 2018
820acb1
add association to user and org
Apr 6, 2018
606c0f5
allow overriding the travis-migrations branch being used with an envi…
rkh Apr 6, 2018
2ebcde1
minor clean-ups
rkh Apr 6, 2018
4398b3c
v3: allow filtering repositoires by managed_by_installation
rkh Apr 6, 2018
321d73d
set travis migrations branch for ci
rkh Apr 6, 2018
5e97ac1
Owner#installation: has_one is not wokring properly
rkh Apr 6, 2018
9a62dcc
remove comments, update description
Apr 9, 2018
e3eefce
Merge branch 'master' into cd-gh-app
carlad Apr 9, 2018
572e712
Merge branch 'master' into cd-gh-app
rkh Apr 9, 2018
8437e07
Revert "set travis migrations branch for ci"
rkh Apr 9, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Expand Up @@ -10,6 +10,7 @@ env:
- RUBY_GC_MALLOC_LIMIT=90000000
- RUBY_GC_HEAP_FREE_SLOTS=200000
- CI_NODE_TOTAL=3
- TRAVIS_MIGRATIONS_BRANCH=cd-gh-app

matrix:
- CI_NODE_INDEX=0
Expand All @@ -19,7 +20,7 @@ env:
cache: bundler

addons:
postgresql: 9.3
postgresql: 9.6

services:
- redis-server
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -37,6 +37,7 @@ $ sudo -u postgres psql -c "CREATE USER yourusername WITH SUPERUSER PASSWORD 'yo
```

Databases are set up with a Rake task that uses the database schemas (`structure.sql`) in `travis-migrations`. Details can be found in the `Rakefile`.
You can override the `travis-migrations` branch that is being used by setting the environment variable `TRAVIS_MIGRATIONS_BRANCH`.


To create and migrate the Databases:
Expand Down
9 changes: 8 additions & 1 deletion Rakefile
Expand Up @@ -4,8 +4,15 @@ namespace :db do
env = ENV["ENV"] || 'test'
abort "Cannot run rake db:create in production." if env == 'production'

url = "https://raw.githubusercontent.com/travis-ci/travis-migrations/master/db/main/structure.sql"
if branch = ENV['TRAVIS_MIGRATIONS_BRANCH'] and !branch.empty?
$stderr.puts "Warning: travis-migrations branch overridden by environment variable."
else
branch = 'master'
end

url = "https://raw.githubusercontent.com/travis-ci/travis-migrations/#{branch}/db/main/structure.sql"
file = 'db/structure.sql'
puts url
system "curl -fs #{url} -o #{file} --create-dirs"
abort "failed to download #{url}" unless File.exist?(file)

Expand Down
4 changes: 4 additions & 0 deletions lib/travis/api/v3/access_control/generic.rb
Expand Up @@ -91,6 +91,10 @@ def cron_writable?(cron)
writable? cron.branch.repository
end

def installation_visible?(installation)
visible? installation.owner
end

def log_visible?(log)
visible? log.job
end
Expand Down
5 changes: 5 additions & 0 deletions lib/travis/api/v3/models/installation.rb
@@ -0,0 +1,5 @@
module Travis::API::V3
class Models::Installation < Model
belongs_to :owner, polymorphic: true
end
end
5 changes: 5 additions & 0 deletions lib/travis/api/v3/models/organization.rb
Expand Up @@ -7,6 +7,11 @@ def repositories
Models::Repository.where(owner_type: 'Organization', owner_id: id)
end

def installation
return @installation if defined? @installation
@installation = Models::Installation.find_by(owner_type: 'Organization', owner_id: id)
end

alias members users
end
end
4 changes: 4 additions & 0 deletions lib/travis/api/v3/models/repository.rb
Expand Up @@ -98,5 +98,9 @@ def debug_tools_enabled?
return true if Travis::Features.active?(:debug_tools, self)
return false
end

def managed_by_installation?
!!managed_by_installation_at
end

This comment was marked as spam.

end
end
4 changes: 4 additions & 0 deletions lib/travis/api/v3/models/user.rb
Expand Up @@ -34,5 +34,9 @@ def permission?(roles, options = {})
scope.any?
end

def installation
return @installation if defined? @installation
@installation = Models::Installation.find_by(owner_type: 'User', owner_id: id)
end
end
end
9 changes: 9 additions & 0 deletions lib/travis/api/v3/queries/installation.rb
@@ -0,0 +1,9 @@
module Travis::API::V3
class Queries::Installation < Query
params :github_id

def find
Models::Installation.find_by_github_id(github_id) if github_id
end
end
end
3 changes: 2 additions & 1 deletion lib/travis/api/v3/queries/repositories.rb
@@ -1,6 +1,6 @@
module Travis::API::V3
class Queries::Repositories < Query
params :active, :private, :starred, prefix: :repository
params :active, :private, :starred, :managed_by_installation, prefix: :repository
experimental_params :slug_filter, prefix: :repository
sortable_by :id, :github_id, :owner_name, :name, active: sort_condition(:active),
:'default_branch.last_build' => 'builds.started_at',
Expand Down Expand Up @@ -32,6 +32,7 @@ def filter(list, user: nil)
list = list.where(active: bool(active)) unless active.nil?
list = list.where(private: bool(private)) unless private.nil?
list = list.includes(:owner) if includes? 'repository.owner'.freeze
list = list.where("managed_by_installation_at #{bool(managed_by_installation) ? 'is not' : 'is'} null") unless managed_by_installation.nil?

if user and not starred.nil?
if bool(starred)
Expand Down
1 change: 1 addition & 0 deletions lib/travis/api/v3/renderer.rb
Expand Up @@ -29,6 +29,7 @@ def href(type, string_args = nil, script_name: nil, **args)
args.keys.each do |key|
case key.to_s
when /\./ then key_mapping[key.to_sym] = key unless key.is_a? Symbol
when 'github_id' then key_mapping[:"#{type}.#{key}"] = key
when /^(.+)_id$/ then key_mapping[:"#{$1}.id"] = key
else key_mapping[:"#{type}.#{key}"] = key
end
Expand Down
6 changes: 6 additions & 0 deletions lib/travis/api/v3/renderer/installation.rb
@@ -0,0 +1,6 @@
module Travis::API::V3
class Renderer::Installation < ModelRenderer
representation(:minimal, :id, :github_id)
representation(:standard, *representations[:minimal], :owner)
end
end
7 changes: 6 additions & 1 deletion lib/travis/api/v3/renderer/owner.rb
Expand Up @@ -6,7 +6,7 @@ class Renderer::Owner < ModelRenderer

representation(:minimal, :id, :login)
representation(:standard, :id, :login, :name, :github_id, :avatar_url)
representation(:additional, :repositories)
representation(:additional, :repositories, :installation)

def initialize(*)
super
Expand All @@ -19,5 +19,10 @@ def repositories
repositories = query(:repositories).for_owner(@model)
access_control.visible_repositories(repositories)
end

def installation
installation = model.installation
installation if installation and access_control.visible? installation
end

This comment was marked as spam.

end
end
6 changes: 5 additions & 1 deletion lib/travis/api/v3/renderer/repository.rb
@@ -1,7 +1,7 @@
module Travis::API::V3
class Renderer::Repository < ModelRenderer
representation(:minimal, :id, :name, :slug)
representation(:standard, :id, :name, :slug, :description, :github_language, :active, :private, :owner, :default_branch, :starred)
representation(:standard, :id, :name, :slug, :description, :github_language, :active, :private, :owner, :default_branch, :starred, :managed_by_installation, :active_on_org)

This comment was marked as spam.

This comment was marked as spam.

representation(:experimental, :id, :name, :slug, :description, :github_language, :active, :private, :owner, :default_branch, :starred, :current_build, :last_started_build)

hidden_representations(:experimental)
Expand Down Expand Up @@ -57,5 +57,9 @@ def included_owner?
def owner_type
@owner_type ||= model.owner_type.downcase if model.owner_type
end

def managed_by_installation
model.managed_by_installation?
end
end
end
5 changes: 5 additions & 0 deletions lib/travis/api/v3/routes.rb
Expand Up @@ -51,6 +51,11 @@ module Routes
end
end

resource :installation do
route '/installation/{installation.github_id}'
get :find
end

This comment was marked as spam.


resource :job do
capture id: :digit
route '/job/{job.id}'
Expand Down
1 change: 1 addition & 0 deletions lib/travis/api/v3/services.rb
Expand Up @@ -18,6 +18,7 @@ module Services
EnvVar = Module.new { extend Services }
EnvVars = Module.new { extend Services }
EnterpriseLicense = Module.new { extend Services }
Installation = Module.new { extend Services }
Job = Module.new { extend Services }
Jobs = Module.new { extend Services }
KeyPair = Module.new { extend Services }
Expand Down
7 changes: 7 additions & 0 deletions lib/travis/api/v3/services/installation/find.rb
@@ -0,0 +1,7 @@
module Travis::API::V3
class Services::Installation::Find < Service
def run!
result find
end
end
end
@@ -1,6 +1,6 @@
module Travis::API::V3
class Services::Repositories::ForCurrentUser < Service
params :active, :private, :starred, :slug_filter, prefix: :repository
params :active, :private, :starred, :slug_filter, :managed_by_installation, prefix: :repository
paginate(default_limit: 100)

def run!
Expand Down
2 changes: 1 addition & 1 deletion lib/travis/api/v3/services/repositories/for_owner.rb
@@ -1,6 +1,6 @@
module Travis::API::V3
class Services::Repositories::ForOwner < Service
params :active, :private, :starred, :slug_filter, prefix: :repository
params :active, :private, :starred, :slug_filter, :managed_by_installation, prefix: :repository
paginate(default_limit: 100)

def run!
Expand Down
1 change: 0 additions & 1 deletion lib/travis/model/repository.rb
Expand Up @@ -194,5 +194,4 @@ def multi_os_enabled?
def dist_group_expansion_enabled?
Travis::Features.enabled_for_all?(:dist_group_expansion) || Travis::Features.active?(:dist_group_expansion, self)
end

end
2 changes: 1 addition & 1 deletion spec/v3/service_index_spec.rb
Expand Up @@ -135,7 +135,7 @@

describe "for_current_user action" do
let(:action) { resource.fetch("actions").fetch("for_current_user") }
specify { expect(action).to include("@type"=>"template", "request_method"=>"GET", "uri_template"=>"#{path}repos{?active,include,limit,offset,private,repository.active,repository.private,repository.starred,sort_by,starred}") }
specify { expect(action).to include("@type"=>"template", "request_method"=>"GET", "uri_template"=>"#{path}repos{?active,include,limit,managed_by_installation,offset,private,repository.active,repository.managed_by_installation,repository.private,repository.starred,sort_by,starred}") }
end
end

Expand Down
51 changes: 51 additions & 0 deletions spec/v3/services/installation/find_spec.rb
@@ -0,0 +1,51 @@
describe Travis::API::V3::Services::Installation::Find, set_app: true do
let(:user) { Travis::API::V3::Models::User.find_by_login('svenfuchs') }
let!(:installation) { Travis::API::V3::Models::Installation.create(owner_type: 'User', owner_id: user.id, github_id: 789) }

let(:token) { Travis::Api::App::AccessToken.create(user: user, app_id: 1) }
let(:headers) {{ 'HTTP_AUTHORIZATION' => "token #{token}" }}

before { user.save! }

describe "authenticated as user with access" do
before { get("/v3/installation/#{installation.github_id}", {}, headers) }
example { expect(last_response).to be_ok }
example { expect(JSON.load(body)).to be == {
"@type" => "installation",
"@href" => "/v3/installation/#{installation.github_id}",
"@representation" => "standard",
"id" => installation.id,
"github_id" => installation.github_id,
"owner" =>{
"@type"=>"user",
"@href"=>"/v3/user/#{user.id}",
"@representation"=>"minimal",
"id"=>user.id,
"login"=>user.login}
}}
end

describe "authenticated as user with access, including installation.owner" do
before { get("/v3/installation/#{installation.github_id}?include=installation.owner", {}, headers) }
example { expect(last_response).to be_ok }
example { expect(JSON.load(body)).to be == {
"@type" => "installation",
"@href" => "/v3/installation/#{installation.github_id}",
"@representation" => "standard",
"id" => installation.id,
"github_id" => installation.github_id,
"owner" =>{
"@type"=>"user",
"@href"=>"/v3/user/#{user.id}",
"@representation"=>"standard",
"@permissions"=>{"read"=>true, "sync"=>true},
"id"=>1,
"login"=>user.login,
"name"=>user.name,
"github_id"=>nil,
"avatar_url"=>"https://0.gravatar.com/avatar/07fb84848e68b96b69022d333ca8a3e2",
"is_syncing"=>nil,
"synced_at"=>nil}
}}
end
end
1 change: 1 addition & 0 deletions spec/v3/services/organization/find_spec.rb
@@ -1,5 +1,6 @@
describe Travis::API::V3::Services::Organization::Find, set_app: true do
let(:org) { Travis::API::V3::Models::Organization.new(login: 'example-org') }

before { org.save! }
after { org.delete }

Expand Down
12 changes: 9 additions & 3 deletions spec/v3/services/owner/find_spec.rb
@@ -1,7 +1,9 @@
describe Travis::API::V3::Services::Owner::Find, set_app: true do

describe "organization" do
let(:org) { Travis::API::V3::Models::Organization.new(login: 'example-org', github_id: 1234) }
before { org.save! }

before { org.save! }
after { org.delete }

describe 'existing org, public api, by login' do
Expand Down Expand Up @@ -84,7 +86,9 @@
"@href" => "/v3/repo/#{repo.id}/branch/master",
"@representation" => "minimal",
"name" => "master"},
"starred" => false
"starred" => false,
"managed_by_installation"=>false,
"active_on_org" =>nil
}]
}}
end
Expand Down Expand Up @@ -137,7 +141,9 @@
"@href" => "/v3/repo/#{repo.id}/branch/master",
"@representation"=> "minimal",
"name" => "master"},
"starred" => false
"starred" => false,
"managed_by_installation"=>false,
"active_on_org" =>nil
}]
}}
end
Expand Down
16 changes: 16 additions & 0 deletions spec/v3/services/repositories/for_current_user_spec.rb
Expand Up @@ -67,6 +67,8 @@
"@representation" => "minimal",
"name" => "master"},
"starred" => false,
"managed_by_installation"=>false,
"active_on_org"=>nil
}]
}}
end
Expand Down Expand Up @@ -114,4 +116,18 @@
example { expect(JSON.load(body)['@href']) .to be == "/v3/repos?starred=false" }
example { expect(JSON.load(body)['repositories']) .to be_empty }
end

describe "filter: managed_by_installation=true" do
before { get("/v3/repos", {"managed_by_installation" => "true"}, headers) }
example { expect(last_response) .to be_ok }
example { expect(JSON.load(body)['@href']) .to be == "/v3/repos?managed_by_installation=true" }
example { expect(JSON.load(body)['repositories']) .to be_empty }
end

describe "filter: managed_by_installation=false" do
before { get("/v3/repos", {"managed_by_installation" => "false"}, headers) }
example { expect(last_response) .to be_ok }
example { expect(JSON.load(body)['@href']) .to be == "/v3/repos?managed_by_installation=false" }
example { expect(JSON.load(body)['repositories']) .not_to be_empty }
end
end
14 changes: 12 additions & 2 deletions spec/v3/services/repositories/for_owner_spec.rb
Expand Up @@ -70,6 +70,8 @@
"@representation" => "minimal",
"name" => "master"},
"starred" => false,
"managed_by_installation"=>false,
"active_on_org" =>nil
}]}}
end

Expand Down Expand Up @@ -111,6 +113,8 @@
"@representation" =>"minimal",
"name" =>"master"},
"starred" =>false,
"managed_by_installation"=>false,
"active_on_org" =>nil,
"last_started_build"=>{
"@type" =>"build",
"@href" =>"/v3/build/#{build.id}",
Expand Down Expand Up @@ -211,6 +215,8 @@
"@representation" =>"minimal",
"name" =>"master"},
"starred" =>false,
"managed_by_installation"=>false,
"active_on_org" =>nil,
"current_build"=>{
"@type" =>"build",
"@href" =>"/v3/build/#{build.id}",
Expand Down Expand Up @@ -346,7 +352,9 @@
"@href" => "/v3/repo/1/branch/master",
"@representation"=>"minimal",
"name" => "master" },
"starred" => false }, {
"starred" => false,
"managed_by_installation"=>false,
"active_on_org" =>nil }, {
"@type" => "repository",
"@href" => "/v3/repo/#{repo2.id}",
"@representation" => "standard",
Expand Down Expand Up @@ -380,6 +388,8 @@
"@href" => "/v3/repo/#{repo2.id}/branch/master",
"@representation"=>"minimal",
"name" =>"master" },
"starred" => false}]}
"starred" => false,
"managed_by_installation"=>false,
"active_on_org" =>nil}]}
end
end