def update_sheet_with_status(self,jira_actions): requests = [] sheet_id = self.sheet_id() status_col = Misc.array_find(self.headers, 'Status') key_col = Misc.array_find(self.headers, 'Key' ) jira_link_col = Misc.array_find(self.headers, 'Jira Link') if status_col: for jira_action in jira_actions: row_index = jira_action.get('index') + 1 status = jira_action.get('status') if status == 'create_issue' : requests.append(self.gsheets().request_cell_set_background_color(sheet_id, status_col, row_index, 1.0, 1.0, 0.5)) elif status == 'issue_created': requests.append(self.gsheets().request_cell_set_background_color(sheet_id, status_col, row_index, 0.5, 1.0, 0.5)) else : requests.append(self.gsheets().request_cell_set_background_color(sheet_id, status_col, row_index, 1.0, 0.5, 0.5)) requests.append(self.gsheets().request_cell_set_value(sheet_id, status_col, row_index, status )) if status == 'issue_created': jira_key = jira_action.get('Jira_Key') jira_link = '=HYPERLINK("https://jira.photobox.com/browse/{0}","{0}")'.format(jira_key) requests.append(self.gsheets().request_cell_set_value(sheet_id, key_col , row_index, jira_key )) requests.append(self.gsheets().request_cell_set_value(sheet_id, jira_link_col, row_index, jira_link)) return self.gsheets().execute_requests(self.file_id, requests)
def calculate_jira_actions(self,sheet_data): jira_actions = [] for row in sheet_data: index = row.get('index') if row.get('Key' ) != 'new' : status = 'skip_issue - Key value was not new' elif Misc.none_or_empty(row, 'Summary' ) : status = 'skip_issue - Summary value was empty' elif Misc.none_or_empty(row, 'Project' ) : status = 'skip_issue - Project value was empty' elif Misc.none_or_empty(row, 'Issue Type') : status = 'skip_issue - Issue Type value was empty' else: status = 'create_issue' jira_actions.append({'index':index , 'status': status, 'row': row}) return jira_actions
def user_info(team_id=None, channel=None, params=None): if len(params) > 0: user_name = ' '.join(params) users = slack(team_id).users() user = users.get(user_name) if user: return "```{0}```".format(Misc.json_format(user)) for user in users.values(): if user.get('id') == user_name or user.get( 'real_name') == user_name: return "```{0}```".format(Misc.json_format(user)) return ":exclamation: Sorry, could not find user with alias `{0}`".format( user_name)
def screenshot(team_id=None, channel=None, params=[]): url = params.pop(0).replace('<', '').replace( '>', '') # fix extra chars added by Slack delay = Misc.to_int(Misc.array_pop(params, 0)) slack_message( ":point_right: taking screenshot of url: {0}".format(url), [], channel, team_id) browser_helper = Browser_Lamdba_Helper().setup() png_data = browser_helper.get_screenshot_png(url, full_page=True, delay=delay) return browser_helper.send_png_data_to_slack(team_id, channel, url, png_data)
def run(event, context): team_id = 'T7F3AUXGV' channel = 'DDKUZTK6X' text = "in API Gateway test..." attachments = [ {'text': "{0}".format(Misc.json_format(event)) , 'color':'good'}] #attachments = [{'text': "{0}".format(event), 'color': 'good'}] slack_message(text, attachments, channel,team_id) result = Misc.json_format({'text': text}) return { 'headers' : {'Content-Type': 'application/json'}, "statusCode" : 209, "body" : result }
def __init__(self, web_root=None): self.src_tmp = '/tmp/temp_web_server' if web_root : self.web_root = web_root else : self.web_root = self.src_tmp + '/html' self.html_file = Files.path_combine(self.web_root, 'index.html') self.port = Misc.random_number(10000,60000) self.server_proc = None
def _create_table_with_headers(team_id, channel, graph_name, headers, columns_defs=[], table_width='1200px'): graph_data = DataTable_Js_Views._get_data(graph_name) if graph_data: nodes = graph_data.get('nodes') rows = [] for index, node in enumerate(nodes.values()): row = [index + 1] for header in headers: value = node.get(header) if type(value).__name__ == 'dict': value = json.dumps(value) else: value = Misc.remove_html_tags(value) row.append(value) rows.append(row) headers.insert(0, '#') return DataTable_Js_Views._create_table( headers, rows, team_id, channel, table_width=table_width, columns_defs=columns_defs, table_title= "<b>{0}</b> <small><small><i>(data from graph)</i></small></small>" .format(graph_name))
def create_graph(self, nodes, edges,options=None): self.web_server.start() url = self.web_server.url(self.web_page) self.render_page.get_page_html_via_browser(url) self.load_page(True) layout = { "springLength" : 100, "springCoeff" : 0.0008, "dragCoeff" : 0.02, "gravity" : -10.2 }; self.invoke_js("set_layout",layout) js_code = "" #for key,issue in nodes.items(): for node in nodes: key = node.get('key') label = node.get('label') img = node.get('img') params = { "label" : label, "img": img} js_code += 'graph.addNode("{0}",{1});'.format(key,Misc.json_dumps(params)) for edge in edges: js_code += 'graph.addLink("{0}","{1}");\n'.format(edge[0],edge[2]) js_code += "run_graph()" self.exec_js(js_code) self.web_server.stop() return 42
def format_node(node): issue = graph_data.get('nodes').get(node.get('id')) if issue: node['label'] = Misc.word_wrap(issue.get('Summary'), 20) #node['label'] = issue.get('Rating') labels = issue.get('Labels') if 'R0' in labels: #node['label'] = issue.get('Summary') node['color'] = '#FF0000' node['font'] = {'color': 'white', 'size': 25} node['mass'] = 2 return node if 'R1' in labels: node['color'] = '#FF6666' node['font'] = {'size': 20} node['mass'] = 3 return node if 'R2' in labels: node['color'] = '#FFAAAA' node['font'] = {'size': 15} #node['mass'] = 1 return node if 'R3' in labels: node['color'] = '#FFDDDD' return node if 'R4' in labels: node['color'] = '#00DDDD' return node
def test_sheet_create(self): file_id = self.get_target_file_id() title = Misc.random_string_and_numbers(2,'from tests_') sheet_id = self.gsheets.sheets_add_sheet(file_id, title) assert len(set(self.gsheets.sheets_properties_by_title(file_id))) == 2 self.gsheets.sheets_rename_sheet(file_id, sheet_id, 'temp title') self.gsheets.sheets_delete_sheet(file_id, sheet_id) assert len(set(self.gsheets.sheets_properties_by_title(file_id))) == 1
def test_update_and_invoke(self): file_id = '1yDxu5YxL9FxY5wQ1EEQlAYGt3flIsm2VTyWwPny5RLA' result = self.simple_test.update_with_src().invoke( {'queryStringParameters': { 'file_id': file_id }}) message = Misc.json_load(result.get('body')) Dev.pprint(message)
def mindmap(team_id=None, channel=None, params=None): (go_js, graph_data) = Go_Js_Views._get_graph_data(params, "mindmap",headless=True) go_js.load_page(False) (nodes, edges) = Go_Js_Views._get_nodes_and_edges(graph_data,text_field='Summary') data = { "class": "go.TreeModel", "nodeDataArray": []} width = Misc.to_int(Misc.array_pop(params, 0)) height = Misc.to_int(Misc.array_pop(params, 0)) if width and height: go_js.browser().sync__browser_width(width,height) else: go_js.set_browser_width_based_on_nodes(nodes) nodes_indexed = {} for index, node in enumerate(nodes): key = node.get('key') text = "{1} | {0}".format(key,node.get('text')) nodes_indexed[key] = {'index':index, 'text': text } root_node_text = "{1} | {0}".format(nodes[0].get('key'), nodes[0].get('text')) data['nodeDataArray'].append({"key": 0, "text": root_node_text }) # add root node first for edge in edges: from_key = edge['from'] to_key = edge['to'] from_issue = nodes_indexed.get(from_key) to_issue = nodes_indexed.get(to_key) if from_issue: parent = nodes_indexed.get(edge['from']).get('index') else: parent = from_key if to_issue: key = nodes_indexed.get(edge['to' ]).get('index') text = nodes_indexed.get(edge['to' ]).get('text') else: key = to_key text = to_key item = {"key": key, "parent": parent, "text": text, "brush": Misc.get_random_color()} data['nodeDataArray'].append(item) go_js.invoke_js("create_graph_from_json", data) js_code = 'layoutAll()' go_js.api_browser.sync__await_for_element('#animationFinished') Dev.pprint(go_js.exec_js(js_code)) return go_js.send_screenshot_to_slack(team_id=team_id, channel=channel)
def _get_graph_data(params, layout=None, headless=True): load_dependencies(['syncer', 'requests']) from view_helpers.Am_Charts import Am_Charts am_charts = Am_Charts(headless=headless, layout=layout) graph_name = Misc.array_pop(params, 0) if graph_name: graph_data = am_charts.get_graph_data(graph_name) return am_charts, graph_data return am_charts, None
def run(event, context): project_name = 'Code_Build_Test' build_id = CodeBuild(project_name=project_name, role_name=project_name).build_start() return { 'headers': { 'Content-Type': 'application/json' }, "statusCode": 200, "body": Misc.json_format({'build_id': build_id}) }
def test_add_element_text(self): (file_id, slide_id) = self.get_target_slide_id() text = Misc.random_string_and_numbers(6,'New Text Field - ') x_pos = 50 y_pos = 250 width = 300 height = 50 new_id = self.gslides.element_create_text(file_id, slide_id, text, x_pos, y_pos, width, height) self.gslides.element_set_text(file_id,new_id,'changed text') sleep(2) self.gslides.element_delete(file_id,new_id)
def request_put(self, path, data): json_data = Misc.json_dumps(data) (server, username, password) = self.config() path = '{0}/rest/api/2/{1}'.format(server, path) headers = {'Content-Type': 'application/json'} response = requests.put(path, json_data, headers=headers, auth=(username, password)) if 200 <= response.status_code < 300: return True Dev.pprint('[Error][request_put]: {0}'.format(response.text)) return False
def get_nodes_and_edges(self, graph_data,nodes=None,edges=None, text_field='Key', append_key_to_text=False): if nodes is None: nodes = [] if edges is None: edges = [] for key,issue in graph_data.get('nodes').items(): if issue and issue.get('Summary'): text = issue.get(text_field) if append_key_to_text: text += " | {0}".format(key) nodes.append({'key': key, 'text': text , 'color': Misc.get_random_color()}) for edge in graph_data.get('edges'): edges.append({ 'from': edge[0], 'text' : edge[1] ,'to': edge[2] ,'color': Misc.get_random_color()}) return nodes,edges
def render(team_id, channel, params): if params: target = params.pop(0) delay = Misc.to_int(Misc.array_pop(params, 0)) if len(params) == 4: clip = { 'x': int(params[0]), 'y': int(params[1]), 'width': int(params[2]), 'height': int(params[3]) } else: clip = None else: return None slack_message(":point_right: rendering file `{0}`".format(target), [], channel, team_id) return Browser_Lamdba_Helper().setup().render_file(team_id, channel, target, clip=clip, delay=delay)
def create_dashboard_with_test_data(self): self.create_dashboard_with_R1_R2() rows = 6 cells = 6 js_codes = [] for i in range(1, cells + 1): for j in range(1, rows + 1): r2_id = "r{0}_{1}".format(i, j) color = Misc.random_number(1, 5) #color = j self.js_apply_css_color(js_codes, r2_id, color) self.js_eval(js_codes) return self
def swimlanes(team_id=None, channel=None, params=None): (go_js, graph_data) = Go_Js_Views._get_graph_data(params, "swimlanes") nodes = [{'key': "Pool1", 'text': "By Risk", 'isGroup': True, 'category': "Pool"}] edges = [] groups = [] colors = ["lightblue","lightgreen","lightyellow","orange"] for key,issue in graph_data.get('nodes').items(): if issue: group = Misc.array_add(groups, issue.get('Rating')) nodes.append({'key': key, 'group':group}) #issue.get('Summary') for group in list(set(groups)): nodes.append({'key': group, 'text': group, 'isGroup': True, 'group' : "Pool1", 'color': Misc.array_pop(colors)}) for edge in graph_data.get('edges'): edges.append({ 'from': edge[0],'to': edge[2]}) js_code = 'relayoutLanes()' return go_js.render(nodes, edges, js_code=js_code, width=1400, team_id=team_id, channel=channel)
def run(event, context): team_id = 'T7F3AUXGV' channel = 'DDKUZTK6X' querystring = event.get('queryStringParameters') if querystring and querystring.get('file_id'): file_id = querystring.get('file_id') if querystring.get('action') == 'diff': payload = { "params": ["diff_sheet", file_id], "channel": "DDKUZTK6X", 'team_id': 'T7F3AUXGV' } elif querystring.get('action') == 'sync': payload = { "params": ["sync_sheet", file_id], "channel": "DDKUZTK6X", 'team_id': 'T7F3AUXGV' } else: payload = { "params": ["load_sheet", file_id], "channel": "DDKUZTK6X", 'team_id': 'T7F3AUXGV' } Lambdas('gs.elastic_jira').invoke(payload) text = ":point_right: [trigger_sync_jira_sheets] completed workflow for file_id: {0} , see channel {1} for more details".format( file_id, channel) status_code = 201 else: text = "Error: file_id value not provided" status_code = 501 slack_message(text, [], channel, team_id) return { 'headers': { 'Content-Type': 'application/json' }, "statusCode": status_code, "body": Misc.json_format({'text': text}) }
def run(event, context): team_id = 'T7F3AUXGV' channel = 'DDKUZTK6X' text = ":robot_face: In trigger_server_reload lambda function \n" payload = { "params": ["server", "reload"], "channel": channel, 'team_id': team_id } result = Lambdas('gs.elastic_jira').invoke(payload) attachments = [{'text': result, 'color': 'good'}] slack_message(text, attachments, channel, team_id) result = Misc.json_format({'text': text}) return { 'headers': { 'Content-Type': 'application/json' }, "statusCode": 209, "body": result }
def node_label(team_id=None, channel=None, params=None): if len(params) < 2: return "':red_circle: Hi, for the `node_label` view, you need to provide the label field name. Try: `Key`, `Summary`, `Rating`, `Status`" label_key = ' '.join(params[1:]) (graph_name, nodes, edges, graph_data, vis_js) = Vis_Js_Views.default(params=params, no_render=True) graph_name += ' | node_label | ' + label_key issues = graph_data.get('nodes') for node in nodes: issue = issues.get(node['label']) if issue: value = str(issue.get(label_key)) node['label'] = Misc.word_wrap(value, 40) for edge in edges: del edge['label'] options = {'nodes': {'shape': 'box'}, 'edges': {'arrows': 'to'}} options = None return vis_js.create_graph_and_send_screenshot_to_slack( graph_name, nodes, edges, options, team_id, channel)
def is_class(self, name): assert Misc.class_name(self.target) in name
def make_random_points(size=None, max=None): if size is None : size = 20 if max is None : max = 100 return [Misc.random_number(0,max) for _ in range(0,size)]
def random_id(self, prefix): return Misc.random_string_and_numbers(6, prefix + "_")
def test_send_and_receive_messages(self): result = self.api.send_message('<@UDK5W7W3T> hello', channel='DDKUZTK6X') Misc.wait(1) messages = self.api.get_messages(channel='DDKUZTK6X', limit=2) Dev.pprint(messages)
def stats(team_id=None, channel=None, params=None): stats = {"# of users": len(list(set(slack(team_id).users())))} return Misc.json_format(stats)
def test_random_filename(self): result = Misc.random_filename() assert len(result) is 14 assert ".tmp" in result
def issue_raw(self, issue_id): if issue_id: path = 'issue/{0}'.format(issue_id) data = self.request_get(path) return Misc.json_load(data)