|
| 1 | +import * as core from '@actions/core'; |
| 2 | +import {google} from 'googleapis'; |
| 3 | +import CONST from '@github/libs/CONST'; |
| 4 | +import GithubUtils from '@github/libs/GithubUtils'; |
| 5 | + |
| 6 | +const PACKAGE_NAME = core.getInput('PACKAGE_NAME', {required: true}); |
| 7 | +const GOOGLE_KEY_FILE = core.getInput('GOOGLE_KEY_FILE', {required: true}); |
| 8 | +const HALTED_STATUS = 'halted'; |
| 9 | + |
| 10 | +async function checkAndroidStatus() { |
| 11 | + const auth = new google.auth.GoogleAuth({ |
| 12 | + keyFile: GOOGLE_KEY_FILE, |
| 13 | + scopes: ['https://www.googleapis.com/auth/androidpublisher'], |
| 14 | + }); |
| 15 | + |
| 16 | + const androidApi = google.androidpublisher({ |
| 17 | + version: 'v3', |
| 18 | + auth, |
| 19 | + }); |
| 20 | + |
| 21 | + try { |
| 22 | + // The Google Play API requires an edit ID to make changes to the app |
| 23 | + const editResponse = await androidApi.edits.insert({ |
| 24 | + packageName: PACKAGE_NAME, |
| 25 | + }); |
| 26 | + const editId = editResponse.data.id ?? 'undefined'; |
| 27 | + |
| 28 | + // Get the production track status |
| 29 | + const trackResponse = await androidApi.edits.tracks.get({ |
| 30 | + packageName: PACKAGE_NAME, |
| 31 | + editId, |
| 32 | + track: 'production', |
| 33 | + }); |
| 34 | + |
| 35 | + const status = trackResponse.data.releases?.[0]?.status ?? 'undefined'; |
| 36 | + console.log('Track status:', status); |
| 37 | + |
| 38 | + // Check if the status is halted |
| 39 | + const HALTED = status === HALTED_STATUS; |
| 40 | + core.setOutput('HALTED', HALTED); |
| 41 | + } catch (error) { |
| 42 | + console.error('Error checking track status:', error); |
| 43 | + process.exit(1); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +async function getLatestReleaseDate() { |
| 48 | + const {data} = await GithubUtils.octokit.repos.getLatestRelease({ |
| 49 | + owner: CONST.GITHUB_OWNER, |
| 50 | + repo: CONST.APP_REPO, |
| 51 | + }); |
| 52 | + |
| 53 | + const releaseDate = data.published_at?.split('T')[0]; |
| 54 | + if (!releaseDate) { |
| 55 | + throw new Error('Unable to retrieve the latest release date from GitHub'); |
| 56 | + } |
| 57 | + |
| 58 | + console.log('Latest release date:', releaseDate); |
| 59 | + return releaseDate; |
| 60 | +} |
| 61 | + |
| 62 | +function calculateRolloutPercentage(releaseDate: string): number { |
| 63 | + const release = new Date(releaseDate); |
| 64 | + const current = new Date(); |
| 65 | + const daysSinceRelease = Math.floor((current.getTime() - release.getTime()) / (1000 * 60 * 60 * 24)); |
| 66 | + console.log('Days since release:', daysSinceRelease); |
| 67 | + |
| 68 | + if (daysSinceRelease === 1) { |
| 69 | + return 0.01; |
| 70 | + } |
| 71 | + if (daysSinceRelease === 2) { |
| 72 | + return 0.02; |
| 73 | + } |
| 74 | + if (daysSinceRelease === 3) { |
| 75 | + return 0.05; |
| 76 | + } |
| 77 | + if (daysSinceRelease === 4) { |
| 78 | + return 0.1; |
| 79 | + } |
| 80 | + if (daysSinceRelease === 5) { |
| 81 | + return 0.2; |
| 82 | + } |
| 83 | + if (daysSinceRelease === 6) { |
| 84 | + return 0.5; |
| 85 | + } |
| 86 | + if (daysSinceRelease === 7) { |
| 87 | + return 1; |
| 88 | + } |
| 89 | + // If we did not get a valid number of days since release (1-7), return -1 |
| 90 | + return -1; |
| 91 | +} |
| 92 | + |
| 93 | +checkAndroidStatus() |
| 94 | + .then(getLatestReleaseDate) |
| 95 | + .then((releaseDate) => { |
| 96 | + const rolloutPercentage = calculateRolloutPercentage(releaseDate); |
| 97 | + console.log('Rollout percentage:', rolloutPercentage); |
| 98 | + core.setOutput('ROLLOUT_PERCENTAGE', rolloutPercentage); |
| 99 | + }); |
0 commit comments