Skip to content

Commit 16cd460

Browse files
committed
feat(github): enhance user activity query documentation for authenticated user access
1 parent ce79d53 commit 16cd460

File tree

1 file changed

+94
-5
lines changed

1 file changed

+94
-5
lines changed

src/mcp_github/github_integration.py

Lines changed: 94 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -668,14 +668,99 @@ def create_release(self, repo_owner: str, repo_name: str, tag_name: str, release
668668

669669
def user_activity_query(self, variables: dict[str, Any], query: str) -> Dict[str, Any]:
670670
"""
671-
Performs a user activity query using GitHub's GraphQL API across all repositories and organisations.
671+
Performs a user activity query using GitHub's GraphQL API for the authenticated user (token owner).
672+
673+
**Critical**: To query activities within a specific organization (e.g., "saidsef"):
674+
1. Query the organization DIRECTLY using `organization(login: "saidsef")`
675+
2. Do NOT use `viewer.contributionsCollection` as it excludes many private org activities
676+
3. Organization name is CASE-SENSITIVE - must match exactly
677+
678+
**For Organization-Specific Activity** (e.g., saidsef):
679+
```graphql
680+
query($orgName: String!, $from: GitTimestamp!, $to: GitTimestamp!) {
681+
organization(login: $orgName) {
682+
login
683+
repositories(first: 100, privacy: PRIVATE, orderBy: {field: PUSHED_AT, direction: DESC}) {
684+
nodes {
685+
name
686+
isPrivate
687+
owner { login }
688+
defaultBranchRef {
689+
target {
690+
... on Commit {
691+
history(since: $from, until: $to) {
692+
totalCount
693+
nodes {
694+
author { user { login } }
695+
committedDate
696+
message
697+
}
698+
}
699+
}
700+
}
701+
}
702+
pullRequests(first: 100, states: [OPEN, CLOSED, MERGED], orderBy: {field: UPDATED_AT, direction: DESC}) {
703+
nodes {
704+
number
705+
title
706+
author { login }
707+
createdAt
708+
state
709+
}
710+
}
711+
issues(first: 100, states: [OPEN, CLOSED], orderBy: {field: UPDATED_AT, direction: DESC}) {
712+
nodes {
713+
number
714+
title
715+
author { login }
716+
createdAt
717+
state
718+
}
719+
}
720+
}
721+
}
722+
}
723+
}
724+
```
725+
726+
**For Authenticated User Activity Across All Orgs**:
727+
```graphql
728+
query($from: DateTime!, $to: DateTime!) {
729+
viewer {
730+
login
731+
contributionsCollection(from: $from, to: $to) {
732+
commitContributionsByRepository(maxRepositories: 100) {
733+
repository { name isPrivate owner { login } }
734+
contributions { totalCount }
735+
}
736+
pullRequestContributionsByRepository(maxRepositories: 100) {
737+
repository { name isPrivate owner { login } }
738+
contributions { totalCount }
739+
}
740+
}
741+
organizations(first: 100) {
742+
nodes { login }
743+
}
744+
}
745+
}
746+
```
747+
672748
Args:
673-
variables (dict[str, Any]): The variables to include in the query i.e. {"login": "username", "from": "2023-01-01", "to": "2023-12-31"}.
674-
query (str): The search query string. GitHub GraphQL query summary collection that includes all activity across all orgs, public and private.
749+
variables (dict[str, Any]): Query variables. Options:
750+
- For org-specific with commit history: {"orgName": "saidsef", "from": "2024-10-01T00:00:00Z", "to": "2024-10-31T23:59:59Z"}
751+
Note: Use GitTimestamp type (ISO 8601 format) for $from/$to when querying commit history
752+
- For all activity: {"from": "2024-10-01T00:00:00Z", "to": "2024-10-31T23:59:59Z"}
753+
Note: Use DateTime type for contributionsCollection queries
754+
query (str): GraphQL query string. Use `organization(login: $orgName)` for specific org queries.
755+
IMPORTANT: Declare variables as `GitTimestamp!` for commit history, `DateTime!` for contributionsCollection.
756+
675757
Returns:
676-
Dict[str, Any]: The JSON response from the GitHub API containing search results if successful.
758+
Dict[str, Any]: The JSON response from GitHub's GraphQL API containing activity data.
759+
760+
Note: Organization queries require `read:org` scope. Organization name is case-sensitive.
761+
GitTimestamp and DateTime both accept ISO 8601 format (YYYY-MM-DDTHH:MM:SSZ).
677762
"""
678-
logging.info("Performing user query on GitHub")
763+
logging.info(f"Performing GraphQL query with variables: {variables}")
679764

680765
try:
681766
response = requests.post(
@@ -686,6 +771,10 @@ def user_activity_query(self, variables: dict[str, Any], query: str) -> Dict[str
686771
)
687772
response.raise_for_status()
688773
query_data = response.json()
774+
775+
if 'errors' in query_data:
776+
logging.error(f"GraphQL errors: {query_data['errors']}")
777+
689778
return query_data
690779
except requests.exceptions.RequestException as req_err:
691780
logging.error(f"Request error during user activity query: {str(req_err)}")

0 commit comments

Comments
 (0)