Skip to content

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

FEATURE: add link to see new commits when updates are available for themes (#6233)

Osama Sayegh18b396ad569c
diff --git a/app/assets/javascripts/admin/templates/customize-themes-show.hbs b/app/assets/javascripts/admin/templates/customize-themes-show.hbs
index fcd933553ca..c83b93a1768 100644
--- a/app/assets/javascripts/admin/templates/customize-themes-show.hbs
+++ b/app/assets/javascripts/admin/templates/customize-themes-show.hbs
@@ -77,6 +77,11 @@
{{else}}
{{#if model.remote_theme.commits_behind}}
{{i18n 'admin.customize.theme.commits_behind' count=model.remote_theme.commits_behind}}
+ {{#if model.remote_theme.github_diff_link}}
+ <a href="{{model.remote_theme.github_diff_link}}">
+ {{i18n 'admin.customize.theme.compare_commits'}}
+ </a>
+ {{/if}}
{{else}}
{{i18n 'admin.customize.theme.up_to_date'}} {{format-date model.remote_theme.updated_at leaveAgo="true"}}
{{/if}}
diff --git a/app/models/remote_theme.rb b/app/models/remote_theme.rb
index 578711a4c59..95889f76bc0 100644
--- a/app/models/remote_theme.rb
+++ b/app/models/remote_theme.rb
@@ -6,6 +6,9 @@ class RemoteTheme < ActiveRecord::Base
ALLOWED_FIELDS = %w{scss embedded_scss head_tag header after_header body_tag footer}
+ GITHUB_REGEXP = /^https?:\/\/github\.com\//
+ GITHUB_SSH_REGEXP = /^git@github\.com:/
+
has_one :theme
def self.update_tgz_theme(filename, user: Discourse.system_user)
@@ -189,6 +192,21 @@ class RemoteTheme < ActiveRecord::Base
end
end
+ def github_diff_link
+ if github_repo_url.present? && local_version != remote_version
+ "#{github_repo_url.gsub(/\.git$/, "")}/compare/#{local_version}...#{remote_version}"
+ end
+ end
+
+ def github_repo_url
+ url = remote_url.strip
+ return url if url.match?(GITHUB_REGEXP)
+
+ if url.match?(GITHUB_SSH_REGEXP)
+ org_repo = url.gsub(GITHUB_SSH_REGEXP, "")
+ "https://github.com/#{org_repo}"
+ end
+ end
end
# == Schema Information
diff --git a/app/serializers/theme_serializer.rb b/app/serializers/theme_serializer.rb
index 47e0e1222bb..fc2ce44cc89 100644
--- a/app/serializers/theme_serializer.rb
+++ b/app/serializers/theme_serializer.rb
@@ -46,13 +46,18 @@ end
class RemoteThemeSerializer < ApplicationSerializer
attributes :id, :remote_url, :remote_version, :local_version, :about_url,
- :license_url, :commits_behind, :remote_updated_at, :updated_at
+ :license_url, :commits_behind, :remote_updated_at, :updated_at,
+ :github_diff_link
# wow, AMS has some pretty nutty logic where it tries to find the path here
# from action dispatch, tell it not to
def about_url
object.about_url
end
+
+ def include_github_diff_link?
+ github_diff_link.present?
+ end
end
class ThemeSerializer < ChildThemeSerializer
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index e19977a233d..7ed274c0609 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -3235,6 +3235,7 @@ en:
commits_behind:
one: "Theme is 1 commit behind!"
other: "Theme is {{count}} commits behind!"
+ compare_commits: "(See new commits)"
scss:
text: "CSS"
title: "Enter custom CSS, we accept all valid CSS and SCSS styles"
diff --git a/spec/models/remote_theme_spec.rb b/spec/models/remote_theme_spec.rb
index 202e170b0a3..1cb02110354 100644
--- a/spec/models/remote_theme_spec.rb
+++ b/spec/models/remote_theme_spec.rb
@@ -152,6 +152,41 @@ describe RemoteTheme do
end
end
+ let(:github_repo) do
+ RemoteTheme.create!(
+ remote_url: "https://github.com/org/testtheme.git",
+ local_version: "a2ec030e551fc8d8579790e1954876fe769fe40a",
+ remote_version: "21122230dbfed804067849393c3332083ddd0c07",
+ commits_behind: 2
+ )
+ end
+
+ let(:gitlab_repo) do
+ RemoteTheme.create!(
+ remote_url: "https://gitlab.com/org/repo.git",
+ local_version: "a2ec030e551fc8d8579790e1954876fe769fe40a",
+ remote_version: "21122230dbfed804067849393c3332083ddd0c07",
+ commits_behind: 5
+ )
+ end
+
+ context "#github_diff_link" do
+ it "is blank for non-github repos" do
+ expect(gitlab_repo.github_diff_link).to be_blank
+ end
+
+ it "returns URL for comparing between local_version and remote_version" do
+ expect(github_repo.github_diff_link).to eq(
+ "https://github.com/org/testtheme/compare/#{github_repo.local_version}...#{github_repo.remote_version}"
+ )
+ end
+
+ it "is blank when theme is up-to-date" do
+ github_repo.update!(local_version: github_repo.remote_version, commits_behind: 0)
+ expect(github_repo.reload.github_diff_link).to be_blank
+ end
+ end
+
context ".out_of_date_themes" do
let(:remote) { RemoteTheme.create!(remote_url: "https://github.com/org/testtheme") }
let!(:theme) { Theme.create!(remote_theme_id: remote.id, name: "Test Theme", user_id: -1) }