|
| 1 | +--- |
| 2 | +title: GitHub Comment |
| 3 | +description: Learn how to create, update, and manage comments on GitHub issues and pull requests using Alchemy. |
| 4 | +--- |
| 5 | + |
| 6 | +# Comment |
| 7 | + |
| 8 | +The Comment resource lets you manage [GitHub issue and pull request comments](https://docs.github.com/en/issues/tracking-your-work-with-issues/using-issues/commenting-on-an-issue). Comments support full GitHub Markdown and can be updated or deleted as needed. |
| 9 | + |
| 10 | +## Minimal Example |
| 11 | + |
| 12 | +Create a comment on an issue: |
| 13 | + |
| 14 | +```ts |
| 15 | +import { Comment } from "alchemy/github"; |
| 16 | + |
| 17 | +const comment = await Comment("issue-comment", { |
| 18 | + owner: "my-org", |
| 19 | + repository: "my-repo", |
| 20 | + issueNumber: 123, |
| 21 | + body: "This is a comment created by Alchemy!" |
| 22 | +}); |
| 23 | +``` |
| 24 | + |
| 25 | +## Pull Request Comment |
| 26 | + |
| 27 | +Comments work the same way for pull requests: |
| 28 | + |
| 29 | +```ts |
| 30 | +import { Comment } from "alchemy/github"; |
| 31 | + |
| 32 | +const prComment = await Comment("pr-comment", { |
| 33 | + owner: "my-org", |
| 34 | + repository: "my-repo", |
| 35 | + issueNumber: 456, // PR number |
| 36 | + body: "## Deployment Status\n\n✅ Successfully deployed to staging!" |
| 37 | +}); |
| 38 | +``` |
| 39 | + |
| 40 | +## Update Comment Content |
| 41 | + |
| 42 | +Comments can be updated by changing the body content: |
| 43 | + |
| 44 | +```ts |
| 45 | +import { Comment } from "alchemy/github"; |
| 46 | + |
| 47 | +const comment = await Comment("status-comment", { |
| 48 | + owner: "my-org", |
| 49 | + repository: "my-repo", |
| 50 | + issueNumber: 789, |
| 51 | + body: "🔄 Deployment in progress..." |
| 52 | +}); |
| 53 | + |
| 54 | +// Later, update the comment |
| 55 | +await Comment("status-comment", { |
| 56 | + owner: "my-org", |
| 57 | + repository: "my-repo", |
| 58 | + issueNumber: 789, |
| 59 | + body: "✅ Deployment completed successfully!" |
| 60 | +}); |
| 61 | +``` |
| 62 | + |
| 63 | +## Allow Comment Deletion |
| 64 | + |
| 65 | +By default comments are preserved to maintain discussion history, but you can opt-in to deletion: |
| 66 | + |
| 67 | +```ts |
| 68 | +import { Comment } from "alchemy/github"; |
| 69 | + |
| 70 | +const comment = await Comment("temp-comment", { |
| 71 | + owner: "my-org", |
| 72 | + repository: "my-repo", |
| 73 | + issueNumber: 123, |
| 74 | + body: "This comment can be deleted", |
| 75 | + allowDelete: true |
| 76 | +}); |
| 77 | +``` |
| 78 | + |
| 79 | +## Custom Authentication |
| 80 | + |
| 81 | +Pass a custom GitHub token for authentication: |
| 82 | + |
| 83 | +```ts |
| 84 | +import { Comment } from "alchemy/github"; |
| 85 | + |
| 86 | +const comment = await Comment("authenticated-comment", { |
| 87 | + owner: "my-org", |
| 88 | + repository: "my-repo", |
| 89 | + issueNumber: 123, |
| 90 | + body: "Comment with custom token", |
| 91 | + token: alchemy.secret(process.env.CUSTOM_GITHUB_TOKEN) |
| 92 | +}); |
| 93 | +``` |
| 94 | + |
| 95 | +## Properties |
| 96 | + |
| 97 | +The Comment resource supports the following properties: |
| 98 | + |
| 99 | +### Input Properties |
| 100 | + |
| 101 | +- `owner` (string, required): Repository owner (user or organization) |
| 102 | +- `repository` (string, required): Repository name |
| 103 | +- `issueNumber` (number, required): Issue or Pull Request number to comment on |
| 104 | +- `body` (string, required): Comment body (supports GitHub Markdown) |
| 105 | +- `allowDelete` (boolean, optional): Whether to allow deletion of the comment. Default: `false` to preserve discussion history |
| 106 | +- `token` (Secret, optional): GitHub API token. If not provided, uses environment variables `GITHUB_TOKEN` or `GITHUB_ACCESS_TOKEN` |
| 107 | + |
| 108 | +### Output Properties |
| 109 | + |
| 110 | +- `id` (string): The resource ID |
| 111 | +- `commentId` (number): The numeric ID of the comment in GitHub |
| 112 | +- `htmlUrl` (string): URL to view the comment |
| 113 | +- `updatedAt` (string): Time at which the comment was created/updated |
| 114 | +- All input properties except `token` |
| 115 | + |
| 116 | +## Authentication |
| 117 | + |
| 118 | +Authentication is handled in the following order: |
| 119 | + |
| 120 | +1. `token` parameter in the resource props (if provided) |
| 121 | +2. `GITHUB_ACCESS_TOKEN` environment variable (for actions with admin permissions) |
| 122 | +3. `GITHUB_TOKEN` environment variable |
| 123 | +4. GitHub CLI token (if gh is installed and authenticated) |
| 124 | + |
| 125 | +The token must have the following permissions: |
| 126 | +- 'repo' scope for private repositories |
| 127 | +- 'public_repo' scope for public repositories |
0 commit comments