# -*- coding: utf-8 -*- """ Created on Fri Aug 2 14:16:27 2024 @author: CHEO001 """ # import base64 import requests import os import pandas as pd from dotenv import load_dotenv load_dotenv() token = os.getenv('GHKEY') headers = {'Authorization': 'token ' + token} # url = 'https://api.github.com/repos/theorchard/terraform-s3' # url = 'https://api.github.com/repos/theorchard/terraform-s3/releases' # url = 'https://raw.githubusercontent.com/theorchard/terraform-static-code-analysis/master/policies/advanced/NonCompliantModules.py' # req = requests.get(url, headers=headers) # if req.status_code == requests.codes.ok: # req = req.json() # the response is a JSON # # req is now a dict with keys: name, encoding, url, size ... # # and content. But it is encoded with base64. # content = base64.decodestring(req['content']) # else: # print('Content was not found.') def getFile(gh_file_raw_url): req = requests.get(gh_file_raw_url, headers=headers) if req.status_code == requests.codes.ok: return req.text else: return None def getMaxVersion(orchard_repo_name): url = f'https://api.github.com/repos/theorchard/{orchard_repo_name}/releases' req = requests.get(url, headers=headers) if req.status_code == requests.codes.ok: maxversion = req.json()[0]['tag_name'] return maxversion else: print(req.status_code) return None def getMinVersion(orchard_repo_name=''): url = 'https://raw.githubusercontent.com/theorchard/terraform-static-code-analysis/master/policies/advanced/NonCompliantModules.py' text = getFile(url) # convert string text into dict d = eval('{'+'{}'.format(text.split('compliant_modules = {')[1].split('}')[0].replace('[','\"').replace(']','\"').strip())+'}') # convert dict to df df = pd.DataFrame(columns=['repo','minver'],data=d.items()) # drops unneeded quotes df['minver'] = df['minver'].str[1:-1] if orchard_repo_name == '': print(df) return df else: if df['repo'].str.contains(orchard_repo_name).any() == True: minver = df[df['repo'] == 'theorchard/'+orchard_repo_name]['minver'].iloc[0] # print(minver) return minver else: print('Invalid repo name, do not prefix org name.') return None