Skip to content

Add internal API to create access token for suggested reviewers

What does this MR do and why?

This MR adds a new internal endpoint for suggested reviewers to get short-lived access tokens and query merge requests via the public API (GraphQL).

  • This API is gated behind a feature flag suggested_reviewers_control.
  • Documentation change to the Internal API page will be in a separate MR.
  • change in this MR is highlighted in #f97f71

Implementations

POST /internal/suggested_reviewers/tokens

Attribute Type Required Description
project_id Interger Yes The ID of the project

The access token is:

  • Generated under suggested_reviewers_bot user
  • Valid for 1 day (we can't go any lower than that given the expires_at only support date granularity)
  • With access level Reporter
  • With scope read_api

Example request:

curl --request POST "https://gitlab.example.com/api/v4/internal/suggested_reviewers/tokens" \
  --header "Gitlab-Sugggested-Reviewers-Api-Request: <jwt_token>" \
  --header "Content-type: application/json" \
  --data '{ "project_id": <project_id> }'

Example response:

{
  "id": 74,
  "name": "Suggested reviewers token",
  "revoked": false,
  "created_at": "2022-11-15T10:11:56.258Z",
  "scopes": [
    "read_api"
  ],
  "user_id": 129,
  "last_used_at": null,
  "active": false,
  "expires_at": "2022-11-15",
  "access_level": 20,
  "token": "glpat-<enacted>"
}

Screenshots or screen recordings

Screenshots are required for UI changes, and strongly recommended for all other merge requests.

How to set up and validate locally

  1. Ensure a SaaS (Gitlab.com) environment
    1. One way of doing this is to add a env.runit file to the root GDK folder with the following snippet
      export GITLAB_SIMULATE_SAAS=1
  2. Set ultimate license on a group http://gdk.test:3000/admin/groups
  3. Create a project in the ultimate group or use an existing one, e.g. http://gdk.test:3000/gitlab-org/gitlab-test
  4. Set the feature flag on rails console bundle exec rails c
    project = Project.find(2)
    Feature.enable(:suggested_reviewers_control, project)
  5. Enable suggested_reviewers_enabled project settings
    project.project_setting.update!(suggested_reviewers_enabled: true)
  6. Get the secret from the console
    secret = Gitlab::AppliedMl::SuggestedReviewers.secret
  7. Generate a JWT Token
    jwt_token = JWT.encode(
      { 'iss' => Gitlab::AppliedMl::SuggestedReviewers::JWT_ISSUER, 'iat' => 1.minute.ago.to_i },
      secret, 'HS256'
    )
  8. Execute a cURL request to create a new access token for the above project
    curl --request POST \
      --url http://gdk.test:3000/api/v4/internal/suggested_reviewers/tokens \
      --header 'Content-Type: application/json' \
      --header 'Gitlab-Sugggested-Reviewers-Api-Request: <jwt_token>' \
      --data '{ "project_id": 1 }'

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #379635

Edited by Tan Le

Merge request reports

Loading