Debian Codegraph @

azure_functions_devops_build/repository/github_repository_manager.py

raw
1 # --------------------------------------------------------------------------------------------
2 # Copyright (c) Microsoft Corporation. All rights reserved.
3 # Licensed under the MIT License. See License.txt in the project root for license information.
4 # --------------------------------------------------------------------------------------------
5
6 import base64
7 from ..exceptions import (
8 GithubContentNotFound,
9 GithubIntegrationRequestError,
10 GithubUnauthorizedError,
11 )
12 from ..base.base_github_manager import BaseGithubManager
13
14
15 class GithubRepositoryManager(BaseGithubManager):
16
17 def check_github_repository(self, repository_fullname):
18 header_parameters = self.construct_github_request_header()
19 request = self._client.get('/repos/{repo}'.format(repo=repository_fullname))
20 response = self._client.send(request, header_parameters)
21 if response.status_code // 100 == 2:
22 return True
23 return False
24
25 def check_github_file(self, repository_fullname, file_path):
26 header_parameters = self.construct_github_request_header()
27 request = self._client.get('/repos/{repo}/contents/{path}'.format(
28 repo=repository_fullname,
29 path=file_path
30 ))
31 response = self._client.send(request, header_parameters)
32 return response.status_code // 100 == 2
33
34 def get_content(self, repository_fullname, file_path, get_metadata=True):
35 header_parameters = self.construct_github_request_header()
36
37 if get_metadata: # Get files metadata
38 header_parameters['Content-Type'] = 'application/json'
39 else: # Get files content
40 header_parameters['Accept'] = 'application/vnd.github.v3.raw'
41
42 request = self._client.get('/repos/{repo}/contents/{path}'.format(
43 repo=repository_fullname,
44 path=file_path
45 ))
46 response = self._client.send(request, header_parameters)
47
48 # The response is a Json content
49 if response.status_code // 100 == 2:
50 return response.json()
51
52 if response.status_code == 401:
53 raise GithubUnauthorizedError('Failed to read {repo}/{path}'.format(
54 repo=repository_fullname,
55 path=file_path
56 ))
57
58 if response.status_code == 404:
59 raise GithubContentNotFound('Failed to find {repo}/{path}'.format(
60 repo=repository_fullname,
61 path=file_path
62 ))
63
64 raise GithubIntegrationRequestError(response.status_code)
65
66 def put_content(self, repository_fullname, file_path, data):
67 header_parameters = self.construct_github_request_header()
68 header_parameters['Content-Type'] = 'Application/Json'
69 request = self._client.put(
70 url='/repos/{repo}/contents/{path}'.format(repo=repository_fullname, path=file_path),
71 headers=header_parameters,
72 content=data
73 )
74
75 response = self._client.send(request)
76 if response.status_code // 100 == 2:
77 return response
78
79 if response.status_code == 401:
80 raise GithubUnauthorizedError('Failed to write {repo}/{path}'.format(
81 repo=repository_fullname,
82 path=file_path
83 ))
84
85 if response.status_code == 404:
86 raise GithubContentNotFound('Failed to find {repo}/{path}'.format(
87 repo=repository_fullname,
88 path=file_path
89 ))
90
91 raise GithubIntegrationRequestError("{res.status_code} {res.url}".format(res=response))
92
93 def commit_file(self, repository_fullname, file_path, commit_message, file_data, sha=None, encode='utf-8'):
94 data = {
95 "branch": "master",
96 "message": "{message}".format(message=commit_message),
97 "content": base64.b64encode(bytes(file_data.encode(encode))).decode('ascii'),
98 }
99
100 if sha:
101 data["sha"] = sha
102
103 return self.put_content(
104 repository_fullname=repository_fullname,
105 file_path=file_path,
106 data=data
107 )