Getting all comments from a given user on a GitHub Discussion

Featured image for sharing metadata for article

As noted previously, my team practices asynchronous, always-updating standups through GitHub Discussions.

As it's performance management season, I'm looking back at what I've done over the last few months, and wanted to get a view of all the standup updates I've made, having spent far too long trying to do it manually, and then pop them into a Markdown file that I could then reference.

To do this, I've crafted the following GraphQL API query:

query ($owner: String!, $repo: String!, $discussionNumber: Int!) {
  repository(owner: $owner, name: $repo) {
    discussion(number: $discussionNumber) {
      comments(last: 100) {
        edges {
          node {
            body
            replies(last: 100) {
              edges {
                node {
                  databaseId
                  body
                  createdAt
                  author {
                    login
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

To make it simpler, I've wrapped this in the GitHub CLI, gh, and used a small Ruby script to post-process:

require 'json'

def exit_usage
  puts 'Usage: discussion-comments.rb $owner $repo $discussion_id $user'
  exit 1
end

query = '
query ($owner: String!, $repo: String!, $discussionNumber: Int!) {
  repository(owner: $owner, name: $repo) {
    discussion(number: $discussionNumber) {
      comments(last: 100) {
        edges {
          node {
            body
            replies(last: 100) {
              edges {
                node {
                  databaseId
                  body
                  createdAt
                  author {
                    login
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
'

owner = ARGV[0]
repo = ARGV[1]
id = ARGV[2]
user = ARGV[3]

exit_usage unless owner
exit_usage unless repo
exit_usage unless id
exit_usage unless user

out = `gh api graphql -f query='#{query}' -F owner=#{owner} -F repo=#{repo} -F discussionNumber=#{id}`

data = JSON.parse(out)

data['data']['repository']['discussion']['comments']['edges'].each do |edge|
  edge['node']['replies']['edges'].each do |reply|
    next unless reply['node']['author']['login'] == user

    puts "## #{reply['node']['createdAt']}"
    puts ''
    puts reply['node']['body']
    puts ''
    puts ''
  end
end

I've purposefully not invested too much time in error handling - maybe one for next quarter's performance review?

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #github #graphql.

This blog post is 100% human-written, and has nothing derived from LLMs (be it code, editorial support or prose).

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.