GeezerVison is showing github.com — Implement pagination with link headers for Adoptium based apis (#1014) · actions/setup-java@43120bc
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: __tests__/distributors/adopt-installer.test.ts +34-6Lines changed: 34 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,6 +14,7 @@ import * as core from '@actions/core'; | 14 | 14 | describe('getAvailableVersions', () => { | 15 | 15 | let spyHttpClient: jest.SpyInstance; | 16 | 16 | let spyCoreError: jest.SpyInstance; | | 17 | + let spyCoreWarning: jest.SpyInstance; | 17 | 18 |
| 18 | 19 | beforeEach(() => { | 19 | 20 | spyHttpClient = jest.spyOn(HttpClient.prototype, 'getJson'); | @@ -26,6 +27,8 @@ describe('getAvailableVersions', () => { | 26 | 27 | // Mock core.error to suppress error logs | 27 | 28 | spyCoreError = jest.spyOn(core, 'error'); | 28 | 29 | spyCoreError.mockImplementation(() => {}); | | 30 | + spyCoreWarning = jest.spyOn(core, 'warning'); | | 31 | + spyCoreWarning.mockImplementation(() => {}); | 29 | 32 | }); | 30 | 33 |
| 31 | 34 | afterEach(() => { | @@ -136,22 +139,19 @@ describe('getAvailableVersions', () => { | 136 | 139 | ); | 137 | 140 |
| 138 | 141 | it('load available versions', async () => { | | 142 | + const nextPageUrl = | | 143 | + 'https://api.adoptopenjdk.net/v3/assets/version/%5B1.0,100.0%5D?page=1&page_size=20'; | 139 | 144 | spyHttpClient = jest.spyOn(HttpClient.prototype, 'getJson'); | 140 | 145 | spyHttpClient | 141 | 146 | .mockReturnValueOnce({ | 142 | 147 | statusCode: 200, | 143 | | - headers: {}, | | 148 | + headers: {link: `<${nextPageUrl}>; rel="next"`}, | 144 | 149 | result: manifestData as any | 145 | 150 | }) | 146 | 151 | .mockReturnValueOnce({ | 147 | 152 | statusCode: 200, | 148 | 153 | headers: {}, | 149 | 154 | result: manifestData as any | 150 | | - }) | 151 | | - .mockReturnValueOnce({ | 152 | | - statusCode: 200, | 153 | | - headers: {}, | 154 | | - result: [] | 155 | 155 | }); | 156 | 156 |
| 157 | 157 | const distribution = new AdoptDistribution( | @@ -166,6 +166,34 @@ describe('getAvailableVersions', () => { | 166 | 166 | const availableVersions = await distribution['getAvailableVersions'](); | 167 | 167 | expect(availableVersions).not.toBeNull(); | 168 | 168 | expect(availableVersions.length).toBe(manifestData.length * 2); | | 169 | + expect(spyHttpClient).toHaveBeenNthCalledWith(2, nextPageUrl); | | 170 | + }); | | 171 | + | | 172 | + it('stops pagination after 1000 pages as a safeguard', async () => { | | 173 | + const nextPageUrl = | | 174 | + 'https://api.adoptopenjdk.net/v3/assets/version/%5B1.0,100.0%5D?page=2&page_size=20'; | | 175 | + spyHttpClient.mockReturnValue({ | | 176 | + statusCode: 200, | | 177 | + headers: {link: `<${nextPageUrl}>; rel="next"`}, | | 178 | + result: [{version_data: {semver: '17.0.1'}, binaries: []}] as any | | 179 | + }); | | 180 | + | | 181 | + const distribution = new AdoptDistribution( | | 182 | + { | | 183 | + version: '11', | | 184 | + architecture: 'x64', | | 185 | + packageType: 'jdk', | | 186 | + checkLatest: false | | 187 | + }, | | 188 | + AdoptImplementation.Hotspot | | 189 | + ); | | 190 | + | | 191 | + await distribution['getAvailableVersions'](); | | 192 | + | | 193 | + expect(spyHttpClient).toHaveBeenCalledTimes(1000); | | 194 | + expect(spyCoreWarning).toHaveBeenCalledWith( | | 195 | + expect.stringContaining('Reached pagination safeguard limit (1000 pages)') | | 196 | + ); | 169 | 197 | }); | 170 | 198 |
| 171 | 199 | it.each([ | |
Collapse file Copy file name to clipboardExpand all lines: __tests__/distributors/semeru-installer.test.ts +31-6Lines changed: 31 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -9,6 +9,7 @@ import * as core from '@actions/core'; | 9 | 9 | describe('getAvailableVersions', () => { | 10 | 10 | let spyHttpClient: jest.SpyInstance; | 11 | 11 | let spyCoreError: jest.SpyInstance; | | 12 | + let spyCoreWarning: jest.SpyInstance; | 12 | 13 |
| 13 | 14 | beforeEach(() => { | 14 | 15 | spyHttpClient = jest.spyOn(HttpClient.prototype, 'getJson'); | @@ -20,6 +21,8 @@ describe('getAvailableVersions', () => { | 20 | 21 | // Mock core.error to suppress error logs | 21 | 22 | spyCoreError = jest.spyOn(core, 'error'); | 22 | 23 | spyCoreError.mockImplementation(() => {}); | | 24 | + spyCoreWarning = jest.spyOn(core, 'warning'); | | 25 | + spyCoreWarning.mockImplementation(() => {}); | 23 | 26 | }); | 24 | 27 |
| 25 | 28 | afterEach(() => { | @@ -82,22 +85,19 @@ describe('getAvailableVersions', () => { | 82 | 85 | ); | 83 | 86 |
| 84 | 87 | it('load available versions', async () => { | | 88 | + const nextPageUrl = | | 89 | + 'https://api.adoptopenjdk.net/v3/assets/version/%5B1.0,100.0%5D?page=1&page_size=20'; | 85 | 90 | spyHttpClient = jest.spyOn(HttpClient.prototype, 'getJson'); | 86 | 91 | spyHttpClient | 87 | 92 | .mockReturnValueOnce({ | 88 | 93 | statusCode: 200, | 89 | | - headers: {}, | | 94 | + headers: {link: `<${nextPageUrl}>; rel="next"`}, | 90 | 95 | result: manifestData as any | 91 | 96 | }) | 92 | 97 | .mockReturnValueOnce({ | 93 | 98 | statusCode: 200, | 94 | 99 | headers: {}, | 95 | 100 | result: manifestData as any | 96 | | - }) | 97 | | - .mockReturnValueOnce({ | 98 | | - statusCode: 200, | 99 | | - headers: {}, | 100 | | - result: [] | 101 | 101 | }); | 102 | 102 |
| 103 | 103 | const distribution = new SemeruDistribution({ | @@ -109,6 +109,31 @@ describe('getAvailableVersions', () => { | 109 | 109 | const availableVersions = await distribution['getAvailableVersions'](); | 110 | 110 | expect(availableVersions).not.toBeNull(); | 111 | 111 | expect(availableVersions.length).toBe(manifestData.length * 2); | | 112 | + expect(spyHttpClient).toHaveBeenNthCalledWith(2, nextPageUrl); | | 113 | + }); | | 114 | + | | 115 | + it('stops pagination after 1000 pages as a safeguard', async () => { | | 116 | + const nextPageUrl = | | 117 | + 'https://api.adoptopenjdk.net/v3/assets/version/%5B1.0,100.0%5D?page=2&page_size=20'; | | 118 | + spyHttpClient.mockReturnValue({ | | 119 | + statusCode: 200, | | 120 | + headers: {link: `<${nextPageUrl}>; rel="next"`}, | | 121 | + result: [{version_data: {semver: '17.0.1'}, binaries: []}] as any | | 122 | + }); | | 123 | + | | 124 | + const distribution = new SemeruDistribution({ | | 125 | + version: '8', | | 126 | + architecture: 'x64', | | 127 | + packageType: 'jdk', | | 128 | + checkLatest: false | | 129 | + }); | | 130 | + | | 131 | + await distribution['getAvailableVersions'](); | | 132 | + | | 133 | + expect(spyHttpClient).toHaveBeenCalledTimes(1000); | | 134 | + expect(spyCoreWarning).toHaveBeenCalledWith( | | 135 | + expect.stringContaining('Reached pagination safeguard limit (1000 pages)') | | 136 | + ); | 112 | 137 | }); | 113 | 138 |
| 114 | 139 | it.each([ | |
Collapse file Copy file name to clipboardExpand all lines: __tests__/distributors/temurin-installer.test.ts +34-6Lines changed: 34 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,6 +12,7 @@ import * as core from '@actions/core'; | 12 | 12 | describe('getAvailableVersions', () => { | 13 | 13 | let spyHttpClient: jest.SpyInstance; | 14 | 14 | let spyCoreError: jest.SpyInstance; | | 15 | + let spyCoreWarning: jest.SpyInstance; | 15 | 16 |
| 16 | 17 | beforeEach(() => { | 17 | 18 | spyHttpClient = jest.spyOn(HttpClient.prototype, 'getJson'); | @@ -23,6 +24,8 @@ describe('getAvailableVersions', () => { | 23 | 24 | // Mock core.error to suppress error logs | 24 | 25 | spyCoreError = jest.spyOn(core, 'error'); | 25 | 26 | spyCoreError.mockImplementation(() => {}); | | 27 | + spyCoreWarning = jest.spyOn(core, 'warning'); | | 28 | + spyCoreWarning.mockImplementation(() => {}); | 26 | 29 | }); | 27 | 30 |
| 28 | 31 | afterEach(() => { | @@ -93,22 +96,19 @@ describe('getAvailableVersions', () => { | 93 | 96 | ); | 94 | 97 |
| 95 | 98 | it('load available versions', async () => { | | 99 | + const nextPageUrl = | | 100 | + 'https://api.adoptium.net/v3/assets/version/%5B1.0,100.0%5D?page=1&page_size=20'; | 96 | 101 | spyHttpClient = jest.spyOn(HttpClient.prototype, 'getJson'); | 97 | 102 | spyHttpClient | 98 | 103 | .mockReturnValueOnce({ | 99 | 104 | statusCode: 200, | 100 | | - headers: {}, | | 105 | + headers: {link: `<${nextPageUrl}>; rel="next"`}, | 101 | 106 | result: manifestData as any | 102 | 107 | }) | 103 | 108 | .mockReturnValueOnce({ | 104 | 109 | statusCode: 200, | 105 | 110 | headers: {}, | 106 | 111 | result: manifestData as any | 107 | | - }) | 108 | | - .mockReturnValueOnce({ | 109 | | - statusCode: 200, | 110 | | - headers: {}, | 111 | | - result: [] | 112 | 112 | }); | 113 | 113 |
| 114 | 114 | const distribution = new TemurinDistribution( | @@ -123,6 +123,34 @@ describe('getAvailableVersions', () => { | 123 | 123 | const availableVersions = await distribution['getAvailableVersions'](); | 124 | 124 | expect(availableVersions).not.toBeNull(); | 125 | 125 | expect(availableVersions.length).toBe(manifestData.length * 2); | | 126 | + expect(spyHttpClient).toHaveBeenNthCalledWith(2, nextPageUrl); | | 127 | + }); | | 128 | + | | 129 | + it('stops pagination after 1000 pages as a safeguard', async () => { | | 130 | + const nextPageUrl = | | 131 | + 'https://api.adoptium.net/v3/assets/version/%5B1.0,100.0%5D?page=2&page_size=20'; | | 132 | + spyHttpClient.mockReturnValue({ | | 133 | + statusCode: 200, | | 134 | + headers: {link: `<${nextPageUrl}>; rel="next"`}, | | 135 | + result: [{version_data: {semver: '17.0.1'}, binaries: []}] as any | | 136 | + }); | | 137 | + | | 138 | + const distribution = new TemurinDistribution( | | 139 | + { | | 140 | + version: '8', | | 141 | + architecture: 'x64', | | 142 | + packageType: 'jdk', | | 143 | + checkLatest: false | | 144 | + }, | | 145 | + TemurinImplementation.Hotspot | | 146 | + ); | | 147 | + | | 148 | + await distribution['getAvailableVersions'](); | | 149 | + | | 150 | + expect(spyHttpClient).toHaveBeenCalledTimes(1000); | | 151 | + expect(spyCoreWarning).toHaveBeenCalledWith( | | 152 | + expect.stringContaining('Reached pagination safeguard limit (1000 pages)') | | 153 | + ); | 126 | 154 | }); | 127 | 155 |
| 128 | 156 | it.each([ | |
Collapse file Copy file name to clipboardExpand all lines: __tests__/util.test.ts +75-1Lines changed: 75 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,10 +4,12 @@ import * as fs from 'fs'; | 4 | 4 | import * as path from 'path'; | 5 | 5 | import { | 6 | 6 | convertVersionToSemver, | | 7 | + getNextPageUrlFromLinkHeader, | 7 | 8 | getVersionFromFileContent, | 8 | 9 | isVersionSatisfies, | 9 | 10 | isCacheFeatureAvailable, | 10 | | - isGhes | | 11 | + isGhes, | | 12 | + validatePaginationUrl | 11 | 13 | } from '../src/util'; | 12 | 14 |
| 13 | 15 | jest.mock('@actions/cache'); | @@ -85,6 +87,78 @@ describe('convertVersionToSemver', () => { | 85 | 87 | }); | 86 | 88 | }); | 87 | 89 |
| | 90 | +describe('getNextPageUrlFromLinkHeader', () => { | | 91 | + it.each([ | | 92 | + [ | | 93 | + { | | 94 | + link: '<https://api.adoptium.net/v3/info/release_versions?page=1&page_size=10>; rel="next"' | | 95 | + }, | | 96 | + 'https://api.adoptium.net/v3/info/release_versions?page=1&page_size=10' | | 97 | + ], | | 98 | + [ | | 99 | + { | | 100 | + Link: '<https://example.com/last?page=5>; rel="last", <https://example.com/next?page=2>; rel="next"' | | 101 | + }, | | 102 | + 'https://example.com/next?page=2' | | 103 | + ], | | 104 | + [ | | 105 | + { | | 106 | + link: '<https://api.adoptium.net/v3/versions?page=3>; type="application/json"; rel="next"' | | 107 | + }, | | 108 | + 'https://api.adoptium.net/v3/versions?page=3' | | 109 | + ], | | 110 | + [{link: '<https://example.com/last?page=5>; rel="last"'}, null], | | 111 | + [{link: '<https://example.com/page?p=2>; rel="nextsomething"'}, null], | | 112 | + [undefined, null] | | 113 | + ])('returns %s -> %s', (headers, expected) => { | | 114 | + expect(getNextPageUrlFromLinkHeader(headers)).toBe(expected); | | 115 | + }); | | 116 | +}); | | 117 | + | | 118 | +describe('validatePaginationUrl', () => { | | 119 | + it('accepts URL with matching origin', () => { | | 120 | + expect( | | 121 | + validatePaginationUrl( | | 122 | + 'https://api.adoptium.net/v3/assets?page=2', | | 123 | + 'https://api.adoptium.net' | | 124 | + ) | | 125 | + ).toBe(true); | | 126 | + }); | | 127 | + | | 128 | + it('rejects URL with different host', () => { | | 129 | + expect( | | 130 | + validatePaginationUrl( | | 131 | + 'https://evil.example.com/steal?data=1', | | 132 | + 'https://api.adoptium.net' | | 133 | + ) | | 134 | + ).toBe(false); | | 135 | + }); | | 136 | + | | 137 | + it('rejects URL with different protocol', () => { | | 138 | + expect( | | 139 | + validatePaginationUrl( | | 140 | + 'http://api.adoptium.net/v3/assets?page=2', | | 141 | + 'https://api.adoptium.net' | | 142 | + ) | | 143 | + ).toBe(false); | | 144 | + }); | | 145 | + | | 146 | + it('returns false for invalid URL', () => { | | 147 | + expect(validatePaginationUrl('not-a-url', 'https://api.adoptium.net')).toBe( | | 148 | + false | | 149 | + ); | | 150 | + }); | | 151 | + | | 152 | + it('accepts URL with explicit default port', () => { | | 153 | + expect( | | 154 | + validatePaginationUrl( | | 155 | + 'https://api.adoptium.net:443/v3/assets?page=2', | | 156 | + 'https://api.adoptium.net' | | 157 | + ) | | 158 | + ).toBe(true); | | 159 | + }); | | 160 | +}); | | 161 | + | 88 | 162 | describe('getVersionFromFileContent', () => { | 89 | 163 | describe('.sdkmanrc', () => { | 90 | 164 | it.each([ | |
Collapse file Copy file name to clipboardExpand all lines: dist/cleanup/index.js +42-1Lines changed: 42 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -52134,7 +52134,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { | 52134 | 52134 | return (mod && mod.__esModule) ? mod : { "default": mod }; | 52135 | 52135 | }; | 52136 | 52136 | Object.defineProperty(exports, "__esModule", ({ value: true })); | 52137 | | -exports.renameWinArchive = exports.getGitHubHttpHeaders = exports.convertVersionToSemver = exports.getVersionFromFileContent = exports.isCacheFeatureAvailable = exports.isGhes = exports.isJobStatusSuccess = exports.getToolcachePath = exports.isVersionSatisfies = exports.getDownloadArchiveExtension = exports.extractJdkFile = exports.getVersionFromToolcachePath = exports.getBooleanInput = exports.getTempDir = void 0; | | 52137 | +exports.renameWinArchive = exports.validatePaginationUrl = exports.getNextPageUrlFromLinkHeader = exports.MAX_PAGINATION_PAGES = exports.getGitHubHttpHeaders = exports.convertVersionToSemver = exports.getVersionFromFileContent = exports.isCacheFeatureAvailable = exports.isGhes = exports.isJobStatusSuccess = exports.getToolcachePath = exports.isVersionSatisfies = exports.getDownloadArchiveExtension = exports.extractJdkFile = exports.getVersionFromToolcachePath = exports.getBooleanInput = exports.getTempDir = void 0; | 52138 | 52138 | const os_1 = __importDefault(__nccwpck_require__(22037)); | 52139 | 52139 | const path_1 = __importDefault(__nccwpck_require__(71017)); | 52140 | 52140 | const fs = __importStar(__nccwpck_require__(57147)); | @@ -52301,6 +52301,47 @@ function getGitHubHttpHeaders() { | 52301 | 52301 | return headers; | 52302 | 52302 | } | 52303 | 52303 | exports.getGitHubHttpHeaders = getGitHubHttpHeaders; | | 52304 | +exports.MAX_PAGINATION_PAGES = 1000; | | 52305 | +function getNextPageUrlFromLinkHeader(headers) { | | 52306 | + var _a; | | 52307 | + if (!headers) { | | 52308 | + return null; | | 52309 | + } | | 52310 | + const linkHeader = (_a = headers.link) !== null && _a !== void 0 ? _a : headers.Link; | | 52311 | + if (!linkHeader) { | | 52312 | + return null; | | 52313 | + } | | 52314 | + const normalizedLinkHeader = Array.isArray(linkHeader) | | 52315 | + ? linkHeader.join(',') | | 52316 | + : linkHeader; | | 52317 | + // Split into individual link-values and find the one with rel="next" | | 52318 | + // RFC 8288 allows rel to appear anywhere among the parameters | | 52319 | + const linkValues = normalizedLinkHeader.split(/,(?=\s*<)/); | | 52320 | + for (const linkValue of linkValues) { | | 52321 | + const urlMatch = linkValue.match(/<([^>]+)>/); | | 52322 | + if (!urlMatch) | | 52323 | + continue; | | 52324 | + const params = linkValue.slice(urlMatch[0].length); | | 52325 | + // Use word boundary to match "next" as a standalone relation type | | 52326 | + // RFC 8288 allows space-separated relation types like rel="next prev" | | 52327 | + if (/;\s*rel="?[^"]*\bnext\b/i.test(params)) { | | 52328 | + return urlMatch[1]; | | 52329 | + } | | 52330 | + } | | 52331 | + return null; | | 52332 | +} | | 52333 | +exports.getNextPageUrlFromLinkHeader = getNextPageUrlFromLinkHeader; | | 52334 | +function validatePaginationUrl(url, allowedOrigin) { | | 52335 | + try { | | 52336 | + const parsed = new URL(url); | | 52337 | + const allowed = new URL(allowedOrigin); | | 52338 | + return parsed.origin === allowed.origin; | | 52339 | + } | | 52340 | + catch (_a) { | | 52341 | + return false; | | 52342 | + } | | 52343 | +} | | 52344 | +exports.validatePaginationUrl = validatePaginationUrl; | 52304 | 52345 | // Rename archive to add extension because after downloading | 52305 | 52346 | // archive does not contain extension type and it leads to some issues | 52306 | 52347 | // on Windows runners without PowerShell Core. | |
|