Try to prevent 403 when accessing the GitHub API (#168)

* Repeat request on error (while logging it)

* Test one change at a time

* Conditionally authorize requests

This seems to affect Windows builds, so might not be the best solution,
but we're trying to isolate the issue to move forward

* Act on CI results

* Ease consumption via a JSON.parse wrapper

We still throw the error, but in a more controlled manner

* Act on CI results
This commit is contained in:
Paulo F. Oliveira
2023-01-04 07:27:57 +00:00
committed by GitHub
parent 2d314bc413
commit 37ac1c5116
5 changed files with 82 additions and 53 deletions
+4 -1
View File
@@ -9,6 +9,9 @@ on:
branches: branches:
- main - main
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
jobs: jobs:
check_integrity: check_integrity:
name: Expected local npm actions name: Expected local npm actions
@@ -28,7 +31,7 @@ jobs:
- run: npm run yamllint - run: npm run yamllint
- run: npm run jslint - run: npm run jslint
- name: Check if build left artifacts - name: Check if build left artifacts
run: git diff --exit-code run: git diff --exit-code --ignore-space-at-eol
unit_tests_ubuntu: unit_tests_ubuntu:
name: Unit tests (Ubuntu) name: Unit tests (Ubuntu)
+3
View File
@@ -9,6 +9,9 @@ on:
branches: branches:
- main - main
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
jobs: jobs:
integration_test: integration_test:
name: > name: >
+3
View File
@@ -9,6 +9,9 @@ on:
branches: branches:
- main - main
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
jobs: jobs:
integration_test: integration_test:
name: > name: >
+36 -26
View File
@@ -7440,7 +7440,7 @@ async function getOTPVersions(osVersion) {
}) })
} else if (process.platform === 'win32') { } else if (process.platform === 'win32') {
otpVersionsListings.forEach((otpVersionsListing) => { otpVersionsListings.forEach((otpVersionsListing) => {
JSON.parse(otpVersionsListing) jsonParse(otpVersionsListing)
.map((x) => x.assets) .map((x) => x.assets)
.flat() .flat()
.filter((x) => x.name.match(/^otp_win64_.*.exe$/)) .filter((x) => x.name.match(/^otp_win64_.*.exe$/))
@@ -7487,7 +7487,7 @@ async function getGleamVersions() {
) )
const gleamVersionsListing = [] const gleamVersionsListing = []
resultJSONs.forEach((resultJSON) => { resultJSONs.forEach((resultJSON) => {
JSON.parse(resultJSON) jsonParse(resultJSON)
.map((x) => x.tag_name) .map((x) => x.tag_name)
.sort() .sort()
.forEach((v) => gleamVersionsListing.push(v)) .forEach((v) => gleamVersionsListing.push(v))
@@ -7502,7 +7502,7 @@ async function getRebar3Versions() {
) )
const rebar3VersionsListing = [] const rebar3VersionsListing = []
resultJSONs.forEach((resultJSON) => { resultJSONs.forEach((resultJSON) => {
JSON.parse(resultJSON) jsonParse(resultJSON)
.map((x) => x.tag_name) .map((x) => x.tag_name)
.sort() .sort()
.forEach((v) => rebar3VersionsListing.push(v)) .forEach((v) => rebar3VersionsListing.push(v))
@@ -7609,33 +7609,33 @@ async function get(url0, pageIdxs) {
function getPage(pageIdx) { function getPage(pageIdx) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const url = new URL(url0) const url = new URL(url0)
const headers = {
'user-agent': 'setup-beam',
}
if (process.env.GITHUB_TOKEN) {
headers.authorization = `Bearer ${process.env.GITHUB_TOKEN}`
}
if (pageIdx !== null) { if (pageIdx !== null) {
url.searchParams.append('page', pageIdx) url.searchParams.append('page', pageIdx)
} }
https https
.get( .get(url, { headers }, (res) => {
url, let data = ''
{ res.on('data', (chunk) => {
headers: { 'user-agent': 'setup-beam' }, data += chunk
}, })
(res) => { res.on('end', () => {
let data = '' if (res.statusCode >= 400 && res.statusCode <= 599) {
res.on('data', (chunk) => { reject(
data += chunk new Error(
}) `Got ${res.statusCode} from ${url}. Exiting with error`,
res.on('end', () => { ),
if (res.statusCode >= 400 && res.statusCode <= 599) { )
reject( } else {
new Error( resolve(data)
`Got ${res.statusCode} from ${url}. Exiting with error`, }
), })
) })
} else {
resolve(data)
}
})
},
)
.on('error', (err) => { .on('error', (err) => {
reject(err) reject(err)
}) })
@@ -7716,6 +7716,16 @@ function parseVersionFile(versionFilePath0) {
return appVersions return appVersions
} }
function jsonParse(maybeJson) {
try {
return JSON.parse(maybeJson)
} catch (exc) {
throw new Error(
`Got an exception when trying to parse non-JSON ${maybeJson}: ${exc}`,
)
}
}
module.exports = { module.exports = {
getOTPVersion, getOTPVersion,
getElixirVersion, getElixirVersion,
+36 -26
View File
@@ -247,7 +247,7 @@ async function getOTPVersions(osVersion) {
}) })
} else if (process.platform === 'win32') { } else if (process.platform === 'win32') {
otpVersionsListings.forEach((otpVersionsListing) => { otpVersionsListings.forEach((otpVersionsListing) => {
JSON.parse(otpVersionsListing) jsonParse(otpVersionsListing)
.map((x) => x.assets) .map((x) => x.assets)
.flat() .flat()
.filter((x) => x.name.match(/^otp_win64_.*.exe$/)) .filter((x) => x.name.match(/^otp_win64_.*.exe$/))
@@ -294,7 +294,7 @@ async function getGleamVersions() {
) )
const gleamVersionsListing = [] const gleamVersionsListing = []
resultJSONs.forEach((resultJSON) => { resultJSONs.forEach((resultJSON) => {
JSON.parse(resultJSON) jsonParse(resultJSON)
.map((x) => x.tag_name) .map((x) => x.tag_name)
.sort() .sort()
.forEach((v) => gleamVersionsListing.push(v)) .forEach((v) => gleamVersionsListing.push(v))
@@ -309,7 +309,7 @@ async function getRebar3Versions() {
) )
const rebar3VersionsListing = [] const rebar3VersionsListing = []
resultJSONs.forEach((resultJSON) => { resultJSONs.forEach((resultJSON) => {
JSON.parse(resultJSON) jsonParse(resultJSON)
.map((x) => x.tag_name) .map((x) => x.tag_name)
.sort() .sort()
.forEach((v) => rebar3VersionsListing.push(v)) .forEach((v) => rebar3VersionsListing.push(v))
@@ -416,33 +416,33 @@ async function get(url0, pageIdxs) {
function getPage(pageIdx) { function getPage(pageIdx) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const url = new URL(url0) const url = new URL(url0)
const headers = {
'user-agent': 'setup-beam',
}
if (process.env.GITHUB_TOKEN) {
headers.authorization = `Bearer ${process.env.GITHUB_TOKEN}`
}
if (pageIdx !== null) { if (pageIdx !== null) {
url.searchParams.append('page', pageIdx) url.searchParams.append('page', pageIdx)
} }
https https
.get( .get(url, { headers }, (res) => {
url, let data = ''
{ res.on('data', (chunk) => {
headers: { 'user-agent': 'setup-beam' }, data += chunk
}, })
(res) => { res.on('end', () => {
let data = '' if (res.statusCode >= 400 && res.statusCode <= 599) {
res.on('data', (chunk) => { reject(
data += chunk new Error(
}) `Got ${res.statusCode} from ${url}. Exiting with error`,
res.on('end', () => { ),
if (res.statusCode >= 400 && res.statusCode <= 599) { )
reject( } else {
new Error( resolve(data)
`Got ${res.statusCode} from ${url}. Exiting with error`, }
), })
) })
} else {
resolve(data)
}
})
},
)
.on('error', (err) => { .on('error', (err) => {
reject(err) reject(err)
}) })
@@ -523,6 +523,16 @@ function parseVersionFile(versionFilePath0) {
return appVersions return appVersions
} }
function jsonParse(maybeJson) {
try {
return JSON.parse(maybeJson)
} catch (exc) {
throw new Error(
`Got an exception when trying to parse non-JSON ${maybeJson}: ${exc}`,
)
}
}
module.exports = { module.exports = {
getOTPVersion, getOTPVersion,
getElixirVersion, getElixirVersion,