| 1 |
import vsts.service_endpoint.v4_1.models as models |
| 2 |
from vsts.exceptions import VstsServiceError |
| 3 |
from ..base.base_manager import BaseManager |
| 4 |
from .service_endpoint_utils import sanitize_github_repository_fullname |
| 5 |
|
| 6 |
class GithubServiceEndpointManager(BaseManager): |
| 7 |
def __init__(self, organization_name, project_name, creds): |
| 8 |
super(GithubServiceEndpointManager, self).__init__( |
| 9 |
creds, organization_name=organization_name, project_name=project_name |
| 10 |
) |
| 11 |
|
| 12 |
def get_github_service_endpoints(self, repository_fullname): |
| 13 |
service_endpoint_name = self._get_service_github_endpoint_name(repository_fullname) |
| 14 |
try: |
| 15 |
result = self._service_endpoint_client.get_service_endpoints_by_names( |
| 16 |
self._project_name, |
| 17 |
[service_endpoint_name], |
| 18 |
type="github" |
| 19 |
) |
| 20 |
except VstsServiceError: |
| 21 |
return [] |
| 22 |
return result |
| 23 |
|
| 24 |
def create_github_service_endpoint(self, repository_fullname, github_pat): |
| 25 |
data = {} |
| 26 |
auth = models.endpoint_authorization.EndpointAuthorization( |
| 27 |
parameters={ |
| 28 |
"accessToken": github_pat |
| 29 |
}, |
| 30 |
scheme="PersonalAccessToken" |
| 31 |
) |
| 32 |
service_endpoint_name = self._get_service_github_endpoint_name(repository_fullname) |
| 33 |
service_endpoint = models.service_endpoint.ServiceEndpoint( |
| 34 |
administrators_group=None, |
| 35 |
authorization=auth, |
| 36 |
data=data, |
| 37 |
name=service_endpoint_name, |
| 38 |
type="github", |
| 39 |
url="http://github.com" |
| 40 |
) |
| 41 |
|
| 42 |
return self._service_endpoint_client.create_service_endpoint(service_endpoint, self._project_name) |
| 43 |
|
| 44 |
def _get_service_github_endpoint_name(self, repository_name): |
| 45 |
return sanitize_github_repository_fullname(repository_name) |