Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- #! /user/bin/python2
- # Import packages and libraries
- from github import Github
- from github import BadCredentialsException
- import gspread_formatting
- import gspread
- import oauth2client
- from oauth2client.service_account import ServiceAccountCredentials
- import getpass
- class ReadRelease:
- # Instances vairables
- sheet = ''
- username = ''
- password = ''
- repository_name = ''
- release_name = ''
- # Get user credentials from consule
- def get_user_credentials (self):
- self.username = raw_input("Github username: ")
- self.password = getpass.getpass(prompt='Github password: ')
- self.repository_name = raw_input("Repository name: ")
- self.release_name = raw_input('Release name: ')
- # Read details of all releases in a repository
- def read_requested_release_detials(self, release, tag_name, sheet, is_release_found):
- is_release_found = True
- release_body = release.body
- lines_in_release_body = (release_body.splitlines(0))
- ReadRelease.read_lines_start_with_dash(self, lines_in_release_body, self.sheet, tag_name)
- # Read all releases names of a repository
- def read_all_releases_names(self, repository, release_name, sheet):
- is_release_found = False
- for release in repository.get_releases():
- tag_name = (release.tag_name).encode('utf-8')
- name = (release.title).encode('utf-8')
- # Filter releases name with give release name
- if name == release_name:
- is_release_found = True
- ReadRelease.read_requested_release_detials(self, release, tag_name, self.sheet, is_release_found)
- if not is_release_found:
- print "'Release Name' is wrong!, Please try again with correct 'release name'"
- else:
- print "Releases inserted to the Google sheet."
- # Insert a line to google sheet file (those issues which are not fixed)
- def issue_not_fixed(self, sheet, tag_name, line, row):
- sheet.insert_row([tag_name, tag_name, line[7:].encode('utf-8'), 'NO'], row)
- # Insert a line to google sheet file and set fix the Is fixed? column
- def issue_fixed(self, sheet, tag_name, line, row):
- sheet.insert_row([tag_name, tag_name, line[7:].encode('utf-8'), 'YES'], row)
- # Read all lines in ralease body whihc start with '-'
- def read_lines_start_with_dash(self, lines_in_release_body, sheet, tag_name):
- # Counter for updating rows in google sheet file
- row = 2
- for line in lines_in_release_body:
- if (line[:1]).encode('utf-8') == '-':
- if line[1:7].encode('utf-8') == ' [FIX]':
- ReadRelease.issue_fixed(self, sheet, tag_name, line, row)
- elif line[1:7].encode('utf-8') == '------':
- break
- else:
- ReadRelease.issue_not_fixed(self, sheet, tag_name, line, row)
- row += 1
- # Set google sheet api credentials and open file on google sheet
- def set_google_sheet_credentials(self, sheet):
- scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
- google_credentials = ServiceAccountCredentials.from_json_keyfile_name('test.json', scope)
- file = gspread.authorize(google_credentials)
- self.sheet = file.open('Copy_of_MOD_Change_Worklog_Tracker.xlsx').sheet1
- # Add headers to the sheet
- self.sheet.update_acell('A1', 'Client Version')
- self.sheet.update_acell('B1', 'Version Reference for Internal Purposes')
- self.sheet.update_acell('C1', 'Proposed Change (Expected Functional Behavior)')
- self.sheet.update_acell('D1', 'Bug Fix?')
- # URL Builder method for github repository
- def prepare_github_repository_url(self):
- return(str('someuseraccount/'+ self.repository_name))
- # Authunticate user in github
- def github_authunticate(self, username, password, url):
- github_account = Github(self.username, self.password)
- return (github_account.get_repo(url))
- def main():
- read_release_obj = ReadRelease()
- read_release_obj.get_user_credentials()
- url = read_release_obj.prepare_github_repository_url()
- # Check if username and password exist
- if read_release_obj.username and read_release_obj.password:
- try:
- github_repo = read_release_obj.github_authunticate(read_release_obj.username, read_release_obj.password, url)
- read_release_obj.set_google_sheet_credentials(read_release_obj.sheet)
- read_release_obj.read_all_releases_names(github_repo, read_release_obj.release_name, read_release_obj.sheet)
- except BadCredentialsException:
- print ('Bad credentials. Try again!')
- else:
- print ('Github credentials are required!')
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment