def add_comment(self, sslVerify, token, pullRequestId, newComments, buildId, workspace, **kwargs): ''' Adds a new comment to the pull request ''' commentBody = ' '.join( newComments ) url = ( f'{self.host}/rest/api/1.0/projects/{self.project}/repos/{self.repository}/pull-requests/{pullRequestId}/comments' ) headers = { 'Authorization' : f'Bearer {token}', 'Content-Type' : 'application/json' } payload = { 'text' : commentBody } payload = json.dumps( payload ) data = payload.encode( 'utf-8' ) print_key_value_list( f'{INFO_TAG} Adding new Comment to:', [ ( 'Host URL', self.host ), ( 'Project Name', self.project ), ( 'Repository', self.repository ), ( 'Target Endpoint', url), ( 'Comment', commentBody ), ( 'PullRequest Id', pullRequestId ) ] ) response = http_request( url, data, headers, 'POST', sslVerify ) if response.statusCode == 201: commentId = str( response.responseBody[ 'id' ] ) commentVersion = str( response.responseBody[ 'version' ] ) save_comment_to_file( commentBody, buildId, commentId, workspace, commentVersion ) print( f'{SUCCESS_LINE} Comment created succesfully with id \'{commentId}\', saved to ./{buildId}-comment.txt' ) else: print( f'{ERROR_LINE} Could not create comment on pull request ({response.responseBody} -- {response.statusCode})' )
def add_comment(self, sslVerify, token, mergeRequestId, newComments, buildId, workspace, **kwargs): ''' Adds a new comment to the merge request ''' commentBody = ' '.join(newComments) url = ( f'{self.host}/api/v4/projects/{self.projectId}/merge_requests/{mergeRequestId}/notes' ) payload = {'body': commentBody} headers = {'Private-Token': token} data = urllib.parse.urlencode(payload).encode('utf-8') print_key_value_list(f'{INFO_TAG} Adding new Comment to:', [('Host URL', self.host), ('Project Id', self.projectId), ('Target Endpoint', url), ('Comment', commentBody), ('MergeRequest Id', mergeRequestId)]) response = http_request(url, data, headers, 'POST', sslVerify) if response.statusCode == 201: commentId = str(response.responseBody['id']) save_comment_to_file(commentBody, buildId, commentId, workspace) print( f'{SUCCESS_LINE} Comment created succesfully with id \'{commentId}\', saved to ./{buildId}-comment.txt' ) else: print( f'{ERROR_LINE} Could not create comment on merge request ({response.responseBody} -- {response.statusCode})' )
def create_tag(self, sslVerify, token, tagName, commitHash, **kwargs): ''' Method for creating new tag ''' message = kwargs['message'] releaseDescription = kwargs['releaseDescription'] url = (f'{self.host}/api/v4/projects/{self.projectId}/repository/tags') headers = {'Private-Token': token} payload = { 'tag_name': tagName, 'ref': commitHash, 'message': message, 'release_description': releaseDescription } data = urllib.parse.urlencode(payload).encode("utf-8") print_key_value_list(f'{INFO_TAG} Creating tag with:', [('Remote URL', self.host), ('Project Id', self.projectId), ('Tag Name', tagName), ('Ref', commitHash), ('Message', message), ('Description', releaseDescription), ('Endpoint', f'{url}')]) response = http_request(url, data, headers, 'POST', sslVerify) if response.statusCode == 201: print(f'{INFO_TAG} Tag Created') else: print( f'{WARNING_TAG} TAG \'{tagName}\' not created. Status code: {response.statusCode}' )
def create_branch(self, sslVerify, token, branchName, commitHash, **kwargs): ''' Method for creating new branch ''' url = ( f'{self.host}/api/v4/projects/{self.projectId}/repository/branches' ) headers = {'Private-Token': token} payload = {'branch': branchName, 'ref': commitHash} data = urllib.parse.urlencode(payload).encode("utf-8") print_key_value_list(f'{INFO_TAG} Creating branch:', [('Remote URL', self.host), ('Project Id', self.projectId), ('Branch Name', branchName), ('Source Ref', commitHash), ('Endpoint', f'{url}')]) response = http_request(url, data, headers, 'POST', sslVerify) if response.statusCode == 201: print(f'{INFO_TAG} Branch \'{branchName}\' created') else: print( f'{WARNING_TAG} Branch \'{branchName}\' not created. Status code: {response.statusCode}' )
def create_thread(self, sslVerify, token, pullRequestId, commentBody, **kwargs): ''' Adds a new comment to the merge request ''' token = base64.b64encode( bytes( f':{token}', 'utf-8' ) ).decode( 'ascii' ) commentBody = commentBody[ 0 ] url = f'{self.host}/{self.organization}/{self.projectName}/_apis/git/repositories/{self.repositoryId}/pullRequests/{pullRequestId}/threads?api-version=6.0' payload = { 'comments': [ { 'parentCommentId': 0, 'content': commentBody, 'commentType': 1 } ], 'status' : 1 } headers = { 'Authorization': f'Basic {token}', 'Content-Type' : 'application/json' } payload = json.dumps( payload ) data = payload.encode( 'utf-8' ) print_key_value_list( f'Adding new Thread to:', [ ( 'Host URL', self.host ), ( 'Project', self.projectName ), ( 'Target Endpoint', url), ( 'Comment', commentBody ), ( 'PullRequest Id', pullRequestId ) ] ) response = http_request( url, data, headers, 'POST', sslVerify ) if response.statusCode == 200: commentId = response.responseBody[ 'id' ] print( f'Thread created succesfully with id \'{commentId}\'' ) print( f'##vso[task.setvariable variable=pr_threat_id]{commentId}' ) else: print( f'Could not create threat on pull request {pullRequestId} ({response.responseBody} -- {response.statusCode})' )
def update_commit_status(self, sslVerify, token, commitHash, status, buildUrl, **kwargs): ''' Updates the commit status ''' url = ( f'{self.host}/api/2.0/repositories/{self.owner}/{self.projectName}/commit/{commitHash}/statuses/build' ) headers = { 'authorization': f'Basic {token}', 'Content-Type': 'application/json' } payload = { 'url': buildUrl, 'state': status, 'key': f'BUILD_{commitHash}' } payload = json.dumps(payload) data = payload.encode('utf-8') print_key_value_list(f'{INFO_TAG} Updating commit status:', [('Host URL', self.host), ('Commmit SHA', commitHash), ('Status', status), ('Build URL', buildUrl), ('Endpoint', url)]) response = http_request(url, data, headers, 'POST', sslVerify) if response.statusCode == 201: print(f'{SUCCESS_LINE} Commit status updated Successfully') else: print(f'{ERROR_LINE} Could not update commit status')
def create_tag(self, sslVerify, token, tagName, commitHash, **kwargs): ''' Method for creating new tag ''' url = ( f'{self.host}/api/2.0/repositories/{self.owner}/{self.projectName}/refs/tags' ) headers = { 'authorization': f'Basic {token}', 'Content-Type': 'application/json' } payload = {'name': tagName, 'target': {'hash': commitHash}} payload = json.dumps(payload) data = payload.encode('utf-8') print_key_value_list(f'{INFO_TAG} Creating tag with:', [('Remote URL', self.host), ('Owner', self.owner), ('Project Name', self.projectName), ('Tag Name', tagName), ('Ref', commitHash), ('Endpoint', f'{url}')]) response = http_request(url, data, headers, 'POST', sslVerify) if response.statusCode == 201: print(f'{INFO_TAG} Tag Created') else: print( f'{WARNING_TAG} TAG \'{tagName}\' not created. Status code: {response.statusCode}' )
def create_branch(self, sslVerify, token, branchName, commitHash, **kwargs): ''' Method for creating new branch ''' url = ( f'{self.host}/rest/api/1.0/projects/{self.project}/repos/{self.repository}/branches' ) headers = { 'Authorization' : f'Bearer {token}', 'Content-Type' : 'application/json' } payload = { 'name' : branchName, 'startPoint' : commitHash } payload = json.dumps( payload ) data = payload.encode( 'utf-8' ) print_key_value_list( f'{INFO_TAG} Creating branch:', [ ( 'Remote URL', self.host ), ( 'Project', self.project ), ( 'Repository', self.repository ), ( 'Branch Name', branchName ), ( 'Source Ref', commitHash ), ( 'Endpoint', f'{url}' ) ] ) response = http_request( url, data, headers, 'POST', sslVerify ) if response.statusCode == 200: print( f'{INFO_TAG} Branch \'{branchName}\' created' ) else: print( f'{WARNING_TAG} Branch \'{branchName}\' not created. Status code: {response.statusCode}' )
def create_tag(self, sslVerify, token, tagName, commitHash, **kwargs): ''' Method for creating new tag ''' message = kwargs[ 'releaseDescription' ] url = ( f'{self.host}/rest/api/1.0/projects/{self.project}/repos/{self.repository}/tags' ) headers = { 'Authorization' : f'Bearer {token}', 'Content-Type' : 'application/json' } payload = { 'name' : tagName, 'startPoint' : commitHash, 'message' : message } payload = json.dumps( payload ) data = payload.encode( 'utf-8' ) print_key_value_list( f'{INFO_TAG} Creating tag with:', [ ( 'Remote URL', self.host ), ( 'Project', self.project ), ( 'Repository', self.repository ), ( 'Tag Name', tagName ), ( 'Ref', commitHash ), ( 'Message', message ), ( 'Endpoint', f'{url}' ) ] ) response = http_request( url, data, headers, 'POST', sslVerify ) if response.statusCode == 200: print( f'{INFO_TAG} Tag Created' ) else: print( f'{WARNING_TAG} TAG \'{tagName}\' not created. Status code: {response.statusCode}' )
def update_commit_status(self, sslVerify, token, commitHash, status, buildUrl, **kwargs): ''' Updates the commit status ''' description = kwargs[ 'description' ] buildId = kwargs[ 'buildId' ] url = ( f'{self.host}/rest/build-status/1.0/commits/{commitHash}' ) headers = { 'authorization' : f'Bearer {token}', 'Content-Type' : 'application/json' } payload = { 'url' : buildUrl, 'state': status, 'key' : f'BUILD_{buildId}', 'name' : buildId, 'description' : description } payload = json.dumps( payload ) data = payload.encode( 'utf-8' ) print_key_value_list( f'{INFO_TAG} Updating commit status:', [ ( 'Host URL', self.host ), ( 'Commmit SHA', commitHash ), ( 'Status', status ), ( 'Build URL', buildUrl ), ( 'Endpoint', url ), ( 'Description', description ) ] ) response = http_request( url, data, headers, 'POST', sslVerify ) if response.statusCode == 204: print( f'{SUCCESS_LINE} Commit status updated Successfully' ) else: print( f'{ERROR_LINE} Could not update commit status' )
def edit_comment(self, sslVerify, token, pullRequestId, newComments, buildId, workspace, **kwargs): ''' Appends message to the pull request's comments ''' commentId, lastComments = get_last_comment(workspace, buildId) commentBody = append_new_comments(newComments, lastComments) url = ( f'{self.host}/api/2.0/repositories/{self.owner}/{self.projectName}/pullrequests/{pullRequestId}/comments/{commentId}' ) headers = { 'Authorization': f'Basic {token}', 'Content-Type': 'application/json' } payload = {'content': {'raw': commentBody}} payload = json.dumps(payload) data = payload.encode('utf-8') print_key_value_list(f'{INFO_TAG} Edditing Comment to:', [('Host URL', self.host), ('Owner', self.owner), ('Project Name', self.projectName), ('Target Endpoint', url), ('Comment', commentBody), ('PullRequest Id', pullRequestId)]) response = http_request(url, data, headers, 'PUT', sslVerify) if response.statusCode == 200: commentId = str(response.responseBody['id']) save_comment_to_file(commentBody, buildId, commentId, workspace) print( f'{SUCCESS_LINE} Comment created succesfully with id \'{commentId}\', saved to ./{buildId}-comment.txt' ) else: print( f'{ERROR_LINE} Could not edit comment on pull request ({response.responseBody} -- {response.statusCode})' )
def update_commit_status(self, sslVerify, token, commitHash, status, buildUrl, **kwargs): ''' Updates the commit status ''' jobName = kwargs['jobName'] url = ( f'{self.host}/api/v4/projects/{self.projectId}/statuses/{commitHash}' ) headers = {'Private-Token': token} payload = {'state': status, 'target_url': buildUrl, 'name': jobName} data = urllib.parse.urlencode(payload).encode('utf-8') print_key_value_list(f'{INFO_TAG} Updating commit status:', [('Host URL', self.host), ('Commmit SHA', commitHash), ('Status', status), ('Build URL', buildUrl), ('Endpoint', url)]) response = http_request(url, data, headers, 'POST', sslVerify) if response.statusCode == 200: print(f'{SUCCESS_LINE} Commit status updated Successfully') else: print(f'{ERROR_LINE} Could not update commit status')