def create_issue(self, data, **kwargs): client = self.get_client() project_id = data.get("project") if not project_id: raise IntegrationError("project kwarg must be provided") try: issue = client.create_issue( project=project_id, data={ "title": data["title"], "description": data["description"] }, ) project = client.get_project(project_id) except ApiError as e: raise IntegrationError(self.message_from_error(e)) project_and_issue_iid = "%s#%s" % (project["path_with_namespace"], issue["iid"]) return { "key": project_and_issue_iid, "title": issue["title"], "description": issue["description"], "url": issue["web_url"], "project": project_id, "metadata": { "display_name": project_and_issue_iid }, }
def create_issue(self, data, **kwargs): client = self.get_client() repo = data.get('repo') if not repo: raise IntegrationError('repo kwarg must be provided') try: issue = client.create_issue(repo=repo, data={ 'title': data['title'], 'body': data['description'], 'assignee': data.get('assignee'), }) except ApiError as e: raise IntegrationError(self.message_from_error(e)) return { 'key': issue['number'], 'title': issue['title'], 'description': issue['body'], 'url': issue['html_url'], 'repo': repo, }
def get_issue(self, issue_id, **kwargs): project_id, issue_num = issue_id.split("#") client = self.get_client() if not project_id: raise IntegrationError("project must be provided") if not issue_num: raise IntegrationError("issue must be provided") try: issue = client.get_issue(project_id, issue_num) project = client.get_project(project_id) except ApiError as e: raise IntegrationError(self.message_from_error(e)) project_and_issue_iid = "%s#%s" % (project["path_with_namespace"], issue["iid"]) return { "key": project_and_issue_iid, "title": issue["title"], "description": issue["description"], "url": issue["web_url"], "project": project_id, "metadata": { "display_name": project_and_issue_iid }, }
def fetch_issue_create_meta(self, client, project_id): try: meta = client.get_create_meta_for_project(project_id) except ApiUnauthorized: logger.info( "jira.fetch-issue-create-meta.unauthorized", extra={"organization_id": self.organization_id, "jira_project": project_id}, ) raise IntegrationError( "Jira returned: Unauthorized. " "Please check your configuration settings." ) except ApiError as exc: logger.info( "jira.fetch-issue-create-meta.error", extra={ "integration_id": self.model.id, "organization_id": self.organization_id, "jira_project": project_id, "error": exc.message, }, ) raise IntegrationError( "There was an error communicating with the Jira API. " "Please try again or contact support." ) return meta
def create_issue(self, data, **kwargs): client = self.get_client() repo = data.get("repo") if not repo: raise IntegrationError("repo kwarg must be provided") try: issue = client.create_issue( repo=repo, data={ "title": data["title"], "body": data["description"], "assignee": data.get("assignee"), }, ) except ApiError as e: raise IntegrationError(self.message_from_error(e)) return { "key": issue["number"], "title": issue["title"], "description": issue["body"], "url": issue["html_url"], "repo": repo, }
def get_issue(self, issue_id, **kwargs): project_id, issue_num = issue_id.split('#') client = self.get_client() if not project_id: raise IntegrationError('project must be provided') if not issue_num: raise IntegrationError('issue must be provided') try: issue = client.get_issue(project_id, issue_num) project = client.get_project(project_id) except ApiError as e: raise IntegrationError(self.message_from_error(e)) project_and_issue_iid = '%s#%s' % (project['path_with_namespace'], issue['iid']) return { 'key': project_and_issue_iid, 'title': issue['title'], 'description': issue['description'], 'url': issue['web_url'], 'project': project_id, 'metadata': { 'display_name': project_and_issue_iid, } }
def create_issue(self, data, **kwargs): client = self.get_client() project_id = data.get('project') if not project_id: raise IntegrationError('project kwarg must be provided') try: issue = client.create_issue(project=project_id, data={ 'title': data['title'], 'description': data['description'], }) project = client.get_project(project_id) except ApiError as e: raise IntegrationError(self.message_from_error(e)) project_and_issue_iid = '%s#%s' % (project['path_with_namespace'], issue['iid']) return { 'key': project_and_issue_iid, 'title': issue['title'], 'description': issue['description'], 'url': issue['web_url'], 'project': project_id, 'metadata': { 'display_name': project_and_issue_iid, } }
def get_repository_choices(self, group, **kwargs): """ Returns the default repository and a set/subset of repositories of associated with the installation """ try: repos = self.get_repositories() except ApiError: raise IntegrationError("Unable to retrive repositories. Please try again later.") else: repo_choices = [(repo["identifier"], repo["name"]) for repo in repos] repo = kwargs.get("repo") if not repo: params = kwargs.get("params", {}) defaults = self.get_project_defaults(group.project_id) repo = params.get("repo", defaults.get("repo")) try: default_repo = repo or repo_choices[0][0] except IndexError: return "", repo_choices # If a repo has been selected outside of the default list of # repos, stick it onto the front of the list so that it can be # selected. try: next(True for r in repo_choices if r[0] == default_repo) except StopIteration: repo_choices.insert(0, self.create_default_repo_choice(default_repo)) return default_repo, repo_choices
def get_repository_choices(self, group, **kwargs): """ Returns the default repository and a set/subset of repositories of asscoaited with the installation """ try: repos = self.get_repositories() except ApiError: raise IntegrationError( 'Unable to retrive repositories. Please try again later.') else: repo_choices = [(repo['identifier'], repo['name']) for repo in repos] params = kwargs.get('params', {}) defaults = self.get_project_defaults(group.project_id) default_repo = params.get('repo', defaults.get('repo') or repo_choices[0][0]) # If a repo has been selected outside of the default list of # repos, stick it onto the front of the list so that it can be # selected. try: next(True for r in repo_choices if r[0] == default_repo) except StopIteration: repo_choices.insert(0, self.create_default_repo_choice(default_repo)) return default_repo, repo_choices
def update_organization_config(self, data): """ Update the configuration field for an organization integration. """ config = self.org_integration.config if "sync_status_forward" in data: project_mappings = data.pop("sync_status_forward") if any( not mapping["on_unresolve"] or not mapping["on_resolve"] for mapping in project_mappings.values() ): raise IntegrationError("Resolve and unresolve status are required.") data["sync_status_forward"] = bool(project_mappings) IntegrationExternalProject.objects.filter( organization_integration_id=self.org_integration.id ).delete() for project_id, statuses in project_mappings.items(): IntegrationExternalProject.objects.create( organization_integration_id=self.org_integration.id, external_id=project_id, resolved_status=statuses["on_resolve"], unresolved_status=statuses["on_unresolve"], ) config.update(data) self.org_integration.update(config=config)
def update_organization_config(self, data): if 'sync_status_forward' in data: project_ids_and_statuses = data.pop('sync_status_forward') if any(not mapping['on_unresolve'] or not mapping['on_resolve'] for mapping in project_ids_and_statuses.values()): raise IntegrationError( 'Resolve and unresolve status are required.') data['sync_status_forward'] = bool(project_ids_and_statuses) IntegrationExternalProject.objects.filter( organization_integration_id=self.org_integration.id, ).delete() for project_id, statuses in project_ids_and_statuses.items(): IntegrationExternalProject.objects.create( organization_integration_id=self.org_integration.id, external_id=project_id, resolved_status=statuses['on_resolve'], unresolved_status=statuses['on_unresolve'], ) config = self.org_integration.config config.update(data) self.org_integration.update(config=config)
def after_link_issue(self, external_issue, **kwargs): data = kwargs["data"] project_id, issue_id = data.get("externalIssue", "").split("#") if not (project_id and issue_id): raise IntegrationError("Project and Issue id must be provided") client = self.get_client() comment = data.get("comment") if not comment: return try: client.create_issue_comment(project_id=project_id, issue_id=issue_id, data={"body": comment}) except ApiError as e: raise IntegrationError(self.message_from_error(e))
def after_link_issue(self, external_issue, **kwargs): data = kwargs["data"] client = self.get_client() repo, issue_num = external_issue.key.split("#") if not repo: raise IntegrationError("repo must be provided") if not issue_num: raise IntegrationError("issue number must be provided") comment = data.get("comment") if comment: try: client.create_comment(repo=repo, issue_id=issue_num, data={"body": comment}) except ApiError as e: raise IntegrationError(self.message_from_error(e))
def create_issue(self, data, **kwargs): if "assignee" not in data: raise IntegrationError("Assignee is required") return { "key": "APP-123", "title": "This is a test external issue title", "description": "This is a test external issue description", }
def get_installation(self, integration_id, organization_id): if integration_id is None: raise IntegrationError("Bitbucket Server requires an integration id.") integration_model = Integration.objects.get( id=integration_id, organizations=organization_id, provider="bitbucket_server" ) return integration_model.get_installation(organization_id)
def compare_commits(self, repo, start_sha, end_sha): installation = Integration.objects.get( id=repo.integration_id).get_installation(repo.organization_id) try: raise IntegrationError("{'error': 'Repository not found'}") except Exception as e: installation.raise_error(e)
def fetch_issue_create_meta(self, client, project_id): try: meta = client.get_create_meta_for_project(project_id) except ApiUnauthorized: raise IntegrationError('Jira returned: Unauthorized. ' 'Please check your configuration settings.') except ApiError as exc: logger.info('jira.error-fetching-issue-config', extra={ 'integration_id': self.model.id, 'organization_id': self.organization_id, 'error': exc.message, }) raise IntegrationError( 'There was an error communicating with the Jira API. ' 'Please try again or contact support.') return meta
def create_issue(self, data, **kwargs): if 'assignee' not in data: raise IntegrationError('Assignee is required') return { 'key': 'APP-123', 'title': 'This is a test external issue title', 'description': 'This is a test external issue description', }
def get_installation(self, integration_id, organization_id): if integration_id is None: raise IntegrationError("%s requires an integration id." % self.name) integration_model = Integration.objects.get( id=integration_id, organizations=organization_id, provider="vsts") return integration_model.get_installation(organization_id)
def get_group_info(self, access_token, installation_data): client = GitLabSetupClient(installation_data['url'], access_token, installation_data['verify_ssl']) try: resp = client.get_group(installation_data['group']) return resp.json except ApiError: raise IntegrationError( 'The requested GitLab group could not be found.')
def get_installation(self, integration_id, organization_id): if integration_id is None: raise IntegrationError('Bitbucket requires an integration id.') integration_model = Integration.objects.get( id=integration_id, organizations=organization_id, provider='bitbucket', ) return integration_model.get_installation(organization_id)
def after_link_issue(self, external_issue, **kwargs): data = kwargs['data'] project_id, issue_id = data.get('externalIssue', '').split('#') if not (project_id and issue_id): raise IntegrationError('Project and Issue id must be provided') client = self.get_client() comment = data.get('comment') if not comment: return try: client.create_issue_comment(project_id=project_id, issue_id=issue_id, data={ 'body': comment, }) except ApiError as e: raise IntegrationError(self.message_from_error(e))
def get_repositories(self, query=None): try: repos = self.get_client().get_repos(self.instance) except ApiError as e: raise IntegrationError(self.message_from_error(e)) data = [] for repo in repos['value']: data.append({ 'name': '%s/%s' % (repo['project']['name'], repo['name']), 'identifier': repo['id'], }) return data
def after_link_issue(self, external_issue, **kwargs): data = kwargs['data'] client = self.get_client() repo, issue_num = external_issue.key.split('#') if not repo: raise IntegrationError('repo must be provided') if not issue_num: raise IntegrationError('issue number must be provided') comment = data.get('comment') if comment: try: client.create_comment(repo=repo, issue_id=issue_num, data={'content': { 'raw': comment }}) except ApiError as e: raise IntegrationError(self.message_from_error(e))
def _validate_repo(self, client, installation, repo): try: repo_data = client.get_repo(repo) except Exception as e: installation.raise_error(e) try: # make sure installation has access to this specific repo client.get_commits(repo) except ApiError as e: raise IntegrationError(u"You must grant Sentry access to {}".format(repo)) return repo_data
def get_repositories(self, query=None): try: repos = self.get_client().get_repos(self.instance) except ApiError as e: raise IntegrationError(self.message_from_error(e)) data = [] for repo in repos["value"]: data.append({ "name": "%s/%s" % (repo["project"]["name"], repo["name"]), "identifier": repo["id"], }) return data
def create_subscription(self, instance, account_id, oauth_data): webhook = WorkItemWebhook() try: subscription, shared_secret = webhook.create_subscription( instance, oauth_data, self.oauth_redirect_url, account_id) except ApiError as e: if e.code != 400 or 'permission' not in e.message: raise e raise IntegrationError( 'You do not have sufficent account access to create an integration.\nPlease check with the owner of this account.' ) subscription_id = subscription['id'] return subscription_id, shared_secret
def get_issue(self, issue_id, **kwargs): data = kwargs['data'] repo = data.get('repo') issue_num = data.get('externalIssue') client = self.get_client() if not repo: raise IntegrationError('repo must be provided') if not issue_num: raise IntegrationError('issue must be provided') try: issue = client.get_issue(repo, issue_num) except ApiError as e: raise IntegrationError(self.message_from_error(e)) return { 'key': issue['number'], 'title': issue['title'], 'description': issue['body'], 'repo': repo, }
def get_issue(self, issue_id, **kwargs): data = kwargs["data"] repo = data.get("repo") issue_num = data.get("externalIssue") client = self.get_client() if not repo: raise IntegrationError("repo must be provided") if not issue_num: raise IntegrationError("issue must be provided") try: issue = client.get_issue(repo, issue_num) except ApiError as e: raise IntegrationError(self.message_from_error(e)) return { "key": issue["number"], "title": issue["title"], "description": issue["body"], "url": issue["html_url"], "repo": repo, }
def create_webhook(self, external_id, webhook_secret, install, credentials): client = JiraServerSetupClient( install['url'], install['consumer_key'], install['private_key'], install['verify_ssl'] ) try: client.create_issue_webhook(external_id, webhook_secret, credentials) except ApiError as err: logger.info('jira-server.webhook.failed', extra={ 'error': six.text_type(err), 'external_id': external_id, }) raise IntegrationError('Could not create issue webhook in Jira')