Skip to main content
A plugin registered with discussions:write can start a discussion on a trail, reply to one, and edit or delete its own messages. The usual pattern is one summary comment per trail that your plugin keeps current. For example, a deploy-preview plugin posts a table of preview URLs when a trail opens, and edits the same comment as each deployment finishes.
Trail comments are experimental. Field names may change before the contract is final.

What you need

  • discussions:write, to post and edit without granting repository read or push access. Register it and have the customer accept it. contents:write also grants posting authority, but is broader.
  • contents:read, to list a trail’s discussions or receive ref and trail webhooks. You need it to find your comment again if you don’t store its IDs; contents:write also satisfies this requirement.
  • An access token scoped to the repository, with both permissions. Mint it with this body:
Send every call below to the repository’s api_base_url, under /api/v1, with the access token as bearer token. The Go SDK does the minting and routing for you.

Post the comment

Start a discussion on the trail:
The title is optional, up to 512 characters. The body is required, up to 65,536 characters, and stores your Markdown text. A 201 returns the new discussion and its first message:
Store discussion.id and message.id with the trail. You need both to edit the comment.

Keep it current

Edit the message whenever your state changes. The new body replaces the old one, so send all of it:
Edit the comment you already posted rather than posting a new one for each update. You can also reply in the discussion, and delete a message that no longer applies.

Find your comment again

If you lost the IDs, list the trail’s discussions with GET /api/v1/repos/{repo_id}/trails/{number}/discussions. While the response has a next_cursor, pass it back as ?cursor= for the next page. Yours is the one whose created_by_identity names your installation:
Then get the discussion for its messages. Each message’s identity has the same shape. Match on installation_id, not on author: for a plugin, author is an empty string and created_by is null.

Who can change your comment

A message belongs to the installation that posted it, not to the token. A later token for the same installation can edit or delete an existing message only while the installation retains live repository coverage and posting authority (discussions:write or contents:write). The message edit and delete APIs reject a different author with 404, including another installation of your plugin or a person. Your plugin cannot change anyone else’s messages. Match the message identity’s automation.installation_id to your saved installation ID to find your own messages.

Find your comment

Where a discussion is rendered, the author label is your plugin slug with words capitalized, followed by (plugin): deploy-previews appears as Deploy Previews (plugin). Use the installation identity and saved IDs for programmatic lookup. Use the repository ID and repository Change number from the trail event to list that Change’s discussions. Find the discussion ID returned when you posted, then get its messages and match the saved message ID. A project Trail can contain Changes from several repositories; its project-level discussions use a separate API.

What discussions:write doesn’t cover

  • Resolving, reopening or retitling an ordinary discussion. These require an account principal; adding contents:write does not enable them for a plugin installation. A discussion linked to a code-review comment has a separate path that allows automation to change resolution with contents:write, but its title is read-only.
  • Starting a review, which needs contents:write.
  • Review comments and approvals.
  • Discussions on a project trail. Only trails on a repository accept plugin comments.
  • Reading discussions, which needs contents:read.

Errors

These are specific to discussions. For errors when minting the token, see Access repositories.

With the Go SDK

Add Discussions: plugin.Write to Config.Permissions, matching your registration. inst.RepositoryClient then returns a client whose Trails methods post and edit comments. This updates a stored summary comment from a trail event. Serialize calls for each installation, repository and trail, and persist the IDs durably. Creation has no idempotency key: concurrent first calls or a crash after creation but before saving can create duplicates. Recover an uncertain create by finding the existing comment before creating again:
loadComment and saveComment are your own functions. Scope them to the installation and repository, and key the comment by the trail’s stable ID. To find a comment you didn’t store, range over client.Trails.ListDiscussionsAll and match CreatedByIdentity.Automation.InstallationID against inst.ID().