Add per work item :read_work_item check

What does this MR do and why?

  • This MR runs the :read_work_item policy check for each work item returned in the listing query. This resolves a bug where previously requirements would be returned even when the feature is disabled.

References

Screenshots or screen recordings

Not provided

How to set up and validate locally

  1. Enable the work_item_rest_api_index feature flag

  2. Create a non-admin member user and test data

project = Project.find(7)
author = User.find(1)
 
member_user = Users::CreateService.new(User.find(1), {
  name: 'Test Member',
  username: 'test_member_wi',
  email: 'test_member_wi@example.com',
  password: 'xK9#mP2$vL5!qR8',
  skip_confirmation: true,
  organization_id: Organizations::Organization.default_organization.id
}).execute
member_user = User.find_by(username: 'test_member_wi')
project.add_developer(member_user)

regular_wi = WorkItem.create!(
  project: project,
  work_item_type: WorkItems::TypesFramework::SystemDefined::Type.find_by(base_type: :issue),
  title: "Regular issue work item",
  author: author
)
 
requirement_wi = WorkItem.create!(
  project: project,
  work_item_type: WorkItems::TypesFramework::SystemDefined::Type.find_by(base_type: 'requirement'),
  title: "Requirement work item",
  author: author
)
 
puts "Regular WI IID: #{regular_wi.iid}, ID: #{regular_wi.id}"
puts "Requirement WI IID: #{requirement_wi.iid}, ID: #{requirement_wi.id}"
  1. Generate a PAT for the member user
token = member_user.personal_access_tokens.create!(
  name: 'test-token-2',
  scopes: [:api],
  expires_at: 1.day.from_now
)
puts token.token
  1. Disable the requirements feature on the project
project.project_feature.update!(requirements_access_level: ProjectFeature::DISABLED)
  1. Verify the bug exists on master

On the master branch, fetch the work items list. The requirement work item should be incorrectly included despite requirements_access_level being disabled:

curl --silent --header "PRIVATE-TOKEN: <your-pat>" \
  "http://localhost:3000/api/v4/projects/7/-/work_items" \
  | jq '[.[] | select(.id == <regular_wi_id> or .id == <requirement_wi_id>) | {id: .id, title: .title}]'

Expected on master (buggy): both work items appear, including the requirement - even though requirements are disabled.

  1. Check out the add-per-work-item-policy-check-in-rest-api branch and restart rails-web

  2. Verify the fix

Fetch the work items list again with the same PAT:

curl --silent --header "PRIVATE-TOKEN: <your-pat>" \
  "http://localhost:3000/api/v4/projects/7/-/work_items" \
  | jq '[.[] | select(.id == <regular_wi_id> or .id == <requirement_wi_id>) | {id: .id, title: .title}]'

Expected: only the regular work item appears; the requirement is excluded.

[
  {
    "id": <regular_wi_id>,
    "title": "Regular issue work item"
  }
]
  1. Re-enable requirements and confirm the requirement reappears
# Rails console
project.project_feature.update!(requirements_access_level: ProjectFeature::ENABLED)
curl --silent --header "PRIVATE-TOKEN: <your-pat>" \
  "http://localhost:3000/api/v4/projects/7/-/work_items" \
  | jq '[.[] | select(.id == <regular_wi_id> or .id == <requirement_wi_id>) | {id: .id, title: .title}]'

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Matt D'Angelo

Merge request reports

Loading
Loading