Skip to content

Commit

Permalink
added the reader and writer of variable
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 acec5c6 commit b89832e
Showing 1 changed file with 67 additions and 3 deletions.
70 changes: 67 additions & 3 deletions scripts/repo-variable-writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
import urllib.request
import json
from urllib.error import HTTPError
import os

RepoName: str = "dipankardas011/test-vars"


def loadYaml(loc: str):
try:
Expand All @@ -20,7 +24,6 @@ def getLatestRelease(org: str, project: str):
url = f"https://api.github.com/repos/{org}/{project}/releases/latest"

try:
# Make the request and read the response
with urllib.request.urlopen(url) as response:
response_body = response.read().decode('utf-8')
data = json.loads(response_body)
Expand All @@ -31,7 +34,55 @@ def getLatestRelease(org: str, project: str):
print("Failed:", e.code, error_body)


def readProjectVariable(proj: str)-> str:
def generateURLAndHeader(proj: str)-> tuple[str, dict[str, str]]:

ghToken = os.getenv('GH_TOKEN')
variable = proj.lower() + "_version"
url = f"https://api.github.com/repos/{RepoName}/actions/variables/{variable}"
headers = {
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {ghToken}",
"X-GitHub-Api-Version": "2022-11-28"
}

return (
url,
headers
)


def writeProjectVariable(proj: str, val: str)-> None:
"""
It writes to the variable name in the repo
${proj}_VERSION
For example,
project is falco
then the variable name is `FALCO_VERSION` = "value"
"""
variable = proj.lower() + "_version"

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

url, headers = generateURLAndHeader(proj)

data_bytes = json.dumps(req_data).encode('utf-8')

_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)
except HTTPError as e:
error_body = e.read().decode('utf-8')
print("Failed:", e.code, error_body)


def readProjectVariable(proj: str)-> str|None:
"""
It fetches the variable name in the repo
${proj}_VERSION
Expand All @@ -40,7 +91,20 @@ def readProjectVariable(proj: str)-> str:
project is falco
then the variable name is `FALCO_VERSION`
"""
return ""
url, headers = generateURLAndHeader(proj)

_request = urllib.request.Request(url, headers=headers, method='GET')

try:
# Make the request and read the response
with urllib.request.urlopen(_request) as response:
response_body = response.read().decode('utf-8')
data = json.loads(response_body)
value = data.get("value", None)
return value
except HTTPError as e:
error_body = e.read().decode('utf-8')
print("Failed:", e.code, error_body)


def iterate_projects(yaml_data):
Expand Down

0 comments on commit b89832e

Please sign in to comment.