Fix race condition while creating protected branch rules
What does this MR do?
Addresses: #335661 (closed) and #335256 (closed)
While creating a security policy project via the GraphQL mutation, we also create protected branch rules for the default branch with no one having push access and only maintainers having merge access. Since the project is created with a default branch and README file, BranchHookService
is executed via post_receive
API which also creates protected branch rules for default branch with maintainer having both push and merge access.
Since these 2 execution are independent, they could run in any order. So, there are 2 different branch protection created for the default main branch sometimes:
Sometimes branch protection with multiple push and merge accesses are created for default main branch:
because ProtectedBranch
model has accepts_nested_attributes_for
push_access_levels
and merge_push_levels
, so while updating it will create new push_access_levels
and merge_push_levels
.
Fix
- Instead of updating the protected branch in
Security::SecurityOrchestrationPolicies::ProjectCreateService
, delete and recreate it with push and merge access rules - Wrap them in a transaction
This MR also addresses 2 other things:
- Change the
push_access_level
fromDeveloper
toMaintainer
as per Sam's comment: #329422 (comment 614394055) - Clean-up needed to remove
receive_message_chain
in specs as mentioned in #335256 (closed)
Changelog is not needed because this is still behind a feature flag and it's not enabled by default.
Screenshots or Screencasts (strongly suggested)
Does this MR meet the acceptance criteria?
Conformity
-
I have included changelog trailers, or none are needed. (Does this MR need a changelog?) -
I have added/updated documentation, or it's not needed. (Is documentation required?) -
I have properly separated EE content from FOSS, or this MR is FOSS only. (Where should EE code go?) -
I have added information for database reviewers in the MR description, or it's not needed. (Does this MR have database related changes?) -
I have self-reviewed this MR per code review guidelines. -
This MR does not harm performance, or I have asked a reviewer to help assess the performance impact. (Merge request performance guidelines) -
I have followed the style guides. -
This change is backwards compatible across updates, or this does not apply.
Availability and Testing
-
I have added/updated tests following the Testing Guide, or it's not needed. (Consider all test levels. See the Test Planning Process.) -
I have tested this MR in all supported browsers, or it's not needed. -
I have informed the Infrastructure department of a default or new setting change per definition of done, or it's not needed.
Security
Does this MR contain changes to processing or storing of credentials or tokens, authorization and authentication methods or other items described in the security review guidelines? If not, then delete this Security section.
-
Label as security and @ mention @gitlab-com/gl-security/appsec
-
The MR includes necessary changes to maintain consistency between UI, API, email, or other methods -
Security reports checked/validated by a reviewer from the AppSec team