From 541bf05e803bb5a63e849eb92b76cb05b4229431 Mon Sep 17 00:00:00 2001 From: oz Date: Wed, 6 Dec 2023 12:55:11 +0100 Subject: [PATCH] wip: test github actions --- .github/workflows/public-release-with-apk.yml | 25 +++++++ publish-release.js | 68 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 .github/workflows/public-release-with-apk.yml create mode 100644 publish-release.js diff --git a/.github/workflows/public-release-with-apk.yml b/.github/workflows/public-release-with-apk.yml new file mode 100644 index 0000000..24b8d95 --- /dev/null +++ b/.github/workflows/public-release-with-apk.yml @@ -0,0 +1,25 @@ +name: Create or Update Release + +on: + workflow_dispatch: + repository_dispatch: + types: [publish-apk] + +jobs: + create-or-update-release: + runs-on: ubuntu-latest + steps: + - name: Checkout script in repo + uses: actions/checkout@v3 + + - name: Run publish-release + uses: actions/github-script@v7 + id: gh-script + env: + RELEASE_ID: ${{ github.event.client_payload.release_id }} + with: + github-token: ${{ secrets.SOURCE_GH_TOKEN }} + result-encoding: string + script: | + const script = require('./publish-release.js') + console.log(script({github, context})) diff --git a/publish-release.js b/publish-release.js new file mode 100644 index 0000000..c587a86 --- /dev/null +++ b/publish-release.js @@ -0,0 +1,68 @@ +const { REPO, RELEASE_ID } = process.env; + +try { + // Fetch release assets from the source repo + const assets = await github.rest.repos.listReleaseAssets({ + owner: context.repo.owner, + repo: 'fedi', + release_id: RELEASE_ID + }); + + console.log(`assets: ${JSON.stringify(assets)}`) + + // Find the APK file + const apkAsset = assets.data.find(asset => asset.name.endsWith('.apk')); + if (!apkAsset) { + throw new Error('APK file not found'); + } + + // Extract version and commit hash from the APK filename + const regex = /app-production-release-(\d+\.\d+\.\d+)-([0-9a-f]+)\.apk/; + const match = apkAsset.name.match(regex); + if (!match) { + throw new Error('Invalid APK filename format'); + } + + const [fullMatch, version, commitHash] = match; + + // Prepare new release details + const newTagName = `v${version}`; + const newTitle = `Fedi Alpha v${version.split('.').slice(0, 2).join('.')} - APK Download`; + const truncatedCommitHash = commitHash.substring(0, 6); + const newFileName = `app-production-release-${version}-${truncatedCommitHash}.apk`; + const newDescription = `Download & Test Fedi Alpha

Download: [${newFileName}](https://github.com/${context.repo.owner}/fedi-alpha/releases/download/${newTagName}/${newFileName})`; + + // Check if a release with the same title exists in the target repo + const releases = await github.rest.repos.listReleases({ + owner: context.repo.owner, + repo: 'fedi-alpha' + }); + + const existingRelease = releases.data.find(release => release.name === newTitle); + + if (existingRelease) { + // Update existing release + await github.rest.repos.updateRelease({ + owner: context.repo.owner, + repo: 'fedi-alpha', + release_id: existingRelease.id, + tag_name: newTagName, + name: newTitle, + body: newDescription + }); + // TODO: Upload new APK and delete old APK + } else { + // Create a new release + await github.rest.repos.createRelease({ + owner: context.repo.owner, + repo: 'fedi-alpha', + tag_name: newTagName, + name: newTitle, + body: newDescription + }); + // TODO: Upload APK + } +} catch (error) { + console.error(error); + process.exit(1); +} \ No newline at end of file