| 1 |
from builtins import next |
| 2 |
from six.moves import configparser |
| 3 |
import datetime |
| 4 |
|
| 5 |
import pytz |
| 6 |
import responses |
| 7 |
|
| 8 |
from bugwarrior.config import ServiceConfig |
| 9 |
from bugwarrior.services.gitlab import GitlabService |
| 10 |
|
| 11 |
from .base import ConfigTest, ServiceTest, AbstractServiceTest |
| 12 |
|
| 13 |
|
| 14 |
class TestGitlabService(ConfigTest): |
| 15 |
|
| 16 |
def setUp(self): |
| 17 |
super(TestGitlabService, self).setUp() |
| 18 |
self.config = configparser.RawConfigParser() |
| 19 |
self.config.add_section('general') |
| 20 |
self.config.add_section('myservice') |
| 21 |
self.config.set('myservice', 'gitlab.login', 'foobar') |
| 22 |
self.config.set('myservice', 'gitlab.token', 'XXXXXX') |
| 23 |
self.service_config = ServiceConfig( |
| 24 |
GitlabService.CONFIG_PREFIX, self.config, 'myservice') |
| 25 |
|
| 26 |
def test_get_keyring_service_default_host(self): |
| 27 |
self.assertEqual( |
| 28 |
GitlabService.get_keyring_service(self.service_config), |
| 29 |
'gitlab://[email protected]') |
| 30 |
|
| 31 |
def test_get_keyring_service_custom_host(self): |
| 32 |
self.config.set('myservice', 'gitlab.host', 'gitlab.example.com') |
| 33 |
self.assertEqual( |
| 34 |
GitlabService.get_keyring_service(self.service_config), |
| 35 |
'gitlab://[email protected]') |
| 36 |
|
| 37 |
def test_add_default_namespace_to_included_repos(self): |
| 38 |
self.config.set('myservice', 'gitlab.include_repos', 'baz, banana/tree') |
| 39 |
service = GitlabService(self.config, 'general', 'myservice') |
| 40 |
self.assertEqual(service.include_repos, ['foobar/baz', 'banana/tree']) |
| 41 |
|
| 42 |
def test_add_default_namespace_to_excluded_repos(self): |
| 43 |
self.config.set('myservice', 'gitlab.exclude_repos', 'baz, banana/tree') |
| 44 |
service = GitlabService(self.config, 'general', 'myservice') |
| 45 |
self.assertEqual(service.exclude_repos, ['foobar/baz', 'banana/tree']) |
| 46 |
|
| 47 |
def test_filter_repos_default(self): |
| 48 |
service = GitlabService(self.config, 'general', 'myservice') |
| 49 |
repo = {'path_with_namespace': 'foobar/baz'} |
| 50 |
self.assertTrue(service.filter_repos(repo)) |
| 51 |
|
| 52 |
def test_filter_repos_exclude(self): |
| 53 |
self.config.set('myservice', 'gitlab.exclude_repos', 'foobar/baz') |
| 54 |
service = GitlabService(self.config, 'general', 'myservice') |
| 55 |
repo = {'path_with_namespace': 'foobar/baz', 'id': 1234} |
| 56 |
self.assertFalse(service.filter_repos(repo)) |
| 57 |
|
| 58 |
def test_filter_repos_exclude_id(self): |
| 59 |
self.config.set('myservice', 'gitlab.exclude_repos', 'id:1234') |
| 60 |
service = GitlabService(self.config, 'general', 'myservice') |
| 61 |
repo = {'path_with_namespace': 'foobar/baz', 'id': 1234} |
| 62 |
self.assertFalse(service.filter_repos(repo)) |
| 63 |
|
| 64 |
def test_filter_repos_include(self): |
| 65 |
self.config.set('myservice', 'gitlab.include_repos', 'foobar/baz') |
| 66 |
service = GitlabService(self.config, 'general', 'myservice') |
| 67 |
repo = {'path_with_namespace': 'foobar/baz', 'id': 1234} |
| 68 |
self.assertTrue(service.filter_repos(repo)) |
| 69 |
|
| 70 |
def test_filter_repos_include_id(self): |
| 71 |
self.config.set('myservice', 'gitlab.include_repos', 'id:1234') |
| 72 |
service = GitlabService(self.config, 'general', 'myservice') |
| 73 |
repo = {'path_with_namespace': 'foobar/baz', 'id': 1234} |
| 74 |
self.assertTrue(service.filter_repos(repo)) |
| 75 |
|
| 76 |
|
| 77 |
class TestGitlabIssue(AbstractServiceTest, ServiceTest): |
| 78 |
maxDiff = None |
| 79 |
SERVICE_CONFIG = { |
| 80 |
'gitlab.host': 'gitlab.example.com', |
| 81 |
'gitlab.login': 'arbitrary_login', |
| 82 |
'gitlab.token': 'arbitrary_token', |
| 83 |
} |
| 84 |
|
| 85 |
def setUp(self): |
| 86 |
super(TestGitlabIssue, self).setUp() |
| 87 |
self.service = self.get_mock_service(GitlabService) |
| 88 |
self.arbitrary_created = ( |
| 89 |
datetime.datetime.utcnow() - datetime.timedelta(hours=1) |
| 90 |
).replace(tzinfo=pytz.UTC, microsecond=0) |
| 91 |
self.arbitrary_updated = datetime.datetime.utcnow().replace( |
| 92 |
tzinfo=pytz.UTC, microsecond=0) |
| 93 |
self.arbitrary_duedate = ( |
| 94 |
datetime.datetime.combine(datetime.date.today(), |
| 95 |
datetime.datetime.min.time()) |
| 96 |
).replace(tzinfo=pytz.UTC) |
| 97 |
self.arbitrary_issue = { |
| 98 |
"id": 42, |
| 99 |
"iid": 3, |
| 100 |
"project_id": 8, |
| 101 |
"title": "Add user settings", |
| 102 |
"description": "", |
| 103 |
"labels": [ |
| 104 |
"feature" |
| 105 |
], |
| 106 |
"milestone": { |
| 107 |
"id": 1, |
| 108 |
"title": "v1.0", |
| 109 |
"description": "", |
| 110 |
"due_date": self.arbitrary_duedate.date().isoformat(), |
| 111 |
"state": "closed", |
| 112 |
"updated_at": "2012-07-04T13:42:48Z", |
| 113 |
"created_at": "2012-07-04T13:42:48Z" |
| 114 |
}, |
| 115 |
"assignee": { |
| 116 |
"id": 2, |
| 117 |
"username": "jack_smith", |
| 118 |
"email": "[email protected]", |
| 119 |
"name": "Jack Smith", |
| 120 |
"state": "active", |
| 121 |
"created_at": "2012-05-23T08:01:01Z" |
| 122 |
}, |
| 123 |
"author": { |
| 124 |
"id": 1, |
| 125 |
"username": "john_smith", |
| 126 |
"email": "[email protected]", |
| 127 |
"name": "John Smith", |
| 128 |
"state": "active", |
| 129 |
"created_at": "2012-05-23T08:00:58Z" |
| 130 |
}, |
| 131 |
"state": "opened", |
| 132 |
"updated_at": self.arbitrary_updated.isoformat(), |
| 133 |
"created_at": self.arbitrary_created.isoformat(), |
| 134 |
"weight": 3, |
| 135 |
"work_in_progress": "true" |
| 136 |
} |
| 137 |
self.arbitrary_extra = { |
| 138 |
'issue_url': 'https://gitlab.example.com/arbitrary_username/project/issues/3', |
| 139 |
'project': 'project', |
| 140 |
'namespace': 'arbitrary_namespace', |
| 141 |
'type': 'issue', |
| 142 |
'annotations': [], |
| 143 |
} |
| 144 |
|
| 145 |
def test_normalize_label_to_tag(self): |
| 146 |
issue = self.service.get_issue_for_record( |
| 147 |
self.arbitrary_issue, |
| 148 |
self.arbitrary_extra |
| 149 |
) |
| 150 |
self.assertEqual(issue._normalize_label_to_tag('needs work'), |
| 151 |
'needs_work') |
| 152 |
|
| 153 |
def test_to_taskwarrior(self): |
| 154 |
self.service.import_labels_as_tags = True |
| 155 |
issue = self.service.get_issue_for_record( |
| 156 |
self.arbitrary_issue, |
| 157 |
self.arbitrary_extra |
| 158 |
) |
| 159 |
|
| 160 |
expected_output = { |
| 161 |
'project': self.arbitrary_extra['project'], |
| 162 |
'priority': self.service.default_priority, |
| 163 |
'annotations': [], |
| 164 |
'tags': [u'feature'], |
| 165 |
'due': self.arbitrary_duedate.replace(microsecond=0), |
| 166 |
'entry': self.arbitrary_created.replace(microsecond=0), |
| 167 |
issue.URL: self.arbitrary_extra['issue_url'], |
| 168 |
issue.REPO: 'project', |
| 169 |
issue.STATE: self.arbitrary_issue['state'], |
| 170 |
issue.TYPE: self.arbitrary_extra['type'], |
| 171 |
issue.TITLE: self.arbitrary_issue['title'], |
| 172 |
issue.NUMBER: str(self.arbitrary_issue['iid']), |
| 173 |
issue.UPDATED_AT: self.arbitrary_updated.replace(microsecond=0), |
| 174 |
issue.CREATED_AT: self.arbitrary_created.replace(microsecond=0), |
| 175 |
issue.DUEDATE: self.arbitrary_duedate, |
| 176 |
issue.DESCRIPTION: self.arbitrary_issue['description'], |
| 177 |
issue.MILESTONE: self.arbitrary_issue['milestone']['title'], |
| 178 |
issue.UPVOTES: 0, |
| 179 |
issue.DOWNVOTES: 0, |
| 180 |
issue.WORK_IN_PROGRESS: 1, |
| 181 |
issue.AUTHOR: 'john_smith', |
| 182 |
issue.ASSIGNEE: 'jack_smith', |
| 183 |
issue.NAMESPACE: 'arbitrary_namespace', |
| 184 |
issue.WEIGHT: 3, |
| 185 |
} |
| 186 |
actual_output = issue.to_taskwarrior() |
| 187 |
|
| 188 |
self.assertEqual(actual_output, expected_output) |
| 189 |
|
| 190 |
def test_work_in_progress(self): |
| 191 |
self.arbitrary_issue['work_in_progress'] = 'false' |
| 192 |
self.service.import_labels_as_tags = True |
| 193 |
issue = self.service.get_issue_for_record( |
| 194 |
self.arbitrary_issue, |
| 195 |
self.arbitrary_extra |
| 196 |
) |
| 197 |
|
| 198 |
expected_output = { |
| 199 |
'project': self.arbitrary_extra['project'], |
| 200 |
'priority': self.service.default_priority, |
| 201 |
'annotations': [], |
| 202 |
'tags': [u'feature'], |
| 203 |
'due': self.arbitrary_duedate.replace(microsecond=0), |
| 204 |
'entry': self.arbitrary_created.replace(microsecond=0), |
| 205 |
issue.URL: self.arbitrary_extra['issue_url'], |
| 206 |
issue.REPO: 'project', |
| 207 |
issue.STATE: self.arbitrary_issue['state'], |
| 208 |
issue.TYPE: self.arbitrary_extra['type'], |
| 209 |
issue.TITLE: self.arbitrary_issue['title'], |
| 210 |
issue.NUMBER: str(self.arbitrary_issue['iid']), |
| 211 |
issue.UPDATED_AT: self.arbitrary_updated.replace(microsecond=0), |
| 212 |
issue.CREATED_AT: self.arbitrary_created.replace(microsecond=0), |
| 213 |
issue.DUEDATE: self.arbitrary_duedate, |
| 214 |
issue.DESCRIPTION: self.arbitrary_issue['description'], |
| 215 |
issue.MILESTONE: self.arbitrary_issue['milestone']['title'], |
| 216 |
issue.UPVOTES: 0, |
| 217 |
issue.DOWNVOTES: 0, |
| 218 |
issue.WORK_IN_PROGRESS: 0, |
| 219 |
issue.AUTHOR: 'john_smith', |
| 220 |
issue.ASSIGNEE: 'jack_smith', |
| 221 |
issue.NAMESPACE: 'arbitrary_namespace', |
| 222 |
issue.WEIGHT: 3, |
| 223 |
} |
| 224 |
actual_output = issue.to_taskwarrior() |
| 225 |
|
| 226 |
self.assertEqual(actual_output, expected_output) |
| 227 |
|
| 228 |
@responses.activate |
| 229 |
def test_issues(self): |
| 230 |
self.add_response( |
| 231 |
'https://gitlab.example.com/api/v4/projects?simple=True&per_page=100&page=1', |
| 232 |
json=[{ |
| 233 |
'id': 1, |
| 234 |
'path': 'arbitrary_username/project', |
| 235 |
'web_url': 'example.com', |
| 236 |
"namespace": { |
| 237 |
"full_path": "arbitrary_username" |
| 238 |
} |
| 239 |
}]) |
| 240 |
|
| 241 |
self.add_response( |
| 242 |
'https://gitlab.example.com/api/v4/projects/1/issues?state=opened&per_page=100&page=1', |
| 243 |
json=[self.arbitrary_issue]) |
| 244 |
|
| 245 |
self.add_response( |
| 246 |
'https://gitlab.example.com/api/v4/projects/1/issues/3/notes?per_page=100&page=1', |
| 247 |
json=[{ |
| 248 |
'author': {'username': 'john_smith'}, |
| 249 |
'body': 'Some comment.' |
| 250 |
}]) |
| 251 |
|
| 252 |
issue = next(self.service.issues()) |
| 253 |
|
| 254 |
expected = { |
| 255 |
'annotations': [u'@john_smith - Some comment.'], |
| 256 |
'description': |
| 257 |
u'(bw)Is#3 - Add user settings .. example.com/issues/3', |
| 258 |
'due': self.arbitrary_duedate, |
| 259 |
'entry': self.arbitrary_created, |
| 260 |
'gitlabassignee': u'jack_smith', |
| 261 |
'gitlabauthor': u'john_smith', |
| 262 |
'gitlabcreatedon': self.arbitrary_created, |
| 263 |
'gitlabdescription': u'', |
| 264 |
'gitlabdownvotes': 0, |
| 265 |
'gitlabmilestone': u'v1.0', |
| 266 |
'gitlabnamespace': u'arbitrary_username', |
| 267 |
'gitlabnumber': '3', |
| 268 |
'gitlabrepo': u'arbitrary_username/project', |
| 269 |
'gitlabstate': u'opened', |
| 270 |
'gitlabtitle': u'Add user settings', |
| 271 |
'gitlabtype': 'issue', |
| 272 |
'gitlabupdatedat': self.arbitrary_updated, |
| 273 |
'gitlabduedate': self.arbitrary_duedate, |
| 274 |
'gitlabupvotes': 0, |
| 275 |
'gitlaburl': u'example.com/issues/3', |
| 276 |
'gitlabwip': 1, |
| 277 |
'gitlabweight': 3, |
| 278 |
'priority': 'M', |
| 279 |
'project': u'arbitrary_username/project', |
| 280 |
'tags': []} |
| 281 |
|
| 282 |
self.assertEqual(issue.get_taskwarrior_record(), expected) |