Skip to content

Add owners internal endpoint

Bishwa Hang Rai requested to merge 443797-add-group-owners-endpoint into master

What does this MR do and why?

This MR adds a new internal endpoint to get owners for a give namespace. This will be used to fetch owners and owner email address in CustomersDot to send notification email.

Why do we need a new internal endpoint? We are making CustomersDot ready for the Cells architecture. And for that we need to migrate GraphQL calls from CustomersDot to GitLab , to internal endpoints. Related issue #443797+

This new endpoint will be used in CustomersDot to fetch the owner's email address, replacing the graphql.

Related CustomersDot MR: https://gitlab.com/gitlab-org/customers-gitlab-com/-/merge_requests/10351+

Rationale

A brief background on why we are migrating/grouping endpoints to internal endpoint under gitlab_subscriptions is in this link: !156759 (merged)

Basically, we need internal call from CustomersDot to GitLab to have custom authentication, and path based routing in Cells architecture. In future, possibly make it only privately accessible from CustomersDot.

Database

We are using existing MemberFinder to query owner and existing function notification_email_for?(namespace) , so no new query has been added in this MR.

Find direct owners of namespace
SQL
SELECT
    "members".*
FROM ( SELECT DISTINCT ON (user_id, invite_email)
        *
    FROM
        "members"
    WHERE
        "members"."type" = 'GroupMember'
        AND "members"."source_type" = 'Namespace'
        AND "members"."requested_at" IS NULL
        AND "members"."source_id" IN (
            SELECT
                "namespaces"."id"
            FROM
                "namespaces"
            WHERE
                "namespaces"."type" = 'Group'
                AND "namespaces"."id" = 9970)
            AND (members.access_level > 5)
        ORDER BY
            user_id,
            invite_email,
            CASE WHEN source_id = 9970
                and source_type = 'Namespace' THEN
                access_level + 1
            ELSE
                access_level
            END DESC,
            expires_at DESC,
            created_at ASC) members
    LEFT OUTER JOIN "users" ON "users"."id" = "members"."user_id"
WHERE
    "members"."type" = 'GroupMember'
    AND (("members"."user_id" IS NULL
            AND "members"."invite_token" IS NOT NULL)
        OR "users"."state" = 'active')
    AND "members"."requested_at" IS NULL
    AND (members.access_level > 5)
    AND "members"."access_level" = 50

PostgresAI: https://console.postgres.ai/gitlab/gitlab-production-main/sessions/29513/commands/91664

Find the notification_settings
SQL
SELECT
    "notification_settings".*
FROM
    "notification_settings"
WHERE
    "notification_settings"."source_type" = 'Namespace'
    AND "notification_settings"."source_id" = 9970
    AND "notification_settings"."user_id" = 11257463

PostgresAI: https://console.postgres.ai/gitlab/gitlab-production-main/sessions/29513/commands/91665

MR acceptance checklist

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

How to set up and validate locally

  1. Checkout this branch and run the gdk server
  2. Go to CustomersDot and and open up console bin/rails c
  3. Run the following commands
[2] pry(main)> Client::GitlabApp.get("/api/v4/internal/gitlab_subscriptions/namespaces/243/owners", { token: Client::GitlabApp::ADMIN_API_TOKEN })
=> [{"user"=>{"id"=>89, "username"=>"addon", "name"=>"Add On"}, "access_level"=>50, "notification_email"=>"addon@gitlab.com"}]
[3] pry(main)>

Alternatively, run the curl command directly:

curl --header "PRIVATE-TOKEN: <admin-token>" "http://gdk.test:3000/api/v4/internal/gitlab_subscriptions/namespaces/123/owners"
Edited by Bishwa Hang Rai

Merge request reports