77
88
99class JIRASensor (PollingSensor ):
10- '''
10+ """
1111 Sensor will monitor for any new projects created in JIRA and
1212 emit trigger instance when one is created.
13- '''
13+ """
14+
1415 def __init__ (self , sensor_service , config = None , poll_interval = 5 ):
15- super (JIRASensor , self ).__init__ (sensor_service = sensor_service ,
16- config = config ,
17- poll_interval = poll_interval )
16+ super (JIRASensor , self ).__init__ (
17+ sensor_service = sensor_service , config = config , poll_interval = poll_interval
18+ )
1819
1920 self ._jira_url = None
2021 # The Consumer Key created while setting up the "Incoming Authentication" in
2122 # JIRA for the Application Link.
22- self ._consumer_key = u''
23+ self ._consumer_key = u""
2324 self ._rsa_key = None
2425 self ._jira_client = None
25- self ._access_token = u''
26- self ._access_secret = u''
26+ self ._access_token = u""
27+ self ._access_secret = u""
2728 self ._projects_available = None
2829 self ._poll_interval = 30
2930 self ._project = None
3031 self ._issues_in_project = None
3132 self ._jql_query = None
32- self ._trigger_name = ' issues_tracker'
33- self ._trigger_pack = ' jira'
34- self ._trigger_ref = '.' .join ([self ._trigger_pack , self ._trigger_name ])
33+ self ._trigger_name = " issues_tracker"
34+ self ._trigger_pack = " jira"
35+ self ._trigger_ref = "." .join ([self ._trigger_pack , self ._trigger_name ])
3536
3637 def _read_cert (self , file_path ):
3738 with open (file_path ) as f :
3839 return f .read ()
3940
4041 def setup (self ):
41- self ._jira_url = self ._config ['url' ]
42- auth_method = self ._config ['auth_method' ]
43-
44- if auth_method == 'oauth' :
45- rsa_cert_file = self ._config ['rsa_cert_file' ]
42+ self ._jira_url = self ._config ["url" ]
43+ auth_method = self ._config ["auth_method" ]
44+
45+ options = {"server" : self ._config ["url" ], "verify" : self ._config ["verify" ]}
46+ # Getting client cert configuration
47+ cert_file_path = self ._config ["client_cert_file" ]
48+ key_file_path = self ._config ["client_key_file" ]
49+ if cert_file_path and key_file_path :
50+ options ["client_cert" ] = (cert_file_path , key_file_path )
51+
52+ if auth_method == "oauth" :
53+ rsa_cert_file = self ._config ["rsa_cert_file" ]
4654 if not os .path .exists (rsa_cert_file ):
47- raise Exception ('Cert file for JIRA OAuth not found at %s.' % rsa_cert_file )
55+ raise Exception (
56+ "Cert file for JIRA OAuth not found at %s." % rsa_cert_file
57+ )
4858 self ._rsa_key = self ._read_cert (rsa_cert_file )
49- self ._poll_interval = self ._config .get (' poll_interval' , self ._poll_interval )
59+ self ._poll_interval = self ._config .get (" poll_interval" , self ._poll_interval )
5060 oauth_creds = {
51- ' access_token' : self ._config [' oauth_token' ],
52- ' access_token_secret' : self ._config [' oauth_secret' ],
53- ' consumer_key' : self ._config [' consumer_key' ],
54- ' key_cert' : self ._rsa_key
61+ " access_token" : self ._config [" oauth_token" ],
62+ " access_token_secret" : self ._config [" oauth_secret" ],
63+ " consumer_key" : self ._config [" consumer_key" ],
64+ " key_cert" : self ._rsa_key ,
5565 }
5666
57- self ._jira_client = JIRA (options = {'server' : self ._jira_url },
58- oauth = oauth_creds )
59- elif auth_method == 'basic' :
60- basic_creds = (self ._config ['username' ], self ._config ['password' ])
61- self ._jira_client = JIRA (options = {'server' : self ._jira_url },
62- basic_auth = basic_creds )
67+ self ._jira_client = JIRA (options = options , oauth = oauth_creds )
68+ elif auth_method == "basic" :
69+ basic_creds = (self ._config ["username" ], self ._config ["password" ])
70+ self ._jira_client = JIRA (options = options , basic_auth = basic_creds )
6371
6472 else :
65- msg = ('You must set auth_method to either "oauth"' ,
66- 'or "basic" your jira.yaml config file.' )
73+ msg = (
74+ 'You must set auth_method to either "oauth"' ,
75+ 'or "basic" your jira.yaml config file.' ,
76+ )
6777 raise Exception (msg )
6878
6979 if self ._projects_available is None :
7080 self ._projects_available = set ()
7181 for proj in self ._jira_client .projects ():
7282 self ._projects_available .add (proj .key )
73- self ._project = self ._config .get (' project' , None )
83+ self ._project = self ._config .get (" project" , None )
7484 if not self ._project or self ._project not in self ._projects_available :
75- raise Exception (' Invalid project (%s) to track.' % self ._project )
76- self ._jql_query = ' project=%s' % self ._project
85+ raise Exception (" Invalid project (%s) to track." % self ._project )
86+ self ._jql_query = " project=%s" % self ._project
7787 all_issues = self ._jira_client .search_issues (self ._jql_query , maxResults = None )
7888 self ._issues_in_project = {issue .key : issue for issue in all_issues }
7989
@@ -93,7 +103,9 @@ def remove_trigger(self, trigger):
93103 pass
94104
95105 def _detect_new_issues (self ):
96- new_issues = self ._jira_client .search_issues (self ._jql_query , maxResults = 50 , startAt = 0 )
106+ new_issues = self ._jira_client .search_issues (
107+ self ._jql_query , maxResults = 50 , startAt = 0
108+ )
97109
98110 for issue in new_issues :
99111 if issue .key not in self ._issues_in_project :
@@ -103,12 +115,12 @@ def _detect_new_issues(self):
103115 def _dispatch_issues_trigger (self , issue ):
104116 trigger = self ._trigger_ref
105117 payload = {}
106- payload [' issue_name' ] = issue .key
107- payload [' issue_url' ] = issue .self
108- payload [' issue_browse_url' ] = self ._jira_url + ' /browse/' + issue .key
109- payload [' project' ] = self ._project
110- payload [' created' ] = issue .raw [' fields' ][ ' created' ]
111- payload [' assignee' ] = issue .raw [' fields' ][ ' assignee' ]
112- payload [' fix_versions' ] = issue .raw [' fields' ][ ' fixVersions' ]
113- payload [' issue_type' ] = issue .raw [' fields' ][ ' issuetype' ][ ' name' ]
118+ payload [" issue_name" ] = issue .key
119+ payload [" issue_url" ] = issue .self
120+ payload [" issue_browse_url" ] = self ._jira_url + " /browse/" + issue .key
121+ payload [" project" ] = self ._project
122+ payload [" created" ] = issue .raw [" fields" ][ " created" ]
123+ payload [" assignee" ] = issue .raw [" fields" ][ " assignee" ]
124+ payload [" fix_versions" ] = issue .raw [" fields" ][ " fixVersions" ]
125+ payload [" issue_type" ] = issue .raw [" fields" ][ " issuetype" ][ " name" ]
114126 self ._sensor_service .dispatch (trigger , payload )
0 commit comments