|
7 | 7 |
|
8 | 8 |
|
9 | 9 | class JIRASensor(PollingSensor): |
10 | | - """ |
| 10 | + ''' |
11 | 11 | Sensor will monitor for any new projects created in JIRA and |
12 | 12 | emit trigger instance when one is created. |
13 | | - """ |
| 13 | + ''' |
14 | 14 |
|
15 | 15 | def __init__(self, sensor_service, config=None, poll_interval=5): |
16 | 16 | super(JIRASensor, self).__init__( |
17 | 17 | sensor_service=sensor_service, config=config, poll_interval=poll_interval |
18 | 18 | ) |
19 | 19 |
|
20 | 20 | self._jira_url = None |
21 | | - # The Consumer Key created while setting up the "Incoming Authentication" in |
| 21 | + # The Consumer Key created while setting up the 'Incoming Authentication' in |
22 | 22 | # JIRA for the Application Link. |
23 | | - self._consumer_key = u"" |
| 23 | + self._consumer_key = u'' |
24 | 24 | self._rsa_key = None |
25 | 25 | self._jira_client = None |
26 | | - self._access_token = u"" |
27 | | - self._access_secret = u"" |
| 26 | + self._access_token = u'' |
| 27 | + self._access_secret = u'' |
28 | 28 | self._projects_available = None |
29 | 29 | self._poll_interval = 30 |
30 | 30 | self._project = None |
31 | 31 | self._issues_in_project = None |
32 | 32 | self._jql_query = None |
33 | | - self._trigger_name = "issues_tracker" |
34 | | - self._trigger_pack = "jira" |
35 | | - 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]) |
36 | 36 |
|
37 | 37 | def _read_cert(self, file_path): |
38 | 38 | with open(file_path) as f: |
39 | 39 | return f.read() |
40 | 40 |
|
41 | 41 | def setup(self): |
42 | | - self._jira_url = self._config["url"] |
43 | | - auth_method = self._config["auth_method"] |
| 42 | + self._jira_url = self._config['url'] |
| 43 | + auth_method = self._config['auth_method'] |
44 | 44 |
|
45 | | - options = {"server": self._config["url"], "verify": self._config["verify"]} |
| 45 | + options = {'server': self._config['url'], |
| 46 | + 'verify': self._config['verify']} |
46 | 47 | # Getting client cert configuration |
47 | | - cert_file_path = self._config["client_cert_file"] |
48 | | - key_file_path = self._config["client_key_file"] |
| 48 | + cert_file_path = self._config['client_cert_file'] |
| 49 | + key_file_path = self._config['client_key_file'] |
49 | 50 | if cert_file_path and key_file_path: |
50 | | - options["client_cert"] = (cert_file_path, key_file_path) |
| 51 | + options['client_cert'] = (cert_file_path, key_file_path) |
51 | 52 |
|
52 | | - if auth_method == "oauth": |
53 | | - rsa_cert_file = self._config["rsa_cert_file"] |
| 53 | + if auth_method == 'oauth': |
| 54 | + rsa_cert_file = self._config['rsa_cert_file'] |
54 | 55 | if not os.path.exists(rsa_cert_file): |
55 | 56 | raise Exception( |
56 | | - "Cert file for JIRA OAuth not found at %s." % rsa_cert_file |
| 57 | + 'Cert file for JIRA OAuth not found at %s.' % rsa_cert_file |
57 | 58 | ) |
58 | 59 | self._rsa_key = self._read_cert(rsa_cert_file) |
59 | | - self._poll_interval = self._config.get("poll_interval", self._poll_interval) |
| 60 | + self._poll_interval = self._config.get( |
| 61 | + 'poll_interval', self._poll_interval) |
60 | 62 | oauth_creds = { |
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, |
| 63 | + 'access_token': self._config['oauth_token'], |
| 64 | + 'access_token_secret': self._config['oauth_secret'], |
| 65 | + 'consumer_key': self._config['consumer_key'], |
| 66 | + 'key_cert': self._rsa_key, |
65 | 67 | } |
66 | 68 |
|
67 | 69 | self._jira_client = JIRA(options=options, oauth=oauth_creds) |
68 | | - elif auth_method == "basic": |
69 | | - basic_creds = (self._config["username"], self._config["password"]) |
| 70 | + elif auth_method == 'basic': |
| 71 | + basic_creds = (self._config['username'], self._config['password']) |
70 | 72 | self._jira_client = JIRA(options=options, basic_auth=basic_creds) |
71 | 73 |
|
72 | 74 | else: |
73 | | - msg = ( |
74 | | - 'You must set auth_method to either "oauth"', |
75 | | - 'or "basic" your jira.yaml config file.', |
76 | | - ) |
| 75 | + msg = ('You must set auth_method to either "oauth"', |
| 76 | + 'or "basic" your jira.yaml config file.', |
| 77 | + ) |
77 | 78 | raise Exception(msg) |
78 | 79 |
|
79 | 80 | if self._projects_available is None: |
80 | 81 | self._projects_available = set() |
81 | 82 | for proj in self._jira_client.projects(): |
82 | 83 | self._projects_available.add(proj.key) |
83 | | - self._project = self._config.get("project", None) |
| 84 | + self._project = self._config.get('project', None) |
84 | 85 | if not self._project or self._project not in self._projects_available: |
85 | | - raise Exception("Invalid project (%s) to track." % self._project) |
86 | | - self._jql_query = "project=%s" % self._project |
87 | | - all_issues = self._jira_client.search_issues(self._jql_query, maxResults=None) |
| 86 | + raise Exception('Invalid project (%s) to track.' % self._project) |
| 87 | + self._jql_query = 'project=%s' % self._project |
| 88 | + all_issues = self._jira_client.search_issues( |
| 89 | + self._jql_query, maxResults=None) |
88 | 90 | self._issues_in_project = {issue.key: issue for issue in all_issues} |
89 | 91 |
|
90 | 92 | def poll(self): |
@@ -115,12 +117,12 @@ def _detect_new_issues(self): |
115 | 117 | def _dispatch_issues_trigger(self, issue): |
116 | 118 | trigger = self._trigger_ref |
117 | 119 | payload = {} |
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"] |
| 120 | + payload['issue_name'] = issue.key |
| 121 | + payload['issue_url'] = issue.self |
| 122 | + payload['issue_browse_url'] = self._jira_url + '/browse/' + issue.key |
| 123 | + payload['project'] = self._project |
| 124 | + payload['created'] = issue.raw['fields']['created'] |
| 125 | + payload['assignee'] = issue.raw['fields']['assignee'] |
| 126 | + payload['fix_versions'] = issue.raw['fields']['fixVersions'] |
| 127 | + payload['issue_type'] = issue.raw['fields']['issuetype']['name'] |
126 | 128 | self._sensor_service.dispatch(trigger, payload) |
0 commit comments