Skip to content

Commit

Permalink
fix(docker): Check if owner is an organization or user
Browse files Browse the repository at this point in the history
GitHub API endpoints differ for organisations and users, and before
check_image script assuming organisations only.
The side effect is individual users using Ort Docker workflow having
all layers rebuilt on every commit that triggers Docker builds.

Signed-off-by: Helio Chissini de Castro <[email protected]>
  • Loading branch information
heliocastro committed Feb 18, 2024
1 parent 563459b commit a971fe8
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions .github/actions/ortdocker/check_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
import sys

import requests
from requests import Response

""" Use current GitHub API to check if a container image with the
given name and version exists.
"""

token = os.getenv("INPUT_TOKEN")
org = os.getenv("GITHUB_REPOSITORY_OWNER")
owner = os.getenv("GITHUB_REPOSITORY_OWNER")
name = os.getenv("INPUT_NAME")
base_version = os.getenv("INPUT_VERSION")
build_args = os.getenv("BUILD_ARGS")
Expand All @@ -41,19 +42,31 @@
print(version)
sys.exit(0)

url = f"https://api.github.com/orgs/{org}/packages/container/ort%2F{name}/versions"

headers = {
headers: dict[str, str] = {
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {token}",
"X-GitHub-Api-Version": "2022-11-28",
}

url: str = f"https://api.github.com/users/{owner}"
response = requests.get(url, headers=headers)
data: Response = response.json()

if data.get("type") == "Organization":
url = (
f"https://api.github.com/orgs/{owner}/packages/container/ort%2F{name}/versions"
)
else:
url = f"https://api.github.com/user/packages/container/ort%2F{name}/versions"

response = requests.get(url, headers=headers)
if response.status_code == 404:
print("none")
else:
data = response.json()
versions = [
item
for sublist in [v["metadata"]["container"]["tags"] for v in response.json()]
for sublist in [v["metadata"]["container"]["tags"] for v in data]
if sublist
for item in sublist
]
Expand Down

0 comments on commit a971fe8

Please sign in to comment.