diff options
author | Douwe Maan <douwe@gitlab.com> | 2015-08-25 18:42:46 -0700 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2015-08-25 18:42:46 -0700 |
commit | 046b28312704f3131e72dcd2dbdacc5264d4aa62 (patch) | |
tree | a8c2b14a6e1db3b662fee2c79af70d9fcb643c2e /app/helpers/ci | |
parent | e449426a4e7d15cdd582d4f136add52cbfb5e04e (diff) | |
download | gitlab-ce-046b28312704f3131e72dcd2dbdacc5264d4aa62.tar.gz |
Groundwork for merging CI into CE
Diffstat (limited to 'app/helpers/ci')
-rw-r--r-- | app/helpers/ci/application_helper.rb | 140 | ||||
-rw-r--r-- | app/helpers/ci/builds_helper.rb | 41 | ||||
-rw-r--r-- | app/helpers/ci/commits_helper.rb | 26 | ||||
-rw-r--r-- | app/helpers/ci/gitlab_helper.rb | 36 | ||||
-rw-r--r-- | app/helpers/ci/icons_helper.rb | 11 | ||||
-rw-r--r-- | app/helpers/ci/projects_helper.rb | 36 | ||||
-rw-r--r-- | app/helpers/ci/routes_helper.rb | 29 | ||||
-rw-r--r-- | app/helpers/ci/runners_helper.rb | 22 | ||||
-rw-r--r-- | app/helpers/ci/triggers_helper.rb | 7 | ||||
-rw-r--r-- | app/helpers/ci/user_helper.rb | 15 | ||||
-rw-r--r-- | app/helpers/ci/user_sessions_helper.rb | 32 |
11 files changed, 395 insertions, 0 deletions
diff --git a/app/helpers/ci/application_helper.rb b/app/helpers/ci/application_helper.rb new file mode 100644 index 00000000000..3198fe55f91 --- /dev/null +++ b/app/helpers/ci/application_helper.rb @@ -0,0 +1,140 @@ +module Ci + module ApplicationHelper + def loader_html + image_tag 'ci/loader.gif', alt: 'Loading' + end + + # Navigation link helper + # + # Returns an `li` element with an 'active' class if the supplied + # controller(s) and/or action(s) are currently active. The content of the + # element is the value passed to the block. + # + # options - The options hash used to determine if the element is "active" (default: {}) + # :controller - One or more controller names to check (optional). + # :action - One or more action names to check (optional). + # :path - A shorthand path, such as 'dashboard#index', to check (optional). + # :html_options - Extra options to be passed to the list element (optional). + # block - An optional block that will become the contents of the returned + # `li` element. + # + # When both :controller and :action are specified, BOTH must match in order + # to be marked as active. When only one is given, either can match. + # + # Examples + # + # # Assuming we're on TreeController#show + # + # # Controller matches, but action doesn't + # nav_link(controller: [:tree, :refs], action: :edit) { "Hello" } + # # => '<li>Hello</li>' + # + # # Controller matches + # nav_link(controller: [:tree, :refs]) { "Hello" } + # # => '<li class="active">Hello</li>' + # + # # Shorthand path + # nav_link(path: 'tree#show') { "Hello" } + # # => '<li class="active">Hello</li>' + # + # # Supplying custom options for the list element + # nav_link(controller: :tree, html_options: {class: 'home'}) { "Hello" } + # # => '<li class="home active">Hello</li>' + # + # Returns a list item element String + def nav_link(options = {}, &block) + if path = options.delete(:path) + if path.respond_to?(:each) + c = path.map { |p| p.split('#').first } + a = path.map { |p| p.split('#').last } + else + c, a, _ = path.split('#') + end + else + c = options.delete(:controller) + a = options.delete(:action) + end + + if c && a + # When given both options, make sure BOTH are active + klass = current_controller?(*c) && current_action?(*a) ? 'active' : '' + else + # Otherwise check EITHER option + klass = current_controller?(*c) || current_action?(*a) ? 'active' : '' + end + + # Add our custom class into the html_options, which may or may not exist + # and which may or may not already have a :class key + o = options.delete(:html_options) || {} + o[:class] ||= '' + o[:class] += ' ' + klass + o[:class].strip! + + if block_given? + content_tag(:li, capture(&block), o) + else + content_tag(:li, nil, o) + end + end + + # Check if a particular controller is the current one + # + # args - One or more controller names to check + # + # Examples + # + # # On TreeController + # current_controller?(:tree) # => true + # current_controller?(:commits) # => false + # current_controller?(:commits, :tree) # => true + def current_controller?(*args) + args.any? { |v| v.to_s.downcase == controller.controller_name } + end + + # Check if a particular action is the current one + # + # args - One or more action names to check + # + # Examples + # + # # On Projects#new + # current_action?(:new) # => true + # current_action?(:create) # => false + # current_action?(:new, :create) # => true + def current_action?(*args) + args.any? { |v| v.to_s.downcase == action_name } + end + + def date_from_to(from, to) + "#{from.to_s(:short)} - #{to.to_s(:short)}" + end + + def body_data_page + path = controller.controller_path.split('/') + namespace = path.first if path.second + + [namespace, controller.controller_name, controller.action_name].compact.join(":") + end + + def duration_in_words(finished_at, started_at) + if finished_at && started_at + interval_in_seconds = finished_at.to_i - started_at.to_i + elsif started_at + interval_in_seconds = Time.now.to_i - started_at.to_i + end + + time_interval_in_words(interval_in_seconds) + end + + def time_interval_in_words(interval_in_seconds) + minutes = interval_in_seconds / 60 + seconds = interval_in_seconds - minutes * 60 + + if minutes >= 1 + "#{pluralize(minutes, "minute")} #{pluralize(seconds, "second")}" + else + "#{pluralize(seconds, "second")}" + end + end + end +end diff --git a/app/helpers/ci/builds_helper.rb b/app/helpers/ci/builds_helper.rb new file mode 100644 index 00000000000..cdabdad17d2 --- /dev/null +++ b/app/helpers/ci/builds_helper.rb @@ -0,0 +1,41 @@ +module Ci + module BuildsHelper + def build_ref_link build + gitlab_ref_link build.project, build.ref + end + + def build_compare_link build + gitlab_compare_link build.project, build.commit.short_before_sha, build.short_sha + end + + def build_commit_link build + gitlab_commit_link build.project, build.short_sha + end + + def build_url(build) + ci_project_build_url(build.project, build) + end + + def build_status_alert_class(build) + if build.success? + 'alert-success' + elsif build.failed? + 'alert-danger' + elsif build.canceled? + 'alert-disabled' + else + 'alert-warning' + end + end + + def build_icon_css_class(build) + if build.success? + 'fa-circle cgreen' + elsif build.failed? + 'fa-circle cred' + else + 'fa-circle light' + end + end + end +end diff --git a/app/helpers/ci/commits_helper.rb b/app/helpers/ci/commits_helper.rb new file mode 100644 index 00000000000..0479bc10594 --- /dev/null +++ b/app/helpers/ci/commits_helper.rb @@ -0,0 +1,26 @@ +module Ci + module CommitsHelper + def commit_status_alert_class(commit) + return unless commit + + case commit.status + when 'success' + 'alert-success' + when 'failed', 'canceled' + 'alert-danger' + when 'skipped' + 'alert-disabled' + else + 'alert-warning' + end + end + + def commit_link(commit) + link_to(commit.short_sha, ci_project_ref_commit_path(commit.project, commit.ref, commit.sha)) + end + + def truncate_first_line(message, length = 50) + truncate(message.each_line.first.chomp, length: length) if message + end + end +end diff --git a/app/helpers/ci/gitlab_helper.rb b/app/helpers/ci/gitlab_helper.rb new file mode 100644 index 00000000000..2b89a0ce93e --- /dev/null +++ b/app/helpers/ci/gitlab_helper.rb @@ -0,0 +1,36 @@ +module Ci + module GitlabHelper + def no_turbolink + { :"data-no-turbolink" => "data-no-turbolink" } + end + + def gitlab_ref_link project, ref + gitlab_url = project.gitlab_url.dup + gitlab_url << "/commits/#{ref}" + link_to ref, gitlab_url, no_turbolink + end + + def gitlab_compare_link project, before, after + gitlab_url = project.gitlab_url.dup + gitlab_url << "/compare/#{before}...#{after}" + + link_to "#{before}...#{after}", gitlab_url, no_turbolink + end + + def gitlab_commit_link project, sha + gitlab_url = project.gitlab_url.dup + gitlab_url << "/commit/#{sha}" + link_to Ci::Commit.truncate_sha(sha), gitlab_url, no_turbolink + end + + def yaml_web_editor_link(project) + commits = project.commits + + if commits.any? && commits.last.push_data[:ci_yaml_file] + "#{@project.gitlab_url}/edit/master/.gitlab-ci.yml" + else + "#{@project.gitlab_url}/new/master" + end + end + end +end diff --git a/app/helpers/ci/icons_helper.rb b/app/helpers/ci/icons_helper.rb new file mode 100644 index 00000000000..ecb6ef7be45 --- /dev/null +++ b/app/helpers/ci/icons_helper.rb @@ -0,0 +1,11 @@ +module Ci + module IconsHelper + def boolean_to_icon(value) + if value.to_s == "true" + content_tag :i, nil, class: 'fa-circle cgreen' + else + content_tag :i, nil, class: 'fa-power-off clgray' + end + end + end +end diff --git a/app/helpers/ci/projects_helper.rb b/app/helpers/ci/projects_helper.rb new file mode 100644 index 00000000000..fd991a4165a --- /dev/null +++ b/app/helpers/ci/projects_helper.rb @@ -0,0 +1,36 @@ +module Ci + module ProjectsHelper + def ref_tab_class ref = nil + 'active' if ref == @ref + end + + def success_ratio(success_builds, failed_builds) + failed_builds = failed_builds.count(:all) + success_builds = success_builds.count(:all) + + return 100 if failed_builds.zero? + + ratio = (success_builds.to_f / (success_builds + failed_builds)) * 100 + ratio.to_i + end + + def markdown_badge_code(project, ref) + url = status_ci_project_url(project, ref: ref, format: 'png') + "[![build status](#{url})](#{ci_project_url(project, ref: ref)})" + end + + def html_badge_code(project, ref) + url = status_ci_project_url(project, ref: ref, format: 'png') + "<a href='#{ci_project_url(project, ref: ref)}'><img src='#{url}' /></a>" + end + + def project_uses_specific_runner?(project) + project.runners.any? + end + + def no_runners_for_project?(project) + project.runners.blank? && + Ci::Runner.shared.blank? + end + end +end diff --git a/app/helpers/ci/routes_helper.rb b/app/helpers/ci/routes_helper.rb new file mode 100644 index 00000000000..f22d5023db5 --- /dev/null +++ b/app/helpers/ci/routes_helper.rb @@ -0,0 +1,29 @@ +module Ci + module RoutesHelper + class Base + include Gitlab::Application.routes.url_helpers + + def default_url_options + { + host: Ci::Settings.gitlab_ci['host'], + protocol: Ci::Settings.gitlab_ci['https'] ? "https" : "http", + port: Ci::Settings.gitlab_ci['port'] + } + end + end + + def url_helpers + @url_helpers ||= Ci::Base.new + end + + def self.method_missing(method, *args, &block) + @url_helpers ||= Ci::Base.new + + if @url_helpers.respond_to?(method) + @url_helpers.send(method, *args, &block) + else + super method, *args, &block + end + end + end +end diff --git a/app/helpers/ci/runners_helper.rb b/app/helpers/ci/runners_helper.rb new file mode 100644 index 00000000000..782208ddfe4 --- /dev/null +++ b/app/helpers/ci/runners_helper.rb @@ -0,0 +1,22 @@ +module Ci + module RunnersHelper + def runner_status_icon(runner) + unless runner.contacted_at + return content_tag :i, nil, + class: "fa-warning-sign", + title: "New runner. Has not connected yet" + end + + status = + if runner.active? + runner.contacted_at > 3.hour.ago ? :online : :offline + else + :paused + end + + content_tag :i, nil, + class: "fa-circle runner-status-#{status}", + title: "Runner is #{status}, last contact was #{time_ago_in_words(runner.contacted_at)} ago" + end + end +end diff --git a/app/helpers/ci/triggers_helper.rb b/app/helpers/ci/triggers_helper.rb new file mode 100644 index 00000000000..caff54c3520 --- /dev/null +++ b/app/helpers/ci/triggers_helper.rb @@ -0,0 +1,7 @@ +module Ci + module TriggersHelper + def build_trigger_url(project_id, ref_name) + "#{Ci::Settings.gitlab_ci.url}/api/v1/projects/#{project_id}/refs/#{ref_name}/trigger" + end + end +end diff --git a/app/helpers/ci/user_helper.rb b/app/helpers/ci/user_helper.rb new file mode 100644 index 00000000000..c332d6ed9cf --- /dev/null +++ b/app/helpers/ci/user_helper.rb @@ -0,0 +1,15 @@ +module Ci + module UserHelper + def user_avatar_url(user = nil, size = nil, default = 'identicon') + size = 40 if size.nil? || size <= 0 + + if user.blank? || user.avatar_url.blank? + 'ci/no_avatar.png' + elsif /^(http(s?):\/\/(www|secure)\.gravatar\.com\/avatar\/(\w*))/ =~ user.avatar_url + Regexp.last_match[0] + "?s=#{size}&d=#{default}" + else + user.avatar_url + end + end + end +end diff --git a/app/helpers/ci/user_sessions_helper.rb b/app/helpers/ci/user_sessions_helper.rb new file mode 100644 index 00000000000..0296a74395c --- /dev/null +++ b/app/helpers/ci/user_sessions_helper.rb @@ -0,0 +1,32 @@ +module Ci + module UserSessionsHelper + def generate_oauth_salt + SecureRandom.hex(16) + end + + def generate_oauth_hmac(salt, return_to) + return unless return_to + digest = OpenSSL::Digest.new('sha256') + key = Gitlab::Application.secrets.db_key_base + salt + OpenSSL::HMAC.hexdigest(digest, key, return_to) + end + + def generate_oauth_state(return_to) + return unless return_to + salt = generate_oauth_salt + hmac = generate_oauth_hmac(salt, return_to) + "#{salt}:#{hmac}:#{return_to}" + end + + def get_ouath_state_return_to(state) + state.split(':', 3)[2] if state + end + + def is_oauth_state_valid?(state) + return true unless state + salt, hmac, return_to = state.split(':', 3) + return false unless return_to + hmac == generate_oauth_hmac(salt, return_to) + end + end +end |