|
| 1 | +const https = require('https'); |
| 2 | + |
| 3 | +const DOCKERHUB_API_URL = 'https://registry.hub.docker.com/v2/repositories/grafana/grafana-dev/tags?page_size=25'; |
| 4 | +const GRAFANA_DEV_TAG_REGEX = /^(\d+\.\d+\.\d+)-(\d+)$/; |
| 5 | +const HTTP_TIMEOUT_MS = 10000; |
| 6 | +const RETRYABLE_ERROR_CODES = ['ECONNRESET', 'ENOTFOUND', 'ECONNREFUSED', 'ETIMEDOUT']; |
| 7 | + |
| 8 | +/** |
| 9 | + * Main entry point |
| 10 | + */ |
| 11 | +module.exports = async ({ core }) => { |
| 12 | + try { |
| 13 | + console.log('Getting latest Grafana dev tag from DockerHub...'); |
| 14 | + |
| 15 | + const latestTag = await getLatestGrafanaDevTag(); |
| 16 | + |
| 17 | + if (!latestTag) { |
| 18 | + core.setFailed('Could not find any Grafana dev tags on DockerHub'); |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + core.info(`Found grafana/grafana-dev:${latestTag}`); |
| 23 | + return latestTag; |
| 24 | + } catch (error) { |
| 25 | + core.setFailed(error.message); |
| 26 | + } |
| 27 | +}; |
| 28 | + |
| 29 | +/** |
| 30 | + * Fetches and returns the latest Grafana dev tag from DockerHub |
| 31 | + * @returns {Promise<string|null>} Latest tag name or null if not found |
| 32 | + */ |
| 33 | +async function getLatestGrafanaDevTag() { |
| 34 | + try { |
| 35 | + console.log('Fetching latest 25 tags from DockerHub...'); |
| 36 | + const response = await httpGet(DOCKERHUB_API_URL); |
| 37 | + |
| 38 | + if (!response?.results?.length) { |
| 39 | + console.log('No tags found'); |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + console.log(`Found ${response.results.length} tags`); |
| 44 | + |
| 45 | + const validTags = response.results |
| 46 | + .map((item) => item.name) |
| 47 | + .map(parseGrafanaDevTag) |
| 48 | + .filter(Boolean) |
| 49 | + .sort((a, b) => b.buildNumber - a.buildNumber); |
| 50 | + |
| 51 | + if (validTags.length === 0) { |
| 52 | + console.log('No valid Grafana dev tags found'); |
| 53 | + return null; |
| 54 | + } |
| 55 | + |
| 56 | + const latestTag = validTags[0]; |
| 57 | + console.log(`Latest tag: ${latestTag.tag} (build ${latestTag.buildNumber}, from ${validTags.length} valid tags)`); |
| 58 | + return latestTag.tag; |
| 59 | + } catch (error) { |
| 60 | + console.log(`Error getting latest tag: ${error.message}`); |
| 61 | + return null; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Parses a Grafana dev tag string and extracts version and build information |
| 67 | + * @param {string} tagName - Tag name to parse (e.g., "12.3.0-17948569556") |
| 68 | + * @returns {Object|null} Parsed tag info or null if invalid |
| 69 | + */ |
| 70 | +function parseGrafanaDevTag(tagName) { |
| 71 | + const match = tagName.match(GRAFANA_DEV_TAG_REGEX); |
| 72 | + if (!match) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + return { |
| 77 | + tag: tagName, |
| 78 | + version: match[1], |
| 79 | + buildNumber: parseInt(match[2], 10), |
| 80 | + }; |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Makes an HTTP GET request with retry logic |
| 85 | + * @param {string} url - URL to fetch |
| 86 | + * @param {number} maxRetries - Maximum number of retry attempts |
| 87 | + * @param {number} retryDelay - Base delay between retries in milliseconds |
| 88 | + * @returns {Promise<Object>} Parsed JSON response |
| 89 | + */ |
| 90 | +function httpGet(url, maxRetries = 10, retryDelay = 2000) { |
| 91 | + return new Promise((resolve, reject) => { |
| 92 | + let attempts = 0; |
| 93 | + let timeoutId = null; |
| 94 | + |
| 95 | + const clearRetryTimeout = () => { |
| 96 | + if (timeoutId) { |
| 97 | + clearTimeout(timeoutId); |
| 98 | + timeoutId = null; |
| 99 | + } |
| 100 | + }; |
| 101 | + |
| 102 | + const scheduleRetry = (error) => { |
| 103 | + if (attempts < maxRetries && isRetryableError(error)) { |
| 104 | + const delay = retryDelay * attempts; |
| 105 | + console.warn(`Retrying ${url} (attempt ${attempts}/${maxRetries}) in ${delay}ms: ${error.message}`); |
| 106 | + timeoutId = setTimeout(makeRequest, delay); |
| 107 | + } else { |
| 108 | + reject(error); |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + const makeRequest = () => { |
| 113 | + attempts++; |
| 114 | + |
| 115 | + const req = https.get(url, { timeout: HTTP_TIMEOUT_MS }, (res) => { |
| 116 | + const chunks = []; |
| 117 | + |
| 118 | + res.on('data', (chunk) => chunks.push(chunk)); |
| 119 | + |
| 120 | + res.on('end', () => { |
| 121 | + clearRetryTimeout(); |
| 122 | + const responseBody = Buffer.concat(chunks).toString(); |
| 123 | + |
| 124 | + if (res.statusCode >= 200 && res.statusCode < 300) { |
| 125 | + try { |
| 126 | + resolve(JSON.parse(responseBody)); |
| 127 | + } catch (parseError) { |
| 128 | + const error = new Error(`Failed to parse JSON from ${url}: ${parseError.message}`); |
| 129 | + error.responseBody = responseBody.substring(0, 500); |
| 130 | + scheduleRetry(error); |
| 131 | + } |
| 132 | + } else if (res.statusCode >= 500) { |
| 133 | + const error = new Error(`Server error ${res.statusCode} from ${url}`); |
| 134 | + error.statusCode = res.statusCode; |
| 135 | + scheduleRetry(error); |
| 136 | + } else { |
| 137 | + const error = new Error(`HTTP ${res.statusCode} error from ${url}`); |
| 138 | + error.statusCode = res.statusCode; |
| 139 | + error.responseBody = responseBody.substring(0, 500); |
| 140 | + reject(error); |
| 141 | + } |
| 142 | + }); |
| 143 | + |
| 144 | + res.on('error', (err) => { |
| 145 | + clearRetryTimeout(); |
| 146 | + scheduleRetry(err); |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + req.on('timeout', () => { |
| 151 | + req.destroy(); |
| 152 | + const error = new Error(`Request timeout for ${url}`); |
| 153 | + error.code = 'ETIMEDOUT'; |
| 154 | + scheduleRetry(error); |
| 155 | + }); |
| 156 | + |
| 157 | + req.on('error', (err) => { |
| 158 | + clearRetryTimeout(); |
| 159 | + scheduleRetry(err); |
| 160 | + }); |
| 161 | + }; |
| 162 | + |
| 163 | + makeRequest(); |
| 164 | + }); |
| 165 | +} |
| 166 | + |
| 167 | +/** |
| 168 | + * Determines if an error is retryable |
| 169 | + * @param {Error} error - Error to check |
| 170 | + * @returns {boolean} True if error is retryable |
| 171 | + */ |
| 172 | +function isRetryableError(error) { |
| 173 | + return RETRYABLE_ERROR_CODES.includes(error.code) || error.statusCode >= 500; |
| 174 | +} |
0 commit comments