-
Notifications
You must be signed in to change notification settings - Fork 29
Improve bumpUpstream: semver sort and Docker registry-based version format resolution #475
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
Open
hcastc00
wants to merge
2
commits into
master
Choose a base branch
from
fix/bump-upstream-semver-sort
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,9 @@ | |
| "lint": "eslint . --ext .ts --fix", | ||
| "build": "tsc", | ||
| "prepublish": "npm run build", | ||
| "pre-commit": "npm run lint && npm run test" | ||
| "pre-commit": "npm run lint && npm run test", | ||
| "cli": "node dist/dappnodesdk.js", | ||
| "start": "yarn cli" | ||
|
Comment on lines
+15
to
+17
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what are these changes for? are you sure these are needed? |
||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
|
|
@@ -94,4 +96,4 @@ | |
| "engines": { | ||
| "node": ">=20.0.0" | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
src/commands/githubActions/bumpUpstream/github/resolveVersionFormat.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import fs from "fs"; | ||
| import path from "path"; | ||
| import { Compose } from "@dappnode/types"; | ||
|
|
||
| /** | ||
| * Given a GitHub release tag (e.g. "v1.17.0", "n8n@2.10.3"), resolves the | ||
| * correct version format by checking the upstream Docker image registry. | ||
| * | ||
| * 1. Finds which compose service uses the given build arg | ||
| * 2. Parses the Dockerfile to extract the Docker image that uses that arg | ||
| * 3. Checks the Docker registry for tag existence (with/without prefix) | ||
| * 4. Returns the version in the format that matches the Docker registry | ||
| */ | ||
| export async function resolveVersionFormat({ | ||
| tag, | ||
| arg, | ||
| compose, | ||
| dir | ||
| }: { | ||
| tag: string; | ||
| arg: string; | ||
| compose: Compose; | ||
| dir: string; | ||
| }): Promise<string> { | ||
| const stripped = stripTagPrefix(tag); | ||
| if (!stripped || stripped === tag) return tag; // No prefix to strip | ||
|
|
||
| try { | ||
| const dockerImage = getDockerImageForArg(compose, arg, dir); | ||
| if (!dockerImage) return tag; | ||
|
|
||
| const tagExists = await checkDockerTagExists(dockerImage, stripped); | ||
| if (tagExists) return stripped; | ||
|
|
||
| return tag; | ||
| } catch (e) { | ||
| console.warn(`Could not resolve version format for ${tag}, using as-is:`, e.message); | ||
| return tag; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Finds the Docker image that uses a given build arg by parsing the Dockerfile. | ||
| */ | ||
| function getDockerImageForArg( | ||
| compose: Compose, | ||
| arg: string, | ||
| dir: string | ||
| ): string | null { | ||
| for (const [, service] of Object.entries(compose.services)) { | ||
| if ( | ||
| typeof service.build !== "string" && | ||
| service.build?.args && | ||
| arg in service.build.args | ||
| ) { | ||
| const buildContext = service.build.context || "."; | ||
| const dockerfileName = service.build.dockerfile || "Dockerfile"; | ||
| const dockerfilePath = path.resolve(dir, buildContext, dockerfileName); | ||
|
|
||
| if (!fs.existsSync(dockerfilePath)) continue; | ||
|
|
||
| const content = fs.readFileSync(dockerfilePath, "utf-8"); | ||
| return extractImageForArg(content, arg); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Parses a Dockerfile to find the FROM line that references the given ARG, | ||
| * and extracts the Docker image name (without the tag). | ||
| * | ||
| * Handles patterns like: | ||
| * FROM ethereum/client-go:${UPSTREAM_VERSION} | ||
| * FROM ethereum/client-go:v${UPSTREAM_VERSION} | ||
| * FROM ollama/ollama:${OLLAMA_VERSION#v} | ||
| * FROM statusim/nimbus-eth2:multiarch-${UPSTREAM_VERSION} | ||
| */ | ||
| function extractImageForArg( | ||
| dockerfileContent: string, | ||
| arg: string | ||
| ): string | null { | ||
| const lines = dockerfileContent.split("\n"); | ||
|
|
||
| for (const line of lines) { | ||
| const trimmed = line.trim(); | ||
| if (!trimmed.startsWith("FROM") || !trimmed.includes(arg)) continue; | ||
|
|
||
| // Match: FROM image:tag_pattern (with optional "AS stage") | ||
| const match = trimmed.match(/^FROM\s+([^:\s]+)/i); | ||
| if (match) return match[1]; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a tag exists on a Docker registry using the Docker Hub v2 API. | ||
| * Supports Docker Hub, ghcr.io, and gcr.io. | ||
| */ | ||
| async function checkDockerTagExists( | ||
| image: string, | ||
| tag: string | ||
| ): Promise<boolean> { | ||
| const url = getRegistryTagUrl(image, tag); | ||
| if (!url) return false; | ||
|
|
||
| try { | ||
| const response = await fetch(url); | ||
| return response.ok; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| function getRegistryTagUrl(image: string, tag: string): string | null { | ||
| // ghcr.io/org/image -> GitHub Container Registry | ||
| if (image.startsWith("ghcr.io/")) { | ||
| const imagePath = image.replace("ghcr.io/", ""); | ||
| return `https://ghcr.io/v2/${imagePath}/manifests/${tag}`; | ||
| } | ||
|
|
||
| // gcr.io/project/image -> Google Container Registry | ||
| if (image.startsWith("gcr.io/")) { | ||
| const imagePath = image.replace("gcr.io/", ""); | ||
| return `https://gcr.io/v2/${imagePath}/manifests/${tag}`; | ||
| } | ||
|
|
||
| // Docker Hub: library/image or org/image | ||
| const dockerImage = image.includes("/") ? image : `library/${image}`; | ||
| return `https://registry.hub.docker.com/v2/repositories/${dockerImage}/tags/${tag}`; | ||
| } | ||
|
|
||
| function stripTagPrefix(tag: string): string | null { | ||
| const match = tag.match(/(\d+\.\d+\.\d+.*)$/); | ||
| return match ? match[1] : null; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont think this should go into this PR