[go: up one dir, main page]

Skip to content

Push rules: add a unique index by project_id

Problem

Discovered in #476212 (comment 2125210241).

There are cases when a project has multiple push rules which should not be possible. I suspect the duplicates get created during the project import process.

This snippet allows to detect duplicates

PushRule.where.not(project_id: nil).group(:project_id).having('count(*) > 1').count

The correct push_rule_id can be detected by project.push_rule.id.

Testing

I found a way to reproduce it locally

  1. Create from template
  2. Select NodeJS Express template
  3. Create project
  4. Project will have multiple push rules records
Click to expand
 #<PushRule:0x00000001517fe110
  id: 252,
  force_push_regex: nil,
  delete_branch_regex: nil,
  commit_message_regex: "",
  deny_delete_tag: false,
  project_id: 381,
  created_at: Mon, 23 Sep 2024 17:34:31.189212000 UTC +00:00,
  updated_at: Mon, 23 Sep 2024 17:34:31.189212000 UTC +00:00,
  author_email_regex: "",
  member_check: false,
  file_name_regex: "",
  is_sample: false,
  max_file_size: 0,
  prevent_secrets: "[FILTERED]",
  branch_name_regex: "",
  reject_unsigned_commits: nil,
  commit_committer_check: false,
  regexp_uses_re2: true,
  reject_non_dco_commits: nil,
  commit_committer_name_check: false,
  commit_message_negative_regex: "",
  organization_id: 1>,
 #<PushRule:0x00000001517fe1b0
  id: 253,
  force_push_regex: nil,
  delete_branch_regex: nil,
  commit_message_regex: "",
  deny_delete_tag: false,
  project_id: 381,
  created_at: Mon, 23 Sep 2024 17:34:35.542765000 UTC +00:00,
  updated_at: Mon, 23 Sep 2024 17:34:35.542765000 UTC +00:00,
  author_email_regex: "",
  member_check: false,
  file_name_regex: "",
  is_sample: false,
  max_file_size: 0,
  prevent_secrets: "[FILTERED]",
  branch_name_regex: "",
  reject_unsigned_commits: nil,
  commit_committer_check: nil,
  regexp_uses_re2: true,
  reject_non_dco_commits: nil,
  commit_committer_name_check: false,
  commit_message_negative_regex: "",
  organization_id: nil>]

Proposal

  1. Add Rails validation to prevent creation of duplicated records of push rules for projects
  2. Detect and remove duplicated records
  3. Add a unique index for project_id field
Edited by Vasilii Iakliushin