Consider removing the use of `skip_callback` and `set_callback`
<!--IssueSummary start-->
<details>
<summary>
Everyone can contribute. [Help move this issue forward](https://handbook.gitlab.com/handbook/marketing/developer-relations/contributor-success/community-contributors-workflows/#contributor-links) while earning points, leveling up and collecting rewards.
</summary>
- [Close this issue](https://contributors.gitlab.com/manage-issue?action=close&projectId=278964&issueIid=247865)
</details>
<!--IssueSummary end-->
## 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)`](https://gitlab.com/gitlab-org/gitlab/blob/72b6bc821/spec/factories/services.rb#L190)
* [`IssueTrackerService.set_callback(:validation, :before, :handle_properties)`](https://gitlab.com/gitlab-org/gitlab/blob/72b6bc821/spec/factories/services.rb#L196)
* [`GitlabSubscription.skip_callback(:commit, :after, :index_namespace)`](https://gitlab.com/gitlab-org/gitlab/blob/72b6bc821/ee/spec/factories/gitlab_subscriptions.rb#L55)
* [`GitlabSubscription.set_callback(:commit, :after, :index_namespace)`](https://gitlab.com/gitlab-org/gitlab/blob/72b6bc821/ee/spec/factories/gitlab_subscriptions.rb#L59)
* [`MergeRequest.skip_callback(:create, :after, :ensure_merge_request_diff)`](https://gitlab.com/gitlab-org/gitlab/blob/72b6bc821/spec/factories/merge_requests.rb#L97)
* [`MergeRequest.set_callback(:create, :after, :ensure_merge_request_diff)`](https://gitlab.com/gitlab-org/gitlab/blob/72b6bc821/spec/factories/merge_requests.rb#L101)
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
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`](https://gitlab.com/gitlab-org/gitlab/-/blob/407725a67357342a3afc513621f4cb19c0246d3b/app/models/project_services/issue_tracker_service.rb#L12) 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 :cop:) 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.
issue