26
26
import logging
27
27
import os
28
28
import re
29
- import tempfile
30
29
try : # Python 2.7+
31
30
from logging import NullHandler
32
31
except ImportError :
@@ -3360,38 +3359,13 @@ def delete_project(self, pid):
3360
3359
if hasattr (pid , 'id' ):
3361
3360
pid = pid .id
3362
3361
3363
- # Check if pid is a number - then we assume that it is
3364
- # projectID
3365
- try :
3366
- str (int (pid )) == pid
3367
- except Exception as e :
3368
- # pid looks like a slug, lets verify that
3369
- r_json = self ._get_json ('project' )
3370
- for e in r_json :
3371
- if e ['key' ] == pid or e ['name' ] == pid :
3372
- pid = e ['id' ]
3373
- break
3374
- else :
3375
- # pid is not a Project
3376
- # not a projectID and not a slug - we raise error here
3377
- raise ValueError ('Parameter pid="%s" is not a Project, '
3378
- 'projectID or slug' % pid )
3379
-
3380
- uri = '/rest/api/2/project/%s' % pid
3381
- url = self ._options ['server' ] + uri
3382
- try :
3383
- r = self ._session .delete (
3384
- url , headers = {'Content-Type' : 'application/json' }
3385
- )
3386
- except JIRAError as je :
3387
- if '403' in str (je ):
3388
- raise JIRAError ('Not enough permissions to delete project' )
3389
- if '404' in str (je ):
3390
- raise JIRAError ('Project not found in Jira' )
3391
- raise je
3392
-
3393
- if r .status_code == 204 :
3394
- return True
3362
+ url = self ._options ['server' ] + '/rest/api/2/project/%s' % pid
3363
+ r = self ._session .delete (url )
3364
+ if r .status_code == 403 :
3365
+ raise JIRAError ('Not enough permissions to delete project' )
3366
+ if r .status_code == 404 :
3367
+ raise JIRAError ('Project not found in Jira' )
3368
+ return r .ok
3395
3369
3396
3370
def _gain_sudo_session (self , options , destination ):
3397
3371
url = self ._options ['server' ] + '/secure/admin/WebSudoAuthenticate.jspa'
@@ -3419,16 +3393,14 @@ def templates(self):
3419
3393
data = json_loads (r )
3420
3394
3421
3395
templates = {}
3422
- # if 'projectTemplates' in data:
3423
- # template_ = data['projectTemplates']
3424
- # el
3425
3396
if 'projectTemplatesGroupedByType' in data :
3426
3397
for group in data ['projectTemplatesGroupedByType' ]:
3427
3398
for t in group ['projectTemplates' ]:
3428
3399
templates [t ['name' ]] = t
3400
+ # pprint(templates.keys())
3429
3401
return templates
3430
3402
3431
- def create_project (self , key , name = None , assignee = None , type = "Software " , template_name = None ):
3403
+ def create_project (self , key , name = None , assignee = None , type = "software " , template_name = None ):
3432
3404
"""Create a project with the specified parameters.
3433
3405
3434
3406
:param key: Mandatory. Must match JIRA project key requirements, usually only 2-10 uppercase characters.
@@ -3447,60 +3419,60 @@ def create_project(self, key, name=None, assignee=None, type="Software", templat
3447
3419
:rtype: Union[bool,int]
3448
3420
3449
3421
"""
3422
+ template_key = None
3423
+
3450
3424
if assignee is None :
3451
3425
assignee = self .current_user ()
3452
3426
if name is None :
3453
3427
name = key
3454
3428
3455
- possible_templates = ['Basic' , 'JIRA Classic' , 'JIRA Default Schemes' , 'Basic software development' ]
3456
-
3457
- if template_name is not None :
3458
- possible_templates = [template_name ]
3429
+ # preference list for picking a default template
3430
+ possible_templates = [
3431
+ 'Scrum software development' , # have Bug
3432
+ 'Agility' , # cannot set summary
3433
+ 'Bug tracking' ,
3434
+ 'JIRA Classic' ,
3435
+ 'JIRA Default Schemes' ,
3436
+ 'Basic software development' ,
3437
+ 'Project management' ,
3438
+ 'Kanban software development' ,
3439
+ 'Task management' ,
3440
+
3441
+ 'Basic' , # does not have Bug
3442
+ 'Content Management' ,
3443
+ 'Customer service' ,
3444
+ 'Document Approval' ,
3445
+ 'IT Service Desk' ,
3446
+ 'Lead Tracking' ,
3447
+ 'Process management' ,
3448
+ 'Procurement' ,
3449
+ 'Recruitment' ,
3450
+ ]
3459
3451
3460
- # https://confluence.atlassian.com/jirakb/creating-a-project-via-rest-based-on-jira-default-schemes-744325852.html
3461
3452
templates = self .templates ()
3462
- # TODO(ssbarnea): find a better logic to pick a default fallback template
3463
- template_key = list (templates .values ())[0 ]['projectTemplateModuleCompleteKey' ]
3464
- for template_name , template_dic in templates .items ():
3465
- if template_name in possible_templates :
3466
- template_key = template_dic ['projectTemplateModuleCompleteKey' ]
3467
- break
3453
+ if not template_name :
3454
+ template_name = next (t for t in possible_templates if t in templates )
3455
+
3456
+ template_key = templates [template_name ]['projectTemplateModuleCompleteKey' ]
3457
+ project_type_key = templates [template_name ]['projectTypeKey' ]
3468
3458
3459
+ # https://confluence.atlassian.com/jirakb/creating-a-project-via-rest-based-on-jira-default-schemes-744325852.html
3460
+ # see https://confluence.atlassian.com/jirakb/creating-projects-via-rest-api-in-jira-963651978.html
3469
3461
payload = {'name' : name ,
3470
3462
'key' : key ,
3471
- 'keyEdited' : 'false' ,
3472
- # 'projectTemplate': 'com.atlassian.jira-core-project-templates:jira-issuetracking',
3473
- # 'permissionScheme': '',
3474
- 'projectTemplateWebItemKey' : template_key ,
3475
- 'projectTemplateModuleKey' : template_key ,
3463
+ 'projectTypeKey' : project_type_key ,
3464
+ 'projectTemplateKey' : template_key ,
3476
3465
'lead' : assignee ,
3477
3466
'assigneeType' : 'PROJECT_LEAD' ,
3478
3467
}
3479
3468
3480
- if self ._version [0 ] > 6 :
3481
- # JIRA versions before 7 will throw an error if we specify type parameter
3482
- payload ['type' ] = type
3483
-
3484
- headers = CaseInsensitiveDict (
3485
- {'Content-Type' : 'application/x-www-form-urlencoded' })
3486
3469
url = self ._options ['server' ] + \
3487
- '/rest/project-templates/latest/templates '
3470
+ '/rest/api/2/project '
3488
3471
3489
- r = self ._session .post (url , data = payload , headers = headers )
3490
-
3491
- if r .status_code == 200 :
3492
- r_json = json_loads (r )
3493
- return r_json
3494
-
3495
- f = tempfile .NamedTemporaryFile (
3496
- suffix = '.html' , prefix = 'python-jira-error-create-project-' , delete = False )
3497
- f .write (r .text )
3498
-
3499
- if self .logging :
3500
- logging .error (
3501
- "Unexpected result while running create project. Server response saved in %s for further investigation [HTTP response=%s]." % (
3502
- f .name , r .status_code ))
3503
- return False
3472
+ r = self ._session .post (url , data = json .dumps (payload ))
3473
+ r .raise_for_status ()
3474
+ r_json = json_loads (r )
3475
+ return r_json
3504
3476
3505
3477
def add_user (self ,
3506
3478
username ,
0 commit comments