Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: update go mod dependencies #14

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions .github/scripts/prepare-matrices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#!/usr/bin/env python3
import importlib.util
import sys
import os

import json
import yaml

from subprocess import check_output

from os.path import isfile

repo_owner = os.environ.get('REPO_OWNER', os.environ.get('GITHUB_REPOSITORY_OWNER'))

TESTABLE_PLATFORMS = ["linux/amd64"]

def load_metadata_file_yaml(file_path):
with open(file_path, "r") as f:
return yaml.safe_load(f)

def load_metadata_file_json(file_path):
with open(file_path, "r") as f:
return json.load(f)

def get_app_metadata(subdir, meta, forRelease=False, channels=None):
appsToBuild = {
"apps": [],
"appsPlatforms": []
}

if channels is None:
channels = meta["channels"]
else:
channels = [channel for channel in meta["channels"] if channel["name"] in channels]


for channel in channels:

# App Name
toBuild = {}
toBuild["name"] = meta["app"]
toBuild["channel"] = channel["name"]
toBuild["update_modules_enabled"] = channel["update_modules"]["enabled"]
toBuild["container_tag_name"] = channel["container_tag_name"]

toBuild["repository"] = meta["repository"]
toBuild["path"] = meta["path"]
toBuild["branch"] = channel["branch"]
toBuild["update_modules"] = channel["update_modules"]
toBuild["publish_artifacts"] = meta["publish_artifacts"]

# Container Tags
toBuild["tags"] = [channel]

# Platform Metadata
for platform in channel["platforms"]:

toBuild.setdefault("platforms", []).append(platform)

target_os = platform.split("/")[0]
target_arch = platform.split("/")[1]

platformToBuild = {}
platformToBuild["name"] = toBuild["name"]
platformToBuild["repository"] = toBuild["repository"]
platformToBuild["path"] = toBuild["path"]
platformToBuild["branch"] = toBuild["branch"]
if meta.get("fetch_full_history"):
platformToBuild["fetch_full_history"] = meta["fetch_full_history"]
platformToBuild["platform"] = platform
platformToBuild["target_os"] = target_os
platformToBuild["target_arch"] = target_arch
platformToBuild["channel"] = channel["name"]
platformToBuild["label_type"]="org.opencontainers.image"

if isfile(os.path.join(subdir, channel["name"], "Dockerfile")):
platformToBuild["dockerfile"] = os.path.join(subdir, channel["name"], "Dockerfile")
platformToBuild["context"] = os.path.join(subdir, channel["name"])
platformToBuild["goss_config"] = os.path.join(subdir, channel["name"], "goss.yaml")
else:
platformToBuild["dockerfile"] = os.path.join(subdir, "Dockerfile")
platformToBuild["context"] = subdir
platformToBuild["goss_config"] = os.path.join(subdir, "ci", "goss.yaml")

platformToBuild["goss_args"] = ""

platformToBuild["tests_enabled"] = channel["tests_enabled"] and platform in TESTABLE_PLATFORMS

platformToBuild["publish_artifacts"] = meta["publish_artifacts"]
if meta.get("binary_name"):
platformToBuild["binary_name"] = meta["binary_name"]

platformToBuild["update_modules_enabled"] = channel["update_modules"]["enabled"]
if channel["update_modules"]["enabled"]:
platformToBuild["update_modules_branch"] = channel["update_modules"]["cosmossdk_branch"]

appsToBuild["appsPlatforms"].append(platformToBuild)
appsToBuild["apps"].append(toBuild)
return appsToBuild

if __name__ == "__main__":
apps = sys.argv[1]
forRelease = sys.argv[2] == "true"
appsToBuild = {
"apps": [],
"appsPlatforms": []
}

if apps != "all":
channels=None
apps = apps.split(",")
if len(sys.argv) == 4:
channels = sys.argv[3].split(",")

for app in apps:
if not os.path.exists(os.path.join("./apps", app)):
print(f"App \"{app}\" not found")
exit(1)

meta = None
if os.path.isfile(os.path.join("./apps", app, "metadata.yaml")):
meta = load_metadata_file_yaml(os.path.join("./apps", app, "metadata.yaml"))
elif os.path.isfile(os.path.join("./apps", app, "metadata.json")):
meta = load_metadata_file_json(os.path.join("./apps", app, "metadata.json"))

appToBuild = get_app_metadata(os.path.join("./apps", app), meta, forRelease, channels=channels)
if appToBuild is not None:
appsToBuild["apps"].extend(appToBuild["apps"])
appsToBuild["appsPlatforms"].extend(appToBuild["appsPlatforms"])
else:
for subdir, dirs, files in os.walk("./apps"):
for file in files:
meta = None
if file == "metadata.yaml":
meta = load_metadata_file_yaml(os.path.join(subdir, file))
elif file == "metadata.json":
meta = load_metadata_file_json(os.path.join(subdir, file))
else:
continue
if meta is not None:
appToBuild = get_app_metadata(subdir, meta, forRelease)
if appToBuild is not None:
appsToBuild["apps"].extend(appToBuild["apps"])
appsToBuild["appsPlatforms"].extend(appToBuild["appsPlatforms"])
print(json.dumps(appsToBuild))
1 change: 1 addition & 0 deletions .github/scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pyyaml
187 changes: 187 additions & 0 deletions .github/scripts/update-go-modules.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
#!/bin/bash

set -oue pipefail

## FUNCTIONS

