|
| 1 | +# Requirements |
| 2 | +# pip install jira |
| 3 | + |
| 4 | +try: |
| 5 | + import simplejson as json |
| 6 | +except ImportError: |
| 7 | + import json |
| 8 | +import os |
| 9 | +import time |
| 10 | + |
| 11 | +from jira.client import JIRA |
| 12 | + |
| 13 | +CONFIG_FILE = './jira_config.json' |
| 14 | + |
| 15 | + |
| 16 | +class JIRASensor(object): |
| 17 | + ''' |
| 18 | + Sensor will monitor for any new projects created in JIRA and |
| 19 | + emit trigger instance when one is created. |
| 20 | + ''' |
| 21 | + def __init__(self, container_service): |
| 22 | + self._container_service = container_service |
| 23 | + self._jira_server = 'https://stackstorm.atlassian.net' |
| 24 | + # The Consumer Key created while setting up the "Incoming Authentication" in |
| 25 | + # JIRA for the Application Link. |
| 26 | + self._consumer_key = u'' |
| 27 | + self._rsa_key = None |
| 28 | + self._jira_client = None |
| 29 | + self._access_token = u'' |
| 30 | + self._access_secret = u'' |
| 31 | + self._projects_available = None |
| 32 | + self._sleep_time = 30 |
| 33 | + self._config = None |
| 34 | + |
| 35 | + def _read_cert(self, file_path): |
| 36 | + with open(file_path) as f: |
| 37 | + return f.read() |
| 38 | + |
| 39 | + def _parse_config(self): |
| 40 | + global CONFIG_FILE |
| 41 | + if not os.path.exists(CONFIG_FILE): |
| 42 | + raise Exception('Config file %s not found.' % CONFIG_FILE) |
| 43 | + with open(CONFIG_FILE) as f: |
| 44 | + self._config = json.load(f) |
| 45 | + rsa_cert_file = self._config['rsa_cert_file'] |
| 46 | + if not os.path.exists(rsa_cert_file): |
| 47 | + raise Exception('Cert file for JIRA OAuth not found at %s.' % rsa_cert_file) |
| 48 | + self._rsa_key = self._read_cert(rsa_cert_file) |
| 49 | + |
| 50 | + def setup(self): |
| 51 | + self._parse_config() |
| 52 | + oauth_creds = { |
| 53 | + 'access_token': self._config['oauth_token'], |
| 54 | + 'access_token_secret': self._config['oauth_secret'], |
| 55 | + 'consumer_key': self._config['consumer_key'], |
| 56 | + 'key_cert': self._rsa_key |
| 57 | + } |
| 58 | + |
| 59 | + self._jira_client = JIRA(options={'server': self._jira_server}, |
| 60 | + oauth=oauth_creds) |
| 61 | + if self._projects_available is None: |
| 62 | + self._projects_available = set() |
| 63 | + for proj in self._jira_client.projects(): |
| 64 | + self._projects_available.add(proj.key) |
| 65 | + |
| 66 | + def start(self): |
| 67 | + while True: |
| 68 | + for proj in self._jira_client.projects(): |
| 69 | + if proj.key not in self._projects_available: |
| 70 | + self._dispatch_trigger(proj) |
| 71 | + self._projects_available.add(proj.key) |
| 72 | + time.sleep(self._sleep_time) |
| 73 | + |
| 74 | + def stop(self): |
| 75 | + pass |
| 76 | + |
| 77 | + def get_trigger_types(self): |
| 78 | + return [ |
| 79 | + { |
| 80 | + 'name': 'st2.jira.project_tracker', |
| 81 | + 'description': 'Stackstorm JIRA projects tracker', |
| 82 | + 'payload_info': ['project_name', 'project_url'] |
| 83 | + } |
| 84 | + ] |
| 85 | + |
| 86 | + def _dispatch_trigger(self, proj): |
| 87 | + trigger = {} |
| 88 | + trigger['name'] = 'st2.jira.projects-tracker' |
| 89 | + payload = {} |
| 90 | + payload['project_name'] = proj.key |
| 91 | + payload['project_url'] = proj.self |
| 92 | + trigger['payload'] = payload |
| 93 | + self._container_service.dispatch(trigger) |
0 commit comments