contentful / contentful-cli

The official Contentful command line interface. Use Contentful features straight from the command line!
https://www.contentful.com/developers/docs/tutorials/cli/
MIT License
328 stars 61 forks source link

Update webhook definitions via: contentful space import #2705

Open marcogrcr opened 2 months ago

marcogrcr commented 2 months ago

I would like to be able to update webhooks using the contentful space import command. Currently, the command only capable of creating webhooks, but fails when attempting to update them.

Expected Behavior

The import of a webhook with an existing id results in a successful update.

Actual Behavior

The import fails with the following error:

{
  "ts": "(redacted)",
  "level": "error",
  "error": {
    "name": "Conflict",
    "entity": {
      // (redacted)
    },
    "data": {
      "status": 409,
      "statusText": "Conflict",
      "message": "Please pass sys.version as HTTP header \"X-Contentful-Version\".",
      "details": {},
      "request": {
        "url": "/spaces/(redacted)/webhook_definitions/(redacted)",
        "headers": {
          "Accept": "application/json, text/plain, */*",
          "Content-Type": "application/vnd.contentful.management.v1+json",
          "X-Contentful-User-Agent": "app contentful.cli/3.3.3; feature space-import; sdk contentful-management.js/11.31.1; platform node.js/v20.11.0; os macOS/v20.11.0;",
          "CF-Sequence": "(redacted)",
          "Authorization": "Bearer (redacted)",
          "User-Agent": "axios/1.7.3",
          "Content-Length": "310",
          "Accept-Encoding": "gzip, compress, deflate, br"
        },
        "method": "put",
        "payloadData": "(redacted)"
      },
      "requestId": "(redacted)"
    }
  }
}

Possible Solution

Based on a superficial analysis, it should be enough to populate the webhooks field of AllDestinationData in getDestinationData(), since it's currently set to an empty array.

Steps to Reproduce

  1. Create a webhook in a space.
  2. Export the webhook definition of that space using the contentful space export command.
  3. Change the name of the webhook in the exported file.
  4. Import the webhook in the same space using the contentful space import command.

Context

I want to maintain my webhooks using an infrastructure as code approach. The contentful space import command seemed like a perfect fit. However, due to this limitation I was forced to implement a custom solution. I would like to not have to maintain a custom solution and instead be able to utilize the official CLI.

Environment

See the X-Contentful-User-Agent HTTP header in the Actual Behavior section.

mgoudy91 commented 2 weeks ago

Hey @marcogrcr thanks for the question! You are correct that the default import behavior does not correctly handle updating webhooks, as it needs a version number which is not natively included in the import. I've added a ticket to our board to track this work, though I'm not sure when we will exactly get to it. In the meantime, a possible workaround that may or may not be useful:

--

Before running the import, use the Contentful Management API to fetch the existing webhooks and their versions. You can do this by querying your space for the list of existing webhooks and retrieving the sys.version for each webhook.

Example with contentful-cli:

contentful webhook get --space-id <space_id> --environment-id <environment_id> --webhook-id <webhook_id>

Modify the webhook data in your import file to include the sys.version field for each webhook definition. For example:

  "name": "My Webhook",
  "url": "https://example.com/webhook",
  "topics": ["Entry.publish"],
  "sys": {
    "id": "my-webhook-id",
    "version": 3
  }
}

This ensures that the correct version is sent with the update request, allowing the update to proceed without conflicts. Like I said, not knowing your workflow for imports this may not necessarily be a great solve, so feel free to ignore.