Skip to content

Commit bafd5a7

Browse files
committed
warning on old version
1 parent 0b03d6e commit bafd5a7

2 files changed

Lines changed: 54 additions & 4 deletions

File tree

.github/workflows/test-action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
test-custom-version:
1717
runs-on: ubuntu-latest
1818
env:
19-
TEST_VERSION: '1.19.0'
19+
TEST_VERSION: '1.18.0'
2020
VERSION_OFFSET: 27 # internal version = minor + offset (v1.19 → v0.46)
2121
steps:
2222
- uses: actions/checkout@v4

index.js

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,48 @@ function resolveUrl(version, bin) {
8888
return `${base}/${last}`;
8989
}
9090

91+
// Returns difference of minor versions between 'latest' and 'version'
92+
// subtracts 'version' from 'latest' (returns negative if 'version' is ahead)
93+
// If minor version cannot be extracted on either number - return 0
94+
function versionDiff(latest, version) {
95+
const extractMinor = (v) => {
96+
const match = v.match(/(\d+)\.(\d+)/);
97+
return match ? parseInt(match[2], 10) : null;
98+
};
99+
100+
const latestMinor = extractMinor(latest);
101+
const versionMinor = extractMinor(version);
102+
if (latestMinor === null || versionMinor === null) {
103+
return 0;
104+
}
105+
return latestMinor - versionMinor;
106+
}
107+
108+
// Warn if version requested is more than 2 versions behind latest (>= 3)
109+
// This function does NOT error
110+
async function warnStaleVersion(version) {
111+
const API_LATEST = 'https://api.github.com/repos/knative/func/releases/latest';
112+
const THRESHOLD = 3;
113+
114+
try {
115+
const response = await fetch(API_LATEST);
116+
if (!response.ok) {
117+
throw new Error(`failed to fetch latest version: ${response.status}`);
118+
}
119+
120+
const data = await response.json();
121+
const latest = data.tag_name;
122+
const diff = versionDiff(latest, version);
123+
if (diff >= THRESHOLD) {
124+
core.warning(
125+
`You are using func ${version}, which is ${diff} versions behind the latest (${latest}). Upgrading is recommended.`
126+
);
127+
}
128+
} catch (error) {
129+
core.debug(`Skipping stale version check: ${error.message}`);
130+
}
131+
}
132+
91133
async function run() {
92134
const osBinName = core.getInput('binary') || getOsBinName();
93135
if (osBinName === "unknown") {
@@ -104,17 +146,20 @@ async function run() {
104146
}
105147

106148
let url;
149+
let version = '';
150+
107151
if (binarySourceInput) {
152+
// Custom binarySource - full URL, skip version check
108153
core.info(`Using custom binary source: ${binarySourceInput}`);
109154
url = binarySourceInput;
110155
} else if (versionInput.toLowerCase().trim() === 'latest') {
111-
// use latest version
156+
// Latest version
112157
core.info("Using latest version...");
113158
url = resolveUrl('latest', osBinName);
114159
} else {
115-
// try smart version update
160+
// Specific version
116161
try {
117-
const version = smartVersionUpdate(versionInput);
162+
version = smartVersionUpdate(versionInput);
118163
url = resolveUrl(version, osBinName);
119164
} catch (error) {
120165
core.setFailed(error.message);
@@ -134,6 +179,11 @@ async function run() {
134179
return;
135180
}
136181

182+
// Warn if using an old version (only for specific versions, not binarySource or latest)
183+
if (version) {
184+
await warnStaleVersion(version);
185+
}
186+
137187
await addBinToPath(fullPathBin);
138188
await exec.exec(fullPathBin, ['version']);
139189
}

0 commit comments

Comments
 (0)