Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
71 changes: 71 additions & 0 deletions src/services/DockerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {ImageRegistryAdapterFactory} from "../registry-factory/ImageRegistryAdap
import logger from "./LoggerService";
import IgnoreService from "./IgnoreService";
import HomeassistantService from "./HomeassistantService";
import axios, { AxiosInstance } from 'axios';
import {mqttClient} from "../index";

/**
Expand All @@ -14,6 +15,7 @@ export default class DockerService {
public static docker = new Docker();
public static events = new EventEmitter();
public static updatingContainers: string[] = [];
public static SourceUrlCache = new Map<string, string>();

// Start listening to Docker events
public static listenToDockerEvents() {
Expand Down Expand Up @@ -124,6 +126,61 @@ export default class DockerService {
return null;
}

/**
* Gets the source repository for the specified Docker image.
* @param imageName - The name of the Docker image.
* @returns A promise that resolves to the source repository URL.
* @throws An error if the source repository could not be found.
*/
public static async getSourceRepo(imageName: string): Promise<string | null> {
// Check cache first
if (DockerService.SourceUrlCache.has(imageName)) {
return DockerService.SourceUrlCache.get(imageName) ?? null;
}

// Try method 1: Check Docker labels
const labels = await DockerService.getImageInfo(imageName).then(
(info) => info.Config.Labels
).catch((error) => {
logger.error("Error getting image info:", error
);
});

if (labels && labels["org.opencontainers.image.source"]) {
const url = labels["org.opencontainers.image.source"];
DockerService.SourceUrlCache.set(imageName, url);
return url;
}

// Try method 2: Check Docker Hub API
const dockerHubUrl = `https://hub.docker.com/v2/repositories/${imageName}`;
const response = await axios.get(dockerHubUrl).catch((error) => {
if (error.response.status === 404) {
logger.info(`Repository not found: ${imageName}`);
} else {
logger.error("Error accessing Docker Hub API:", error);
}
});

if (response && response.status === 200) {
const data = response.data;
const fullDescription = data.full_description || "";
if (!fullDescription.toLowerCase().includes("[github]")) {
return null;
}

const url = this.parseGithubUrl(fullDescription);

// Cache URL
if (url !== null) {
DockerService.SourceUrlCache.set(imageName, url);
return url;
}
}

return null;
}

/**
* Gets the inspect information for the specified Docker image.
*
Expand Down Expand Up @@ -349,6 +406,20 @@ export default class DockerService {
return containers.some((container) => container.Image.match(imageWithAnyTag));
});
}

/**
* Parses a GitHub URL from a full description.
* @param fullDescription - The full description to parse.
* @returns The GitHub URL or `null` if it could not be parsed.
*/
private static parseGithubUrl(fullDescription: string): string | null {
const startIndex = fullDescription.indexOf("[github");
const endIndex = fullDescription.indexOf("]", startIndex);
if (startIndex !== -1 && endIndex !== -1) {
return fullDescription.slice(startIndex, endIndex).replace("[github]", "");
}
return null;
}
}

// Start listening to Docker events
Expand Down
10 changes: 9 additions & 1 deletion src/services/HomeassistantService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,14 @@ export default class HomeassistantService {

// Update entity payload
const updateTopic = `${config.mqtt.topic}/${formatedImage}/update`;
const sourceRepo = await DockerService.getSourceRepo(image);

if (sourceRepo) {
logger.info(`Found source repository: ${sourceRepo}`);
} else {
logger.warn(`Could not find source repository for ${image}`);
}

let updatePayload: any;
if (haLegacy) {
updatePayload = {
Expand Down Expand Up @@ -457,7 +465,7 @@ export default class HomeassistantService {
installed_version: `${tag}: ${currentDigest?.substring(0, 12)}`,
latest_version: newDigest ? `${tag}: ${newDigest?.substring(0, 12)}` : null,
release_summary: "",
release_url: "https://github.com/MichelFR/MqDockerUp",
release_url: `${sourceRepo ? sourceRepo : "https://github.com/MichelFR/MqDockerUp"}`,
entity_picture: "https://raw.githubusercontent.com/MichelFR/MqDockerUp/refs/heads/main/assets/logo_200x200.png",
title: `${image}:${tag}`,
in_progress: false,
Expand Down