Skip to content

fix: repair dead discussion mentor-counting branch#776

Open
kaizeenn wants to merge 2 commits into
github-community-projects:mainfrom
kaizeenn:fix/774-discussion-mentor-counting
Open

fix: repair dead discussion mentor-counting branch#776
kaizeenn wants to merge 2 commits into
github-community-projects:mainfrom
kaizeenn:fix/774-discussion-mentor-counting

Conversation

@kaizeenn

Copy link
Copy Markdown

Description

Fixes #774.

Three layered defects made enable_mentor_count a complete no-op (or a crash) for GitHub Discussions:

1. GraphQL fetched no comment author data

discussions.py queried comments(first: 1) { nodes { createdAt } } — no author field, and only 1 comment. Expanded to comments(first: 100) with author { login __typename } on each comment node. Added author { login __typename } to the discussion node itself so the opener's login is available for the self-comment filter.

2. Attribute access on dict nodes would AttributeError

most_active_mentors.py used comment.user.login, comment.submitted_at, comment.ready_for_review_at on plain GraphQL dicts. Replaced with comment.get('author') or {} dict access throughout.

3. Self-reference in ignore_comment

The old code passed comment.user as both issue_user and comment_user, so the guard comment_user.login == issue_user.login always fired and every comment was skipped. Fixed by threading discussion['author']['login'] as the discussion-author login and comparing directly, without going through ignore_comment (which expects github3 objects, not dicts).

Structural fix

Moved the discussion block outside the if issue: guard so it actually runs when the call-site passes issue=None, discussion=<dict> (as issue_metrics.py does).

Development notes

Changed files: discussions.py, most_active_mentors.py, test_most_active_mentors.py.

Added TestCountCommentsDiscussions with 7 cases:

  • basic count
  • author self-comment is ignored
  • bot commenter is ignored
  • ignore_users respected
  • multiple distinct commenters
  • empty comments list
  • null author (deleted account) handled gracefully

pytest test_most_active_mentors.py11 passed (4 baseline + 7 new). Commit signed off (DCO).

Three layered defects made `enable_mentor_count` a no-op (or crash) for
discussions (reported in github-community-projects#774):

1. GraphQL fetched no comment author data
   discussions.py queried `comments(first: 1) { nodes { createdAt } }`,
   so each comment node had no author field. Expanded to
   `comments(first: 100)` with `author { login __typename }`, and added
   `author { login __typename }` to the discussion node itself so the
   opener's login is available for the self-comment filter.

2. Attribute access on dict nodes would AttributeError
   most_active_mentors.py used `comment.user.login`, `comment.submitted_at`
   etc. on plain GraphQL dicts. Replaced with `comment.get('author') or {}`
   dict access throughout.

3. Self-reference in ignore_comment
   The old code passed `comment.user` as both `issue_user` AND
   `comment_user`, so the 'same user' guard always fired and every comment
   was skipped. Replaced by threading `discussion['author']['login']` as
   the discussion-author login and comparing directly.

Also moved the discussion block outside the `if issue:` guard so it
runs when `issue=None` (the call-site in issue_metrics.py passes
`issue=None, discussion=<dict>`).

Added TestCountCommentsDiscussions with 7 cases: basic count, author
self-comment ignored, bot ignored, ignore_users respected, multiple
commenters, empty comments, null author (deleted account).

Closes github-community-projects#774

Signed-off-by: kaizeenn <khairil0153@gmail.com>
@jmeridth

Copy link
Copy Markdown
Collaborator

@kaizeenn looks like we have some python linting failing.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Repairs the previously non-functional “mentor counting” path for GitHub Discussions by expanding the GraphQL payload and updating count_comments_per_user to correctly process Discussion comment dicts (including self-comment and bot filtering), with added unit coverage for the discussion branch.

Changes:

  • Expand get_discussions GraphQL query to include discussion/comment authors and fetch up to 100 comments per discussion.
  • Rewrite the Discussions branch in count_comments_per_user to use dict-safe access and correct self-comment filtering.
  • Add a dedicated TestCountCommentsDiscussions suite covering core counting and ignore behaviors.
Show a summary per file
File Description
discussions.py Expands GraphQL fields for discussion + comment authors and increases fetched comment nodes to support mentor counting.
most_active_mentors.py Fixes the discussion mentor-counting branch to work with GraphQL dict payloads and correct ignore logic.
test_most_active_mentors.py Adds 7 tests covering discussion mentor counting scenarios (self, bots, ignore list, null author, etc.).

Copilot's findings

  • Files reviewed: 3/3 changed files
  • Comments generated: 2

Comment thread most_active_mentors.py
Comment on lines +118 to +123
if discussion and len(discussion["comments"]["nodes"]) > 0:
discussion_author_login = (
discussion.get("author") or {}
).get("login", "")
for comment in discussion["comments"]["nodes"]:
comment_author = comment.get("author") or {}
Comment thread most_active_mentors.py
Comment on lines +141 to +144
if comment_login in mentor_count:
mentor_count[comment_login] += 1
else:
mentor_count[comment_login] = 1
@kaizeenn

Copy link
Copy Markdown
Author

Hi @jmeridth @zkoppert, I've formatted the code with black and all CI checks are now green. Ready for review!

@zkoppert

zkoppert commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator

Hey @kaizeenn! It looks like the copilot review bot raised some good points. Could you take a look at the fixes for those?

@jmeridth

jmeridth commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

🤖 Now that the PyGithub migration (#789) has landed on main, this PR will need a rebase. The good news is that the actual logic in the PR is still correct and does not need to change.

Here is what to expect when rebasing:

  • discussions.py applies cleanly. The GraphQL query changes do not overlap with anything from refactor: migrate from github3.py to PyGithub #789.
  • most_active_mentors.py will conflict in the discussion branch block (lines 121-141). The resolution is straightforward since the PR replaces that same dead code block. The only contextual difference is that ignore_comment lost its github3.users.User type annotations and gained a None guard for ghost users, but that does not affect this PR since the discussion branch does its own inline dict-based filtering.
  • test_most_active_mentors.py will conflict at the end of the file. The migration added two new test classes (TestGhostUserHandling and TestMostActiveMentorsExtraBranches) after the spot where this PR appends TestCountCommentsDiscussions. Just place the new class after the existing ones.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Discussion mentor counting is dead code (GraphQL + ignore_comment self-reference + dict-vs-object mismatch)

4 participants