Skip to content

Commit

Permalink
Create description and attachment getter methods for google issue tra… (
Browse files Browse the repository at this point in the history
#4360)

…cker

These functions are not yet used anywhere, but soon will be c:
  • Loading branch information
pgrace-google authored Nov 1, 2024
1 parent 8dffd9d commit 432d549
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import urllib.parse

from google.auth import exceptions
import googleapiclient.http

from clusterfuzz._internal.issue_management import issue_tracker
from clusterfuzz._internal.issue_management.google_issue_tracker import client
Expand Down Expand Up @@ -1034,9 +1035,9 @@ def _get_relative_component_path(self, component_id):
try:
component = self._execute(
self.client.components().get(componentId=str(component_id)))
except IssueTrackerError as e:
if isinstance(e, IssueTrackerNotFoundError):
return None
except IssueTrackerNotFoundError:
return None
except IssueTrackerError:
logs.error('Failed to retrieve component.', component_id=component_id)
return None

Expand All @@ -1057,12 +1058,59 @@ def get_issue(self, issue_id):
issue = self._execute(self.client.issues().get(issueId=str(issue_id)))
logs.info('google_issue_tracker: get_issue. issue: %s' % issue)
return Issue(issue, False, self)
except IssueTrackerError as e:
if isinstance(e, IssueTrackerNotFoundError):
return None
except IssueTrackerNotFoundError:
return None
except IssueTrackerError:
logs.error('Failed to retrieve issue.', issue_id=issue_id)
return None

def get_description(self, issue_id):
"""Gets the content of the description for the issue with the given ID."""
try:
comments = self._execute(
self.client.issues().comments().list(issueId=str(issue_id)))
logs.info('google_issue_tracker: get_description comments: %s' % comments)
for comment in comments['issueComments']:
if comment['commentNumber'] == 1:
return comment['comment']
return None
except IssueTrackerNotFoundError:
return None
except IssueTrackerError:
logs.error('Failed to retrieve issue description.', issue_id=issue_id)
return None

def get_attachment_metadata(self, issue_id):
"""Gets the attachment metadata of an issue with the given ID."""
try:
attachment_metadata = self._execute(
self.client.issues().attachments().list(issueId=str(issue_id)))
logs.info('google_issue_tracker: get_attachment_metadata: %s' %
attachment_metadata)
return attachment_metadata['attachments']
except IssueTrackerNotFoundError:
return None
except IssueTrackerError:
logs.error(
'Failed to retrieve issue attachment metadata.', issue_id=issue_id)
return None

def get_attachment(self, resource_name):
"""Gets the attachment for the given resource name."""
try:
http_request = self.client.media().download(resourceName=resource_name)
http_request.uri = http_request.uri.replace('alt=json', 'alt=media')
http_request.postproc = googleapiclient.http.HttpRequest.null_postproc
attachment = self._execute(http_request)
logs.info('google_issue_tracker: get_attachment attachment downloaded')
return attachment
except IssueTrackerNotFoundError:
return None
except IssueTrackerError:
logs.error(
'Failed to retrieve attachment for resource name: %s' % resource_name)
return None

def find_issues(self, keywords=None, only_open=None):
"""Finds issues."""
page_token = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1199,3 +1199,146 @@ def test_get_severity_from_label_value(self):
self.assertEqual(
case['expected'], actual, 'failed test %s. expected %s. actual %s' %
(case['name'], case['expected'], actual))

def test_get_description(self):
"""Test a basic get_description."""
self.client.issues().get().execute.return_value = BASIC_ISSUE
self.client.issues().comments().list().execute.return_value = {
'issueComments': [
{
'comment': 'test body',
'lastEditor': {
'emailAddress': '[email protected]',
'userGaiaStatus': 'ACTIVE',
},
'modifiedTime': '2019-06-25T01:29:30.021Z',
'issueId': '68828938',
'commentNumber': 1,
'formattingMode': 'PLAIN',
},
{
'comment': 'not test body',
'lastEditor': {
'emailAddress': '[email protected]',
'userGaiaStatus': 'ACTIVE',
},
'modifiedTime': '2019-06-25T02:29:30.021Z',
'issueId': '68828938',
'commentNumber': 2,
'formattingMode': 'PLAIN',
},
],
'totalSize':
2,
}
description = self.issue_tracker.get_description(68828938)
self.assertEqual('test body', description)

def test_get_blank_description(self):
"""Test a basic get_description."""
self.client.issues().get().execute.return_value = BASIC_ISSUE
self.client.issues().comments().list().execute.return_value = {
'issueComments': [
{
'comment': '',
'lastEditor': {
'emailAddress': '[email protected]',
'userGaiaStatus': 'ACTIVE',
},
'modifiedTime': '2019-06-25T01:29:30.021Z',
'issueId': '68828938',
'commentNumber': 1,
'formattingMode': 'PLAIN',
},
{
'comment': 'not test body',
'lastEditor': {
'emailAddress': '[email protected]',
'userGaiaStatus': 'ACTIVE',
},
'modifiedTime': '2019-06-25T02:29:30.021Z',
'issueId': '68828938',
'commentNumber': 2,
'formattingMode': 'PLAIN',
},
],
'totalSize':
2,
}
description = self.issue_tracker.get_description(68828938)
self.assertEqual('', description)

def test_get_attachment_metadata(self):
"""Test a basic get_attachment_metadata."""
self.client.issues().get().execute.return_value = BASIC_ISSUE
self.client.issues().attachments().list().execute.return_value = {
'attachments': [
{
'attachmentId':
'60127668',
'contentType':
'text/html',
'length':
'458',
'filename':
'test.html',
'attachmentDataRef': {
'resourceName': 'attachment:373893311:60127668'
},
'etag':
'TXpjek9Eb3pNekV4TFRZd01USTNOalk0TFRjNE9URTROVFl4TlE9PQ=='
},
{
'attachmentId':
'2',
'contentType':
'text/js',
'length':
'125',
'filename':
'test.js',
'attachmentDataRef': {
'resourceName': 'attachment:373893311:2'
},
'etag':
'TXetagk9Ea3pNekV4TFRZd01USTNOalk0TFRjNE9URTROVFl4TlE9PQ=='
},
],
'totalSize':
2,
}
attachment_data = self.issue_tracker.get_attachment_metadata(68828938)
self.assertEqual('test.html', attachment_data[0]['filename'])
self.assertEqual('text/html', attachment_data[0]['contentType'])
self.assertEqual(2, len(attachment_data))

def test_get_no_attachment_metadata(self):
"""Test an empty get_attachment_metadata."""
self.client.issues().get().execute.return_value = BASIC_ISSUE
self.client.issues().attachments().list().execute.return_value = {
'attachments': [],
'totalSize': 0,
}
attachment_data = self.issue_tracker.get_attachment_metadata(68828938)
self.assertEqual([], attachment_data)

def test_get_attachment(self):
"""Test a basic get_attachment."""
self.client.media().download(
resourceName='attachment:373893311:60127668'
).execute.return_value = ({
'content-type':
'application/octet-stream',
'content-length':
'458',
'content-disposition':
'attachment',
'status':
'200',
'content-location':
'https://issuetracker.googleapis.com/v1/media/attachment:373893311:60127668?alt=media'
}, b'<!DOCTYPE html>\n<html>hello world</html>')
attachment = self.issue_tracker.get_attachment(
'attachment:373893311:60127668')
self.assertEqual(b'<!DOCTYPE html>\n<html>hello world</html>',
attachment[1])

0 comments on commit 432d549

Please sign in to comment.