diff --git a/app/assets/javascripts/dispatcher.js b/app/assets/javascripts/dispatcher.js index 88b4267359ad91f086c86ebf5d7ae636bb332903..e2bc11038da851b1c244d3f677c291605de7d3d7 100644 --- a/app/assets/javascripts/dispatcher.js +++ b/app/assets/javascripts/dispatcher.js @@ -156,7 +156,7 @@ import initChangesDropdown from './init_changes_dropdown'; new UsersSelect(); break; case 'projects:merge_requests:index': - new UserCallout(); + new UserCallout({ setCalloutPerProject: true }); break; case 'projects:merge_requests:index': case 'projects:issues:index': @@ -346,7 +346,7 @@ import initChangesDropdown from './init_changes_dropdown'; case 'projects:show': shortcut_handler = new ShortcutsNavigation(); new NotificationsForm(); - new UserCallout(); + new UserCallout({ setCalloutPerProject: true }); if ($('#tree-slider').length) new TreeView(); if ($('.blob-viewer').length) new BlobViewer(); @@ -367,7 +367,7 @@ import initChangesDropdown from './init_changes_dropdown'; new NewBranchForm($('.js-new-pipeline-form')); break; case 'projects:pipelines:index': - new UserCallout(); + new UserCallout({ setCalloutPerProject: true }); break; case 'projects:pipelines:builds': case 'projects:pipelines:failures': @@ -426,7 +426,7 @@ import initChangesDropdown from './init_changes_dropdown'; new TreeView(); new BlobViewer(); new NewCommitForm($('.js-create-dir-form')); - new UserCallout(); + new UserCallout({ setCalloutPerProject: true }); $('#tree-slider').waitForImages(function() { gl.utils.ajaxGet(document.querySelector('.js-tree-content').dataset.logsPath); }); diff --git a/app/assets/javascripts/user_callout.js b/app/assets/javascripts/user_callout.js index ff2208baeab6c264a11a0ac2249ea480cad7a545..eab6cee08868aa9182df2c70fb010b7c93155ce5 100644 --- a/app/assets/javascripts/user_callout.js +++ b/app/assets/javascripts/user_callout.js @@ -1,7 +1,11 @@ import Cookies from 'js-cookie'; export default class UserCallout { - constructor(className = 'user-callout') { + constructor(options = {}) { + this.options = options; + + const className = this.options.className || 'user-callout'; + this.userCalloutBody = $(`.${className}`); this.cookieName = this.userCalloutBody.data('uid'); this.isCalloutDismissed = Cookies.get(this.cookieName); @@ -17,7 +21,11 @@ export default class UserCallout { dismissCallout(e) { const $currentTarget = $(e.currentTarget); - Cookies.set(this.cookieName, 'true', { expires: 365 }); + if (this.options.setCalloutPerProject) { + Cookies.set(this.cookieName, 'true', { expires: 365, path: gon.project_url }); + } else { + Cookies.set(this.cookieName, 'true', { expires: 365 }); + } if ($currentTarget.hasClass('close')) { this.userCalloutBody.remove(); diff --git a/app/controllers/projects/application_controller.rb b/app/controllers/projects/application_controller.rb index d7dd8ddcb7d888ba55dde202ad9b787c609a5bc3..cc0d9119ff39dffe62819524faa3de773e8e5bf7 100644 --- a/app/controllers/projects/application_controller.rb +++ b/app/controllers/projects/application_controller.rb @@ -5,6 +5,7 @@ class Projects::ApplicationController < ApplicationController before_action :redirect_git_extension before_action :project before_action :repository + before_action :add_gon_project_variables layout 'project' helper_method :repository, :can_collaborate_with_project? diff --git a/lib/gitlab/gon_helper.rb b/lib/gitlab/gon_helper.rb index 9bcc579278f44583064ff204b42a207a1bc9b9f5..c8564951a2460db513a6278af84aec080a88e6cf 100644 --- a/lib/gitlab/gon_helper.rb +++ b/lib/gitlab/gon_helper.rb @@ -28,5 +28,9 @@ module Gitlab gon.current_user_avatar_url = current_user.avatar_url end end + + def add_gon_project_variables + gon.project_url = project_url(project) + end end end diff --git a/spec/javascripts/user_callout_spec.js b/spec/javascripts/user_callout_spec.js index 28d0c7dcd99b502137b3873220f9e93cb2b6d007..b64d4468ad5f94b915fcdc7158efbdd0486526fc 100644 --- a/spec/javascripts/user_callout_spec.js +++ b/spec/javascripts/user_callout_spec.js @@ -33,4 +33,26 @@ describe('UserCallout', function () { this.userCalloutBtn.click(); expect(Cookies.get(USER_CALLOUT_COOKIE)).toBe('true'); }); + + describe('Sets cookie with setCalloutPerProject', () => { + let originalGon; + beforeEach(() => { + originalGon = window.gon; + window.gon = Object.assign({}, { + project_url: 'http://localhost:3000/gitlab-org/gitlab-ce', + }); + this.userCallout = new UserCallout({ setCalloutPerProject: true }); + }); + + afterEach(() => { + window.gon = originalGon; + }); + + it('sets a cookie when the user clicks the close button', () => { + this.userCalloutBtn.click(); + // Note the path of a cookie is not accessible via JS, we can not test for that + // We can test if a cookie is set when an option is provided + expect(Cookies.get(USER_CALLOUT_COOKIE)).toBe('true'); + }); + }); });