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

Gist from App Compose YAML #290

Merged
merged 1 commit into from
Jun 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/controllers/apps_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ def compose_yml
end
end

def compose_gist
app = App.find(params[:id])
gist = Converters::AppConverter.new(app).to_compose_gist
html_url = gist[:html_url]
raw_url = gist[:files][Converters::AppConverter::DOCKER_COMPOSE_FILENAME][:raw_url]
render json: { links: { gist: { href: html_url, raw_url: raw_url } } }, status: 201
end

private

def app_update_params
Expand Down
11 changes: 11 additions & 0 deletions app/models/converters/app_converter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Converters
class AppConverter
DOCKER_COMPOSE_FILENAME = 'docker-compose.yml'

attr_reader :app

Expand All @@ -19,8 +20,18 @@ def to_compose_yaml
compose_hash.to_yaml
end

def to_compose_gist(filename=DOCKER_COMPOSE_FILENAME)
github_client.create_gist(description: 'created by Panamax',
public: true,
files: { filename => { content: to_compose_yaml } })
end

private

def github_client
@github_client ||= Octokit::Client.new
end

def service_to_image(service)
ServiceConverter.new(service).to_image
end
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
put :rebuild
post :template
get :compose_yml
post :compose_gist
end

resources :categories, only: [:index, :show, :create, :update, :destroy]
Expand Down
33 changes: 33 additions & 0 deletions spec/controllers/apps_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -324,4 +324,37 @@
expect(response.body).to eq('{"compose_yaml":"---\\\\n"}')
end
end

describe '#compose_gist' do
fixtures :services

let(:app) { App.first }
let(:converter_response) do
{
html_url: 'html',
files: { 'docker-compose.yml' => { raw_url: 'raw' } }
}
end
let(:converter) { double('converter', to_compose_gist: converter_response) }
let(:expected_response) { { links: { gist: { href: 'html', raw_url: 'raw' } } } }

before do
allow(Converters::AppConverter).to receive(:new).with(app).and_return(converter)
end

it 'invokes the app converter' do
expect(Converters::AppConverter).to receive(:new).with(app)
post :compose_gist, id: app.id, format: :json
end

it 'returns a created status' do
post :compose_gist, id: app.id, format: :json
expect(response.status).to eq(201)
end

it 'returns the gist URIs' do
post :compose_gist, id: app.id, format: :json
expect(response.body).to eq expected_response.to_json
end
end
end
22 changes: 22 additions & 0 deletions spec/models/converters/app_converter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,26 @@
expect(rb.keys).to match_array apps(:app1).services.map(&:name)
end
end

context '#to_compose_gist' do
fixtures :apps, :services

let(:github_client) { double('github_client', create_gist: {}) }
let(:create_params) do
{
description: 'created by Panamax',
public: true,
files: { 'docker-compose.yml' => { content: subject.to_compose_yaml } }
}
end

before do
allow(Octokit::Client).to receive(:new).and_return(github_client)
end

it 'uses Octokit to create a new gist' do
expect(github_client).to receive(:create_gist).with(create_params)
subject.to_compose_gist
end
end
end