Created
June 1, 2022 11:23
-
-
Save cvzi/b1639b7d1f9da7e348dd97a885bb5f99 to your computer and use it in GitHub Desktop.
shields.io Badge for current build on F-Droid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* global addEventListener, Response, fetch */ | |
// http://f-droid-build.cuzi.workers.dev/{package}/ | |
const fetchConfig = { | |
cf: { | |
// Always cache this fetch regardless of content type | |
// for a max of 10min before revalidating the resource | |
cacheTtl: 10 * 60, | |
cacheEverything: true | |
} | |
} | |
const responseConfig = { | |
headers: { | |
'content-type': 'application/json;charset=UTF-8', | |
'source-code': 'github.com/cvzi/ScreenshotTile_miscellaneous/cloudflare/FDroidBuildBadge.js' | |
} | |
} | |
function findBuild (data, packageName) { | |
if (data.failedBuilds.some(v => v[0] === packageName)) { | |
return -1 | |
} | |
return data.successfulBuilds.filter(v => v.id === packageName) | |
} | |
async function getRunningBuild (packageName) { | |
const url = 'https://f-droid.org/repo/status/running.json' | |
const data = await (await fetch(url, fetchConfig)).json() | |
return findBuild(data, packageName) | |
} | |
async function getLastBuild (packageName) { | |
const url = 'https://f-droid.org/repo/status/build.json' | |
const data = await (await fetch(url, fetchConfig)).json() | |
return findBuild(data, packageName) | |
} | |
async function getNeedsUpdate (packageName) { | |
const url = 'https://f-droid.org/repo/status/update.json' | |
const data = await (await fetch(url, fetchConfig)).json() | |
return data.needsUpdate.some(v => v === packageName) | |
} | |
async function badge (packageName) { | |
const response = { | |
schemaVersion: 1, | |
label: 'F-Droid Build', | |
cacheSeconds: 600 | |
} | |
let builds = await getRunningBuild(packageName) | |
if (!builds) { | |
builds = await getLastBuild(packageName) | |
} | |
if (builds === -1) { | |
response.message = 'Failed' | |
response.color = 'red' | |
} else if (!builds || builds.length === 0) { | |
const needsUpdate = await getNeedsUpdate(packageName) | |
if (needsUpdate) { | |
response.message = 'Waiting for build' | |
response.color = 'blue' | |
} else { | |
response.message = 'No builds' | |
response.color = 'silver' | |
} | |
} else { | |
response.message = 'Succeeded: ' + builds.map(v => v.CurrentVersion).join(', ') | |
response.color = 'green' | |
} | |
return JSON.stringify(response) | |
} | |
async function handleRequest (request) { | |
const { pathname } = new URL(request.url) | |
const packageName = pathname.replace(/^\/|\/$/g, '').trim() | |
if (packageName && packageName.length > 3) { | |
return new Response(await badge(packageName), responseConfig) | |
} else { | |
return new Response(JSON.stringify({ | |
schemaVersion: 1, | |
label: 'Error', | |
message: 'No package name', | |
color: 'orange' | |
}, null, 2), responseConfig) | |
} | |
} | |
addEventListener('fetch', event => { | |
event.respondWith(handleRequest(event.request)) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment