Guest User

Untitled

a guest
Jan 30th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. #! /user/bin/python2
  3.  
  4. # Import packages and libraries
  5. from github import Github
  6. from github import BadCredentialsException
  7. import gspread_formatting
  8. import gspread
  9. import oauth2client
  10. from oauth2client.service_account import ServiceAccountCredentials
  11. import getpass
  12.  
  13.  
  14. class ReadRelease:
  15. # Instances vairables
  16. sheet = ''
  17. username = ''
  18. password = ''
  19. repository_name = ''
  20. release_name = ''
  21.  
  22. # Get user credentials from consule
  23. def get_user_credentials (self):
  24. self.username = raw_input("Github username: ")
  25. self.password = getpass.getpass(prompt='Github password: ')
  26. self.repository_name = raw_input("Repository name: ")
  27. self.release_name = raw_input('Release name: ')
  28.  
  29. # Read details of all releases in a repository
  30. def read_requested_release_detials(self, release, tag_name, sheet, is_release_found):
  31. is_release_found = True
  32. release_body = release.body
  33. lines_in_release_body = (release_body.splitlines(0))
  34. ReadRelease.read_lines_start_with_dash(self, lines_in_release_body, self.sheet, tag_name)
  35.  
  36. # Read all releases names of a repository
  37. def read_all_releases_names(self, repository, release_name, sheet):
  38. is_release_found = False
  39. for release in repository.get_releases():
  40. tag_name = (release.tag_name).encode('utf-8')
  41. name = (release.title).encode('utf-8')
  42. # Filter releases name with give release name
  43. if name == release_name:
  44. is_release_found = True
  45. ReadRelease.read_requested_release_detials(self, release, tag_name, self.sheet, is_release_found)
  46. if not is_release_found:
  47. print "'Release Name' is wrong!, Please try again with correct 'release name'"
  48. else:
  49. print "Releases inserted to the Google sheet."
  50.  
  51. # Insert a line to google sheet file (those issues which are not fixed)
  52. def issue_not_fixed(self, sheet, tag_name, line, row):
  53. sheet.insert_row([tag_name, tag_name, line[7:].encode('utf-8'), 'NO'], row)
  54.  
  55. # Insert a line to google sheet file and set fix the Is fixed? column
  56. def issue_fixed(self, sheet, tag_name, line, row):
  57. sheet.insert_row([tag_name, tag_name, line[7:].encode('utf-8'), 'YES'], row)
  58.  
  59. # Read all lines in ralease body whihc start with '-'
  60. def read_lines_start_with_dash(self, lines_in_release_body, sheet, tag_name):
  61. # Counter for updating rows in google sheet file
  62. row = 2
  63. for line in lines_in_release_body:
  64. if (line[:1]).encode('utf-8') == '-':
  65. if line[1:7].encode('utf-8') == ' [FIX]':
  66. ReadRelease.issue_fixed(self, sheet, tag_name, line, row)
  67. elif line[1:7].encode('utf-8') == '------':
  68. break
  69. else:
  70. ReadRelease.issue_not_fixed(self, sheet, tag_name, line, row)
  71. row += 1
  72.  
  73. # Set google sheet api credentials and open file on google sheet
  74. def set_google_sheet_credentials(self, sheet):
  75. scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
  76. google_credentials = ServiceAccountCredentials.from_json_keyfile_name('test.json', scope)
  77. file = gspread.authorize(google_credentials)
  78. self.sheet = file.open('Copy_of_MOD_Change_Worklog_Tracker.xlsx').sheet1
  79. # Add headers to the sheet
  80. self.sheet.update_acell('A1', 'Client Version')
  81. self.sheet.update_acell('B1', 'Version Reference for Internal Purposes')
  82. self.sheet.update_acell('C1', 'Proposed Change (Expected Functional Behavior)')
  83. self.sheet.update_acell('D1', 'Bug Fix?')
  84.  
  85. # URL Builder method for github repository
  86. def prepare_github_repository_url(self):
  87. return(str('someuseraccount/'+ self.repository_name))
  88.  
  89. # Authunticate user in github
  90. def github_authunticate(self, username, password, url):
  91. github_account = Github(self.username, self.password)
  92. return (github_account.get_repo(url))
  93.  
  94. def main():
  95. read_release_obj = ReadRelease()
  96.  
  97. read_release_obj.get_user_credentials()
  98.  
  99. url = read_release_obj.prepare_github_repository_url()
  100.  
  101. # Check if username and password exist
  102. if read_release_obj.username and read_release_obj.password:
  103. try:
  104. github_repo = read_release_obj.github_authunticate(read_release_obj.username, read_release_obj.password, url)
  105.  
  106. read_release_obj.set_google_sheet_credentials(read_release_obj.sheet)
  107.  
  108. read_release_obj.read_all_releases_names(github_repo, read_release_obj.release_name, read_release_obj.sheet)
  109.  
  110. except BadCredentialsException:
  111. print ('Bad credentials. Try again!')
  112.  
  113. else:
  114. print ('Github credentials are required!')
  115.  
  116. if __name__ == "__main__":
  117. main()
Add Comment
Please, Sign In to add comment