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.rbno longer deletescontinuous_sync. It calls two overridable helpers, with the old strip as the CE default. The newee/lib/ee/api/import_github.rboverrides them.- The EE helper reads
optional_stagesonly when it is a hash. Routes that don't declare it, likePOST /import/github/gists, can receive a string or an array, and are unaffected. - The
422failure entry keeps the genericUnprocessable entitytext, and theoptional_stagesdescription mentionscontinuous_sync.openapi_v3.yamlis updated to match.
- The EE helper reads
- Hostname gate (EE): when
continuous_syncis set,github_hostnamemust be blank,github.comorapi.github.com, with an optionalhttps://, case-insensitive. Anything else returns422: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_tokenon the service. It is never read from a request parameter. - Service (EE):
- A PAT request skips
validate_user_credential, stores nouser_credential_id, and creates the sync repository withcredential_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 withgithub_continuous_importoff, it returns422and 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).
- A PAT request skips
- Service (CE): unchanged.
- Worker: in
EnableContinuousSyncWorker, a PAT repository stays connected without a webhook when GitHub answers an exactForbiddenorNotFound.- 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 withcapture_exception: false, so this expected case does not go to Sentry. The new string is inlocale/gitlab.pot. - An OAuth
Forbiddenstill disconnects. A401still disconnects both types. Rate-limit errors, which areForbiddensubclasses, still retry.
- Logging: the new log lines use the LabKit field constants (
LOG_MESSAGE,ERROR_TYPE,GL_PROJECT_ID). - Docs:
doc/api/import.mdhas acontinuous_syncentry under theoptional_stageskeys, covering the422cases and what happens when the token can't create a webhook, plus a history entry for the feature flag. Theoptional_stagestable row is unchanged. I also dropped the stale"import_warning": nullline 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
reposcope 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
403when 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
NotFoundhandling: a PAT repository also stays connected onNotFound, 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.rbcovers 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#deletecalls 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_hostnameset to another host returns422.- The project's sync repository has
credential_typepersonal_access_token. - With the flag off, the same request returns
422and 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/gistswithoptional_stagessent as a string returns202, not500.
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.