|
| 1 | +module.exports = async ({ github, context, core }) => { |
| 2 | + const { RELEASE_ID } = process.env; |
| 3 | + |
| 4 | + const publish = async () => { |
| 5 | + console.log('Starting the publish process.'); |
| 6 | + |
| 7 | + // Fetch release assets from the source repo |
| 8 | + console.log(`Fetching release assets from the source repository`); |
| 9 | + const assets = await github.rest.repos.listReleaseAssets({ |
| 10 | + owner: context.repo.owner, |
| 11 | + repo: 'fedi', |
| 12 | + release_id: RELEASE_ID, |
| 13 | + }); |
| 14 | + |
| 15 | + // Find the APK file |
| 16 | + const apkAsset = assets.data.find((asset) => asset.name.endsWith('.apk')); |
| 17 | + if (!apkAsset) { |
| 18 | + throw new Error('APK file not found'); |
| 19 | + } |
| 20 | + |
| 21 | + console.log(`APK file found: ${apkAsset.name}`); |
| 22 | + |
| 23 | + // Extract version and commit hash from the APK filename |
| 24 | + const regex = /app-production-release-(\d+\.\d+\.\d+)-([0-9a-f]+)\.apk/; |
| 25 | + const match = apkAsset.name.match(regex); |
| 26 | + if (!match) { |
| 27 | + throw new Error('Invalid APK filename format'); |
| 28 | + } |
| 29 | + |
| 30 | + const [fullMatch, version, commitHash] = match; |
| 31 | + |
| 32 | + console.log( |
| 33 | + `Extracted version: ${version} and commit hash: ${commitHash} from APK filename` |
| 34 | + ); |
| 35 | + |
| 36 | + // Prepare new release details |
| 37 | + const newTagName = `v${version}`; |
| 38 | + const newTitle = `Fedi Alpha v${version.split('.').slice(0, 2).join('.')} - APK Download`; |
| 39 | + const truncatedCommitHash = commitHash.substring(0, 6); |
| 40 | + const newFileName = `app-production-release-${version}-${truncatedCommitHash}.apk`; |
| 41 | + const newDescription = `Download & Test Fedi Alpha <br><br> Download: [${newFileName}](https://github.com/${context.repo.owner}/fedi-alpha/releases/download/${newTagName}/${newFileName})`; |
| 42 | + |
| 43 | + console.log(`New release details prepared. Tag: ${newTagName}, Title: ${newTitle}`); |
| 44 | + |
| 45 | + // Check if a release with the same title exists in the target repo |
| 46 | + console.log('Checking for existing release in the target repository.'); |
| 47 | + const releases = await github.rest.repos.listReleases({ |
| 48 | + owner: context.repo.owner, |
| 49 | + repo: 'fedi-alpha', |
| 50 | + }); |
| 51 | + |
| 52 | + // Function to upload a new APK to a release |
| 53 | + async function uploadNewApk(releaseId) { |
| 54 | + // Download the APK from the source repository |
| 55 | + const apkBuffer = await github.rest.repos.getReleaseAsset({ |
| 56 | + owner: context.repo.owner, |
| 57 | + repo: 'fedi', |
| 58 | + asset_id: apkAsset.id, |
| 59 | + headers: { |
| 60 | + Accept: 'application/octet-stream', |
| 61 | + }, |
| 62 | + }); |
| 63 | + |
| 64 | + // Upload the APK to the target repository |
| 65 | + await github.rest.repos.uploadReleaseAsset({ |
| 66 | + url: `https://uploads.github.com/repos/${ |
| 67 | + context.repo.owner |
| 68 | + }/fedi-alpha/releases/${releaseId}/assets?name=${encodeURIComponent(newFileName)}`, |
| 69 | + headers: { |
| 70 | + 'content-type': 'application/vnd.android.package-archive', |
| 71 | + 'content-length': apkBuffer.data.length, |
| 72 | + }, |
| 73 | + data: apkBuffer.data, |
| 74 | + name: newFileName, |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + // Function to delete old APK from a release |
| 79 | + async function deleteOldApk(releaseId) { |
| 80 | + const assets = await github.rest.repos.listReleaseAssets({ |
| 81 | + owner: context.repo.owner, |
| 82 | + repo: 'fedi-alpha', |
| 83 | + release_id: releaseId, |
| 84 | + }); |
| 85 | + |
| 86 | + const oldApkAsset = assets.data.find((asset) => asset.name.endsWith('.apk')); |
| 87 | + if (oldApkAsset) { |
| 88 | + await github.rest.repos.deleteReleaseAsset({ |
| 89 | + owner: context.repo.owner, |
| 90 | + repo: 'fedi-alpha', |
| 91 | + asset_id: oldApkAsset.id, |
| 92 | + }); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + const existingRelease = releases.data.find((release) => release.name === newTitle); |
| 97 | + |
| 98 | + if (existingRelease) { |
| 99 | + console.log('Existing release found, updating description & download link...'); |
| 100 | + |
| 101 | + // Update existing release |
| 102 | + await github.rest.repos.updateRelease({ |
| 103 | + owner: context.repo.owner, |
| 104 | + repo: 'fedi-alpha', |
| 105 | + release_id: existingRelease.id, |
| 106 | + tag_name: newTagName, |
| 107 | + name: newTitle, |
| 108 | + body: newDescription, |
| 109 | + }); |
| 110 | + |
| 111 | + console.log('Existing release updated, proceeding to update APK.'); |
| 112 | + |
| 113 | + // Upload new APK and delete old APK |
| 114 | + await deleteOldApk(existingRelease.id); |
| 115 | + await uploadNewApk(existingRelease.id); |
| 116 | + console.log('Existing release updated with new APK.'); |
| 117 | + } else { |
| 118 | + console.log('No existing release found, creating a new one.'); |
| 119 | + |
| 120 | + // Create a new release |
| 121 | + const newRelease = await github.rest.repos.createRelease({ |
| 122 | + owner: context.repo.owner, |
| 123 | + repo: 'fedi-alpha', |
| 124 | + tag_name: newTagName, |
| 125 | + name: newTitle, |
| 126 | + body: newDescription, |
| 127 | + }); |
| 128 | + |
| 129 | + console.log('New release created, proceeding to upload APK.'); |
| 130 | + |
| 131 | + // Upload APK |
| 132 | + await uploadNewApk(newRelease.data.id); |
| 133 | + console.log('New release created with uploaded APK.'); |
| 134 | + } |
| 135 | + }; |
| 136 | + |
| 137 | + try { |
| 138 | + await publish(); |
| 139 | + console.log('Publish process completed.'); |
| 140 | + } catch (error) { |
| 141 | + console.error('Error encountered during the publish process:', error); |
| 142 | + process.exit(1); |
| 143 | + } |
| 144 | +}; |
0 commit comments