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 22 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
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -19,7 +19,7 @@ env:
cache: bundler

addons:
postgresql: 9.3
postgresql: 9.6

services:
- redis-server
Expand Down
3 changes: 2 additions & 1 deletion Rakefile
Expand Up @@ -4,7 +4,8 @@ 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"
# url = "https://raw.githubusercontent.com/travis-ci/travis-migrations/master/db/main/structure.sql"
url = "https://raw.githubusercontent.com/travis-ci/travis-migrations/cd-gh-app/db/main/structure.sql"
file = 'db/structure.sql'
system "curl -fs #{url} -o #{file} --create-dirs"
abort "failed to download #{url}" unless File.exist?(file)
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
2 changes: 1 addition & 1 deletion lib/travis/api/v3/models/organization.rb
Expand Up @@ -2,7 +2,7 @@ module Travis::API::V3
class Models::Organization < Model
has_many :memberships
has_many :users, through: :memberships

def repositories
Models::Repository.where(owner_type: 'Organization', owner_id: id)
end
Expand Down
5 changes: 5 additions & 0 deletions lib/travis/api/v3/models/repository.rb
Expand Up @@ -98,5 +98,10 @@ def debug_tools_enabled?
return true if Travis::Features.active?(:debug_tools, self)
return false
end

def managed_by_installation
return true if read_attribute(:managed_by_installation_on)
return false
end

This comment was marked as spam.

end
end
10 changes: 10 additions & 0 deletions lib/travis/api/v3/queries/installation.rb
@@ -0,0 +1,10 @@
module Travis::API::V3
class Queries::Installation < Query
params :id

This comment was marked as spam.


def find
return Models::Installation.where(github_id: id) if id

This comment was marked as spam.

raise WrongParams, 'missing github_id'.freeze
end
end
end
2 changes: 1 addition & 1 deletion lib/travis/api/v3/queries/organization.rb
@@ -1,6 +1,6 @@
module Travis::API::V3
class Queries::Organization < Query
params :id, :login, :github_id
params :id, :login, :github_id, :github_installation_id

This comment was marked as spam.


def find
return Models::Organization.find_by_id(id) if id
Expand Down
2 changes: 1 addition & 1 deletion lib/travis/api/v3/queries/user.rb
@@ -1,7 +1,7 @@
module Travis::API::V3
class Queries::User < Query
setup_sidekiq(:user_sync, queue: :sync, class_name: "Travis::GithubSync::Worker")
params :id, :login, :email, :github_id, :is_syncing
params :id, :login, :email, :github_id, :is_syncing, :github_installation_id

This comment was marked as spam.


def find
return Models::User.find_by_id(id) if id
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_type, :owner_id)

This comment was marked as spam.

end
end
2 changes: 1 addition & 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
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 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
25 changes: 25 additions & 0 deletions spec/v3/services/installation/find_spec.rb
@@ -0,0 +1,25 @@
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! }
before { installation.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",
"@permissions" => {"read"=>true, "sync"=>true},
"id" => installation.id,
"github_id" => installation.github_id,
"owner_type" => installation.owner_type,
"owner_id" => installation.owner_id
}}
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
2 changes: 2 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
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
8 changes: 8 additions & 0 deletions spec/v3/services/repository/find_spec.rb
Expand Up @@ -87,6 +87,8 @@
"@representation" => "minimal",
"name" => "master"},
"starred" => false,
"active_on_org" => nil,
"managed_by_installation" => false
})}
end

Expand Down Expand Up @@ -334,4 +336,10 @@
before { get("/v3/repo/#{repo.id}?include=repository.last_build.branch") }
include_examples '400 wrong params', 'illegal format for include parameter'
end

describe "repo managed by a github installation" do
before { repo.update_attribute(:managed_by_installation_on, "2017-11-12T12:00:00Z") }
before { get("/v3/repo/#{repo.id}") }
example { expect(parsed_body).to include("managed_by_installation" => true )}
end
end
2 changes: 2 additions & 0 deletions spec/v3/services/user/find_spec.rb
Expand Up @@ -4,6 +4,8 @@
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/user/#{user.id}", {}, headers) }
example { expect(last_response).to be_ok }
Expand Down