diff --git a/app/assets/javascripts/pages/projects/blob/show/index.js b/app/assets/javascripts/pages/projects/blob/show/index.js
index 84e5bb3c46e271c8639014cb4b3f27af72e815fd..aee67899ca217d7667a97b7539ec4df6e160b410 100644
--- a/app/assets/javascripts/pages/projects/blob/show/index.js
+++ b/app/assets/javascripts/pages/projects/blob/show/index.js
@@ -3,6 +3,7 @@ import commitPipelineStatus from '~/projects/tree/components/commit_pipeline_sta
import BlobViewer from '~/blob/viewer/index';
import initBlob from '~/pages/projects/init_blob';
import GpgBadges from '~/gpg_badges';
+import '~/sourcegraph/load';
document.addEventListener('DOMContentLoaded', () => {
new BlobViewer(); // eslint-disable-line no-new
diff --git a/app/assets/javascripts/pages/projects/commit/show/index.js b/app/assets/javascripts/pages/projects/commit/show/index.js
index 5aa4734244e702fed6e8b073e3003522b43084b8..0eb6f2318398a3ea2599354c5edb165b13cffbf5 100644
--- a/app/assets/javascripts/pages/projects/commit/show/index.js
+++ b/app/assets/javascripts/pages/projects/commit/show/index.js
@@ -9,6 +9,7 @@ import initNotes from '~/init_notes';
import initChangesDropdown from '~/init_changes_dropdown';
import initDiffNotes from '~/diff_notes/diff_notes_bundle';
import { fetchCommitMergeRequests } from '~/commit_merge_requests';
+import '~/sourcegraph/load';
document.addEventListener('DOMContentLoaded', () => {
const hasPerfBar = document.querySelector('.with-performance-bar');
diff --git a/app/assets/javascripts/pages/projects/merge_requests/init_merge_request_show.js b/app/assets/javascripts/pages/projects/merge_requests/init_merge_request_show.js
index fa1de1f13cb630f9a3e3b0986a294212951395a7..16034313af20d14d144546a7ed82ab94fa9a53fb 100644
--- a/app/assets/javascripts/pages/projects/merge_requests/init_merge_request_show.js
+++ b/app/assets/javascripts/pages/projects/merge_requests/init_merge_request_show.js
@@ -5,6 +5,7 @@ import { handleLocationHash } from '~/lib/utils/common_utils';
import howToMerge from '~/how_to_merge';
import initPipelines from '~/commit/pipelines/pipelines_bundle';
import initVueIssuableSidebarApp from '~/issuable_sidebar/sidebar_bundle';
+import initSourcegraph from '~/sourcegraph';
import initWidget from '../../../vue_merge_request_widget';
export default function() {
@@ -19,4 +20,5 @@ export default function() {
handleLocationHash();
howToMerge();
initWidget();
+ initSourcegraph();
}
diff --git a/app/assets/javascripts/sourcegraph/index.js b/app/assets/javascripts/sourcegraph/index.js
new file mode 100644
index 0000000000000000000000000000000000000000..796e90bf08e2659c3926f677af5f8a9b8cf4fd6e
--- /dev/null
+++ b/app/assets/javascripts/sourcegraph/index.js
@@ -0,0 +1,28 @@
+function loadScript(path) {
+ const script = document.createElement('script');
+ script.type = 'application/javascript';
+ script.src = path;
+ script.defer = true;
+ document.head.appendChild(script);
+}
+
+/**
+ * Loads the Sourcegraph integration for support for Sourcegraph extensions and
+ * code intelligence.
+ */
+export default function initSourcegraph() {
+ const { url } = gon.sourcegraph || {};
+
+ if (!url) {
+ return;
+ }
+
+ const assetsUrl = new URL('/assets/webpack/sourcegraph/', window.location.href);
+ const scriptPath = new URL('scripts/integration.bundle.js', assetsUrl).href;
+
+ window.SOURCEGRAPH_ASSETS_URL = assetsUrl.href;
+ window.SOURCEGRAPH_URL = url;
+ window.SOURCEGRAPH_INTEGRATION = 'gitlab-integration';
+
+ loadScript(scriptPath);
+}
diff --git a/app/assets/javascripts/sourcegraph/load.js b/app/assets/javascripts/sourcegraph/load.js
new file mode 100644
index 0000000000000000000000000000000000000000..f9491505d4262dea53a4d0ec232372ef9dd6ade9
--- /dev/null
+++ b/app/assets/javascripts/sourcegraph/load.js
@@ -0,0 +1,6 @@
+import initSourcegraph from './index';
+
+/**
+ * Load sourcegraph in it's own listener so that it's isolated from failures.
+ */
+document.addEventListener('DOMContentLoaded', initSourcegraph);
diff --git a/app/controllers/concerns/sourcegraph_gon.rb b/app/controllers/concerns/sourcegraph_gon.rb
new file mode 100644
index 0000000000000000000000000000000000000000..01925cf9d4d60450f9c9adad468328f0b0a85467
--- /dev/null
+++ b/app/controllers/concerns/sourcegraph_gon.rb
@@ -0,0 +1,30 @@
+# frozen_string_literal: true
+
+module SourcegraphGon
+ extend ActiveSupport::Concern
+
+ included do
+ before_action :push_sourcegraph_gon, if: :html_request?
+ end
+
+ private
+
+ def push_sourcegraph_gon
+ return unless sourcegraph_enabled?
+
+ gon.push({
+ sourcegraph: { url: Gitlab::CurrentSettings.sourcegraph_url }
+ })
+ end
+
+ def sourcegraph_enabled?
+ Gitlab::CurrentSettings.sourcegraph_enabled && sourcegraph_enabled_for_project? && current_user&.sourcegraph_enabled
+ end
+
+ def sourcegraph_enabled_for_project?
+ return false unless project && Gitlab::Sourcegraph.feature_enabled?(project)
+ return project.public? if Gitlab::CurrentSettings.sourcegraph_public_only
+
+ true
+ end
+end
diff --git a/app/controllers/profiles/preferences_controller.rb b/app/controllers/profiles/preferences_controller.rb
index 42d4d785174396c7e13b391ef450e582d860d26b..214640a5295f782e932f8629aab83c07411c0c86 100644
--- a/app/controllers/profiles/preferences_controller.rb
+++ b/app/controllers/profiles/preferences_controller.rb
@@ -47,7 +47,8 @@ def preferences_param_names
:preferred_language,
:time_display_relative,
:time_format_in_24h,
- :show_whitespace_in_diffs
+ :show_whitespace_in_diffs,
+ :sourcegraph_enabled
]
end
end
diff --git a/app/controllers/projects/blob_controller.rb b/app/controllers/projects/blob_controller.rb
index 205ec288ce9c2e1277d8c7659c9e5f2112e30243..7c97f771a70e0d96969bb53e1328fec0ce7c04b8 100644
--- a/app/controllers/projects/blob_controller.rb
+++ b/app/controllers/projects/blob_controller.rb
@@ -8,6 +8,8 @@ class Projects::BlobController < Projects::ApplicationController
include NotesHelper
include ActionView::Helpers::SanitizeHelper
include RedirectsForMissingPathOnTree
+ include SourcegraphGon
+
prepend_before_action :authenticate_user!, only: [:edit]
around_action :allow_gitaly_ref_name_caching, only: [:show]
diff --git a/app/controllers/projects/commit_controller.rb b/app/controllers/projects/commit_controller.rb
index 939a09d4fd27a376d785ed7caff5f030b6ff995d..afb670b687bc00d3f4a9ff803482e8372bb1facd 100644
--- a/app/controllers/projects/commit_controller.rb
+++ b/app/controllers/projects/commit_controller.rb
@@ -8,6 +8,7 @@ class Projects::CommitController < Projects::ApplicationController
include CreatesCommit
include DiffForPath
include DiffHelper
+ include SourcegraphGon
# Authorize
before_action :require_non_empty_project
diff --git a/app/controllers/projects/merge_requests_controller.rb b/app/controllers/projects/merge_requests_controller.rb
index 9f6f6621bf4891e05cd8104b6e20ac16aba88d59..8e5fe022dfca54d789e3024c54b8a4d66aa4c586 100644
--- a/app/controllers/projects/merge_requests_controller.rb
+++ b/app/controllers/projects/merge_requests_controller.rb
@@ -9,6 +9,7 @@ class Projects::MergeRequestsController < Projects::MergeRequests::ApplicationCo
include ToggleAwardEmoji
include IssuableCollections
include RecordUserLastActivity
+ include SourcegraphGon
skip_before_action :merge_request, only: [:index, :bulk_update]
before_action :whitelist_query_limiting, only: [:assign_related_issues, :update]
diff --git a/app/helpers/application_settings_helper.rb b/app/helpers/application_settings_helper.rb
index f0868a8d377a27f0fc190b3c83b503ad09bbec79..a011209375e21ed5b64ad619e0b9eb9dca5361ae 100644
--- a/app/helpers/application_settings_helper.rb
+++ b/app/helpers/application_settings_helper.rb
@@ -259,6 +259,9 @@ def visible_attributes
:shared_runners_text,
:sign_in_text,
:signup_enabled,
+ :sourcegraph_enabled,
+ :sourcegraph_url,
+ :sourcegraph_public_only,
:terminal_max_session_time,
:terms,
:throttle_authenticated_api_enabled,
diff --git a/app/helpers/sourcegraph_helper.rb b/app/helpers/sourcegraph_helper.rb
new file mode 100644
index 0000000000000000000000000000000000000000..cc5a5c77e9a3810c93345e68f2fb526e01a9d188
--- /dev/null
+++ b/app/helpers/sourcegraph_helper.rb
@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+
+module SourcegraphHelper
+ def sourcegraph_url_message
+ link_start = ''.html_safe % { url: Gitlab::CurrentSettings.sourcegraph_url }
+ link_end = "#{sprite_icon('external-link', size: 12, css_class: 'ml-1 vertical-align-center')}".html_safe
+
+ message =
+ if Gitlab::CurrentSettings.sourcegraph_url_is_com?
+ s_('SourcegraphPreferences|Uses %{link_start}Sourcegraph.com%{link_end}.').html_safe
+ else
+ s_('SourcegraphPreferences|Uses a custom %{link_start}Sourcegraph instance%{link_end}.').html_safe
+ end
+
+ message % { link_start: link_start, link_end: link_end }
+ end
+
+ def sourcegraph_experimental_message
+ if Gitlab::Sourcegraph.feature_conditional?
+ s_("SourcegraphPreferences|This feature is experimental and currently limited to certain projects.")
+ elsif Gitlab::CurrentSettings.sourcegraph_public_only
+ s_("SourcegraphPreferences|This feature is experimental and limited to public projects.")
+ else
+ s_("SourcegraphPreferences|This feature is experimental.")
+ end
+ end
+end
diff --git a/app/models/application_setting.rb b/app/models/application_setting.rb
index b47e1142ccab91e1fce7beb8183b52c442966370..4028d711fd1aebd1e559791d114340b2f023036d 100644
--- a/app/models/application_setting.rb
+++ b/app/models/application_setting.rb
@@ -99,6 +99,10 @@ class ApplicationSetting < ApplicationRecord
presence: true,
if: :plantuml_enabled
+ validates :sourcegraph_url,
+ presence: true,
+ if: :sourcegraph_enabled
+
validates :snowplow_collector_hostname,
presence: true,
hostname: true,
@@ -343,6 +347,10 @@ class ApplicationSetting < ApplicationRecord
end
after_commit :expire_performance_bar_allowed_user_ids_cache, if: -> { previous_changes.key?('performance_bar_allowed_group_id') }
+ def sourcegraph_url_is_com?
+ !!(sourcegraph_url =~ /\Ahttps:\/\/(www\.)?sourcegraph\.com/)
+ end
+
def self.create_from_defaults
transaction(requires_new: true) do
super
diff --git a/app/models/application_setting_implementation.rb b/app/models/application_setting_implementation.rb
index 80715fae68df2237a2e2d1e31f274a0a01680931..7bb89f0d1e20c043e91d0e150a91ae0bf5955673 100644
--- a/app/models/application_setting_implementation.rb
+++ b/app/models/application_setting_implementation.rb
@@ -102,6 +102,9 @@ def defaults
shared_runners_text: nil,
sign_in_text: nil,
signup_enabled: Settings.gitlab['signup_enabled'],
+ sourcegraph_enabled: false,
+ sourcegraph_url: nil,
+ sourcegraph_public_only: true,
terminal_max_session_time: 0,
throttle_authenticated_api_enabled: false,
throttle_authenticated_api_period_in_seconds: 3600,
diff --git a/app/models/user.rb b/app/models/user.rb
index f704589ad80e7f6ccfe195d4bd4157df57fed96a..d0e758b0055fff17675d67c5d4bc8745f088997e 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -240,6 +240,7 @@ def update_tracked_fields!(request)
delegate :time_display_relative, :time_display_relative=, to: :user_preference
delegate :time_format_in_24h, :time_format_in_24h=, to: :user_preference
delegate :show_whitespace_in_diffs, :show_whitespace_in_diffs=, to: :user_preference
+ delegate :sourcegraph_enabled, :sourcegraph_enabled=, to: :user_preference
delegate :setup_for_company, :setup_for_company=, to: :user_preference
accepts_nested_attributes_for :user_preference, update_only: true
diff --git a/app/views/admin/application_settings/_sourcegraph.html.haml b/app/views/admin/application_settings/_sourcegraph.html.haml
new file mode 100644
index 0000000000000000000000000000000000000000..23cda0334a2d596e7cb2078198371cc901138792
--- /dev/null
+++ b/app/views/admin/application_settings/_sourcegraph.html.haml
@@ -0,0 +1,38 @@
+- return unless Gitlab::Sourcegraph.feature_available?
+- expanded = integration_expanded?('sourcegraph_')
+
+%section.settings.as-sourcegraph.no-animate#js-sourcegraph-settings{ class: ('expanded' if expanded) }
+ .settings-header
+ %h4
+ = _('Sourcegraph')
+ %button.btn.btn-default.js-settings-toggle{ type: 'button' }
+ = expanded ? _('Collapse') : _('Expand')
+ %p
+ - link_start = ''.html_safe % { url: 'https://sourcegraph.com/' }
+ - link_end = "#{sprite_icon('external-link', size: 12, css_class: 'ml-1 vertical-align-center')}".html_safe
+ = s_('SourcegraphAdmin|Enable code intelligence powered by %{link_start}Sourcegraph%{link_end} on your GitLab instance\'s code views and merge requests.').html_safe % { link_start: link_start, link_end: link_end }
+ %span
+ = link_to s_('SourcegraphAdmin|More information'), help_page_path('integration/sourcegraph.md'), target: '_blank'
+
+
+ .settings-content
+ = form_for @application_setting, url: integrations_admin_application_settings_path(anchor: 'js-sourcegraph-settings'), html: { class: 'fieldset-form' } do |f|
+ = form_errors(@application_setting)
+
+ %fieldset
+ .form-group
+ .form-check
+ = f.check_box :sourcegraph_enabled, class: 'form-check-input'
+ = f.label :sourcegraph_enabled, s_('SourcegraphAdmin|Enable Sourcegraph'), class: 'form-check-label'
+ .form-group
+ .form-check
+ = f.check_box :sourcegraph_public_only, class: 'form-check-input'
+ = f.label :sourcegraph_public_only, s_('SourcegraphAdmin|Block on private and internal projects'), class: 'form-check-label'
+ .form-text.text-muted
+ = s_('SourcegraphAdmin|If checked, only public projects will have code intelligence and communicate with Sourcegraph.')
+ .form-group
+ = f.label :sourcegraph_url, s_('SourcegraphAdmin|Sourcegraph URL'), class: 'label-bold'
+ = f.text_field :sourcegraph_url, class: 'form-control', placeholder: s_('SourcegraphAdmin|e.g. https://sourcegraph.example.com')
+ .form-text.text-muted
+ = s_('SourcegraphAdmin|Configure the URL to a Sourcegraph instance which can read your GitLab projects.')
+ = f.submit s_('SourcegraphAdmin|Save changes'), class: 'btn btn-success'
diff --git a/app/views/admin/application_settings/integrations.html.haml b/app/views/admin/application_settings/integrations.html.haml
index 3f459e0f4916564b976b292e17ecee4e1120ec76..0aa833e49a84f72341c46d8421ecc06f18888ad0 100644
--- a/app/views/admin/application_settings/integrations.html.haml
+++ b/app/views/admin/application_settings/integrations.html.haml
@@ -4,6 +4,7 @@
= render_if_exists 'admin/application_settings/elasticsearch_form'
= render 'admin/application_settings/plantuml'
+= render 'admin/application_settings/sourcegraph'
= render_if_exists 'admin/application_settings/slack'
= render 'admin/application_settings/third_party_offers'
= render 'admin/application_settings/snowplow'
diff --git a/app/views/profiles/preferences/_sourcegraph.html.haml b/app/views/profiles/preferences/_sourcegraph.html.haml
new file mode 100644
index 0000000000000000000000000000000000000000..20a904694ca98ad9207bf76e85523d44878cde68
--- /dev/null
+++ b/app/views/profiles/preferences/_sourcegraph.html.haml
@@ -0,0 +1,26 @@
+- return unless Gitlab::Sourcegraph::feature_available? && Gitlab::CurrentSettings.sourcegraph_enabled
+- sourcegraph_url = Gitlab::CurrentSettings.sourcegraph_url
+
+.col-sm-12
+ %hr
+
+.col-lg-4.profile-settings-sidebar
+ %h4.prepend-top-0
+ = s_('Preferences|Integrations')
+ %p
+ = s_('Preferences|Customize integrations with third party services.')
+ = succeed '.' do
+ = link_to _('Learn more'), help_page_path('user/profile/preferences.md', anchor: 'integrations'), target: '_blank'
+.col-lg-8
+ %label.label-bold
+ = s_('Preferences|Sourcegraph')
+ = link_to icon('question-circle'), help_page_path('user/profile/preferences.md', anchor: 'sourcegraph'), target: '_blank', class: 'has-tooltip', title: _('More information')
+ .form-group.form-check
+ = f.check_box :sourcegraph_enabled, class: 'form-check-input'
+ = f.label :sourcegraph_enabled, class: 'form-check-label' do
+ - link_start = ''.html_safe % { url: sourcegraph_url }
+ - link_end = ''.html_safe
+ = s_('Preferences|Enable integrated code intelligence on code views').html_safe % { link_start: link_start, link_end: link_end }
+ .form-text.text-muted
+ = sourcegraph_url_message
+ = sourcegraph_experimental_message
diff --git a/app/views/profiles/preferences/show.html.haml b/app/views/profiles/preferences/show.html.haml
index 84657592cd822b5b1bd529fc39402880d7e732af..bf76b7379dd362022c8370d88db7c5b31dcadff8 100644
--- a/app/views/profiles/preferences/show.html.haml
+++ b/app/views/profiles/preferences/show.html.haml
@@ -111,6 +111,9 @@
= time_display_label
.form-text.text-muted
= s_('Preferences|For example: 30 mins ago.')
+
+ = render 'sourcegraph', f: f
+
.col-lg-4.profile-settings-sidebar
.col-lg-8
.form-group
diff --git a/config/webpack.config.js b/config/webpack.config.js
index 1bcd8b68ac9265e1248a6ba7d2de599d1a05fbf1..9c7a3f42c97f8ed940639bf95c32787b5c168977 100644
--- a/config/webpack.config.js
+++ b/config/webpack.config.js
@@ -299,6 +299,11 @@ module.exports = {
from: path.join(ROOT_PATH, 'node_modules/pdfjs-dist/cmaps/'),
to: path.join(ROOT_PATH, 'public/assets/webpack/cmaps/'),
},
+ {
+ from: path.join(ROOT_PATH, 'node_modules/@sourcegraph/code-host-integration/'),
+ to: path.join(ROOT_PATH, 'public/assets/webpack/sourcegraph/'),
+ ignore: ['package.json'],
+ },
{
from: path.join(
ROOT_PATH,
diff --git a/db/migrate/20190827222124_add_sourcegraph_configuration_to_application_settings.rb b/db/migrate/20190827222124_add_sourcegraph_configuration_to_application_settings.rb
new file mode 100644
index 0000000000000000000000000000000000000000..e624642c2fcda6c04ed79f7988c22cc11ec50139
--- /dev/null
+++ b/db/migrate/20190827222124_add_sourcegraph_configuration_to_application_settings.rb
@@ -0,0 +1,21 @@
+# frozen_string_literal: true
+
+# See http://doc.gitlab.com/ce/development/migration_style_guide.html
+# for more information on how to write migrations for GitLab.
+
+class AddSourcegraphConfigurationToApplicationSettings < ActiveRecord::Migration[5.2]
+ include Gitlab::Database::MigrationHelpers
+
+ # Set this constant to true if this migration requires downtime.
+ DOWNTIME = false
+
+ def up
+ add_column(:application_settings, :sourcegraph_enabled, :boolean, default: false, null: false)
+ add_column(:application_settings, :sourcegraph_url, :string, null: true, limit: 255)
+ end
+
+ def down
+ remove_column(:application_settings, :sourcegraph_enabled)
+ remove_column(:application_settings, :sourcegraph_url)
+ end
+end
diff --git a/db/migrate/20191107173446_add_sourcegraph_admin_and_user_preferences.rb b/db/migrate/20191107173446_add_sourcegraph_admin_and_user_preferences.rb
new file mode 100644
index 0000000000000000000000000000000000000000..731ed82c9994f038f9eda53fd94666e44a0eff8a
--- /dev/null
+++ b/db/migrate/20191107173446_add_sourcegraph_admin_and_user_preferences.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class AddSourcegraphAdminAndUserPreferences < ActiveRecord::Migration[5.2]
+ include Gitlab::Database::MigrationHelpers
+
+ DOWNTIME = false
+
+ def up
+ add_column(:application_settings, :sourcegraph_public_only, :boolean, default: true, null: false)
+ add_column(:user_preferences, :sourcegraph_enabled, :boolean)
+ end
+
+ def down
+ remove_column(:application_settings, :sourcegraph_public_only)
+ remove_column(:user_preferences, :sourcegraph_enabled)
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 72d1d957d6bc48d35634218f43d595d8510df154..0c542490d1e8ab25e378672b0f5ec5d4ac76f7ac 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -352,6 +352,9 @@
t.string "snowplow_app_id"
t.datetime_with_timezone "productivity_analytics_start_date"
t.string "default_ci_config_path", limit: 255
+ t.boolean "sourcegraph_enabled", default: false, null: false
+ t.string "sourcegraph_url", limit: 255
+ t.boolean "sourcegraph_public_only", default: true, null: false
t.index ["custom_project_templates_group_id"], name: "index_application_settings_on_custom_project_templates_group_id"
t.index ["file_template_project_id"], name: "index_application_settings_on_file_template_project_id"
t.index ["instance_administration_project_id"], name: "index_applicationsettings_on_instance_administration_project_id"
@@ -3771,6 +3774,7 @@
t.boolean "time_format_in_24h"
t.string "projects_sort", limit: 64
t.boolean "show_whitespace_in_diffs", default: true, null: false
+ t.boolean "sourcegraph_enabled"
t.boolean "setup_for_company"
t.index ["user_id"], name: "index_user_preferences_on_user_id", unique: true
end
diff --git a/doc/api/settings.md b/doc/api/settings.md
index f63466298e3762d8622a935bd059a92b32b3a2dd..51d5e5f35d76d77ae022bd7d83225c3488216ebb 100644
--- a/doc/api/settings.md
+++ b/doc/api/settings.md
@@ -324,6 +324,9 @@ are listed in the descriptions of the relevant settings.
| `snowplow_enabled` | boolean | no | Enable snowplow tracking. |
| `snowplow_app_id` | string | no | The Snowplow site name / application id. (e.g. `gitlab`) |
| `snowplow_iglu_registry_url` | string | no | The Snowplow base Iglu Schema Registry URL to use for custom context and self describing events'|
+| `sourcegraph_enabled` | boolean | no | Enables Sourcegraph integration. Default is `false`. **If enabled, requires** `sourcegraph_url`. |
+| `sourcegraph_url` | string | required by: `sourcegraph_enabled` | The Sourcegraph instance URL for integration. |
+| `sourcegraph_public_only` | boolean | no | Blocks Sourcegraph from being loaded on private and internal projects. Defaul is `true`. |
| `terminal_max_session_time` | integer | no | Maximum time for web terminal websocket connection (in seconds). Set to `0` for unlimited time. |
| `terms` | text | required by: `enforce_terms` | (**Required by:** `enforce_terms`) Markdown content for the ToS. |
| `throttle_authenticated_api_enabled` | boolean | no | (**If enabled, requires:** `throttle_authenticated_api_period_in_seconds` and `throttle_authenticated_api_requests_per_period`) Enable authenticated API request rate limit. Helps reduce request volume (e.g. from crawlers or abusive bots). |
diff --git a/doc/integration/README.md b/doc/integration/README.md
index 7a2c9b9bc543fe86fb96dd08e3099a648bcc6afa..3f33aa94cb902c76bb0841213d1f01ef1d73c580 100644
--- a/doc/integration/README.md
+++ b/doc/integration/README.md
@@ -54,6 +54,7 @@ GitLab can be integrated with the following enhancements:
- Add GitLab actions to [Gmail actions buttons](gmail_action_buttons_for_gitlab.md).
- Configure [PlantUML](../administration/integration/plantuml.md) to use diagrams in AsciiDoc documents.
- Attach merge requests to [Trello](trello_power_up.md) cards.
+- Enable integrated code intelligence powered by [Sourcegraph](sourcegraph.md).
## Project services
diff --git a/doc/integration/img/sourcegraph_admin_v12_5.png b/doc/integration/img/sourcegraph_admin_v12_5.png
new file mode 100644
index 0000000000000000000000000000000000000000..23e38f566193f206c2d547521bdeb7f10295a7e5
Binary files /dev/null and b/doc/integration/img/sourcegraph_admin_v12_5.png differ
diff --git a/doc/integration/img/sourcegraph_demo_v12_5.png b/doc/integration/img/sourcegraph_demo_v12_5.png
new file mode 100644
index 0000000000000000000000000000000000000000..c70448c0a8a479f7be409f2f9886be5bad04115e
Binary files /dev/null and b/doc/integration/img/sourcegraph_demo_v12_5.png differ
diff --git a/doc/integration/img/sourcegraph_popover_v12_5.png b/doc/integration/img/sourcegraph_popover_v12_5.png
new file mode 100644
index 0000000000000000000000000000000000000000..878d61436464e87c2584933d440be6070ebef698
Binary files /dev/null and b/doc/integration/img/sourcegraph_popover_v12_5.png differ
diff --git a/doc/integration/img/sourcegraph_user_preferences_v12_5.png b/doc/integration/img/sourcegraph_user_preferences_v12_5.png
new file mode 100644
index 0000000000000000000000000000000000000000..2c0e138e296c17c248f163bb6c45e8cbedd8e3ee
Binary files /dev/null and b/doc/integration/img/sourcegraph_user_preferences_v12_5.png differ
diff --git a/doc/integration/sourcegraph.md b/doc/integration/sourcegraph.md
new file mode 100644
index 0000000000000000000000000000000000000000..fc45b270d80ea1c35c554011bb60d2ba30dc08ba
--- /dev/null
+++ b/doc/integration/sourcegraph.md
@@ -0,0 +1,119 @@
+---
+type: reference, how-to
+---
+
+# Sourcegraph integration
+
+> [Introduced](https://gitlab.com/gitlab-org/gitlab/merge_requests/16556) in GitLab 12.5. Please note that this integration is [behind a feature flag](#enable-the-sourcegraph-feature-flag).
+
+[Sourcegraph](https://sourcegraph.com) provides code intelligence features, natively integrated into the GitLab UI.
+
+For GitLab.com users, see [Sourcegraph for GitLab.com](#sourcegraph-for-gitlabcom).
+
+
+
+NOTE: **Note:**
+This feature requires user opt-in. After Sourcegraph has been enabled for your GitLab instance,
+you can choose to enable Sourcegraph [through your user preferences](#enable-sourcegraph-in-user-preferences).
+
+## Set up for self-managed GitLab instances **(CORE ONLY)**
+
+Before you can enable Sourcegraph code intelligence in GitLab you will need to:
+
+- Enable the `sourcegraph` feature flag for your GitLab instance.
+- Configure a Sourcegraph instance with your GitLab instance as an external service.
+
+### Enable the Sourcegraph feature flag
+
+NOTE: **Note:**
+If you are running a self-managed instance, the Sourcegraph integration will not be available
+unless the feature flag `sourcegraph` is enabled. This can be done from the Rails console
+by instance administrators.
+
+Use these commands to start the Rails console:
+
+```sh
+# Omnibus GitLab
+gitlab-rails console
+
+# Installation from source
+cd /home/git/gitlab
+sudo -u git -H bin/rails console RAILS_ENV=production
+```
+
+Then run the following command to enable the feature flag:
+
+```
+Feature.enable(:sourcegraph)
+```
+
+You can also enable the feature flag only for specific projects with:
+
+```
+Feature.enable(:sourcegraph, Project.find_by_full_path('my_group/my_project'))
+```
+
+### Set up a self-managed Sourcegraph instance
+
+If you are new to Sourcegraph, head over to the [Sourcegraph installation documentation](https://docs.sourcegraph.com/admin) and get your instance up and running.
+
+### Connect your Sourcegraph instance to your GitLab instance
+
+1. Navigate to the site admin area in Sourcegraph.
+1. [Configure your GitLab external service](https://docs.sourcegraph.com/admin/external_service/gitlab).
+You can skip this step if you already have your GitLab repositories searchable in Sourcegraph.
+1. Validate that you can search your repositories from GitLab in your Sourcegraph instance by running a test query.
+1. Add your GitLab instance URL to the [`corsOrigin` setting](https://docs.sourcegraph.com/admin/config/site_config#corsOrigin) in your site configuration.
+
+### Configure your GitLab instance with Sourcegraph
+
+1. In GitLab, go to **Admin Area > Settings > Integrations**.
+1. Expand the **Sourcegraph** configuration section.
+1. Check **Enable Sourcegraph**.
+1. Set the Sourcegraph URL to your Sourcegraph instance, e.g., `https://sourcegraph.example.com`.
+
+
+
+## Enable Sourcegraph in user preferences
+
+If a GitLab administrator has enabled Sourcegraph, you can enable this feature in your user preferences.
+
+1. In GitLab, click your avatar in the top-right corner, then click **Settings**. On the left-hand nav, click **Preferences**.
+1. Under **Integrations**, find the **Sourcegraph** section.
+1. Check **Enable Sourcegraph**.
+
+
+
+## Using Sourcegraph code intelligence
+
+Once enabled, participating projects will have a code intelligence popover available in
+the following code views:
+
+- Merge request diffs
+- Commit view
+- File view
+
+When visiting one of these views, you can now hover over a code reference to see a popover with:
+
+- Details on how this reference was defined.
+- **Go to definition**, which navigates to the line of code where this reference was defined.
+- **Find references**, which navigates to the configured Sourcegraph instance, showing a list of references to the hilighted code.
+
+
+
+## Sourcegraph for GitLab.com
+
+Sourcegraph powered code intelligence will be incrementally rolled out on GitLab.com. It will eventually be
+available for all public projects, but for now, it is only available for some specific [`gitlab-org` projects](https://gitlab.com/gitlab-org/).
+
+If you have a private or internal project and would like integrated code intelligence, please consider
+setting up a self-managed GitLab instance.
+
+## Sourcegraph and Privacy
+
+From Sourcegraph's [extension documentation](https://docs.sourcegraph.com/integration/browser_extension#privacy) which is the
+engine behind the native GitLab integration:
+
+> Sourcegraph integrations never send any logs, pings, usage statistics, or telemetry to Sourcegraph.com.
+> They will only connect to Sourcegraph.com as required to provide code intelligence or other functionality on public code.
+> As a result, no private code, private repository names, usernames, or any other specific data is sent to Sourcegraph.com.
diff --git a/doc/user/profile/preferences.md b/doc/user/profile/preferences.md
index 9b43f6317d0f17067a5a04da5aece5de900f46fc..b299c74c8f4feed1d2ac853369e8155326d16581 100644
--- a/doc/user/profile/preferences.md
+++ b/doc/user/profile/preferences.md
@@ -128,6 +128,19 @@ You can choose one of the following options as the first day of the week:
If you select **System Default**, the system-wide default setting will be used.
+## Integrations
+
+Configure your preferences with third-party services which provide enhancements to your GitLab experience.
+
+### Sourcegraph
+
+NOTE: **Note:**
+This setting is only visible if Sourcegraph has been enabled by a GitLab administrator.
+
+Manage the availability of integrated code intelligence features powered by
+Sourcegraph. View [the Sourcegraph feature documentation](../../integration/sourcegraph.md#enable-sourcegraph-in-user-preferences)
+for more information.
+