Skip to content

Commit b4c04ac

Browse files
authored
External providers: better logging for GitLab (#7385)
* External providers: better logging for GitLab Ref #7370
1 parent 7274123 commit b4c04ac

File tree

4 files changed

+33
-38
lines changed

4 files changed

+33
-38
lines changed

readthedocs/oauth/services/github.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66

77
from allauth.socialaccount.models import SocialToken
88
from allauth.socialaccount.providers.github.views import GitHubOAuth2Adapter
9-
10-
from django.db.models import Q
119
from django.conf import settings
10+
from django.db.models import Q
1211
from django.urls import reverse
1312
from requests.exceptions import RequestException
1413

@@ -23,7 +22,6 @@
2322
from ..models import RemoteOrganization, RemoteRepository
2423
from .base import Service, SyncServiceError
2524

26-
2725
log = logging.getLogger(__name__)
2826

2927

@@ -465,7 +463,7 @@ def send_build_status(self, build, commit, state):
465463
if resp.status_code == 201:
466464
log.info(
467465
"GitHub commit status created for project: %s, commit status: %s",
468-
project,
466+
project.slug,
469467
github_build_state,
470468
)
471469
return True
@@ -474,16 +472,16 @@ def send_build_status(self, build, commit, state):
474472
log.info(
475473
'GitHub project does not exist or user does not have '
476474
'permissions: project=%s, user=%s, status=%s, url=%s',
477-
project,
478-
self.user,
475+
project.slug,
476+
self.user.username,
479477
resp.status_code,
480478
statuses_url,
481479
)
482480
return False
483481

484482
log.warning(
485483
'Unknown GitHub status API response: project=%s, user=%s, status_code=%s',
486-
project,
484+
project.slug,
487485
self.user,
488486
resp.status_code
489487
)
@@ -493,7 +491,7 @@ def send_build_status(self, build, commit, state):
493491
except (RequestException, ValueError):
494492
log.exception(
495493
'GitHub commit status creation failed for project: %s',
496-
project,
494+
project.slug,
497495
)
498496
# Response data should always be JSON, still try to log if not
499497
# though

readthedocs/oauth/services/gitlab.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,24 @@
33
import json
44
import logging
55
import re
6+
from urllib.parse import urljoin, urlparse
67

78
from allauth.socialaccount.providers.gitlab.views import GitLabOAuth2Adapter
89
from django.conf import settings
910
from django.urls import reverse
1011
from requests.exceptions import RequestException
1112

13+
from readthedocs.builds import utils as build_utils
1214
from readthedocs.builds.constants import (
1315
BUILD_STATUS_SUCCESS,
1416
SELECT_BUILD_STATUS,
1517
)
16-
from readthedocs.builds import utils as build_utils
1718
from readthedocs.integrations.models import Integration
1819
from readthedocs.projects.models import Project
1920

2021
from ..models import RemoteOrganization, RemoteRepository
2122
from .base import Service, SyncServiceError
2223

23-
24-
try:
25-
from urlparse import urljoin, urlparse
26-
except ImportError:
27-
from urllib.parse import urljoin, urlparse # noqa
28-
2924
log = logging.getLogger(__name__)
3025

3126

@@ -530,25 +525,26 @@ def send_build_status(self, build, commit, state):
530525
url = self.adapter.provider_base_url
531526

532527
try:
528+
statuses_url = f'{url}/api/v4/projects/{repo_id}/statuses/{commit}'
533529
resp = session.post(
534-
f'{url}/api/v4/projects/{repo_id}/statuses/{commit}',
530+
statuses_url,
535531
data=json.dumps(data),
536532
headers={'content-type': 'application/json'},
537533
)
538534

539535
if resp.status_code == 201:
540536
log.info(
541537
"GitLab commit status created for project: %s, commit status: %s",
542-
project,
538+
project.slug,
543539
gitlab_build_state,
544540
)
545541
return True
546542

547543
if resp.status_code in [401, 403, 404]:
548544
log.info(
549-
'GitLab project does not exist or user does not have '
550-
'permissions: project=%s',
551-
project,
545+
'GitLab project does not exist or user does not have permissions: '
546+
'project=%s, user=%s, status=%s, url=%s',
547+
project.slug, self.user.username, resp.status_code, statuses_url,
552548
)
553549
return False
554550

@@ -558,7 +554,7 @@ def send_build_status(self, build, commit, state):
558554
except (RequestException, ValueError):
559555
log.exception(
560556
'GitLab commit status creation failed for project: %s',
561-
project,
557+
project.slug,
562558
)
563559
# Response data should always be JSON, still try to log if not
564560
# though

readthedocs/projects/tasks.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1941,7 +1941,6 @@ def send_build_status(build_pk, commit, status):
19411941
user_accounts = service_class.for_user(user)
19421942
# Try to loop through users all social accounts to send a successful request
19431943
for account in user_accounts:
1944-
# Currently we only support GitHub Status API
19451944
if account.provider_name == provider_name:
19461945
success = account.send_build_status(build, commit, status)
19471946
if success:

readthedocs/rtd_tests/tests/test_oauth.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
# -*- coding: utf-8 -*-
21
from unittest import mock
2+
33
from django.conf import settings
44
from django.contrib.auth.models import User
55
from django.test import TestCase
66
from django.test.utils import override_settings
77
from django.urls import reverse
8-
98
from django_dynamic_fixture import get
109

11-
from readthedocs.builds.constants import EXTERNAL, BUILD_STATUS_SUCCESS
12-
from readthedocs.builds.models import Version, Build
10+
from readthedocs.builds.constants import BUILD_STATUS_SUCCESS, EXTERNAL
11+
from readthedocs.builds.models import Build, Version
1312
from readthedocs.integrations.models import (
13+
BitbucketWebhook,
1414
GitHubWebhook,
1515
GitLabWebhook,
16-
BitbucketWebhook
1716
)
1817
from readthedocs.oauth.models import RemoteOrganization, RemoteRepository
1918
from readthedocs.oauth.services import (
@@ -184,7 +183,7 @@ def test_send_build_status_successful(self, session, mock_logger):
184183
self.assertTrue(success)
185184
mock_logger.info.assert_called_with(
186185
"GitHub commit status created for project: %s, commit status: %s",
187-
self.project,
186+
self.project.slug,
188187
BUILD_STATUS_SUCCESS
189188
)
190189

@@ -202,8 +201,8 @@ def test_send_build_status_404_error(self, session, mock_logger):
202201
mock_logger.info.assert_called_with(
203202
'GitHub project does not exist or user does not have '
204203
'permissions: project=%s, user=%s, status=%s, url=%s',
205-
self.project,
206-
self.user,
204+
self.project.slug,
205+
self.user.username,
207206
404,
208207
'https://api.github.com/repos/pypa/pip/statuses/1234'
209208
)
@@ -219,7 +218,7 @@ def test_send_build_status_value_error(self, session, mock_logger):
219218
self.assertFalse(success)
220219
mock_logger.exception.assert_called_with(
221220
'GitHub commit status creation failed for project: %s',
222-
self.project,
221+
self.project.slug,
223222
)
224223

225224
@override_settings(DEFAULT_PRIVACY_LEVEL='private')
@@ -927,7 +926,7 @@ def setUp(self):
927926
self.service = GitLabService(user=self.user, account=None)
928927
self.external_version = get(Version, project=self.project, type=EXTERNAL)
929928
self.external_build = get(
930-
Build, project=self.project, version=self.external_version
929+
Build, project=self.project, version=self.external_version, commit=1234,
931930
)
932931
self.integration = get(
933932
GitLabWebhook,
@@ -1031,7 +1030,7 @@ def test_send_build_status_successful(self, repo_id, session, mock_logger):
10311030
self.assertTrue(success)
10321031
mock_logger.info.assert_called_with(
10331032
"GitLab commit status created for project: %s, commit status: %s",
1034-
self.project,
1033+
self.project.slug,
10351034
BUILD_STATUS_SUCCESS
10361035
)
10371036

@@ -1040,7 +1039,7 @@ def test_send_build_status_successful(self, repo_id, session, mock_logger):
10401039
@mock.patch('readthedocs.oauth.services.gitlab.GitLabService._get_repo_id')
10411040
def test_send_build_status_404_error(self, repo_id, session, mock_logger):
10421041
session().post.return_value.status_code = 404
1043-
repo_id().return_value = '9999'
1042+
repo_id.return_value = '9999'
10441043

10451044
success = self.service.send_build_status(
10461045
self.external_build,
@@ -1050,9 +1049,12 @@ def test_send_build_status_404_error(self, repo_id, session, mock_logger):
10501049

10511050
self.assertFalse(success)
10521051
mock_logger.info.assert_called_with(
1053-
'GitLab project does not exist or user does not have '
1054-
'permissions: project=%s',
1055-
self.project
1052+
'GitLab project does not exist or user does not have permissions: '
1053+
'project=%s, user=%s, status=%s, url=%s',
1054+
self.project.slug,
1055+
self.user.username,
1056+
404,
1057+
'https://gitlab.com/api/v4/projects/9999/statuses/1234',
10561058
)
10571059

10581060
@mock.patch('readthedocs.oauth.services.gitlab.log')
@@ -1069,7 +1071,7 @@ def test_send_build_status_value_error(self, repo_id, session, mock_logger):
10691071
self.assertFalse(success)
10701072
mock_logger.exception.assert_called_with(
10711073
'GitLab commit status creation failed for project: %s',
1072-
self.project,
1074+
self.project.slug,
10731075
)
10741076

10751077
@mock.patch('readthedocs.oauth.services.gitlab.log')

0 commit comments

Comments
 (0)