forked from release-engineering/Sync2Jira
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdownstream_issue.py
More file actions
1629 lines (1375 loc) · 58 KB
/
Copy pathdownstream_issue.py
File metadata and controls
1629 lines (1375 loc) · 58 KB
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is part of sync2jira.
# Copyright (C) 2016 Red Hat, Inc.
#
# sync2jira is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# sync2jira is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with sync2jira; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110.15.0 USA
#
# Authors: Ralph Bean <rbean@redhat.com>
from datetime import datetime, timezone
import difflib
import logging
import operator
import os
import re
from typing import Any, Dict, Optional, Union
import unicodedata
from dotenv import load_dotenv
from jira import JIRAError
import jira.client
from jira.client import Issue as JIssue
from jira.client import ResultList
import pypandoc
import snowflake.connector
import Rover_Lookup
from sync2jira.intermediary import Issue, PR
from sync2jira.jira_auth import (
build_jira_client_kwargs,
invalidate_oauth2_cache_for_config,
)
load_dotenv()
# The date the service was upgraded
# This is used to ensure legacy comments are not touched
UPDATE_DATE = datetime(2019, 7, 9, 18, 18, 36, 480291, tzinfo=timezone.utc)
# Jira REST API rejects comment bodies longer than this (characters).
JIRA_TEXT_BODY_MAX_CHARS = 32750
# When truncating, keep at least this many characters of the original body.
# If the hyperlink(s) wouldn't leave this much room, drop the hyperlink instead.
JIRA_TEXT_BODY_MIN_CHARS = 1024
log = logging.getLogger("sync2jira")
logging.getLogger("snowflake.connector").setLevel(logging.WARNING)
remote_link_title = "Upstream issue"
duplicate_issues_subject = "FYI: Duplicate Sync2jira Issues"
SNOWFLAKE_QUERY = f"""
SELECT
CONCAT(p.PKEY, '-', a.issue_key) AS issue_key,
remote_link_url,
updated
FROM
(
SELECT
ji.PROJECT_ID AS project_id,
ji.ISSUENUM AS issue_key,
rl.URL AS remote_link_url,
ji.updated
FROM
JIRA_DB.CLOUD_MARTS.JIRA_REMOTELINK AS rl
INNER JOIN JIRA_DB.CLOUD_MARTS.JIRA_ISSUE AS ji ON ji.ID = rl.ISSUEID
AND rl.TITLE = '{remote_link_title}' AND rl.URL = ?
) AS a
LEFT JOIN JIRA_DB.CLOUD_MARTS.JIRA_PROJECT AS p on a.project_id = p.ID
"""
GH_URL_PATTERN = re.compile(r"https://github\.com/[^/]+/[^/]+/(issues|pull)/\d+")
field_name_cache = {}
class UrlCache(dict):
"""A dict-like object, intended to be used as a cache, which contains a
limited number of entries -- excess entries are deleted in FIFO order.
"""
MAX_SIZE = 20000
def __setitem__(self, key, value):
while len(self) >= self.MAX_SIZE:
del self[next(iter(self))]
super().__setitem__(key, value)
jira_cache = UrlCache()
def validate_github_url(url):
"""URL validation"""
return bool(GH_URL_PATTERN.fullmatch(url))
def get_snowflake_conn():
"""Get Snowflake connection - lazy initialization
Supports two authentication methods:
1. JWT authentication with private key file (if SNOWFLAKE_PRIVATE_KEY_FILE is set)
2. Password authentication with PAT (if SNOWFLAKE_PAT is set)
"""
account = os.getenv("SNOWFLAKE_ACCOUNT")
user = os.getenv("SNOWFLAKE_USER")
role = os.getenv("SNOWFLAKE_ROLE")
warehouse = os.getenv("SNOWFLAKE_WAREHOUSE", "DEFAULT")
database = os.getenv("SNOWFLAKE_DATABASE", "JIRA_DB")
schema = os.getenv("SNOWFLAKE_SCHEMA", "PUBLIC")
# Build base connection parameters
conn_params = {
"account": account,
"user": user,
"role": role,
"warehouse": warehouse,
"database": database,
"schema": schema,
"paramstyle": "qmark",
}
# Check for private key file (JWT authentication)
private_key_file = os.getenv("SNOWFLAKE_PRIVATE_KEY_FILE")
if private_key_file:
conn_params["authenticator"] = "SNOWFLAKE_JWT"
conn_params["private_key_file"] = private_key_file
# Add private key file password if specified
private_key_file_pwd = os.getenv("SNOWFLAKE_PRIVATE_KEY_FILE_PWD")
if private_key_file_pwd:
conn_params["private_key_file_pwd"] = private_key_file_pwd
else:
# Fall back to password authentication
password = os.getenv("SNOWFLAKE_PAT")
if not password:
raise ValueError(
"Either SNOWFLAKE_PRIVATE_KEY_FILE or SNOWFLAKE_PAT must be set"
)
conn_params["password"] = password
return snowflake.connector.connect(**conn_params)
def execute_snowflake_query(issue):
if not validate_github_url(issue.url):
log.error(f"Invalid GitHub URL format: {issue.url}")
return []
conn = get_snowflake_conn()
# Execute the Snowflake query
with conn as c:
cursor = c.cursor()
cursor.execute(SNOWFLAKE_QUERY, (issue.url,))
results = cursor.fetchall()
cursor.close()
return results
def _build_field_name_cache(client):
"""Build the field name cache for the given JIRA client."""
global field_name_cache
# Reset the cache to just the standard fields
field_name_cache = {
f: f for f in ("priority", "assignee", "summary", "description")
}
# fetching the custom fields from the JIRA client
try:
all_fields = client.fields()
except Exception as e:
log.error(f"Error building field name cache: {e}")
raise
# updating the cache with the custom fields
for field in all_fields:
field_name_cache[field["name"]] = field["id"]
def _get_field_id_by_name(client, field_name):
"""
Convert a human-readable custom field name to its JIRA field ID.
:param jira.client.JIRA client: JIRA client
:param str field_name: Human-readable field name (e.g., "Story Points", "Epic Link")
:returns: Field ID (e.g., "customfield_12310243") or None if not found
:rtype: Optional[str]
"""
# Check cache first
if field_ID := field_name_cache.get(field_name):
return field_ID
# If not in cache, build the cache
_build_field_name_cache(client)
return field_name_cache.get(field_name)
def _resolve_field_identifier(client, field_identifier):
"""
Resolve a field identifier (either a name or an ID) to a field ID.
If the identifier is already an ID (starts with 'customfield_'), return it as-is.
Otherwise, treat it as a name and convert it to an ID.
:param jira.client.JIRA client: JIRA client
:param str field_identifier: Field name (e.g., "Story Points") or ID (e.g., "customfield_12310243")
:returns: Field ID or None if not found
:rtype: Optional[str]
"""
# If it's already a customfield ID, return as-is
if field_identifier.startswith("customfield_"):
return field_identifier
# Otherwise, treat it as a name (custom field name or standard field) and convert to ID
return _get_field_id_by_name(client, field_identifier)
def check_jira_status(client):
"""
Function tests the status of the JIRA server.
:param jira.client.JIRA client: JIRA client
:return: True/False if the server is up
:rtype: Bool
"""
# Search for any issue remote title
try:
client.server_info()
return True
except Exception:
return False
def _comment_format(comment):
"""
Function to format JIRA comments.
:param dict comment: Upstream comment
:returns: Comments formatted
:rtype: String
"""
pretty_date = comment["date_created"].strftime("%a %b %d")
return "[%s] Upstream, %s wrote [%s]:\n\n{quote}\n%s\n{quote}" % (
comment["id"],
comment["author"],
pretty_date,
comment["body"],
)
def _truncate_jira_text(
body: str,
upstream_issue_url: Optional[str] = None,
max_chars: Optional[int] = None,
) -> str:
"""
Ensure ``body`` fits within *max_chars* (default: comment limit).
If truncated, a notice is prepended and a truncation marker appended. When
an upstream URL is available it is included — *unless* the link is so
long that it would eat into the minimum body budget, in which case the link
is dropped entirely.
"""
if max_chars is None:
max_chars = JIRA_TEXT_BODY_MAX_CHARS
if len(body) <= max_chars:
return body
log.info(
"Truncating Jira text body from %d to max %d characters",
len(body),
max_chars,
)
head = "{warning}*(Truncated.)*{warning}\n"
tail = "\n\n{warning}*....*{warning}\n"
link_block = ""
if upstream_issue_url:
link_block = f"[See More|{upstream_issue_url}]\n"
fixed_overhead = len(head) + len(tail) + 1 # +1 for the \n before body
link_overhead = 2 * len(link_block)
if fixed_overhead + link_overhead + JIRA_TEXT_BODY_MIN_CHARS > max_chars:
link_block = ""
link_overhead = 0
core_len = max_chars - fixed_overhead - link_overhead
return head + link_block + "\n" + body[:core_len] + tail + link_block
def _comment_format_legacy(comment):
"""
Legacy function to format JIRA comments.
This is still used to match comments so no
duplicates are created.
:param dict comment: Upstream comment
:returns: Comments formatted
:rtype: String
"""
return "Upstream, %s wrote:\n\n{quote}\n%s\n{quote}" % (
comment["name"],
comment["body"],
)
def get_jira_client(issue, config, invalidate_oauth2_cache=False):
"""
Function to match and create JIRA client.
:param sync2jira.intermediary.Issue issue: Issue object
:param dict config: Config dict
:param bool invalidate_oauth2_cache: If True, clear OAuth2 token cache for this
instance before building the client (e.g. after a JIRAError on retry).
:returns: Matching JIRA client
:rtype: jira.client.JIRA
"""
# The name of the jira instance to use is stored under the 'map'
# key in the config where each upstream is mapped to jira projects.
# It is conveniently added to the Issue object from intermediary.py
# so we can use it here:
if not isinstance(issue, Issue) and not isinstance(issue, PR):
log.error("passed in issue is not an Issue instance")
log.error("It is a %s", type(issue).__name__)
raise TypeError(f"Got {type(issue).__name__}, expected Issue")
# Use the Jira instance set in the issue config. If none then
# use the configured default jira instance.
jira_instance = issue.downstream.get(
"jira_instance", config["sync2jira"].get("default_jira_instance")
)
if not jira_instance:
log.error("No jira_instance for issue and there is no default in the config")
raise Exception("No configured jira_instance for issue")
jira_instance_config = config["sync2jira"]["jira"][jira_instance]
if invalidate_oauth2_cache:
invalidate_oauth2_cache_for_config(jira_instance_config)
client_kwargs = build_jira_client_kwargs(jira_instance_config)
client = jira.client.JIRA(**client_kwargs)
client.server_info() # This raises an exception if authentication was not successful
return client
def get_existing_jira_issue(client, issue, config):
"""
Get a jira issue by the linked remote issue.
:param jira.client.JIRA client: JIRA client
:param sync2jira.intermediary.Issue issue: Issue object
:param Dict config: Config dict
:returns: Returns a list of matching JIRA issues if any are found
:rtype: JIssue or None
"""
issue_keys = _get_existing_jira_issue_keys(issue)
if not issue_keys:
return None
jql = f"key in ({','.join(issue_keys)})"
results: ResultList[JIssue] = client.search_issues(jql)
if not results:
# JQL/search index can lag right after create, or hide archived issues.
# Resolve by issue key (direct GET) before giving up and duplicating.
for key in issue_keys:
try:
found = client.issue(key)
except JIRAError:
continue
log.info(
"JQL missed key %s for upstream %s; using direct issue fetch.",
key,
issue.url,
)
results = ResultList[JIssue]((found,))
break
else:
log.warning(
"Downstream issue not found for upstream %s after JQL %r and direct fetch for keys %s.",
issue.url,
jql,
issue_keys,
)
return None
# If there is more than one issue, remove duplicates and filter the list
# down to one.
if len(results) > 1:
results = _filter_downstream_issues(results, issue, client, config)
# If there is more than one result, select only the most-recently updated one.
if len(results) > 1:
log.debug(
"Found %i results for query with issue %r",
len(results),
issue.url,
)
results.sort(
key=lambda x: datetime.strptime(
x.fields.updated, "%Y-%m-%dT%H:%M:%S.%f+0000"
),
reverse=True, # Biggest (most recent) first
)
results = ResultList[JIssue]((results[0],)) # A list of one item
# Cache the result for next time and return it.
jira_cache[issue.url] = results[0].key
return results[0]
def _get_existing_jira_issue_keys(issue: Issue) -> tuple[str, ...]:
"""
Retrieve downstream Jira issue keys corresponding to a given upstream issue.
The function first checks the local cache; if no cached result is found,
it queries Snowflake. Returns empty tuple if no matches are found.
:param sync2jira.intermediary.Issue issue: Issue object
:returns: A tuple of Jira issue keys, empty if no matches are found
:rtype: Tuple[str, ...]
"""
if result := jira_cache.get(issue.url):
issue_keys = (result,)
else:
results = execute_snowflake_query(issue)
if not results:
return ()
issue_keys = tuple(row[0] for row in results)
return issue_keys
def _filter_downstream_issues(
results: ResultList[JIssue],
issue: Issue,
client: jira.client.JIRA,
config,
) -> ResultList[JIssue]:
"""
Remove duplicates; if the result would be an empty list, the original list
is returned.
:param ResultList[JIssue] results: Query results list
:param sync2jira.intermediary.Issue issue: Target Issue object
:param jira.client.JIRA client: JIRA client
:param Dict config: Config dict
:returns: a filtered list of matching JIRA issues or the original input
:rtype: ResultList[JIssue]
"""
filtered_results = ResultList[JIssue]()
# TODO: there is pagure-specific code in here that handles the case where a
# dropped issue's URL is re-used by an issue opened later.
# I.e. pagure re-uses IDs.
for result in results:
description = result.fields.description or ""
summary = result.fields.summary or ""
if (
issue.id in description
or issue.title == summary
or re.search(
r"\[[a-zA-Z0-9!@#$%^&*()_+\-=\[\]{};':\\|,.<>/?]*] "
+ issue.upstream_title,
summary,
)
):
username = find_username(issue, config)
search = check_comments_for_duplicate(client, result, username)
filtered_results.append(search if search else result)
return filtered_results if filtered_results else results
def find_username(_issue, config):
"""
Finds JIRA username for an issue object.
:param sync2jira.intermediary.Issue _issue: Issue object (not used)
:param Dict config: Config dict
:returns: Username string
:rtype: String
"""
return config["sync2jira"]["jira_username"]
def _jira_user_display_label(user) -> Optional[str]:
"""Best-effort display string for a Jira User (Cloud: displayName, else name)."""
if not user:
return None
return getattr(user, "displayName", None) or getattr(user, "name", None)
def check_comments_for_duplicate(client, result, username):
"""
Checks comment of JIRA issue to see if it has been
marked as a duplicate.
:param jira.client.JIRA client: JIRA client
:param jira.resource.Issue result: JIRA issue
:param string username: Username of JIRA user
:returns: duplicate JIRA issue or None
:rtype: jira.resource.Issue or None
"""
for comment in client.comments(result):
search = re.search(r"Marking as duplicate of (\w*)-(\d*)", comment.body)
author_label = _jira_user_display_label(comment.author)
if search and author_label == username:
issue_id = search.groups()[0] + "-" + search.groups()[1]
return client.issue(issue_id)
return None
def _find_comment_in_jira(comment, j_comments, issue_url: Optional[str] = None):
"""
Helper function to filter out comments that are matching.
:param Dict comment: Individual comment from upstream
:param List j_comments: Comments from JIRA downstream
:param Optional[str] upstream_issue_url: Upstream issue URL for truncation notices
:returns: Item/None
:rtype: jira.resource.Comment/None
"""
if comment["date_created"] < UPDATE_DATE:
# If the comment date is prior to the update_date, we should not try to
# touch the comment; return the item as is.
return comment
formatted_comment = _truncate_jira_text(_comment_format(comment), issue_url)
legacy_formatted_comment = _comment_format_legacy(comment)
for item in j_comments:
if item.raw["body"] == legacy_formatted_comment:
# If the comment is in the legacy comment format,
# return the item
return item
if str(comment["id"]) in item.raw["body"]:
# The comment id's match, if they don't have the same body,
# we need to edit the comment
if item.raw["body"] != formatted_comment:
# We need to update the comment
item.update(body=formatted_comment)
log.info("Updated one comment")
# Now we can just return the item
return item
return None
def _comment_matching(g_comments, j_comments, upstream_issue_url: Optional[str] = None):
"""
Function to filter out comments that are matching.
:param List g_comments: Comments from Issue object
:param List j_comments: Comments from JIRA downstream
:param Optional[str] upstream_issue_url: Upstream issue URL (for Jira truncation)
:returns: Returns a list of comments that are not matching
:rtype: List
"""
return list(
filter(
lambda x: _find_comment_in_jira(x, j_comments, upstream_issue_url) is None
or x["changed"] is not None,
g_comments,
)
)
def _get_existing_jira_issue_legacy(client, issue):
"""
This is our old way of matching issues: use the special url field.
This will be phased out and removed in a future release.
"""
kwargs = dict(issue.downstream.items())
kwargs["External issue URL"] = str(issue.url)
kwargs = sorted(kwargs.items(), key=operator.itemgetter(0))
query = (
" AND ".join(f"'{k}'='{v}'" for k, v in kwargs if v is not None)
+ " AND (resolution is null OR resolution = Duplicate)"
)
results = client.search_issues(query)
if results:
return results[0]
else:
return None
def attach_link(client, downstream, remote_link):
"""
Attaches the upstream link to the JIRA ticket.
:param jira.client.JIRA client: JIRA client
:param jira.resources.Issue downstream: Response from creating the JIRA ticket
:param dict remote_link: Remote link dict with {'url': ..., 'title': ... }
:return: downstream: Response from creating the JIRA ticket
:rtype: jira.resources.Issue
"""
log.info("Attaching tracking link %r to %r", remote_link, downstream.key)
# This is crazy. Querying for application links requires admin perms which
# we don't have, so duck-punch the client to think it has already made the
# query.
client._applicationlinks = [] # pylint: disable=protected-access
# Add the link.
client.add_remote_link(downstream.id, remote_link)
# Finally, after we've added the link, we have to edit the issue so that it
# gets re-indexed; otherwise our searches won't work. Also, handle some
# weird API changes here...
log.debug("Modifying desc of %r to trigger re-index.", downstream.key)
modified_desc = (downstream.fields.description or "") + " "
downstream.update({"description": modified_desc})
return downstream
def _upgrade_jira_issue(client, downstream, issue, config):
"""
Given an old legacy-style downstream issue, upgrade it to a new-style issue
by marking it with an external-url field value.
"""
log.info("Upgrading %r %r issue for %r", downstream.key, issue.downstream, issue)
if config["sync2jira"]["testing"]:
log.info("Testing flag is true. Skipping actual upgrade.")
return
# Do it!
remote_link = dict(url=issue.url, title=remote_link_title)
attach_link(client, downstream, remote_link)
def match_user(emails: list[str], client: jira.client.JIRA) -> Optional[Dict[str, str]]:
"""Match an upstream user to an assignable downstream Jira user.
Returns a dict with ``name`` (display name) and ``accountId`` for Jira Cloud,
or None on failure.
"""
for email in emails:
# Get a list from Jira of users that match the supplied email address.
# Use query= for Jira Cloud (GDPR strict mode rejects the username param).
users = client.search_users(query=email)
if not users:
continue
if len(users) == 1:
u = users[0]
return {
"name": getattr(u, "displayName", None) or "<name-not-available>",
"accountId": getattr(u, "accountId", None),
}
limit = 5
log.warning(
"Found %d Jira users for %r: %s%s",
len(users),
email,
", ".join(
getattr(u, "displayName", "<name-not-available>")
for u in users[0:limit]
),
"..." if len(users) > limit else "",
)
for user in users:
# Filter by email when present (can be omitted in Cloud when hidden).
if getattr(user, "emailAddress", None) == email:
name = getattr(user, "displayName", None) or "<no-name-available>"
aid = getattr(user, "accountId", None)
log.info("Found matching user: %r", name)
return {"name": name, "accountId": aid}
else:
log.warning("Found no Jira user which matches %r", email)
return None
def assign_user(
client: jira.client.JIRA, issue: Issue, downstream: JIssue, remove_all=False
):
"""
Attempts to assign a JIRA issue to the correct
user based on the issue.
:param jira.client.JIRA client: JIRA Client
:param sync2jira.intermediary.Issue issue: Issue object
:param jira.resources.Issue downstream: JIRA issue object
:param Bool remove_all: Flag to indicate if we should reset the assignees in the JIRA issue
:returns: Nothing
"""
# If removeAll flag, then we need to reset the assignees
if remove_all:
# Update the issue to have no assignees
downstream.update(assignee={"name": ""})
log.info("Cleared assignment of %s.", downstream.key)
return
# JIRA only supports one assignee; if we have more than one (i.e., from
# GitHub), assign the issue to the first user (i.e., issue.assignee[0])
# whose name is present and matches an acceptable Jira user.
# See if any of the upstream assignees has a downstream email address.
for assignee in issue.assignee:
emails = Rover_Lookup.github_username_to_emails(
assignee["login"],
ldap_server=os.getenv("LDAP_SERVER"),
ldap_base_dn=os.getenv("LDAP_BASE_DN"),
ldap_bind_dn=os.getenv("LDAP_BIND_DN"),
ldap_password=os.getenv("LDAP_PASSWORD"),
)
if not emails:
continue
# Try to match the upstream assignee's emails to a Jira user
matched = match_user(emails, client)
if matched and matched["accountId"]:
# Jira Cloud assigns by accountId
downstream.update({"assignee": {"accountId": matched["accountId"]}})
log.info(
"Assigned %s to %r,%r",
downstream.key,
matched["name"],
matched["accountId"],
)
return
if issue.assignee:
log.warning(
"Unable to assign %s from upstream assignees %s in %s",
downstream.key,
str([a.get("fullname", a.get("login", "<name>")) for a in issue.assignee]),
issue.url,
)
# No downstream match for the upstream assignee; if there is a configured
# owner for the project, assign it to them.
owner = issue.downstream.get("owner")
if owner:
client.assign_issue(downstream.id, owner)
log.info("Assigned %s to owner: %s", downstream.key, owner)
return
def change_status(client, downstream, status, issue: Union[Issue, PR]):
"""
Change the status of JIRA issue.
:param jira.client.JIRA client: JIRA client
:param jira.resources.Issue/PR downstream: JIRA issue or PR object
:param String status: Title of status to which issue should be move
:param sync2jira.intermediary.Issue issue: Issue object
"""
transitions = client.transitions(downstream)
tid = ""
for t in transitions:
if t["name"] and status.upper() == str(t["name"]).upper():
tid = int(t["id"])
break
if tid:
try:
client.transition_issue(downstream, tid)
log.info(
"Updated %s to %s status for issue %s",
downstream.key,
status,
issue.url,
)
except JIRAError as exc:
log.error(
"Updating %s to %s status for issue %s failed: %s",
downstream.key,
status,
issue.url,
exc,
)
else:
log.warning(
"Could not update %s to %s status for issue %s",
downstream.key,
status,
issue.url,
)
def _get_preferred_issue_types(config, issue):
"""
Determine the appropriate issue type to specify when creating the
downstream (Jira) issue. In order of preference:
- the issue type(s) from the mapping in the configuration file (if
present), selected based on the upstream "tags" (labels)
- the default issue type configured for the project (if any)
- the upstream issue type (if any)
- "Story" if the issue title contains "RFE"
- otherwise, "Bug".
In all cases, a list of one item is returned, except when the upstream
issue has multiple tags which match multiple entries in the configured
mapping, in which case multiple entries are returned, sorted in ascending
lexicographical order.
:param Dict config: Config dict
:param sync2jira.intermediary.Issue issue: Issue object
:returns: A list of issue types in order of preference
:rtype: List
"""
# History:
# https://github.com/release-engineering/Sync2Jira/issues/147
# Configuration artifact:
# 'issue_types': {
# 'bug': 'Bug',
# 'enhancement': 'Story'
# }
cmap = config["sync2jira"].get("map", {})
conf = cmap.get("github", {}).get(issue.upstream, {})
if issue_types := conf.get("issue_types"):
type_list = [v for k, v in issue_types.items() if k in issue.tags]
if type_list:
type_list.sort()
return type_list
if issue_type := conf.get("type"):
return [issue_type]
if issue.issue_type:
return [issue.issue_type]
if "RFE" in issue.title:
return ["Story"]
return ["Bug"]
def _create_jira_issue(client, issue, config):
"""
Create a JIRA issue and adds all relevant
information in the issue to the JIRA issue.
:param jira.client.JIRA client: JIRA client
:param sync2jira.intermediary.Issue issue: Issue object
:param Dict config: Config dict
:returns: Returns JIRA issue that was created
:rtype: jira.resources.Issue
"""
custom_fields = issue.downstream.get("custom_fields", {})
preferred_types = _get_preferred_issue_types(config, issue)
description = _build_description(issue)
kwargs = dict(
summary=issue.title,
description=description,
issuetype=dict(name=preferred_types[0]),
)
if issue.downstream["project"]:
kwargs["project"] = dict(key=issue.downstream["project"])
if issue.downstream.get("component"):
# TODO - make this a list in the config
kwargs["components"] = [dict(name=issue.downstream["component"])]
for key, custom_field in custom_fields.items():
# If key is a field name, resolve it to an ID
field_id = _resolve_field_identifier(client, key)
if not field_id:
raise ValueError(
f"Could not resolve custom field '{key}' to an ID, skipping"
)
if type(custom_field) is str:
kwargs[field_id] = custom_field.replace("[remote-link]", issue.url)
else:
kwargs[field_id] = custom_field
# Add labels if needed
if "labels" in issue.downstream.keys():
kwargs["labels"] = issue.downstream["labels"]
log.info("Creating issue for %r: %r", issue, kwargs)
if config["sync2jira"]["testing"]:
log.info("Testing flag is true. Skipping actual creation.")
return None
downstream = client.create_issue(**kwargs)
jira_cache[issue.url] = downstream.key
# Add values to the Epic link, QA, and EXD-Service fields if present
if (
issue.downstream.get("epic-link")
or issue.downstream.get("qa-contact")
or issue.downstream.get("EXD-Service")
):
if issue.downstream.get("epic-link"):
# Try to get and update the custom field
custom_field: Optional[str] = _get_field_id_by_name(client, "Epic Link")
if custom_field:
try:
downstream.update({custom_field: issue.downstream["epic-link"]})
except JIRAError:
client.add_comment(
downstream,
f"Error adding Epic-Link: {issue.downstream['epic-link']}",
)
else:
log.warning("Could not resolve 'Epic Link' field name to ID")
if issue.downstream.get("qa-contact"):
# Try to get and update the custom field
custom_field = _get_field_id_by_name(client, "QA Contact")
if custom_field:
downstream.update({custom_field: issue.downstream["qa-contact"]})
else:
log.warning("Could not resolve 'QA Contact' field name to ID")
if issue.downstream.get("EXD-Service"):
# Try to update the custom field
exd_service_info = issue.downstream["EXD-Service"]
custom_field = _get_field_id_by_name(client, "EXD-Service")
if custom_field:
try:
downstream.update(
{
custom_field: {
"value": f"{exd_service_info['guild']}",
"child": {"value": f"{exd_service_info['value']}"},
}
}
)
except JIRAError:
client.add_comment(
downstream,
f"Error adding EXD-Service field.\n"
f"Project: {exd_service_info['guild']}\n"
f"Value: {exd_service_info['value']}",
)
else:
log.warning("Could not resolve 'EXD-Service' field name to ID")
# Add upstream issue ID in comment if required
if "upstream_id" in issue.downstream.get("issue_updates", []):
comment = (
f"Creating issue for "
f"[{issue.upstream}-#{issue.upstream_id}|{issue.url}]"
)
client.add_comment(downstream, comment)
if len(preferred_types) > 1:
comment = "Some labels look like issue types but were not considered: "
comment += str(preferred_types[1:])
client.add_comment(downstream, comment)
remote_link = dict(url=issue.url, title=remote_link_title)
attach_link(client, downstream, remote_link)
default_status = issue.downstream.get("default_status")
if default_status is not None:
change_status(client, downstream, default_status, issue)
# Update relevant information (i.e., tags, assignees, etc.) if the User
# opted in
_update_jira_issue(downstream, issue, client, config)
return downstream
def _label_matching(jira_labels, issue_labels):
"""
Filters through jira_labels to ensure no duplicate labels are present and
no jira_labels are removed.
:param List jira_labels: Existing JIRA labels
:param List issue_labels: Upstream labels
:returns: Updated filtered labels
:rtype: List
"""
# We want to get the union of the jira_labels and the issue_labels --
# i.e., all the labels in jira_labels without duplicates from issue_labels
updated_labels = list(set(jira_labels).union(set(issue_labels)))
# Return our labels
return updated_labels
def _update_jira_issue(existing, issue, client, config):
"""
Updates an existing JIRA issue (i.e., tags, assignee, comments, etc.).
:param jira.resources.Issue existing: Existing JIRA issue that was found
:param sync2jira.intermediary.Issue issue: Upstream issue we're pulling data from
:param jira.client.JIRA client: JIRA Client
:returns: Nothing
"""
# Start with comments
# Only synchronize comments for listings that op-in
log.info("Updating information for upstream issue: %s", issue.url)