summaryrefslogtreecommitdiff
path: root/tools/libgithub/issue.py
blob: f0e8238bc95a501031774cbe8d377588cf07facb (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import sys

from .label import *
from .user import *
from typing import Optional

@dataclass
class Issue:
    yaml_data: Optional[dict] = None

    def __eq__(self, other):
        if isinstance(other, Issue):
            return self.unique_id == other.unique_id
        return False

    def __hash__(self):
        return hash(self.unique_id)
    
    def __init__(self,id=None, title=None, body=None, label_ids=None, file_path=None, data=None):
        if data is not None:
            self.yaml_ctor(data)
        else:
            self.id = id
            self.title = title
            self.body = body
            self.label_ids = label_ids
            self.file_path = file_path

    def yaml_ctor(self,data):
        for tu, labels, file_path in get_translation_units(data):
            self.id = None
            self.title = tu
            self.body = None
            self.file_path = file_path
            self.label_ids = []

    def set_open(self):
        pass

    def set_closed(self):
        LOG.debug(f'Closing issue {self.id}')

        mutation = """
        mutation CloseIssue($id: ID!) {
            closeIssue(input: {issueId: $id}) {
                clientMutationId
            }
        }
        """

        variables = {
            "id": self.id
        }

        data = GraphQLClient.get_instance().make_request(mutation, variables)
        if data:
            LOG.info(f'Closed issue {self.id}')
        else:
            LOG.error(f'Failed to close issue {self.id}')
            sys.exit(1)
            

            

    @staticmethod
    def delete_all():
        LOG.debug(f'Deleting all issues in {RepoInfo.owner.name}/{RepoInfo.name}')

        issue_state = StateFile.data["issues"]
        if issue_state is not None and len(issue_state) > 0:
            for issue in issue_state.copy():
                Issue(
                    file_path=issue["file_path"],
                    id=issue["id"],
                    title=issue["file_path"]
                ).delete()
        else:
            LOG.warning(f'No issues found in state file, nothing to delete.')
            return
        
    @staticmethod
    def get_all_from_github() -> list['Issue']:
        return Issue.get_open() + Issue.get_closed()

    @staticmethod
    def get_closed() -> list['Issue']:
        return Issue.get_by_state("CLOSED")
    
    @staticmethod
    def get_open() -> list['Issue']:
        return Issue.get_by_state("OPEN")
    
    @staticmethod
    def get_all_from_yaml(data, project_name):
        ret_issues = []
        labels_dict = {label['name']: label['id'] for label in StateFile.data["labels"]}

        for d in data:
            if d.get('project', {}).get('title', 'MISSING_TITLE') != project_name and project_name is not None:
                LOG.debug("Project name was passed in but doesn't match the current project, skipping.")
                continue
            # Get tu, labels, filepath for current project
            tu_info = get_translation_units(d)

            # Add in project name as a label
            for idx, (tu, labels, path) in enumerate(tu_info):
                new_labels = labels + [d['project']['title']]
                tu_info[idx] = (tu, new_labels, path)
            

            for tu, labels, file_path in tu_info:
                state_label_ids = []

                # Fetch label ids from state file
                for label in labels:
                    if label in labels_dict:
                        LOG.debug(f'Found label {label} for TU {tu} in state file, adding to issue.')
                        state_label_ids.append(labels_dict[label])
                    else:
                        LOG.error(f'Label {label} not found in state file, please run ./tp github-sync-labels first.')
                        sys.exit(1)

                issue = Issue(
                    id=None, # set in check_and_create or create method
                    title=tu,
                    body=None,
                    label_ids=state_label_ids,
                    file_path=file_path
                )

                ret_issues.append(issue)

        return ret_issues
    
    def get_all_assignees(self) -> list[User]:
        LOG.debug(f'Getting all assignees on for Issue {self.title}')

        query = '''
        query ($id: ID!) {
            node(id: $id) {
                ... on Issue {
                    assignees(first: 100) {
                        nodes {
                            id
                            login
                        }
                    }
                }
            }
        }
        '''

        variables = {
            "id": self.id
        }

        data = GraphQLClient.get_instance().make_request(query, variables)
        if data:
            assignees = data["data"]["node"]["assignees"]["nodes"]
            LOG.debug(f'Got assignees: {assignees}')

            ret_users = []
            for assignee in assignees:
                ret_users.append(User(id=assignee["id"],name=assignee["login"]))
            
            return ret_users
        else:
            LOG.error(f'Failed to get assignees for issue {self.title}')
            sys.exit(1)
    
    @staticmethod
    def get_by_unique_id(unique_id: str) -> 'Issue':
        LOG.debug(f'Getting issue with unique ID {unique_id} on {RepoInfo.owner.name}/{RepoInfo.name}')

        query = '''
        query ($owner: String!, $repo: String!, $first: Int!, $after: String) {
            repository(owner: $owner, name: $repo) {
                issues(first: $first, after: $after) {
                    pageInfo {
                        endCursor
                        hasNextPage
                    }
                    nodes {
                        title
                        id
                        body
                        labels(first: 100) {
                            nodes {
                                id
                                name
                            }
                        }
                    }
                }
            }
        }
        '''

        has_next_page = True
        cursor = None

        while has_next_page:
            variables = {
                "owner": RepoInfo.owner.name,
                "repo": RepoInfo.name,
                "first": 100,
                "after": cursor
            }

            data = GraphQLClient.get_instance().make_request(query, variables)
            if data:
                issues = data['data']['repository']['issues']['nodes']
                for issue in issues:
                    labels = sorted([label['name'] for label in issue['labels']['nodes']])
                    issue_unique_id = issue['title'] + '-' + '-'.join(labels)
                    if issue_unique_id == unique_id:
                        return Issue(
                            id=issue['id'], 
                            title=issue['title'],
                            body=issue['body'],
                            labels=[
                                Label(
                                    id=label['id'], 
                                    name=label['name']
                                ) for label in issue['labels']['nodes']
                            ],
                            unique_id=unique_id
                        )

                page_info = data['data']['repository']['issues']['pageInfo']
                has_next_page = page_info['hasNextPage']
                cursor = page_info['endCursor']
            else:
                LOG.warning(f'No issue found with unique ID {unique_id}')
                return None

    @staticmethod
    def get_by_state(state: str) -> list['Issue']:
        LOG.debug(f'Getting {state} issues on {RepoInfo.owner.name}/{RepoInfo.name}')
        
        query = '''
        query ($owner: String!, $repo: String!, $state: [IssueState!], $cursor: String) {
            repository(owner: $owner, name: $repo) {
                issues(first: 100, states: $state, after: $cursor) {
                    pageInfo {
                        endCursor
                        hasNextPage
                    }
                    nodes {
                        title
                        id
                        body
                        labels(first: 100) {
                            nodes {
                                id
                                name
                            }
                        }
                    }
                }
            }
        }
        '''

        
        all_issues = []
        has_next_page = True
        cursor = None

        while has_next_page:
            variables = {
                "owner": RepoInfo.owner.name,
                "repo": RepoInfo.name,
                "state": state,
                "cursor": cursor
            }

            data = GraphQLClient.get_instance().make_request(query, variables)
            if data:
                issues = data['data']['repository']['issues']['nodes']
                for issue in issues:
                    insert_issue = Issue(
                        id=issue['id'], 
                        title=issue['title'],
                        body=issue['body']
                    )

                    all_issues.append(insert_issue)


                LOG.debug(f'{state} issues retrieved: {issues}')
                page_info = data['data']['repository']['issues']['pageInfo']
                has_next_page = page_info['hasNextPage']
                cursor = page_info['endCursor']

        LOG.debug(f'All {state} issues retrieved: {all_issues}')
        return all_issues
    
    @staticmethod
    def get_labels_by_id(issue_id: str) -> list[Label]:
        LOG.debug(f'Getting all labels for issue {issue_id} on {RepoInfo.owner.name}/{RepoInfo.name}')

        query = '''
        query ($id: ID!) {
            node(id: $id) {
                ... on Issue {
                    labels(first: 100) {
                        nodes {
                            id
                            name
                        }
                    }
                }
            }
        }
        '''

        variables = {
            "id": issue_id
        }

        data = GraphQLClient.get_instance().make_request(query, variables)
        if data:
            labels = data['data']['node']['labels']['nodes']
            LOG.debug(f'Labels retrieved: {labels} for issue {issue_id}')

            ret_labels = []
            for label in labels:
                label = Label(
                    id=label["id"],
                    name=label["name"]
                )

                ret_labels.append(label)

            return ret_labels
        else:
            LOG.debug(f'No labels found for issue {issue_id}')
            return []
        
    def attach_labels(self) -> None:
        LOG.debug(f'Attaching labels to issue {self.id} on {RepoInfo.owner.name}/{RepoInfo.name}')
    
    def check_and_create(self) -> None:
        issues = StateFile.data.get('issues')

        if issues is None:
            issue_dict = {}
        else:
            issue_dict = {issue['file_path']: issue for issue in issues}

        if self.file_path in issue_dict:
            LOG.info(f"Issue {self.title} from TU {self.file_path} already setup!")
            self.id = issue_dict[self.file_path]["id"]
        else:
            LOG.debug(f'Creating missing issue {self.title}.')
            self.create()

    def add_assignees(self, assignees: list[User]) -> None:
        LOG.debug(f'Adding assignees to issue {self.id} on {RepoInfo.owner.name}/{RepoInfo.name}')

        mutation = """
        mutation UpdateIssue($input: UpdateIssueInput!) {
            updateIssue(input: $input) {
                clientMutationId
            }
        }
        """

        input_dict = {
            "assigneeIds": [assignee.id for assignee in assignees],
            "id": self.id
        }

        variables = {
            "input": input_dict
        }

        data = GraphQLClient.get_instance().make_request(mutation, variables)
        if data:
            LOG.debug(f'Assignees added to issue {self.id} on {RepoInfo.owner.name}/{RepoInfo.name}')
        else:
            LOG.error(f'Assignees could not be added to issue {self.id} on {RepoInfo.owner.name}/{RepoInfo.name}')

    def create(self):
        repo_id = RepoInfo.id
        mutation = """
        mutation CreateIssue($input: CreateIssueInput!) {
            createIssue(input: $input) {
                issue {
                    id
                    title
                }
            }
        }
        """

        input_dict = {
            "repositoryId": repo_id,
            "title": self.title,
            "body": self.body,
        }

        if self.label_ids is not None:
            input_dict["labelIds"] = self.label_ids

        variables = {
            "input": input_dict
        }

        data = GraphQLClient.get_instance().make_request(mutation, variables)
        if data:
            self.id = data["data"]["createIssue"]["issue"]["id"]
            self.title = data["data"]["createIssue"]["issue"]["title"]
            self.write_state_to_file()
            LOG.info(f'Created Issue {self.title} with ID {self.id}')
            return self.id
        
    def delete(self) -> None:
        LOG.debug(f'Deleting issue {self.title} with ID {self.id}')
        mutation = '''
        mutation DeleteIssue($id: ID!) {
            deleteIssue(input: {issueId: $id}) {
                clientMutationId
            }
        }
        '''

        variables = {
            "id": self.id
        }

        data = GraphQLClient.get_instance().make_request(mutation, variables)
        if data:
            self.write_state_to_file(delete=True)
            LOG.info(f'Successfully deleted issue {self.title}.')
        else:
            LOG.error(f'Failed to delete issue {self.title}')

    def get_id(self,number) -> None:
        LOG.debug(f'Getting ID for issue {self.title} on {RepoInfo.owner.name}/{RepoInfo.name}')

        query = '''
        query ($number: Int!, $owner: String!, $repo: String!) {
            repository(owner: $owner, name: $repo) {
                issue(number: $number) {
                    id
                }
            }
        }
        '''

        variables = {
            "owner": RepoInfo.owner.name,
            "repo": RepoInfo.name,
            "number": number
        }

        data = GraphQLClient.get_instance().make_request(query, variables)
        if data:
            self.id = data['data']['repository']['issue']['id']
            LOG.debug(f'ID retrieved: {self.id} for issue {self.title}')
        else:
            LOG.error(f'No ID found for issue {self.title}')
            sys.exit(1)

    def write_state_to_file(self, delete: bool = False):        
        state = {
            "id": self.id,
            "title": self.title,
            "body": self.body,
            "label_ids": self.label_ids,
            "file_path": self.file_path,
        }

        curr_state_issues = StateFile.data.get("issues", None)
        if curr_state_issues is not None:
            for i, issue in enumerate(curr_state_issues):
                if issue['id'] == self.id:
                    if delete:
                        del StateFile.data['issues'][i]
                        break
                    else:
                        StateFile.data['issues'][i] = state
                        break
            else:
                StateFile.data['issues'].append((state))
        else:
            StateFile.data['issues'] = [state]


        with open(StateFile.file_name, 'w') as f:
            yaml.safe_dump(StateFile.data, f)