Skip to content
GitLab
Next
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • GitLab GitLab
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 44,763
    • Issues 44,763
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
    • Requirements
  • Merge requests 1,330
    • Merge requests 1,330
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
    • Test Cases
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Container Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Metrics
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Code review
    • Insights
    • Issue
    • Repository
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • GitLab.orgGitLab.org
  • GitLabGitLab
  • Issues
  • #247865
Closed
Open
Issue created Sep 11, 2020 by Peter Leitzen@splattael🔴Maintainer

Consider removing the use of `skip_callback` and `set_callback`

Problem

In some factories we are using skip_callback (via after(:build) and set_callback to restore (via after(:create)):

  • IssueTrackerService.skip_callback(:validation, :before, :handle_properties)
  • IssueTrackerService.set_callback(:validation, :before, :handle_properties)
  • GitlabSubscription.skip_callback(:commit, :after, :index_namespace)
  • GitlabSubscription.set_callback(:commit, :after, :index_namespace)
  • MergeRequest.skip_callback(:create, :after, :ensure_merge_request_diff)
  • MergeRequest.set_callback(:create, :after, :ensure_merge_request_diff)

Now, if we just build a factory the skipped (read: removed) callback is not restored and this is permanent for the rest of spec suite's lifetime.

Example

Let's look at the JiraService spec but it could be any other as well:

diff --git a/spec/models/project_services/jira_service_spec.rb b/spec/models/project_services/jira_service_spec.rb
index 6da16905363..7efeb13a41f 100644
--- a/spec/models/project_services/jira_service_spec.rb
+++ b/spec/models/project_services/jira_service_spec.rb
@@ -12,6 +12,10 @@ RSpec.describe JiraService do
   let(:transition_id) { 'test27' }
   let(:server_info_results) { { 'deploymentType' => 'Cloud' } }
 
+  before(:all) do
+    build(:jira_service, :without_properties_callback)
+  end
+
   before do
     WebMock.stub_request(:get, /serverInfo/).to_return(body: server_info_results.to_json )
   end

After this line, handle_properties callback is disabled for the rest of the lifetime.

Luckily, we'd get failures in these cases but that might not always be true:

  41) JiraService overriding properties when data are stored in properties behaves like handles jira fields #update basic update leaves properties field emtpy
      Failure/Error: IssueTrackerService.skip_callback(:validation, :before, :handle_properties)

      ArgumentError:
        Before validation callback :handle_properties has not been defined
      Shared Example Group: "handles jira fields" called from ./spec/models/project_services/jira_service_spec.rb:395
      # ./spec/factories/services.rb:190:in `block (3 levels) in <main>'
      # ./spec/models/project_services/jira_service_spec.rb:392:in `block (4 levels) in <main>'
      # ./spec/spec_helper.rb:326:in `block (3 levels) in <main>'
      # ./spec/support/sidekiq_middleware.rb:9:in `with_sidekiq_server_middleware'
      # ./spec/spec_helper.rb:317:in `block (2 levels) in <main>'
      # ./spec/spec_helper.rb:313:in `block (3 levels) in <main>'
      # ./spec/spec_helper.rb:313:in `block (2 levels) in <main>'
      # -e:1:in `<main>'

Proposed solution

Consider removing and banning (via 👮) the use of skip_callback and set_callback in spec factories, specs or even in complete codebase.

As an alternative, we could try to use ActiveRecord's custom context. See https://guides.rubyonrails.org/active_record_validations.html#on.

Edited Sep 11, 2020 by Peter Leitzen
Assignee
Assign to
Time tracking