|
1 | 1 | const fs = require('fs'); |
2 | 2 | const {SUFFIX_TO_NAME_MAP, RELEASE_ASSET_SUFFIXES_ALL} = require("./constants"); |
| 3 | +const {fetchWithRetries} = require("./fetchWithRetries"); |
3 | 4 |
|
4 | 5 | const ALL_SUFFIXES = Object.values(RELEASE_ASSET_SUFFIXES_ALL); |
5 | 6 | function getAssetType(assetName) { |
@@ -125,29 +126,21 @@ function toStr(obj) { |
125 | 126 | return JSON.stringify(obj, null, 2); |
126 | 127 | } |
127 | 128 |
|
128 | | -function isRunningInGitHubActions() { |
129 | | - return process.env.GITHUB_ACTIONS === 'true'; |
130 | | -} |
131 | | - |
132 | 129 | const DOWNLOAD_HISTORY_URL = "https://public-stats.phcode.io/generated/download_history.json"; |
133 | | -async function getCurrentHistoryData() { |
134 | | - if(isRunningInGitHubActions()){ |
135 | | - // in github actions, there can be times when fetch fails in the automated hourly |
136 | | - // workflows. In that case we should fail early, else it will reset the history |
137 | | - // if we do try catch and return null. We dont want automated workflows resetting |
138 | | - // history. |
139 | | - const fetchedData = await fetch(DOWNLOAD_HISTORY_URL); |
140 | | - return fetchedData.json(); |
141 | | - } |
142 | | - try{ |
143 | | - const fetchedData = await fetch(DOWNLOAD_HISTORY_URL); |
144 | | - return await fetchedData.json(); |
145 | | - } catch (e) { |
146 | | - console.error("No previous data", e); |
| 130 | + |
| 131 | +function validateHistoryPayload(data) { |
| 132 | + if (!data || typeof data !== 'object') return 'response was not an object'; |
| 133 | + if (!data.prodReleaseHistory || typeof data.prodReleaseHistory !== 'object') { |
| 134 | + return 'missing prodReleaseHistory'; |
147 | 135 | } |
| 136 | + if (Object.keys(data.prodReleaseHistory).length === 0) return 'prodReleaseHistory is empty'; |
148 | 137 | return null; |
149 | 138 | } |
150 | 139 |
|
| 140 | +async function getCurrentHistoryData() { |
| 141 | + return fetchWithRetries(DOWNLOAD_HISTORY_URL, validateHistoryPayload, 'download_history'); |
| 142 | +} |
| 143 | + |
151 | 144 | async function updateDownloadHistory(releases) { |
152 | 145 | const existingHistory = await getCurrentHistoryData(); |
153 | 146 | const existingProdReleaseHistory = (existingHistory && existingHistory.prodReleaseHistory) ? |
|
0 commit comments