backstage-plugins icon indicating copy to clipboard operation
backstage-plugins copied to clipboard

feat(bulk-import): perform more dry-run checks in the `POST /imports` backend endpoint [RHIDP-3339]

Open rm3l opened this issue 1 year ago • 3 comments

What does this PR do / why we need it:

The POST /imports endpoint supports a dryRun parameter, which currently only checks if the specified entities do exist in the catalog. It won't attempt to create any PRs in the specified repos. This PR adds additional checks:

  • if the repo already has a catalog-info YAML file in its default branch; it adds a new possible error string: CATALOG_INFO_FILE_EXISTS_IN_REPO
  • if the repo is empty; it adds a new possible error string: REPO_EMPTY

This is needed to improve the related frontend plugin.

BREAKING CHANGES:

  • The error when the specified entity already exists in the catalog has been renamed from CONFLICT to CATALOG_ENTITY_CONFLICT
  • Previous, when the dryRun query param was set to true, all items in the request body needed to have a non-blank catalogEntityName field, otherwise a 400 Bad Request was returned. This field is no longer mandatory. If it is missing, the catalog entity check will be skipped, but the new checks will still be performed. So the API now always returns a 202 status with the checks errors.

Which issue(s) this PR fixes:

Fixes https://issues.redhat.com/browse/RHIDP-3339

PR acceptance criteria:

  • [x] Unit tests

  • [ ] Integration tests

  • [x] Documentation

How to test changes / Special notes to the reviewer:

  • Add at least one GitHub integration - see https://github.com/janus-idp/backstage-plugins/tree/main/plugins/bulk-import-backend#usage
  • Start the bulk import backend:
yarn workspace @janus-idp/backstage-plugin-bulk-import-backend run start
  • Example response:

[
  {
    "errors": [],
    "catalogEntityName": "animated-happiness",
    "repository": {
      "url": "https://github.com/lemra-org-ent-2/animated-happiness",
      "name": "animated-happiness",
      "organization": "lemra-org-ent-2"
    }
  },
  {
    "errors": [
      "CATALOG_ENTITY_CONFLICT",
      "CATALOG_INFO_FILE_EXISTS_IN_REPO"
    ],
    "catalogEntityName": "java-quarkus-starter",
    "repository": {
      "url": "https://github.com/lemra-org-ent-1/java-quarkus-starter",
      "name": "java-quarkus-starter",
      "organization": "lemra-org-ent-1"
    }
  },
  {
    "errors": [
      "REPO_EMPTY"
    ],
    "catalogEntityName": "reimagined-octo-spork-empty",
    "repository": {
      "url": "https://github.com/lemra-org/reimagined-octo-spork-empty",
      "name": "reimagined-octo-spork-empty",
      "organization": "lemra-org"
    }
  }
]

rm3l avatar Jul 27 '24 07:07 rm3l

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Once this PR has been reviewed and has the lgtm label, please ask for approval from rm3l. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment Approvers can cancel approval by writing /approve cancel in a comment

openshift-ci[bot] avatar Jul 27 '24 07:07 openshift-ci[bot]

/cc @debsmita1 @ciiay

rm3l avatar Jul 27 '24 07:07 rm3l

CATALOG_INFO_FILE_EXISTS_IN_REPO

curl -X POST http://localhost:7007/api/bulk-import-backend/imports?dryRun=true -d '[{"catalogEntityName": "test-node","repository": {"url": "https://github.com/test-org-pat/test-node", "defaultBranch": "main"}, "catalogInfoContent": "---\napiVersion: backstage.io/v1alpha1\nkind: Component\nmetadata:\n  name: catalog-test-repo\n  annotations:\n    github.com/project-slug: test-org-pat/catalog-test-repo\nspec:\n  type: other\n  lifecycle: unknown\n  owner: user:default/pataknight\n---\n"}]' -H "Content-Type: application/json" -H "Authorization: Bearer $token" -v | jq

[
  {
    "errors": [
      "CATALOG_ENTITY_CONFLICT",
      "CATALOG_INFO_FILE_EXISTS_IN_REPO"
    ],
    "catalogEntityName": "test-node",
    "repository": {
      "url": "https://github.com/test-org-pat/test-node",
      "name": "test-node",
      "organization": "test-org-pat"
    }
  }
]

REPO_EMPTY

curl -X POST http://localhost:7007/api/bulk-import-backend/imports?dryRun=true -d '[{"catalogEntityName": "catalog-test-repo","repository": {"url": "https://github.com/test-org-pat/catalog-test-repo", "defaultBranch": "main"}, "catalogInfoContent": "---\napiVersion: backstage.io/v1alpha1\nkind: Component\nmetadata:\n  name: catalog-test-repo\n  annotations:\n    github.com/project-slug: test-org-pat/catalog-test-repo\nspec:\n  type: other\n  lifecycle: unknown\n  owner: user:default/pataknight\n---\n"}]' -H "Content-Type: application/json" -H "Authorization: Bearer $token" -v | jq

[
  {
    "errors": [
      "REPO_EMPTY"
    ],
    "catalogEntityName": "catalog-test-repo",
    "repository": {
      "url": "https://github.com/test-org-pat/catalog-test-repo",
      "name": "catalog-test-repo",
      "organization": "test-org-pat"
    }
  }
]

Overall, it looks good. Quick question, were you planning on resolving the sonarcloud issue in this PR?

@PatAKnight Thanks for reviewing this PR. I've refactored it to fix the issues reported by SonarCloud (mostly code duplication).

rm3l avatar Aug 19 '24 23:08 rm3l