Support continuous sync in the GitHub import API

What does this MR do and why?

Lets POST /import/github request continuous sync with a personal access token (PAT). Until now the route always dropped the continuous_sync optional stage, so only the UI could turn sync on.

This is MR 2 of 3 for #634424 (closed). It builds on the credential_type column from !270471 (merged). The restore endpoint for a revoked token is MR 3.

On CE nothing changes: the stage is still dropped. The rest applies to EE.

Changes

  • Route and helpers: lib/api/import_github.rb no longer deletes continuous_sync. It calls two overridable helpers, with the old strip as the CE default. The new ee/lib/ee/api/import_github.rb overrides them.
    • The EE helper reads optional_stages only when it is a hash. Routes that don't declare it, like POST /import/github/gists, can receive a string or an array, and are unaffected.
    • The 422 failure entry keeps the generic Unprocessable entity text, and the optional_stages description mentions continuous_sync. openapi_v3.yaml is updated to match.
  • Hostname gate (EE): when continuous_sync is set, github_hostname must be blank, github.com or api.github.com, with an optional https://, case-insensitive. Anything else returns 422: http://, a port, userinfo or an unparsable value. The gate runs before a client is built, and the client then uses the default host.
  • Credential type (EE): the route sets credential_type = :personal_access_token on the service. It is never read from a request parameter.
  • Service (EE):
    • A PAT request skips validate_user_credential, stores no user_credential_id, and creates the sync repository with credential_type: personal_access_token. The OAuth path and the license check are unchanged.
    • The flag check runs first in validate_context. For the GitHub provider with github_continuous_import off, it returns 422 and creates no project. Gitea still drops the stage.
    • There is no preflight check of the token's scopes. A token that can't create a webhook is handled by the worker (see below).
  • Service (CE): unchanged.
  • Worker: in EnableContinuousSyncWorker, a PAT repository stays connected without a webhook when GitHub answers an exact Forbidden or NotFound.
    • The worker logs a warning, with the original Octokit error class, and the repair pass picks up changes.
    • It records a non-fatal import failure with a small WebhookNotCreatedError. The message is translated and written for users, so the raw GitHub response is not stored. It is recorded with capture_exception: false, so this expected case does not go to Sentry. The new string is in locale/gitlab.pot.
    • An OAuth Forbidden still disconnects. A 401 still disconnects both types. Rate-limit errors, which are Forbidden subclasses, still retry.
  • Logging: the new log lines use the LabKit field constants (LOG_MESSAGE, ERROR_TYPE, GL_PROJECT_ID).
  • Docs: doc/api/import.md has a continuous_sync entry under the optional_stages keys, covering the 422 cases and what happens when the token can't create a webhook, plus a history entry for the feature flag. The optional_stages table row is unchanged. I also dropped the stale "import_warning": null line from the example response. It already existed on master, and the serializer only adds that key when a warning is passed.

Behavior change to call out

With github_continuous_import off, a GitHub API import that passes continuous_sync now returns 422 instead of importing without sync. The work item's acceptance criteria ask for this. Gitea requests and CE are unchanged.

How this was checked against GitHub

  • A classic token with only the repo scope can create a webhook.
  • A fine-grained token owned by the org, with Metadata and Webhooks, passes the importer's repository and collaborators checks.
  • A fine-grained token that can see the repo but has no Webhooks permission gets 403 when listing or creating hooks.
  • Fine-grained tokens for org repos need the org as resource owner. Otherwise every call returns 404.

Not in this MR

  • The restore endpoint for a revoked token (MR 3).
  • The importer error messages and user docs that say fine-grained tokens are not supported, tracked in #639657 (closed). The later import stages are untested with a fine-grained token.

Review notes

  • NotFound handling: a PAT repository also stays connected on NotFound, which usually means the token can no longer see the repository. That follows the plan, but say if a hidden repository should disconnect instead.
  • Specs: the CE request spec, a new EE request spec (including the gists route), the EE service spec and the worker spec cover the changes. spec/lib/api/helpers/import_github_helpers_spec.rb covers the CE helper defaults on their own, because the EE overrides hide them in an EE run.
  • Database review: the Danger database warnings point at Hash#delete calls on in-memory params. This MR has no queries, scopes or migrations.

How to set up and validate locally

With an EE license and github_continuous_import enabled, run this with your own values:

curl --request POST --header "PRIVATE-TOKEN: <gitlab token>" \
  --header "Content-Type: application/json" \
  --data '{"personal_access_token":"<github token>","repo_id":<id>,"target_namespace":"<group>","optional_stages":{"continuous_sync":true}}' \
  "https://gdk.test:3000/api/v4/import/github"

Then check that:

  • github_hostname set to another host returns 422.
  • The project's sync repository has credential_type personal_access_token.
  • With the flag off, the same request returns 422 and creates no project.
  • With a fine-grained token that has no Webhooks permission, the import succeeds, the sync repository stays connected without a webhook, and an import failure with the user-facing message is recorded.
  • POST /import/github/gists with optional_stages sent as a string returns 202, not 500.

References

Relates to #634424 (closed)

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 Carla Drago

Merge request reports

Loading
Loading