-
Notifications
You must be signed in to change notification settings - Fork 43
/
client.py
1652 lines (1401 loc) · 67.5 KB
/
client.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
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
import copy
import logging
from multiprocessing.pool import ThreadPool as Pool
from typing import Union
import six
import xmltodict
from rtcclient import exception
from rtcclient import urlencode, urlparse, urlquote, OrderedDict
from rtcclient.base import RTCBase
from rtcclient.models import FiledAgainst, FoundIn, Comment, Action, State # noqa: F401
from rtcclient.models import IncludedInBuild, ChangeSet, Attachment # noqa: F401
from rtcclient.models import Severity, Priority, ItemType, SavedQuery # noqa: F401
from rtcclient.models import TeamArea, Member, Administrator, PlannedFor # noqa: F401
from rtcclient.project_area import ProjectArea # noqa: F401
from rtcclient.query import Query
from rtcclient.template import Templater
from rtcclient.utils import capitalize
from rtcclient.workitem import Workitem # noqa: F401
class RTCClient(RTCBase):
"""A wrapped class for :class:`RTC Client` to perform all related
operations
:param url: the rtc url (e.g. https://your_domain:9443/jazz)
:param username: the rtc username
:param password: the rtc password
:param proxies: (optional) Dictionary mapping protocol to the URL of
the proxy.
:param searchpath: (optional) the folder to store your templates.
If `None`, the default search path
(/your/site-packages/rtcclient/templates) will be loaded.
:param ends_with_jazz: (optional but important) Set to `True` (default) if
the url ends with 'jazz', otherwise to `False` if with 'ccm'
(Refer to issue #68 for details)
:type ends_with_jazz: bool
Tips: You can also customize your preferred properties to be returned
by specified `returned_properties` when the called methods have
this optional parameter, which can also GREATLY IMPROVE the performance
of this client especially when getting or querying lots of workitems.
Important Note: `returned_properties` is an advanced parameter, the
returned properties can be found in `ClassInstance.field_alias.values()`,
e.g. `myworkitem1.field_alias.values()`. If you don't care the performance,
just leave it alone with `None`.
"""
log = logging.getLogger("client.RTCClient")
def __init__(self,
url,
username,
password,
email=None,
proxies=None,
searchpath=None,
ends_with_jazz=True,
verify: Union[bool, str] = False,
old_rtc_authentication=False,
**kwargs):
"""Initialization
See params above
"""
self.username = username
self.password = password
self.email = email
self.proxies = proxies
self.verify = verify
self.old_rtc_authentication = old_rtc_authentication
RTCBase.__init__(self, url, **kwargs)
if not isinstance(ends_with_jazz, bool):
raise exception.BadValue("ends_with_jazz is not boolean")
self.jazz = ends_with_jazz
self.headers = self._get_headers()
self.searchpath = searchpath
self.templater = Templater(self, searchpath=self.searchpath)
self.query = Query(self)
def __str__(self):
return "RTC Server at %s" % self.url
def get_rtc_obj(self):
return self
def _get_headers(self):
if self.jazz is True:
_allow_redirects = True
else:
_allow_redirects = False
_headers = {"Content-Type": self.CONTENT_XML}
resp = self.get(self.url + "/authenticated/identity",
auth=(self.username, self.password),
verify=self.verify,
headers=_headers,
proxies=self.proxies,
allow_redirects=_allow_redirects)
if self.old_rtc_authentication:
# works with server that needs 0.6.0 version
_headers["Content-Type"] = self.CONTENT_URL_ENCODED
if resp.headers.get("set-cookie") is not None:
_headers["Cookie"] = resp.headers.get("set-cookie")
credentials = urlencode({
"j_username": self.username,
"j_password": self.password
})
resp = self.post(self.url + "/authenticated/j_security_check",
data=credentials,
verify=False,
headers=_headers,
proxies=self.proxies,
allow_redirects=_allow_redirects)
# authfailed
authfailed = resp.headers.get("x-com-ibm-team-repository-web-auth-msg")
if authfailed == "authfailed":
raise exception.RTCException("Authentication Failed: "
"Invalid username or password")
# header changes in 6.0.3, issue #92
authfailedloc = resp.headers.get("Location")
if authfailedloc is not None and authfailedloc.endswith("authfailed"):
raise exception.RTCException("Authentication Failed: "
"Invalid username or password")
# fix issue #68
if not _allow_redirects:
if resp.headers.get("set-cookie") is not None:
_headers["Cookie"] = resp.headers.get("set-cookie")
resp = self.get(self.url + "/authenticated/identity",
auth=(self.username, self.password),
verify=self.verify,
headers=_headers,
proxies=self.proxies,
allow_redirects=_allow_redirects)
# fix issue #68
if not _allow_redirects:
_headers["Cookie"] += "; " + resp.headers.get("set-cookie")
else:
_headers["Cookie"] = resp.headers.get("set-cookie")
# For Form Challenge
auth_msg = resp.headers.get("X-com-ibm-team-repository-web-auth-msg")
if auth_msg == "authrequired":
post_data = {
'j_username': self.username,
'j_password': self.password
}
temp_headers = {"Content-Type": self.CONTENT_URL_ENCODED}
resp = self.post(self.url + "/authenticated/j_security_check",
data=post_data,
verify=self.verify,
headers=temp_headers,
proxies=self.proxies,
allow_redirects=True)
authfailed = resp.headers.get(
"x-com-ibm-team-repository-web-auth-msg")
if authfailed == "authfailed":
raise exception.RTCException("Authentication Failed: "
"Invalid username or password")
else:
if resp.status_code == 302:
authfailedloc = resp.headers.get("Location")
if authfailedloc is not None and authfailedloc in "/auth/authfailed":
raise exception.RTCException(
"Authentication Failed: "
"Invalid username or password")
elif resp.status_code != 200:
raise exception.RTCException("Authentication Failed: "
"Invalid username or password")
if not _allow_redirects:
_headers["Cookie"] += "; " + resp.headers.get("set-cookie")
else:
_headers["Cookie"] = resp.headers.get("set-cookie")
_headers["Accept"] = self.CONTENT_XML
return _headers
def relogin(self):
"""Relogin the RTC Server/Jazz when the token expires
"""
self.log.info("Cookie expires. Relogin to get a new cookie.")
self.headers = None
self.headers = self._get_headers()
self.log.debug("Successfully relogin.")
def getProjectAreas(self, archived=False, returned_properties=None):
"""Get all :class:`rtcclient.project_area.ProjectArea` objects
If no :class:`rtcclient.project_area.ProjectArea` objects are
retrieved, `None` is returned.
:param archived: (default is False) whether the project area
is archived
:param returned_properties: the returned properties that you want.
Refer to :class:`rtcclient.client.RTCClient` for more explanations
:return: A :class:`list` that contains all the
:class:`rtcclient.project_area.ProjectArea` objects
:rtype: list
"""
return self._getProjectAreas(archived=archived,
returned_properties=returned_properties)
def getProjectArea(self,
projectarea_name,
archived=False,
returned_properties=None):
"""Get :class:`rtcclient.project_area.ProjectArea` object by its name
:param projectarea_name: the project area name
:param archived: (default is False) whether the project area
is archived
:param returned_properties: the returned properties that you want.
Refer to :class:`rtcclient.client.RTCClient` for more explanations
:return: the :class:`rtcclient.project_area.ProjectArea` object
:rtype: rtcclient.project_area.ProjectArea
"""
if not isinstance(projectarea_name,
six.string_types) or not projectarea_name:
excp_msg = "Please specify a valid ProjectArea name"
self.log.error(excp_msg)
raise exception.BadValue(excp_msg)
self.log.debug("Try to get <ProjectArea %s>", projectarea_name)
rp = returned_properties
proj_areas = self._getProjectAreas(archived=archived,
returned_properties=rp,
projectarea_name=projectarea_name)
if proj_areas is not None:
proj_area = proj_areas[0]
self.log.info("Find <ProjectArea %s>", proj_area)
return proj_area
self.log.error("No ProjectArea named %s", projectarea_name)
raise exception.NotFound("No ProjectArea named %s" % projectarea_name)
def _getProjectAreas(self,
archived=False,
returned_properties=None,
projectarea_name=None,
projectarea_id=None):
rp = returned_properties
filter_rule = None
if projectarea_name is not None:
fpaname_rule = ("dc:title", None, projectarea_name)
filter_rule = self._add_filter_rule(filter_rule, fpaname_rule)
if projectarea_id is not None:
paid_url = "/".join([self.url, "oslc/projectareas", projectarea_id])
fpaid_rule = ("@rdf:resource", None, paid_url)
filter_rule = self._add_filter_rule(filter_rule, fpaid_rule)
return self._get_paged_resources("ProjectArea",
page_size="10",
archived=archived,
returned_properties=rp,
filter_rule=filter_rule)
def _add_filter_rule(self, filter_rule, added_rule):
if filter_rule is None:
filter_rule = [added_rule]
else:
filter_rule.append(added_rule)
return filter_rule
def getProjectAreaByID(self,
projectarea_id,
archived=False,
returned_properties=None):
"""Get :class:`rtcclient.project_area.ProjectArea` object by its id
:param projectarea_id: the :class:`rtcclient.project_area.ProjectArea`
id
:param archived: (default is False) whether the project area
is archived
:param returned_properties: the returned properties that you want.
Refer to :class:`rtcclient.client.RTCClient` for more explanations
:return: the :class:`rtcclient.project_area.ProjectArea` object
:rtype: rtcclient.project_area.ProjectArea
"""
if not isinstance(projectarea_id,
six.string_types) or not projectarea_id:
excp_msg = "Please specify a valid ProjectArea ID"
self.log.error(excp_msg)
raise exception.BadValue(excp_msg)
self.log.debug("Try to get <ProjectArea> by its id: %s", projectarea_id)
rp = returned_properties
proj_areas = self._getProjectAreas(archived=archived,
returned_properties=rp,
projectarea_id=projectarea_id)
if proj_areas is not None:
proj_area = proj_areas[0]
self.log.info("Find <ProjectArea %s>", proj_area)
return proj_area
self.log.error("No ProjectArea's ID is %s", projectarea_id)
raise exception.NotFound("No ProjectArea's ID is %s" % projectarea_id)
def getProjectAreaID(self, projectarea_name, archived=False):
"""Get :class:`rtcclient.project_area.ProjectArea` id by its name
:param projectarea_name: the project area name
:param archived: (default is False) whether the project area
is archived
:return: the :class:`string` object
:rtype: string
"""
self.log.debug("Get the ProjectArea id by its name: %s",
projectarea_name)
proj_area = self.getProjectArea(projectarea_name, archived=archived)
if proj_area:
return proj_area.id
raise exception.NotFound("No ProjectArea named %s" % projectarea_name)
def getProjectAreaIDs(self, projectarea_name=None, archived=False):
"""Get all :class:`rtcclient.project_area.ProjectArea` id(s)
by project area name
If `projectarea_name` is `None`, all the
:class:`rtcclient.project_area.ProjectArea` id(s) will be returned.
:param projectarea_name: the project area name
:param archived: (default is False) whether the project area
is archived
:return: a :class:`list` that contains all the :class:`ProjectArea` ids
:rtype: list
"""
projectarea_ids = list()
if projectarea_name and isinstance(projectarea_name, six.string_types):
projectarea_id = self.getProjectAreaID(projectarea_name,
archived=archived)
projectarea_ids.append(projectarea_id)
elif projectarea_name is None:
projectareas = self.getProjectAreas(archived=archived)
if projectareas is None:
return None
projectarea_ids = [proj_area.id for proj_area in projectareas]
else:
error_msg = "Invalid ProjectArea name: [%s]" % projectarea_name
self.log.error(error_msg)
raise exception.BadValue(error_msg)
return projectarea_ids
def checkProjectAreaID(self, projectarea_id, archived=False):
"""Check the validity of :class:`rtcclient.project_area.ProjectArea` id
:param projectarea_id: the :class:`rtcclient.project_area.ProjectArea`
id
:param archived: (default is False) whether the project area is
archived
:return: `True` or `False`
:rtype: bool
"""
self.log.debug("Check the validity of the ProjectArea id: %s",
projectarea_id)
proj_areas = self._getProjectAreas(archived=archived,
projectarea_id=projectarea_id)
if proj_areas is not None:
proj_area = proj_areas[0]
self.log.info("Find <ProjectArea %s> whose id is: %s", proj_area,
projectarea_id)
return True
self.log.error("No ProjectArea whose id is: %s", projectarea_id)
return False
def getTeamArea(self,
teamarea_name,
projectarea_id=None,
projectarea_name=None,
archived=False,
returned_properties=None):
"""Get :class:`rtcclient.models.TeamArea` object by its name
If `projectarea_id` or `projectarea_name` is
specified, then the matched :class:`rtcclient.models.TeamArea`
in that project area will be returned.
Otherwise, only return the first found
:class:`rtcclient.models.TeamArea` with that name.
:param teamarea_name: the team area name
:param projectarea_id: the :class:`rtcclient.project_area.ProjectArea`
id
:param projectarea_name: the project area name
:param archived: (default is False) whether the team area
is archived
:param returned_properties: the returned properties that you want.
Refer to :class:`rtcclient.client.RTCClient` for more explanations
:return: the :class:`rtcclient.models.TeamArea` object
:rtype: rtcclient.models.TeamArea
"""
if not isinstance(teamarea_name, six.string_types) or not teamarea_name:
excp_msg = "Please specify a valid TeamArea name"
self.log.error(excp_msg)
raise exception.BadValue(excp_msg)
self.log.debug("Try to get <TeamArea %s>", teamarea_name)
teamareas = self._getTeamAreas(projectarea_id=projectarea_id,
projectarea_name=projectarea_name,
archived=archived,
returned_properties=returned_properties,
teamarea_name=teamarea_name)
if teamareas is not None:
teamarea = teamareas[0]
self.log.info("Find <TeamArea %s>", teamarea)
return teamarea
self.log.error("No TeamArea named %s", teamarea_name)
raise exception.NotFound("No TeamArea named %s" % teamarea_name)
def getTeamAreas(self,
projectarea_id=None,
projectarea_name=None,
archived=False,
returned_properties=None):
"""Get all :class:`rtcclient.models.TeamArea` objects by
project area id or name
If both `projectarea_id` and `projectarea_name` are `None`,
all team areas in all project areas will be returned.
If no :class:`rtcclient.models.TeamArea` objects are retrieved,
`None` is returned.
:param projectarea_id: the :class:`rtcclient.project_area.ProjectArea`
id
:param projectarea_name: the project area name
:param archived: (default is False) whether the team areas
are archived
:param returned_properties: the returned properties that you want.
Refer to :class:`rtcclient.client.RTCClient` for more explanations
:return: a :class:`list` that contains all the
:class:`rtcclient.models.TeamArea` objects
:rtype: list
"""
return self._getTeamAreas(projectarea_id=projectarea_id,
projectarea_name=projectarea_name,
archived=archived,
returned_properties=returned_properties)
def _getTeamAreas(self,
projectarea_id=None,
projectarea_name=None,
archived=False,
returned_properties=None,
teamarea_name=None):
projarea_id = self._pre_get_resource(projectarea_id=projectarea_id,
projectarea_name=projectarea_name)
rp = returned_properties
filter_rule = None
if teamarea_name is not None:
ftaname_rule = ("dc:title", None, teamarea_name)
filter_rule = self._add_filter_rule(filter_rule, ftaname_rule)
return self._get_paged_resources("TeamArea",
projectarea_id=projarea_id,
page_size="100",
archived=archived,
returned_properties=rp,
filter_rule=filter_rule)
def getOwnedBy(self, username, projectarea_id=None, projectarea_name=None):
parse_result = urlparse.urlparse(self.url)
new_path = "/".join(["/jts/users", urlquote(username)])
new_parse_result = urlparse.ParseResult(scheme=parse_result.scheme,
netloc=parse_result.netloc,
path=new_path,
params=parse_result.params,
query=parse_result.query,
fragment=parse_result.fragment)
return Member(urlparse.urlunparse(new_parse_result), self)
def getPlannedFor(self,
plannedfor_name,
projectarea_id=None,
projectarea_name=None,
archived=False,
returned_properties=None):
"""Get :class:`rtcclient.models.PlannedFor` object by its name
:param plannedfor_name: the plannedfor name
:param projectarea_id: the :class:`rtcclient.project_area.ProjectArea`
id
:param projectarea_name: the project area name
:param archived: (default is False) whether the plannedfor
is archived
:param returned_properties: the returned properties that you want.
Refer to :class:`rtcclient.client.RTCClient` for more explanations
:return: the :class:`rtcclient.models.PlannedFor` object
:rtype: rtcclient.models.PlannedFor
"""
if not isinstance(plannedfor_name,
six.string_types) or not plannedfor_name:
excp_msg = "Please specify a valid PlannedFor name"
self.log.error(excp_msg)
raise exception.BadValue(excp_msg)
self.log.debug("Try to get <PlannedFor %s>", plannedfor_name)
rp = returned_properties
plannedfors = self._getPlannedFors(projectarea_id=projectarea_id,
projectarea_name=projectarea_name,
archived=archived,
returned_properties=rp,
plannedfor_name=plannedfor_name)
if plannedfors is not None:
plannedfor = plannedfors[0]
self.log.info("Find <PlannedFor %s>", plannedfor)
return plannedfor
self.log.error("No PlannedFor named %s", plannedfor_name)
raise exception.NotFound("No PlannedFor named %s" % plannedfor_name)
def getPlannedFors(self,
projectarea_id=None,
projectarea_name=None,
archived=False,
returned_properties=None):
"""Get all :class:`rtcclient.models.PlannedFor` objects by
project area id or name
If both `projectarea_id` and `projectarea_name` are None,
all the plannedfors in all project areas will be returned.
If no :class:`rtcclient.models.PlannedFor` objecs are retrieved,
`None` is returned.
:param projectarea_id: the :class:`rtcclient.project_area.ProjectArea`
id
:param projectarea_name: the project area name
:param archived: (default is False) whether the plannedfors
are archived
:param returned_properties: the returned properties that you want.
Refer to :class:`rtcclient.client.RTCClient` for more explanations
:return: a :class:`list` that contains all the
:class:`rtcclient.models.PlannedFor` objects
:rtype: list
"""
return self._getPlannedFors(projectarea_id=projectarea_id,
projectarea_name=projectarea_name,
archived=archived,
returned_properties=returned_properties)
def _getPlannedFors(self,
projectarea_id=None,
projectarea_name=None,
archived=False,
returned_properties=None,
plannedfor_name=None):
projarea_id = self._pre_get_resource(projectarea_id=projectarea_id,
projectarea_name=projectarea_name)
filter_rule = None
if plannedfor_name is not None:
fpfname_rule = ("dc:title", None, plannedfor_name)
filter_rule = self._add_filter_rule(filter_rule, fpfname_rule)
rp = returned_properties
return self._get_paged_resources("PlannedFor",
projectarea_id=projarea_id,
page_size="100",
archived=archived,
returned_properties=rp,
filter_rule=filter_rule)
def getSeverity(self,
severity_name,
projectarea_id=None,
projectarea_name=None):
"""Get :class:`rtcclient.models.Severity` object by its name
At least either of `projectarea_id` and `projectarea_name` is given
:param severity_name: the severity name
:param projectarea_id: the :class:`rtcclient.project_area.ProjectArea`
id
:param projectarea_name: the project area name
:return: the :class:`rtcclient.models.Severity` object
:rtype: rtcclient.models.Severity
"""
self.log.debug("Try to get <Severity %s>", severity_name)
if not isinstance(severity_name, six.string_types) or not severity_name:
excp_msg = "Please specify a valid Severity name"
self.log.error(excp_msg)
raise exception.BadValue(excp_msg)
severities = self._getSeverities(projectarea_id=projectarea_id,
projectarea_name=projectarea_name,
severity_name=severity_name)
if severities is not None:
severity = severities[0]
self.log.info("Find <Severity %s>", severity)
return severity
self.log.error("No Severity named %s", severity_name)
raise exception.NotFound("No Severity named %s" % severity_name)
def getSeverities(self, projectarea_id=None, projectarea_name=None):
"""Get all :class:`rtcclient.models.Severity` objects by
project area id or name
At least either of `projectarea_id` and `projectarea_name` is given
If no :class:`rtcclient.models.Severity` is retrieved,
`None` is returned.
:param projectarea_id: the :class:`rtcclient.project_area.ProjectArea`
id
:param projectarea_name: the project area name
:return: a :class:`list` that contains all the
:class:`rtcclient.models.Severity` objects
:rtype: list
"""
return self._getSeverities(projectarea_id=projectarea_id,
projectarea_name=projectarea_name)
def _getSeverities(self,
projectarea_id=None,
projectarea_name=None,
severity_name=None):
projarea_id = self._pre_get_resource(projectarea_id=projectarea_id,
projectarea_name=projectarea_name)
if projarea_id is None:
self.log.error("Please input either-or between "
"projectarea_id and projectarea_name")
raise exception.EmptyAttrib("At least input either-or between "
"projectarea_id and projectarea_name")
filter_rule = None
if severity_name is not None:
fsname_rule = ("dc:title", None, severity_name)
filter_rule = self._add_filter_rule(filter_rule, fsname_rule)
return self._get_paged_resources("Severity",
projectarea_id=projarea_id,
page_size="10",
filter_rule=filter_rule)
def getPriority(self,
priority_name,
projectarea_id=None,
projectarea_name=None):
"""Get :class:`rtcclient.models.Priority` object by its name
At least either of `projectarea_id` and `projectarea_name` is given
:param priority_name: the priority name
:param projectarea_id: the :class:`rtcclient.project_area.ProjectArea`
id
:param projectarea_name: the project area name
:return: the :class:`rtcclient.models.Priority` object
:rtype: rtcclient.models.Priority
"""
self.log.debug("Try to get <Priority %s>", priority_name)
if not isinstance(priority_name, six.string_types) or not priority_name:
excp_msg = "Please specify a valid Priority name"
self.log.error(excp_msg)
raise exception.BadValue(excp_msg)
priorities = self._getPriorities(projectarea_id=projectarea_id,
projectarea_name=projectarea_name,
priority_name=priority_name)
if priorities is not None:
priority = priorities[0]
self.log.info("Find <Priority %s>", priority)
return priority
self.log.error("No Priority named %s", priority_name)
raise exception.NotFound("No Priority named %s" % priority_name)
def getPriorities(self, projectarea_id=None, projectarea_name=None):
"""Get all :class:`rtcclient.models.Priority` objects by
project area id or name
At least either of `projectarea_id` and `projectarea_name` is given.
If no :class:`rtcclient.models.Priority` is retrieved,
`None` is returned.
:param projectarea_id: the :class:`rtcclient.project_area.ProjectArea`
id
:param projectarea_name: the project area name
:return: a :class:`list` contains all the
:class:`rtcclient.models.Priority` objects
:rtype: list
"""
return self._getPriorities(projectarea_id=projectarea_id,
projectarea_name=projectarea_name)
def _getPriorities(self,
projectarea_id=None,
projectarea_name=None,
priority_name=None):
projarea_id = self._pre_get_resource(projectarea_id=projectarea_id,
projectarea_name=projectarea_name)
if projarea_id is None:
self.log.error("Please input either-or between "
"projectarea_id and projectarea_name")
raise exception.EmptyAttrib("At least input either-or between "
"projectarea_id and projectarea_name")
filter_rule = None
if priority_name is not None:
fpname_rule = ("dc:title", None, priority_name)
filter_rule = self._add_filter_rule(filter_rule, fpname_rule)
return self._get_paged_resources("Priority",
projectarea_id=projarea_id,
page_size="10",
filter_rule=filter_rule)
def getFoundIn(self,
foundin_name,
projectarea_id=None,
projectarea_name=None,
archived=False):
"""Get :class:`rtcclient.models.FoundIn` object by its name
:param foundin_name: the foundin name
:param projectarea_id: the :class:`rtcclient.project_area.ProjectArea`
id
:param projectarea_name: the project area name
:param archived: (default is False) whether the foundin is archived
:return: the :class:`rtcclient.models.FoundIn` object
:rtype: rtcclient.models.FoundIn
"""
self.log.debug("Try to get <FoundIn %s>", foundin_name)
if not isinstance(foundin_name, six.string_types) or not foundin_name:
excp_msg = "Please specify a valid PlannedFor name"
self.log.error(excp_msg)
raise exception.BadValue(excp_msg)
foundins = self._getFoundIns(projectarea_id=projectarea_id,
projectarea_name=projectarea_name,
archived=archived,
foundin_name=foundin_name)
if foundins is not None:
foundin = foundins[0]
self.log.info("Find <FoundIn %s>", foundin)
return foundin
self.log.error("No FoundIn named %s", foundin_name)
raise exception.NotFound("No FoundIn named %s" % foundin_name)
def getFoundIns(self,
projectarea_id=None,
projectarea_name=None,
archived=False):
"""Get all :class:`rtcclient.models.FoundIn` objects by
project area id or name
If both `projectarea_id` and `projectarea_name` are `None`,
all the foundins in all project areas will be returned.
If no :class:`rtcclient.models.FoundIn` objects are retrieved,
`None` is returned.
:param projectarea_id: the :class:`rtcclient.project_area.ProjectArea`
id
:param projectarea_name: the project area name
:param archived: (default is False) whether the foundins are archived
:return: a :class:`list` that contains all the
:class:`rtcclient.models.FoundIn` objects
:rtype: list
"""
return self._getFoundIns(projectarea_id=projectarea_id,
projectarea_name=projectarea_name,
archived=archived)
def _getFoundIns(self,
projectarea_id=None,
projectarea_name=None,
archived=False,
foundin_name=None):
projarea_id = self._pre_get_resource(projectarea_id=projectarea_id,
projectarea_name=projectarea_name)
filter_rule = None
if foundin_name is not None:
ffname_rule = ("dc:title", None, foundin_name)
filter_rule = self._add_filter_rule(filter_rule, ffname_rule)
return self._get_paged_resources("FoundIn",
projectarea_id=projarea_id,
page_size="100",
archived=archived,
filter_rule=filter_rule)
def getFiledAgainst(self,
filedagainst_name,
projectarea_id=None,
projectarea_name=None,
archived=False):
"""Get :class:`rtcclient.models.FiledAgainst` object by its name
:param filedagainst_name: the filedagainst name
:param projectarea_id: the :class:`rtcclient.project_area.ProjectArea`
id
:param projectarea_name: the project area name
:param archived: (default is False) whether the filedagainst is
archived
:return: the :class:`rtcclient.models.FiledAgainst` object
:rtype: rtcclient.models.FiledAgainst
"""
self.log.debug("Try to get <FiledAgainst %s>", filedagainst_name)
if not isinstance(filedagainst_name,
six.string_types) or not filedagainst_name:
excp_msg = "Please specify a valid FiledAgainst name"
self.log.error(excp_msg)
raise exception.BadValue(excp_msg)
fas = self._getFiledAgainsts(projectarea_id=projectarea_id,
projectarea_name=projectarea_name,
archived=archived,
filedagainst_name=filedagainst_name)
if fas is not None:
filedagainst = fas[0]
self.log.info("Find <FiledAgainst %s>", filedagainst)
return filedagainst
error_msg = "No FiledAgainst named %s" % filedagainst_name
self.log.error(error_msg)
raise exception.NotFound(error_msg)
def getFiledAgainsts(self,
projectarea_id=None,
projectarea_name=None,
archived=False):
"""Get all :class:`rtcclient.models.FiledAgainst` objects by
project area id or name
If both `projectarea_id` and `projectarea_name` are `None`,
all the filedagainsts in all project areas will be returned.
If no :class:`rtcclient.models.FiledAgainst` objects are retrieved,
`None` is returned.
:param projectarea_id: the :class:`rtcclient.project_area.ProjectArea`
id
:param projectarea_name: the project area name
:param archived: (default is False) whether the filedagainsts are
archived
:return: a :class:`list` that contains all the
:class:`rtcclient.models.FiledAgainst` objects
:rtype: list
"""
return self._getFiledAgainsts(projectarea_id=projectarea_id,
projectarea_name=projectarea_name,
archived=archived)
def _getFiledAgainsts(self,
projectarea_id=None,
projectarea_name=None,
archived=False,
filedagainst_name=None):
projarea_id = self._pre_get_resource(projectarea_id=projectarea_id,
projectarea_name=projectarea_name)
filter_rule = None
if filedagainst_name is not None:
ffaname_rule = ("dc:title", None, filedagainst_name)
filter_rule = self._add_filter_rule(filter_rule, ffaname_rule)
return self._get_paged_resources("FiledAgainst",
projectarea_id=projarea_id,
page_size="100",
archived=archived,
filter_rule=filter_rule)
def getTemplate(self,
copied_from,
template_name=None,
template_folder=None,
keep=False,
encoding="UTF-8"):
"""Get template from some to-be-copied workitems
More details, please refer to
:class:`rtcclient.template.Templater.getTemplate`
"""
return self.templater.getTemplate(copied_from,
template_name=template_name,
template_folder=template_folder,
keep=keep,
encoding=encoding)
def getTemplates(self,
workitems,
template_folder=None,
template_names=None,
keep=False,
encoding="UTF-8"):
"""Get templates from a group of to-be-copied workitems
and write them to files named after the names in `template_names`
respectively.
More details, please refer to
:class:`rtcclient.template.Templater.getTemplates`
"""
self.templater.getTemplates(workitems,
template_folder=template_folder,
template_names=template_names,
keep=keep,
encoding=encoding)
def listFields(self, template):
"""List all the attributes to be rendered from the template file
:param template: The template to render.
The template is actually a file, which is usually generated
by :class:`rtcclient.template.Templater.getTemplate` and can also
be modified by user accordingly.
:return: a :class:`set` that contains all the needed attributes
:rtype: set
More details, please refer to
:class:`rtcclient.template.Templater.listFieldsFromWorkitem`
"""
return self.templater.listFields(template)
def listFieldsFromWorkitem(self, copied_from, keep=False):
"""List all the attributes to be rendered directly from some
to-be-copied workitems
More details, please refer to
:class:`rtcclient.template.Templater.listFieldsFromWorkitem`
"""
return self.templater.listFieldsFromWorkitem(copied_from, keep=keep)
def getWorkitem(self,
workitem_id,
returned_properties=None,
skip_full_attributes=True):
"""Get :class:`rtcclient.workitem.Workitem` object by its id/number
:param workitem_id: the workitem id/number
(integer or equivalent string)
:param returned_properties: the returned properties that you want.
Refer to :class:`rtcclient.client.RTCClient` for more explanations
:param skip_full_attributes: flag to retrieve all attributes.
:return: the :class:`rtcclient.workitem.Workitem` object
:rtype: rtcclient.workitem.Workitem
"""
try:
if isinstance(workitem_id, bool):
raise ValueError("Invalid Workitem id")
if isinstance(workitem_id, six.string_types):
workitem_id = int(workitem_id)