Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: add branch option to remote theme import

Erin Kosewic51aba32651c6
diff --git a/app/assets/javascripts/admin/controllers/modals/admin-import-theme.js.es6 b/app/assets/javascripts/admin/controllers/modals/admin-import-theme.js.es6
index 9ce6105acb9..4b9a5aa6ddd 100644
--- a/app/assets/javascripts/admin/controllers/modals/admin-import-theme.js.es6
+++ b/app/assets/javascripts/admin/controllers/modals/admin-import-theme.js.es6
@@ -44,7 +44,8 @@ export default Ember.Controller.extend(ModalFunctionality, {
options.data.append("theme", $("#file-input")[0].files[0]);
} else {
options.data = {
- remote: this.get("uploadUrl")
+ remote: this.get("uploadUrl"),
+ branch: this.get("branch")
};
if (this.get("privateChecked")) {
diff --git a/app/assets/javascripts/admin/templates/modal/admin-import-theme.hbs b/app/assets/javascripts/admin/templates/modal/admin-import-theme.hbs
index 1de5edef8ca..ed8e7ed681a 100644
--- a/app/assets/javascripts/admin/templates/modal/admin-import-theme.hbs
+++ b/app/assets/javascripts/admin/templates/modal/admin-import-theme.hbs
@@ -14,14 +14,21 @@
<label class="radio" for="remote">{{i18n 'upload_selector.from_the_web'}}</label>
{{#if remote}}
<div class="inputs">
+ <div class='repo'>
{{input value=uploadUrl placeholder="https://github.com/discourse/sample_theme"}}
<span class="description">{{i18n 'admin.customize.theme.import_web_tip'}}</span>
+ </div>
+ <div class='branch'>
+ {{input value=branch placeholder="beta"}}
+ <span class="description">{{i18n 'admin.customize.theme.remote_branch'}}</span>
+ </div>
+ <div class='check-private'>
+ <label>
+ {{input type="checkbox" checked=privateChecked}}
+ {{i18n 'admin.customize.theme.is_private'}}
+ </label>
+ </div>
{{#if checkPrivate}}
- <div class='check-private'>
- <label>
- {{input type="checkbox" checked=privateChecked}}
- {{i18n 'admin.customize.theme.is_private'}}
- </label>
{{#if privateChecked}}
{{#if publicKey}}
<div class='public-key'>
@@ -30,10 +37,8 @@
</div>
{{/if}}
{{/if}}
- </div>
{{/if}}
</div>
-
{{/if}}
</div>
{{/d-modal-body}}
diff --git a/app/assets/stylesheets/common/admin/customize.scss b/app/assets/stylesheets/common/admin/customize.scss
index 9b5c879fd57..ce73260b585 100644
--- a/app/assets/stylesheets/common/admin/customize.scss
+++ b/app/assets/stylesheets/common/admin/customize.scss
@@ -543,6 +543,10 @@
width: 27%;
}
+.modal-body .inputs .branch {
+ margin-top: 10px;
+}
+
.modal-body .inputs .check-private {
margin-top: 10px;
label {
diff --git a/app/controllers/admin/themes_controller.rb b/app/controllers/admin/themes_controller.rb
index 7207392b0f8..3e39de47a88 100644
--- a/app/controllers/admin/themes_controller.rb
+++ b/app/controllers/admin/themes_controller.rb
@@ -76,7 +76,8 @@ class Admin::ThemesController < Admin::AdminController
end
elsif params[:remote]
begin
- @theme = RemoteTheme.import_theme(params[:remote], current_user, private_key: params[:private_key])
+ branch = params[:branch] ? params[:branch] : nil
+ @theme = RemoteTheme.import_theme(params[:remote], current_user, private_key: params[:private_key], branch: branch)
render json: @theme, status: :created
rescue RuntimeError => e
Discourse.warn_exception(e, message: "Error importing theme")
diff --git a/app/models/remote_theme.rb b/app/models/remote_theme.rb
index 9548f22fe07..eff45a652b1 100644
--- a/app/models/remote_theme.rb
+++ b/app/models/remote_theme.rb
@@ -38,8 +38,8 @@ class RemoteTheme < ActiveRecord::Base
end
end
- def self.import_theme(url, user = Discourse.system_user, private_key: nil)
- importer = ThemeStore::GitImporter.new(url, private_key: private_key)
+ def self.import_theme(url, user = Discourse.system_user, private_key: nil, branch: nil)
+ importer = ThemeStore::GitImporter.new(url, private_key: private_key, branch: branch)
importer.import!
theme_info = JSON.parse(importer["about.json"])
@@ -50,6 +50,7 @@ class RemoteTheme < ActiveRecord::Base
theme.remote_theme = remote_theme
remote_theme.private_key = private_key
+ remote_theme.branch = branch
remote_theme.remote_url = importer.url
remote_theme.update_from_remote(importer)
@@ -73,7 +74,7 @@ class RemoteTheme < ActiveRecord::Base
end
def update_remote_version
- importer = ThemeStore::GitImporter.new(remote_url, private_key: private_key)
+ importer = ThemeStore::GitImporter.new(remote_url, private_key: private_key, branch: branch)
begin
importer.import!
rescue ThemeStore::GitImporter::ImportFailed => err
@@ -91,7 +92,7 @@ class RemoteTheme < ActiveRecord::Base
unless importer
cleanup = true
- importer = ThemeStore::GitImporter.new(remote_url, private_key: private_key)
+ importer = ThemeStore::GitImporter.new(remote_url, private_key: private_key, branch: branch)
begin
importer.import!
rescue ThemeStore::GitImporter::ImportFailed => err
@@ -235,6 +236,7 @@ end
# id :integer not null, primary key
# remote_url :string not null
# remote_version :string
+# branch :string
# local_version :string
# about_url :string
# license_url :string
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 3a46df40bff..6e4fbe2b4fe 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -3270,6 +3270,7 @@ en:
import_web_tip: "Repository containing theme"
import_file_tip: ".dcstyle.json file containing theme"
is_private: "Theme is in a private git repository"
+ remote_branch: "Branch"
public_key: "Grant the following public key access to the repo:"
about_theme: "About Theme"
license: "License"
diff --git a/db/migrate/20180831182853_add_branch_to_remote_theme.rb b/db/migrate/20180831182853_add_branch_to_remote_theme.rb
new file mode 100644
index 00000000000..ba3bd42655b
--- /dev/null
+++ b/db/migrate/20180831182853_add_branch_to_remote_theme.rb
@@ -0,0 +1,5 @@
+class AddBranchToRemoteTheme < ActiveRecord::Migration[5.2]
+ def change
+ add_column :remote_themes, :branch, :string
+ end
+end
diff --git a/lib/theme_store/git_importer.rb b/lib/theme_store/git_importer.rb
index 9238c4228b7..423ffffe001 100644
--- a/lib/theme_store/git_importer.rb
+++ b/lib/theme_store/git_importer.rb
@@ -5,13 +5,14 @@ class ThemeStore::GitImporter
class ImportFailed < StandardError; end
attr_reader :url
- def initialize(url, private_key: nil)
+ def initialize(url, private_key: nil, branch: nil)
@url = url
if @url.start_with?("https://github.com") && [email protected]_with?(".git")
@url += ".git"
end
@temp_folder = "#{Pathname.new(Dir.tmpdir).realpath}/discourse_theme_#{SecureRandom.hex}"
@private_key = private_key
+ @branch = branch
end
def import!
@@ -67,7 +68,11 @@ class ThemeStore::GitImporter
def import_public!
begin
- Discourse::Utils.execute_command("git", "clone", @url, @temp_folder)
+ if @branch.present?
+ Discourse::Utils.execute_command("git", "clone", "--single-branch", "-b", @branch, @url, @temp_folder)
+ else
+ Discourse::Utils.execute_command("git", "clone", @url, @temp_folder)
+ end
rescue => err
raise ImportFailed.new(err.message)
end
@@ -83,9 +88,12 @@ class ThemeStore::GitImporter
end
begin
- Discourse::Utils.execute_command({
- 'GIT_SSH_COMMAND' => "ssh -i #{ssh_folder}/id_rsa -o StrictHostKeyChecking=no"
- }, "git", "clone", @url, @temp_folder)
+ git_ssh_command = { 'GIT_SSH_COMMAND' => "ssh -i #{ssh_folder}/id_rsa -o StrictHostKeyChecking=no" }
+ if @branch.present?
+ Discourse::Utils.execute_command(git_ssh_command, "git", "clone", "--single-branch", "-b", @branch, @url, @temp_folder)
+ else
+ Discourse::Utils.execute_command(git_ssh_command, "git", "clone", @url, @temp_folder)
+ end
rescue => err
raise ImportFailed.new(err.message)
end
diff --git a/spec/components/theme_store/git_importer_spec.rb b/spec/components/theme_store/git_importer_spec.rb
new file mode 100644
index 00000000000..177e6bf34df
--- /dev/null
+++ b/spec/components/theme_store/git_importer_spec.rb
@@ -0,0 +1,54 @@
+
+# encoding: utf-8
+
+require 'rails_helper'
+require 'theme_store/git_importer'
+
+describe ThemeStore::GitImporter do
+
+ context "#import" do
+
+ let(:url) { "https://github.com/example/example.git" }
+ let(:ssh_url) { "[email protected]:example/example.git" }
+ let(:branch) { "dev" }
+
+ before do
+ hex = "xxx"
+ SecureRandom.stubs(:hex).returns(hex)
+ @temp_folder = "#{Pathname.new(Dir.tmpdir).realpath}/discourse_theme_#{hex}"
+ @ssh_folder = "#{Pathname.new(Dir.tmpdir).realpath}/discourse_theme_ssh_#{hex}"
+ end
+
+ it "should import from http url" do
+ Discourse::Utils.expects(:execute_command).with("git", "clone", url, @temp_folder)
+
+ importer = ThemeStore::GitImporter.new(url)
+ importer.import!
+ end
+
+ it "should import from ssh url" do
+ Discourse::Utils.expects(:execute_command).with({
+ 'GIT_SSH_COMMAND' => "ssh -i #{@ssh_folder}/id_rsa -o StrictHostKeyChecking=no"
+ }, "git", "clone", ssh_url, @temp_folder)
+
+ importer = ThemeStore::GitImporter.new(ssh_url, private_key: "private_key")
+ importer.import!
+ end
+
+ it "should import branch from http url" do
+ Discourse::Utils.expects(:execute_command).with("git", "clone", "--single-branch", "-b", branch, url, @temp_folder)
+
+ importer = ThemeStore::GitImporter.new(url, branch: branch)
+ importer.import!
+ end
+
+ it "should import branch from ssh url" do
+ Discourse::Utils.expects(:execute_command).with({
+ 'GIT_SSH_COMMAND' => "ssh -i #{@ssh_folder}/id_rsa -o StrictHostKeyChecking=no"
+ }, "git", "clone", "--single-branch", "-b", branch, ssh_url, @temp_folder)
+
+ importer = ThemeStore::GitImporter.new(ssh_url, private_key: "private_key", branch: branch)
+ importer.import!
+ end
+ end
+end