Skip to content

Commit

Permalink
updated the script
Browse files Browse the repository at this point in the history
Signed-off-by: Dipankar Das <[email protected]>
  • Loading branch information
dipankardas011 committed Jun 11, 2024
1 parent b89832e commit 0eae819
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/get-latest-releases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ jobs:


- name: load the projects
env:
GH_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}
working-directory: scripts
run: |
python3 repo-variable-writer.py
Expand Down
40 changes: 29 additions & 11 deletions scripts/repo-variable-writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from urllib.error import HTTPError
import os

RepoName: str = "dipankardas011/test-vars"
RepoName: str = "cncf-tags/green-reviews-tooling"


def loadYaml(loc: str):
Expand All @@ -20,7 +20,7 @@ def loadYaml(loc: str):
print(f"Error: An unexpected error occurred: {e}")


def getLatestRelease(org: str, project: str):
def getLatestRelease(org: str, project: str)->str|None:
url = f"https://api.github.com/repos/{org}/{project}/releases/latest"

try:
Expand Down Expand Up @@ -51,7 +51,7 @@ def generateURLAndHeader(proj: str)-> tuple[str, dict[str, str]]:
)


def writeProjectVariable(proj: str, val: str)-> None:
def writeProjectVariable(proj: str, val: str)-> bool:
"""
It writes to the variable name in the repo
${proj}_VERSION
Expand All @@ -63,9 +63,10 @@ def writeProjectVariable(proj: str, val: str)-> None:
variable = proj.lower() + "_version"

req_data = {
"name": f"{variable.upper}",
"name": variable.upper(),
"value": val
}
print(req_data)

url, headers = generateURLAndHeader(proj)

Expand All @@ -74,12 +75,16 @@ def writeProjectVariable(proj: str, val: str)-> None:
_request = urllib.request.Request(url, headers=headers, data=data_bytes, method='PATCH')
try:
with urllib.request.urlopen(_request) as response:
response_body = response.read().decode('utf-8')
data = json.loads(response_body)
print(data)
if response.getcode() >= 200 and response.getcode() < 300:
print(f"Success: updated the variable for {proj} to {val}!")
return True
else:
return False

except HTTPError as e:
error_body = e.read().decode('utf-8')
print("Failed:", e.code, error_body)
return False


def readProjectVariable(proj: str)-> str|None:
Expand Down Expand Up @@ -116,15 +121,28 @@ def iterate_projects(yaml_data):

print(f"Project Name: {projName}")
print(f"Organization: {orgName}")

release = getLatestRelease(orgName, projName)
print(f"Latest Release: {release}")

sub_components = project.get('sub_components', [])
if sub_components:
print("Sub-components:")
for sub_component in sub_components:
print(f" - {sub_component}")

release = getLatestRelease(orgName, projName)
print(f"Latest Release: {release}")
storedRelease = readProjectVariable(projName)
print(f"StoredVersion: {storedRelease}")

if release:
if storedRelease != release:
print("Update of variable needed")
if not writeProjectVariable(proj=projName, val=release):
print("Failed to perform variable updates")
os._exit(1)
else:
print("Already in latest")
else:
print(f"Error invalid release version from github api")

print()
except Exception as e:
print(f"Error: An error occurred while iterating over the projects: {e}")
Expand Down

0 comments on commit 0eae819

Please sign in to comment.