|
| 1 | +module.exports = async ({ inputs, core, glob, fetch, require }) => { |
| 2 | + const fs = require('fs') |
| 3 | + const path = require('path') |
| 4 | + |
| 5 | + // remove when using Node 18 |
| 6 | + // https://github.com/actions/github-script/issues/310 |
| 7 | + const { FormData, Blob, File } = require('formdata-node') |
| 8 | + |
| 9 | + const auth = `X-ApiKey ${inputs.hackageToken}` |
| 10 | + const uploadUrlSuffix = inputs.candidate ? '/candidates' : '/upload' |
| 11 | + const packageUrlSuffix = inputs.candidate ? '/candidate' : '' |
| 12 | + const checkExists = !inputs.candidate |
| 13 | + |
| 14 | + const archivePaths = await glob.create(`${inputs.packagesPath}/*.tar.gz`).then((g) => g.glob()) |
| 15 | + for (const archivePath of archivePaths) { |
| 16 | + const name = path.basename(archivePath, '.tar.gz') |
| 17 | + const uploadUrl = `${inputs.hackageServer}/packages${uploadUrlSuffix}` |
| 18 | + const packageUrl = `${inputs.hackageServer}/packages/${name}${packageUrlSuffix}` |
| 19 | + |
| 20 | + // check if package already uploaded |
| 21 | + if (checkExists) { |
| 22 | + const existsResp = await fetch(packageUrl, { method: 'HEAD' }) |
| 23 | + if (existsResp.status != 404) { |
| 24 | + core.info(`Package "${name}" already exists on Hackage`) |
| 25 | + continue |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + // upload package |
| 30 | + core.info(`Uploading package "${name}" to ${uploadUrl}`) |
| 31 | + const uploadBody = new FormData() |
| 32 | + const archiveFile = loadFile(archivePath, { require }) |
| 33 | + uploadBody.set('package', archiveFile) |
| 34 | + const uploadResp = await fetch(uploadUrl, { |
| 35 | + method: 'POST', |
| 36 | + headers: { 'Authorization': auth }, |
| 37 | + body: uploadBody, |
| 38 | + }) |
| 39 | + console.log(uploadBody) |
| 40 | + console.log(uploadResp) |
| 41 | + if (uploadResp.status >= 400) { |
| 42 | + const uploadRespBody = await uploadResp.text() |
| 43 | + throw new Error(`Failed to upload package "${name}": ${uploadRespBody}`) |
| 44 | + } |
| 45 | + core.info(`Successfully uploaded ${packageUrl}`) |
| 46 | + |
| 47 | + // upload docs |
| 48 | + const docsArchive = `${inputs.docsPath}/${name}-docs.tar.gz` |
| 49 | + const docsUrl = `${packageUrl}/docs` |
| 50 | + if (inputs.docsPath && fs.existsSync(docsArchive)) { |
| 51 | + core.info(`Uploading documentation for "${name}" to ${docsUrl}`) |
| 52 | + const docsBody = await fetch.blobFrom(docsArchive, 'application/octet-stream') |
| 53 | + await fetch(docsUrl, { |
| 54 | + method: 'PUT', |
| 55 | + headers: { |
| 56 | + 'Content-Type': 'application/x-tar', |
| 57 | + 'Content-Encoding': 'gzip', |
| 58 | + 'Authorization': auth, |
| 59 | + }, |
| 60 | + body: docsBody, |
| 61 | + }) |
| 62 | + core.info(`Successfully uploaded ${docsUrl}`) |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +const loadFile = (path, { require }) => { |
| 68 | + // remove when using Node 18 |
| 69 | + // https://github.com/actions/github-script/issues/310 |
| 70 | + const fs = require('fs') |
| 71 | + const { Blob, File } = require('formdata-node') |
| 72 | + |
| 73 | + const contents = fs.readFileSync(path) |
| 74 | + const blob = new Blob([contents], { type: 'application/octet-stream' }) |
| 75 | + return new File([blob], path) |
| 76 | +} |
0 commit comments