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_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_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 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 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 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 notify_validate(smtp_server, status, recipients, recipients_cc, files, replyto=None): ''' Notifies of the result of a validate, requires: - Logged in smtp server - The result of the validation ['error', 'success'] - The recipients of the email - The Carbon Copy Recipients of the email - The files to attach (images not included) - The replyto email, if None === the email used to log in''' print_key_value_list(f'{INFO_TAG} Sending \'selenium\' email:', [('Status', status), ('Files', files), ('Recipients', recipients), ('Recipients CC', recipients_cc), ('Reply-To', replyto)]) salutation = get_salutation() with open(f'{PWD}/resources/templates/validate/{status}.html', encoding='utf8') as template: html_file = template.read() template = Template(html_file) html = template.safe_substitute(SALUTATION=salutation, MR_TITLE=MR_TITLE, TARGET_BRANCH=TARGET_BRANCH, MR_URL=MR_URL, status=status, BUILD_ID=BUILD_ID, MR_IID=MR_IID, SOURCE_BRANCH=SOURCE_BRANCH, JOB_LINK=BLUEOCEAN_URL) html = html.replace('../../imgs/', 'cid:') msg = MIMEMultipart() msg["Subject"] = f"[JENKINS][{status.upper()}] #{MR_IID} - {MR_TITLE}" msg["From"] = smtp_server.user msg["To"] = ", ".join(recipients) msg["Cc"] = ", ".join(recipients_cc) if replyto: msg.add_header('reply-to', replyto) body = MIMEText(html, 'html') # attach the body with the msg instance msg.attach(body) sel_photos = [ Resources.GIT_BRANCH, Resources.LS_LOGO, Resources.JENKINS_LOGO, Resources.GITLAB_LOGO, Resources.PULL_REQUEST ] attach_files(msg, files, ARTIFACT_FOLDER, PWD, sel_photos) smtp_server.sendmail(msg["From"], recipients + recipients_cc, msg.as_string())
def create_release(version, target, remote, token, project_id=None, sha_commits=None, iids=None, ssl_verify=False): ''' Creates a release branch with the mr passed onto the branch passed ''' print_key_value_list(f'{INFO_TAG} Creating release', [('Sha Commits', sha_commits), ('Iids', iids), ('Version', version), ('Target Branch', target)]) if iids: sha_commits = get_shas_for_iids(token, iids, project_id, None, ssl_verify) checkout(target, remote, reset=False) branch_name = f'release/{version}' create_branch(branch_name, do_checkout=True) for sha_commit in sha_commits: cherry_pick(sha_commit)
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 selenium(smtp_server, recipients, recipients_cc, replyto=None): ''' Notifies selenium errors ''' print_key_value_list(f'{INFO_TAG} Sending \'selenium\' email:', [('Recipients', recipients), ('Recipients CC', recipients_cc), ('Reply-To', replyto)]) salutation = get_salutation() with open(f'{PWD}/resources/templates/errors/selenium_error.html', encoding='utf8') as template: html_file = template.read() template = Template(html_file) html = template.safe_substitute(SALUTATION=salutation, MR_TITLE=MR_TITLE, ALLURE_URL=ALLURE_URL, BUILD_ID=BUILD_ID, SOURCE_BRANCH=SOURCE_BRANCH, JOB_LINK=BLUEOCEAN_URL) html = html.replace('../../imgs/', 'cid:') msg = MIMEMultipart() msg["Subject"] = f"[JENKINS][SELENIUM-ERROR] #{MR_IID} - {MR_TITLE}" msg["From"] = smtp_server.user # TODO peta aqui msg["To"] = ", ".join(recipients) msg["Cc"] = ", ".join(recipients_cc) msg.add_header('reply-to', replyto) body = MIMEText(html, 'html') # attach the body with the msg instance msg.attach(body) sel_photos = [ Resources.GIT_BRANCH, Resources.CLIENT_LOGO, Resources.LS_LOGO, Resources.JENKINS_LOGO, Resources.ALLURE_LOGO ] attach_files(msg, [], None, PWD, sel_photos) smtp_server.sendmail(msg["From"], recipients + recipients_cc, msg.as_string())
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 create_tag_terminal(self, tagName, commitHash): print( f'{INFO_TAG} Flag -gt detected. Tag will be created by Git Terminal' ) print_key_value_list(f'{INFO_TAG} Creating tag with:', [('Remote URL', self.host), ('Tag Name', tagName), ('Ref', commitHash)]) command = f'git tag {tagName} {commitHash}' stdout, code = call_subprocess(command) if code == 128: raise DuplicateRemote(tagName, commitHash, 'Tag') command = f'git push origin {tagName}' stdout, code = call_subprocess(command) if code == 0: print(f'{INFO_TAG} Tag Created') else: print(f"{WARNING_TAG} Tag Created but not pushed to remote.") raise Exception(code)
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 create_branch_terminal(self, branchName, commitHash): print( f'{INFO_TAG} Flag -gt detected. branch will be created by Git Terminal' ) print_key_value_list(f'{INFO_TAG} Creating branch with:', [('Remote URL', self.host), ('Branch Name', branchName), ('Source Ref', commitHash)]) command = f'git checkout -b {branchName} {commitHash}' stdout, code = call_subprocess(command) if code == 128: raise DuplicateRemote(branchName, commitHash, 'Branch') command = f'git push origin {branchName}' stdout, code = call_subprocess(command) if code == 0: print(f'{INFO_TAG} Branch Created') else: print(f"{WARNING_TAG} Branch Created but not pushed to remote.") raise Exception(code)
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')