-
Notifications
You must be signed in to change notification settings - Fork 56
/
discussions.py
74 lines (60 loc) · 2.2 KB
/
discussions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
This module provides functions for working with discussions in a GitHub repository.
Functions:
get_discussions(repo_url: str, token: str, search_query: str) -> List[Dict]:
Get a list of discussions in a GitHub repository that match the search query.
"""
import requests
def get_discussions(token: str, search_query: str, ghe: str):
"""Get a list of discussions in a GitHub repository that match the search query.
Args:
token (str): A personal access token for GitHub.
search_query (str): The search query to filter discussions by.
Returns:
list: A list of discussions in the repository that match the search query.
"""
# Construct the GraphQL query
query = """
query($query: String!) {
search(query: $query, type: DISCUSSION, first: 100) {
edges {
node {
... on Discussion {
title
url
createdAt
comments(first: 1) {
nodes {
createdAt
}
}
answerChosenAt
closedAt
}
}
}
}
}
"""
# Remove the type:discussions filter from the search query
search_query = search_query.replace("type:discussions ", "")
# Set the variables for the GraphQL query
variables = {"query": search_query}
# Send the GraphQL request
api_endpoint = f"{ghe}/api/v3" if ghe else "https://api.github.com"
headers = {"Authorization": f"Bearer {token}"}
response = requests.post(
f"{api_endpoint}/graphql",
json={"query": query, "variables": variables},
headers=headers,
timeout=60,
)
# Check for errors in the GraphQL response
if response.status_code != 200 or "errors" in response.json():
raise ValueError("GraphQL query failed")
data = response.json()["data"]
# Extract the discussions from the GraphQL response
discussions = []
for edge in data["search"]["edges"]:
discussions.append(edge["node"])
return discussions