-
Notifications
You must be signed in to change notification settings - Fork 31
/
github_git_repository.py
103 lines (78 loc) · 3.42 KB
/
github_git_repository.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
import requests
import utils
import logging
from clients.github_client import GitHubClient
from repositories.git_repository import GitRepositoryInterface
class GitHubGitRepository(GitRepositoryInterface):
MAX_DESCR_LENGTH = 140
def __init__(self):
self.gitops_repo_name = utils.getenv("GITHUB_GITOPS_MANIFEST_REPO_NAME") # gitops-manifests
self.github_client = GitHubClient()
self.headers = self.github_client.get_rest_api_headers()
self.rest_api_url = self.github_client.get_rest_api_url()
def post_commit_status(self, commit_status):
url = f'{self.rest_api_url}/{self.gitops_repo_name}/statuses/{commit_status.commit_id}'
github_state = self._map_to_github_state(commit_status.state)
message = commit_status.message
if len(message) > self.MAX_DESCR_LENGTH:
message = message[:self.MAX_DESCR_LENGTH]
data = {'state': github_state, 'description': message, 'context': commit_status.status_name}
logging.info(f'Url {url}: Headers {self.headers}: Data {data}')
response = requests.post(url=url, headers=self.headers, json=data)
# Throw appropriate exception if request failed
response.raise_for_status()
def is_commit_finished(self, commit_id):
url = f'{self.rest_api_url}/{self.gitops_repo_name}/commits/{commit_id}/status'
response = requests.get(url=url, headers=self.headers)
# Throw appropriate exception if request failed
response.raise_for_status()
responseJSON = response.json()
# logging.debug(f'Url {url}: Headers {self.headers}: Response {responseJSON}')
state = responseJSON['state']
logging.info(f'Commit {commit_id}: {state}')
return state == 'failure' or state == 'success'
def _map_to_github_state(self, reason):
state_map = {
"Suspended": "error",
"ReconciliationSucceeded": "success",
"ReconciliationFailed": "failure",
"Progressing": "pending",
"DependencyNotReady": "pending",
"PruneFailed": "failure",
"ArtifactFailed": "failure",
"BuildFailed": "failure",
"HealthCheckFailed": "failure",
"ValidationFailed": "failure",
"NotApplicable": "success",
"info": "pending",
"error": "failure",
"Succeeded": "success",
"Failed": "failure",
"Error": "error",
"Inconclusive": "pending",
"Running": "pending",
"OutOfSync": "pending",
"Synced": "success",
"Unknown": "success",
"Progressing": "pending",
"Degraded": "error",
"Healthy": "success",
"Missing": "failure"
}
return state_map[reason]
def get_commit_message(self, commit_id):
url = f'{self.rest_api_url}/{self.gitops_repo_name}/commits/{commit_id}'
response = requests.get(url=url, headers=self.headers)
# Throw appropriate exception if request failed
response.raise_for_status()
responseJSON = response.json()
commitMessage = responseJSON['commit']['message']
return commitMessage
def get_pr_num(self, commit_id) -> str:
pass
def get_pr_metadata(self, commit_id):
pass
def get_prs(self, pr_status):
pass