# Function to get the latest commit SHA for a given repo and branch
get_latest_commit() {
local repo=$1
local branch=$2
output=$(git ls-remote "https://github.com/${repo}.git" "${branch}" | cut -f1)

if [ -z "$output" ]; then
echo "Error: Failed to retrieve commit hash from ${repo}/${branch}" >&2
exit 1
else
echo "$output"
fi
}

# Function to get pseudo-version from commit SHA
get_pseudo_version() {
local repo=$1
local commit_sha=$2

pseudo_version=$(go list -m -f '{{.Version}}' $repo@$commit_sha 2>/dev/null)

if [ -z "$pseudo_version" ]; then
echo "Error: Unable to find pseudo-version for $repo@$commit_sha"
return 1
else
echo "${pseudo_version}"
fi
}

replace_module() {
local commit=$1
pseudo_version=$(get_pseudo_version "$module" "$commit")

if [ $? -ne 0 ]; then
echo "Error occurred while getting pseudo-version for $module@$commit" >&2
exit 1
fi

echo "Updating $module to pseudo-version $pseudo_version"

go mod edit -replace=$module=$module@$pseudo_version

return 0
}

get_module() {
local commit=$1

echo "Get $module to commit $commit"

go get $module@$commit

return 0
}

# Function to check if current module is replaced by a local path
check_replaced_local() {
go mod edit --json | jq -e --arg v "$module" '
(.Replace[] | select(.Old.Path | contains($v))) as $replacement
| if $replacement.New.Path | contains("../") then
0
else
error("No ../ found in the New.Path")
end
' > /dev/null 2>&1

if [ $? -ne 0 ]; then
return 1
else
return 0
fi
}

## VARIABLES

# github.com/cosmos/cosmos-sdk branch to follow
COSMOSSDK_BRANCH=${COSMOSSDK_BRANCH}
# github.com/cometbft/cometbft branch to follow
COMETBFT_BRANCH=${COMETBFT_BRANCH:-main}

## INTERNAL VARIABLES

### Commits
cosmossdk_latest_commit_main=$(get_latest_commit "cosmos/cosmos-sdk" "main")
echo "cosmos/cosmos-sdk main latest_commit: $cosmossdk_latest_commit_main"
cosmossdk_latest_commit_branch=$(get_latest_commit "cosmos/cosmos-sdk" "$COSMOSSDK_BRANCH")
echo "cosmos/cosmos-sdk $COSMOSSDK_BRANCH latest_commit: $cosmossdk_latest_commit_branch"
cometbft_latest_commit_branch=$(get_latest_commit "cometbft/cometbft" "$COMETBFT_BRANCH")
echo "cometbft/cometbft $COMETBFT_BRANCH latest_commit: $cometbft_latest_commit_branch"

### go.mod : Extract module paths and versions from current folder
modules=$(go mod edit --json | jq -r '.Require[] | select(.Path | contains("/")) | .Path')

# Parse every module in go.mod, and update dependencies according to logic
for module in $modules; do

echo "module: $module"

# Checking if module is not already replaced with local path
if ! check_replaced_local; then

# Checking cosmos-sdk modules
case "$module" in
*cosmossdk.io*)

if [[ "$COSMOSSDK_BRANCH" =~ "v0.50.x" ]]; then
case "$module" in
# Force checking specific modules to HEAD of main instead of release
*core/testing*|*depinject*|*log*|*math*|*schema*)
if ! replace_module "$cosmossdk_latest_commit_main"; then
echo "Failed to update module $module after trying main."
exit 1
fi
;;
*api*|*core*|*collections*|*errors*)
echo "ignore $module"
;;
*)
if ! replace_module "$cosmossdk_latest_commit_branch"; then
echo "Failed to update module $module after trying $COSMOSSDK_BRANCH."
fi
;;
esac
elif [[ "$COSMOSSDK_BRANCH" =~ "v0.52.x" ]]; then
case "$module" in
# Force checking specific modules to HEAD of refs/heads/release/v0.52.x instead of main
*client/v2*|*x/*)
# Exception for x/tx
if [[ "$module" =~ "x/tx" ]]; then
if ! replace_module "$cosmossdk_latest_commit_main"; then
echo "Failed to update module $module after trying main."
exit 1
fi
elif ! replace_module "$cometbft_latest_commit_branch"; then
echo "Failed to update module $module after trying main."
exit 1
if ! replace_module "$cosmossdk_latest_commit_branch"; then
echo "Failed to update module $module after trying $COSMOSSDK_BRANCH."
fi
fi
;;
*errors*)
echo "ignore $module"
;;
*)
if ! replace_module "$cosmossdk_latest_commit_main"; then
echo "Faiif ! replace_module "$cometbft_latest_commit_branch"; then
echo "Failed to update module $module after trying main."
exit 1led to update module $module after trying main."
fi
;;
esac
fi
;;
*github.com/cosmos/cosmos-sdk*)

# modules that need to follow HEAD on release branch
if ! replace_module "$cosmossdk_latest_commit_branch"; then
echo "Failed to update module $module after trying $COSMOSSDK_BRANCH."
exit 1
fi
;;
*github.com/cometbft/cometbft*)
# Do not execute on cosmos-sdk release branches
if [[ "$COSMOSSDK_BRANCH" == "main" ]]; then
# Exclude cometbft/cometbft-db from logic
if [[ ! "$module" =~ "cometbft-db" ]]; then
if ! get_module "$cometbft_latest_commit_branch"; then
echo "Failed to update module $module after trying main."
exit 1
fi
fi
fi
esac
else
echo "module $module is already replaced by local path"
fi
done

go mod verify
go mod tidy
Loading
Loading