From 17f2b9c8215b45dbcce4d5b954276044e02c264c Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Thu, 15 Jan 2015 12:23:47 +0100 Subject: [PATCH 01/13] Fix githubify helper Conflicts: assets/scripts/app/helpers/helpers.coffee --- assets/scripts/app/helpers/helpers.coffee | 26 +++++++++++------------ 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/assets/scripts/app/helpers/helpers.coffee b/assets/scripts/app/helpers/helpers.coffee index 42cd88f1..385035dd 100644 --- a/assets/scripts/app/helpers/helpers.coffee +++ b/assets/scripts/app/helpers/helpers.coffee @@ -4,7 +4,6 @@ require 'helpers/urls' config_keys_map = Travis.CONFIG_KEYS_MAP config = Travis.config githubCommitUrl = Travis.Urls.githubCommit -currentDate = Travis.currentDate timeago = $.timeago intersect = $.intersect only = $.only @@ -47,9 +46,9 @@ formatConfig = (config) -> formatMessage = (message, options) -> message = message || '' message = message.split(/\n/)[0] if options.short - message = @_emojize(@_escape(message)) + message = _emojize(_escape(message)) if !!options.repo - message = @githubify(message, options.repo.get('owner'), options.repo.get('name')) + message = githubify(message, options.repo.get('owner'), options.repo.get('name')) if !!options.pre message = message.replace /\n/g, '
' message @@ -61,8 +60,8 @@ timeAgoInWords = (date) -> timeago.distanceInWords date durationFrom = (started, finished) -> - started = started and @_toUtc(new Date(@_normalizeDateString(started))) - finished = if finished then @_toUtc(new Date(@_normalizeDateString(finished))) else @_nowUtc() + started = started and _toUtc(new Date(_normalizeDateString(started))) + finished = if finished then _toUtc(new Date(_normalizeDateString(finished))) else _nowUtc() if started && finished then Math.round((finished - started) / 1000) else 0 timeInWords = (duration) -> @@ -82,13 +81,12 @@ timeInWords = (duration) -> if result.length > 0 then result.join(' ') else '-' githubify = (text, owner, repo) -> - self = this - text = text.replace @_githubReferenceRegexp, (reference, matchedOwner, matchedRepo, matchedNumber) -> - self._githubReferenceLink(reference, { owner: owner, repo: repo }, { owner: matchedOwner, repo: matchedRepo, number: matchedNumber } ) - text = text.replace @_githubUserRegexp, (reference, username) -> - self._githubUserLink(reference, username) - text = text.replace @_githubCommitReferenceRegexp, (reference, matchedOwner, matchedRepo, matchedSHA) -> - self._githubCommitReferenceLink(reference, { owner: owner, repo: repo }, { owner: matchedOwner, repo: matchedRepo, sha: matchedSHA }) + text = text.replace _githubReferenceRegexp, (reference, matchedOwner, matchedRepo, matchedNumber) -> + _githubReferenceLink(reference, { owner: owner, repo: repo }, { owner: matchedOwner, repo: matchedRepo, number: matchedNumber } ) + text = text.replace _githubUserRegexp, (reference, username) -> + _githubUserLink(reference, username) + text = text.replace _githubCommitReferenceRegexp, (reference, matchedOwner, matchedRepo, matchedSHA) -> + _githubCommitReferenceLink(reference, { owner: owner, repo: repo }, { owner: matchedOwner, repo: matchedRepo, sha: matchedSHA }) text _githubReferenceRegexp = new RegExp("([\\w-]+)?\\/?([\\w-]+)?(?:#|gh-)(\\d+)", 'g') @@ -118,7 +116,9 @@ _normalizeDateString = (string) -> string _nowUtc = -> - @_toUtc currentDate() + # TODO: we overwrite Travis.currentDate in tests, so we need to leave this + # global usage as it is for now, but it should be removed at some point + _toUtc Travis.currentDate() _toUtc = (date) -> Date.UTC date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds() From a0a7056f1da0b5adb5e1542c74a0e38d90edb6a0 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Fri, 16 Jan 2015 11:49:25 +0100 Subject: [PATCH 02/13] Add script/prepare_deploy to add fingerprint to js and css files --- script/prepare_deploy | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100755 script/prepare_deploy diff --git a/script/prepare_deploy b/script/prepare_deploy new file mode 100755 index 00000000..f5dad55b --- /dev/null +++ b/script/prepare_deploy @@ -0,0 +1,26 @@ +#!/usr/bin/env ruby + +require 'digest' +require 'fileutils' + +def fingerprint(file_path) + # file_path is relative to public + public_file_path = "public/#{file_path}" + digest = Digest::MD5.file public_file_path + extension = File.extname(file_path) + basename = File.basename(file_path, extension) + dirname = File.dirname(file_path) + new_file_path = "#{dirname}/#{basename}-#{digest}#{extension}" + new_public_file_path = "public/#{new_file_path}" + FileUtils.mv(public_file_path, new_public_file_path) + + index_content = File.read("public/index.html") + index_content.gsub!(file_path, new_file_path) + File.open("public/index.html", "w") { |f| + f.write index_content + } +end + +fingerprint('scripts/app.js') +fingerprint('styles/app.css') +fingerprint('styles/dashboard.css') From 06795152aa9a2d500002cbfad9ff01848c7c0613 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Fri, 16 Jan 2015 12:01:21 +0100 Subject: [PATCH 03/13] Check if file exists --- script/prepare_deploy | 1 + 1 file changed, 1 insertion(+) diff --git a/script/prepare_deploy b/script/prepare_deploy index f5dad55b..d8931438 100755 --- a/script/prepare_deploy +++ b/script/prepare_deploy @@ -6,6 +6,7 @@ require 'fileutils' def fingerprint(file_path) # file_path is relative to public public_file_path = "public/#{file_path}" + return unless File.exists?(public_file_path) digest = Digest::MD5.file public_file_path extension = File.extname(file_path) basename = File.basename(file_path, extension) From fc8f1d77f9c87bea74cf9b3b9fa12f59baad360f Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Fri, 16 Jan 2015 12:17:10 +0100 Subject: [PATCH 04/13] Return different headers for fingerprinted files --- lib/travis/web/app.rb | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/lib/travis/web/app.rb b/lib/travis/web/app.rb index 39b5317a..e7a6dc73 100644 --- a/lib/travis/web/app.rb +++ b/lib/travis/web/app.rb @@ -82,19 +82,30 @@ class Travis::Web::App def response_for(file, options = {}) content = File.read(file) - set_config(content, options) if config_needed?(file) - set_title(content) if index?(file) + if fingerprinted?(file) + headers = { + 'Content-Length' => content.bytesize.to_s, + 'Cache-Control' => cache_control(file), + 'Content-Location' => path_for(file), + 'Content-Type' => mime_type(file), + 'Expires' => (server_start + age).httpdate, + } + else + set_config(content, options) if config_needed?(file) + set_title(content) if index?(file) + + headers = { + 'Content-Length' => content.bytesize.to_s, + 'Cache-Control' => cache_control(file), + 'Content-Location' => path_for(file), + 'Content-Type' => mime_type(file), + 'ETag' => %Q{"#{version}"}, + 'Last-Modified' => server_start.httpdate, + 'Expires' => (server_start + age).httpdate, + 'Vary' => vary_for(file) + } + end - headers = { - 'Content-Length' => content.bytesize.to_s, - 'Cache-Control' => cache_control(file), - 'Content-Location' => path_for(file), - 'Content-Type' => mime_type(file), - 'ETag' => %Q{"#{version}"}, - 'Last-Modified' => server_start.httpdate, - 'Expires' => (server_start + age).httpdate, - 'Vary' => vary_for(file) - } [ 200, headers, [content] ] end @@ -110,6 +121,11 @@ class Travis::Web::App file.end_with?('index.html') end + def fingerprinted?(file) + basename = File.basename(file) + basename =~ /-[a-f0-9]{32}.(css|js)$/ + end + def cache_control(file) case path_for(file) when '/' then "public, must-revalidate" From e0b223b8a512672f33f8405936bfdce54a61222f Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Fri, 16 Jan 2015 12:28:27 +0100 Subject: [PATCH 05/13] IndexError controller should now be called MainError --- assets/scripts/app/controllers.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/scripts/app/controllers.coffee b/assets/scripts/app/controllers.coffee index 4b62306d..22f829e4 100644 --- a/assets/scripts/app/controllers.coffee +++ b/assets/scripts/app/controllers.coffee @@ -40,7 +40,7 @@ Travis.FirstSyncController = Em.Controller.extend isSyncing: Ember.computed.alias('user.isSyncing') -Travis.IndexErrorController = Em.Controller.extend() +Travis.MainErrorController = Em.Controller.extend() Travis.BuildsItemController = Em.ObjectController.extend(Travis.GithubUrlProperties, needs: ['builds'] isPullRequestsListBinding: 'controllers.builds.isPullRequestsList' From 8c543418152c96fa3e51421902cb61c33fa6a957 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Fri, 16 Jan 2015 12:41:03 +0100 Subject: [PATCH 06/13] Add ETag to fingerprinted files --- lib/travis/web/app.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/travis/web/app.rb b/lib/travis/web/app.rb index e7a6dc73..3a5ea66e 100644 --- a/lib/travis/web/app.rb +++ b/lib/travis/web/app.rb @@ -89,6 +89,7 @@ class Travis::Web::App 'Content-Location' => path_for(file), 'Content-Type' => mime_type(file), 'Expires' => (server_start + age).httpdate, + 'ETag' => %Q{"#{version}"} } else set_config(content, options) if config_needed?(file) From d53987c6e89118561e757d4449022eea4f6facbf Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Mon, 19 Jan 2015 10:40:46 +0100 Subject: [PATCH 07/13] Allow to set assets host with ASSETS_HOST This can be used to set a specific host for a CDN. --- config.ru | 2 +- lib/travis/web/app.rb | 5 +++++ public/index.html | 4 ++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/config.ru b/config.ru index 24b0f492..bb685ce3 100644 --- a/config.ru +++ b/config.ru @@ -63,5 +63,5 @@ run Travis::Web::App.build( customer_io_site_id: ENV['CUSTOMER_IO_SITE_ID'], pro: ENV['TRAVIS_PRO'], code_climate: ENV['CODE_CLIMATE'], - code_climate_url: ENV['CODE_CLIMATE_URL'] + code_climate_url: ENV['CODE_CLIMATE_URL'], ) diff --git a/lib/travis/web/app.rb b/lib/travis/web/app.rb index 3a5ea66e..2f969c39 100644 --- a/lib/travis/web/app.rb +++ b/lib/travis/web/app.rb @@ -94,6 +94,7 @@ class Travis::Web::App else set_config(content, options) if config_needed?(file) set_title(content) if index?(file) + set_assets_host(content) if index?(file) headers = { 'Content-Length' => content.bytesize.to_s, @@ -158,6 +159,10 @@ class Travis::Web::App content.gsub!(/\{\{title\}\}/, ENV['SITE_TITLE'] || default_title) end + def set_assets_host(content) + content.gsub!(/\{\{assets_host\}\}/, ENV['ASSETS_HOST'] || '') + end + def set_config(string, opts = {}) string.gsub! %r(]*>) do %() diff --git a/public/index.html b/public/index.html index 673b0ae0..f15c5cb7 100644 --- a/public/index.html +++ b/public/index.html @@ -23,8 +23,8 @@ - - + + + +