You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过GitHub GraphQL API获取GitHub Action中讨论的评论Node ID

问题:GitHub Action中获取触发评论的Node ID

我正在开发一个GitHub Action,当特定讨论(例如org/repo/discussions/6#discussioncomment-7349183)的评论被创建或编辑时触发,希望Action能回复评论,内容为带链接的"your pipeline is running here"。

发现只能通过GraphQL添加回复,示例代码如下:

mutation {
  addDiscussionComment(input:{  
    discussionId: "D_something",
    replyToId: "DC_something_", 
    body: "Test reply", 
  }) {
    clientMutationId
    comment {
      id
      body  
    }
  }
}

我想通过github.event.comment.id获取评论的Node ID作为replyToId,但讨论的comments查询仅支持after/before/first/last参数,只能通过类似下面的查询批量获取:

{
  repository(owner: "org", name: "repo") {
    discussion(number: 6) {
      comments(first: 1) {
        nodes {
          id
        }
      }
    }
  }
}

但触发的评论不一定是最新的,无法确定其位置,临时方案是获取前100条评论的id和url,通过匹配url结尾的github.event.comment.id来找到对应的Node ID,想知道有没有更简洁的方案。


更优解决方案

不需要批量查询匹配,GitHub GraphQL提供了直接通过评论的数据库ID(即github.event.comment.id)查询Node ID的方式,使用discussionComment字段即可精准定位。

查询示例

{
  discussionComment(databaseId: YOUR_COMMENT_ID) {
    id
    discussion {
      id
    }
  }
}

在GitHub Action中使用的步骤

  1. 替换查询中的YOUR_COMMENT_ID为github.event.comment.id的值,比如在Action中直接代入:
{
  discussionComment(databaseId: ${{ github.event.comment.id }}) {
    id
    discussion {
      id
    }
  }
}
  1. 执行该查询后,就能直接拿到当前触发评论的Node ID(discussionComment.id),以及所属讨论的Node ID(discussionComment.discussion.id),直接用于后续的addDiscussionComment mutation。

这种方式无需批量获取评论再匹配,直接精准定位目标评论,效率更高且更可靠。

内容的提问来源于stack exchange,提问作者XuanInsr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 06:42:37