1616TIME_TIL_STALE = 90 # days
1717TIME_TIL_CLOSE = 14 # days
1818
19+
1920def graphql (query , variables = None ):
2021 resp = requests .post (API_URL , headers = HEADERS , json = {"query" : query , "variables" : variables or {}})
2122 if resp .status_code != 200 or 'errors' in resp .json ():
2223 print (f"GraphQL error: { resp .text } " )
2324 return None
2425 return resp .json ()['data' ]
2526
27+
2628def get_discussions ():
2729 discussions = []
2830 cursor = None
@@ -53,6 +55,7 @@ def get_discussions():
5355 cursor = page_info ['endCursor' ]
5456 return discussions
5557
58+
5659def get_comments (discussion_number ):
5760 query = """
5861 query($owner: String!, $name: String!, $number: Int!) {
@@ -74,23 +77,12 @@ def get_comments(discussion_number):
7477 return []
7578 return data ['repository' ]['discussion' ]['comments' ]['nodes' ]
7679
80+
81+ # GitHub Discussions do not support adding labels via API for GitHub Apps/integrations.
7782def add_label (discussion_number , label ):
78- print (f"Adding label '{ label } ' to discussion #{ discussion_number } " )
79- query = """
80- mutation($input: AddLabelsToLabelableInput!) {
81- addLabelsToLabelable(input: $input) {
82- clientMutationId
83- }
84- }
85- """
86- # Get discussion node id
87- node_id = get_discussion_node_id (discussion_number )
88- label_id = get_label_node_id (label )
89- if not node_id or not label_id :
90- print ("Could not get node IDs for label/discussion." )
91- return
92- variables = {"input" : {"labelableId" : node_id , "labelIds" : [label_id ]}}
93- graphql (query , variables )
83+ print (f"Skipping label '{ label } ' for discussion #{ discussion_number } (not supported by API)" )
84+ return
85+
9486
9587def get_discussion_node_id (discussion_number ):
9688 query = """
@@ -106,6 +98,7 @@ def get_discussion_node_id(discussion_number):
10698 return None
10799 return data ['repository' ]['discussion' ]['id' ]
108100
101+
109102def get_label_node_id (label_name ):
110103 query = """
111104 query($owner: String!, $name: String!, $label: String!) {
@@ -120,6 +113,7 @@ def get_label_node_id(label_name):
120113 return None
121114 return data ['repository' ]['label' ]['id' ]
122115
116+
123117def post_comment (discussion_number , body ):
124118 print (f"Posting comment to discussion #{ discussion_number } " )
125119 query = """
@@ -136,6 +130,7 @@ def post_comment(discussion_number, body):
136130 variables = {"input" : {"discussionId" : node_id , "body" : body }}
137131 graphql (query , variables )
138132
133+
139134def close_and_lock (discussion_number ):
140135 print (f"Closing and locking discussion #{ discussion_number } " )
141136 query = """
@@ -152,6 +147,7 @@ def close_and_lock(discussion_number):
152147 variables = {"input" : {"discussionId" : node_id , "locked" : True , "state" : "CLOSED" }}
153148 graphql (query , variables )
154149
150+
155151def main ():
156152 now = datetime .now (UTC )
157153 discussions = get_discussions ()
@@ -169,13 +165,10 @@ def main():
169165 continue
170166
171167 # Close and lock after 14 days of being stale
172- if STALE_LABEL in labels and stale_comment :
168+ if stale_comment :
173169 stale_time = datetime .strptime (stale_comment ['createdAt' ], "%Y-%m-%dT%H:%M:%SZ" ).replace (tzinfo = UTC )
174170 if (now - stale_time ).days >= TIME_TIL_CLOSE :
175171 # Check for any comments after stale comment
176172 recent_comments = [c for c in comments if datetime .strptime (c ['createdAt' ], "%Y-%m-%dT%H:%M:%SZ" ).replace (tzinfo = UTC ) > stale_time ]
177173 if not recent_comments :
178174 close_and_lock (number )
179-
180- if __name__ == "__main__" :
181- main ()
0 commit comments