def IngestFieldRefs(field_refs, config): """Return IDs of specified fields or raise NoSuchFieldDefException.""" fids_by_name = { fd.field_name.lower(): fd.field_id for fd in config.field_defs } result = [] for field_ref in field_refs: fid = fids_by_name.get(field_ref.field_name.lower()) if fid: result.append(fid) else: raise exceptions.NoSuchFieldDefException() return result
def BulkUpdateApprovals(self, mc, request): """Update multiple issues' approval and return the updated issue_refs.""" if not request.issue_refs: raise exceptions.InputException('Param `issue_refs` empty.') project, issue_ids, config = self._GetProjectIssueIDsAndConfig( mc, request.issue_refs) approval_fd = tracker_bizobj.FindFieldDef(request.field_ref.field_name, config) if not approval_fd: raise exceptions.NoSuchFieldDefException() if request.HasField('approval_delta'): approval_delta = converters.IngestApprovalDelta( mc.cnxn, self.services.user, request.approval_delta, mc.auth.user_id, config) else: approval_delta = tracker_pb2.ApprovalDelta() # No bulk adding approval attachments for now. with work_env.WorkEnv(mc, self.services, phase='updating approvals') as we: updated_issue_ids = we.BulkUpdateIssueApprovals( issue_ids, approval_fd.field_id, project, approval_delta, request.comment_content, send_email=request.send_email) with mc.profiler.Phase('converting to response objects'): issue_ref_pairs = we.GetIssueRefs(updated_issue_ids) issue_refs = [ converters.ConvertIssueRef(pair) for pair in issue_ref_pairs.values() ] response = issues_pb2.BulkUpdateApprovalsResponse( issue_refs=issue_refs) return response
def UpdateApproval(self, mc, request): """Update an approval and return the updated approval in a reponse proto.""" project, issue, config = self._GetProjectIssueAndConfig( mc, request.issue_ref, use_cache=False) approval_fd = tracker_bizobj.FindFieldDef(request.field_ref.field_name, config) if not approval_fd: raise exceptions.NoSuchFieldDefException() if request.HasField('approval_delta'): approval_delta = converters.IngestApprovalDelta( mc.cnxn, self.services.user, request.approval_delta, mc.auth.user_id, config) else: approval_delta = tracker_pb2.ApprovalDelta() attachments = converters.IngestAttachmentUploads(request.uploads) with work_env.WorkEnv(mc, self.services) as we: av, _comment = we.UpdateIssueApproval( issue.issue_id, approval_fd.field_id, approval_delta, request.comment_content, request.is_description, attachments=attachments, send_email=request.send_email, kept_attachments=list(request.kept_attachments)) with mc.profiler.Phase('converting to response objects'): users_by_id = framework_views.MakeAllUserViews( mc.cnxn, self.services.user, av.approver_ids, [av.setter_id]) framework_views.RevealAllEmailsToMembers(mc.auth, project, users_by_id) response = issues_pb2.UpdateApprovalResponse() response.approval.CopyFrom( converters.ConvertApproval(av, users_by_id, config)) return response
def HandleRequest(self, mr): """Process the task to notify users after an approval change. Args: mr: common information parsed from the HTTP request. Returns: Results dictionary in JSON format which is useful just for debugging. The main goal is the side-effect of sending emails. """ send_email = bool(mr.GetIntParam('send_email')) issue_id = mr.GetPositiveIntParam('issue_id') approval_id = mr.GetPositiveIntParam('approval_id') comment_id = mr.GetPositiveIntParam('comment_id') hostport = mr.GetParam('hostport') params = dict( temporary='', hostport=hostport, issue_id=issue_id ) logging.info('approval change params are %r', params) issue, approval_value = self.services.issue.GetIssueApproval( mr.cnxn, issue_id, approval_id, use_cache=False) project = self.services.project.GetProject(mr.cnxn, issue.project_id) config = self.services.config.GetProjectConfig(mr.cnxn, issue.project_id) approval_fd = tracker_bizobj.FindFieldDefByID(approval_id, config) if approval_fd is None: raise exceptions.NoSuchFieldDefException() # GetCommentsForIssue will fill the sequence for all comments, while # other method for getting a single comment will not. # The comment sequence is especially useful for Approval issues with # many comment sections. comment = None all_comments = self.services.issue.GetCommentsForIssue(mr.cnxn, issue_id) for c in all_comments: if c.id == comment_id: comment = c break if not comment: raise exceptions.NoSuchCommentException() field_user_ids = set() relevant_fds = [fd for fd in config.field_defs if not fd.approval_id or fd.approval_id is approval_value.approval_id] for fd in relevant_fds: field_user_ids.update( notify_reasons.ComputeNamedUserIDsToNotify(issue.field_values, fd)) users_by_id = framework_views.MakeAllUserViews( mr.cnxn, self.services.user, [issue.owner_id], approval_value.approver_ids, tracker_bizobj.UsersInvolvedInComment(comment), list(field_user_ids)) tasks = [] if send_email: tasks = self._MakeApprovalEmailTasks( hostport, issue, project, approval_value, approval_fd.field_name, comment, users_by_id, list(field_user_ids), mr.perms) notified = notify_helpers.AddAllEmailTasks(tasks) return { 'params': params, 'notified': notified, 'tasks': tasks, }