-
Notifications
You must be signed in to change notification settings - Fork 0
ci(cws): monitor Chrome Web Store status #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| name: Chrome Web Store Status | ||
|
|
||
| on: | ||
| workflow_dispatch: | ||
| inputs: | ||
| expected_version: | ||
| description: Expected extension version. Defaults to package.json. | ||
| required: false | ||
| default: "" | ||
| require_published: | ||
| description: Fail unless the expected version is published. | ||
| required: true | ||
| default: "false" | ||
| type: choice | ||
| options: | ||
| - "false" | ||
| - "true" | ||
| schedule: | ||
| - cron: "17 */6 * * *" | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| status: | ||
| name: Check Chrome Web Store status | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 5 | ||
| environment: chrome-web-store-status | ||
|
|
||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 | ||
|
|
||
| - name: Set up Node.js | ||
| uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e | ||
| with: | ||
| node-version: 22.13.0 | ||
|
|
||
| - name: Check Chrome Web Store status | ||
| run: node scripts/check-chrome-web-store-status.mjs | ||
| env: | ||
| CWS_EXTENSION_ID: nfnbhekccajjfgkppolomflaeledoccb | ||
| CWS_EXPECTED_VERSION: ${{ inputs.expected_version }} | ||
| CWS_REQUIRE_PUBLISHED: ${{ inputs.require_published || 'false' }} | ||
| CWS_PUBLISHER_ID: ${{ vars.CWS_PUBLISHER_ID }} | ||
| CWS_SERVICE_ACCOUNT_JSON: ${{ secrets.CWS_SERVICE_ACCOUNT_JSON }} |
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| export interface CheckChromeWebStoreStatusOptions { | ||
| argv?: string[]; | ||
| cwd?: string; | ||
| env?: Record<string, string | undefined>; | ||
| fetchImpl?: (url: string, init?: RequestInit) => Promise<Response>; | ||
| write?: (line: string) => void; | ||
| } | ||
|
|
||
| export interface ChromeWebStoreStatusSummary { | ||
| extensionId: string; | ||
| publisherId: string | null; | ||
| expectedVersion: string | null; | ||
| submittedVersion: string | null; | ||
| publishedVersion: string | null; | ||
| latestObservedVersion: string | null; | ||
| states: string[]; | ||
| expectedSubmitted: boolean | null; | ||
| expectedPublished: boolean | null; | ||
| pendingReview: boolean; | ||
| published: boolean; | ||
| failed: boolean; | ||
| } | ||
|
|
||
| export function checkChromeWebStoreStatus( | ||
| options?: CheckChromeWebStoreStatusOptions, | ||
| ): Promise<ChromeWebStoreStatusSummary>; | ||
|
|
||
| export function summarizeChromeWebStoreStatus( | ||
| status: Record<string, unknown>, | ||
| options?: { | ||
| extensionId?: string; | ||
| expectedVersion?: string; | ||
| publisherId?: string | null; | ||
| }, | ||
| ): ChromeWebStoreStatusSummary; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| /* global fetch */ | ||
| import { readFile } from "node:fs/promises"; | ||
| import path from "node:path"; | ||
| import { pathToFileURL } from "node:url"; | ||
|
|
||
| import { fetchChromeWebStoreStatus } from "./publish-chrome-web-store.mjs"; | ||
|
|
||
| const DEFAULT_EXTENSION_ID = "nfnbhekccajjfgkppolomflaeledoccb"; | ||
| const FAILURE_STATES = new Set([ | ||
| "CANCELLED", | ||
| "FAILED", | ||
| "FAILURE", | ||
| "REJECTED", | ||
| "REJECTED_FOR_POLICY", | ||
| ]); | ||
| const PENDING_STATES = new Set([ | ||
| "IN_REVIEW", | ||
| "PENDING", | ||
| "PENDING_REVIEW", | ||
| "PENDING_REVIEW_PUBLISH", | ||
| "SUBMITTED", | ||
| ]); | ||
| const PUBLISHED_STATES = new Set(["OK", "PUBLISHED", "PUBLIC", "LIVE"]); | ||
|
|
||
| export async function checkChromeWebStoreStatus({ | ||
| argv = process.argv.slice(2), | ||
| cwd = process.cwd(), | ||
| env = process.env, | ||
| fetchImpl = fetch, | ||
| write = console.log, | ||
| } = {}) { | ||
| const args = parseArgs(argv); | ||
| const extensionId = args.extensionId ?? env.CWS_EXTENSION_ID ?? DEFAULT_EXTENSION_ID; | ||
| const expectedVersion = | ||
| nonEmptyString(args.expectedVersion) ?? | ||
| nonEmptyString(env.CWS_EXPECTED_VERSION) ?? | ||
| (await readPackageVersion(cwd)); | ||
| const requirePublished = | ||
| parseOptionalBoolean(args.requirePublished ?? env.CWS_REQUIRE_PUBLISHED, "requirePublished") ?? | ||
| false; | ||
|
|
||
| const status = await fetchChromeWebStoreStatus({ | ||
| extensionId, | ||
| publisherId: args.publisherId ?? env.CWS_PUBLISHER_ID, | ||
| env, | ||
| fetchImpl, | ||
| }); | ||
| const summary = summarizeChromeWebStoreStatus(status, { | ||
| extensionId, | ||
| expectedVersion, | ||
| publisherId: args.publisherId ?? env.CWS_PUBLISHER_ID ?? null, | ||
| }); | ||
|
|
||
| assertChromeWebStoreStatus(summary, { requirePublished }); | ||
| write(JSON.stringify(summary, null, 2)); | ||
| return summary; | ||
| } | ||
|
|
||
| export function summarizeChromeWebStoreStatus( | ||
| status, | ||
| { extensionId = DEFAULT_EXTENSION_ID, expectedVersion, publisherId = null } = {}, | ||
| ) { | ||
| const submittedVersions = uniqueStrings([ | ||
| ...distributionVersions(status.submittedItemRevisionStatus), | ||
| ...distributionVersions(status.itemRevisionStatus), | ||
| ]); | ||
| const publishedVersions = uniqueStrings([ | ||
| ...distributionVersions(status.publishedItemRevisionStatus), | ||
| ...distributionVersions(status.publicItemRevisionStatus), | ||
| ]); | ||
| const submittedVersion = firstString(submittedVersions); | ||
| const publishedVersion = firstString(publishedVersions); | ||
| const anyVersion = firstString([...collectValuesByKey(status, "crxVersion")]); | ||
| const topLevelStates = [status.itemState, status.state, status.reviewState, status.publishState]; | ||
| const submittedRevisionStates = revisionStates(status.submittedItemRevisionStatus); | ||
| const publishedRevisionStates = revisionStates(status.publishedItemRevisionStatus); | ||
| const states = uniqueStrings([ | ||
| status.lastAsyncUploadState, | ||
| ...topLevelStates, | ||
| ...submittedRevisionStates, | ||
| ...publishedRevisionStates, | ||
| ]); | ||
| const normalizedStates = states.map((state) => state.toUpperCase()); | ||
| const normalizedPublishedStates = uniqueStrings([ | ||
| ...topLevelStates, | ||
| ...publishedRevisionStates, | ||
| ]).map((state) => state.toUpperCase()); | ||
| const hasFailureState = | ||
| normalizedStates.some((state) => FAILURE_STATES.has(state)) || | ||
| status.takenDown === true || | ||
| status.warned === true; | ||
| const hasPendingState = normalizedStates.some((state) => PENDING_STATES.has(state)); | ||
| const hasPublishedState = normalizedPublishedStates.some((state) => PUBLISHED_STATES.has(state)); | ||
| const expectedSubmitted = expectedVersion ? submittedVersions.includes(expectedVersion) : null; | ||
| const expectedPublished = expectedVersion ? publishedVersions.includes(expectedVersion) : null; | ||
| const published = | ||
| !hasFailureState && hasPublishedState && Boolean(expectedVersion ? expectedPublished : true); | ||
|
|
||
| return { | ||
| extensionId, | ||
| publisherId, | ||
| expectedVersion: expectedVersion ?? null, | ||
| submittedVersion: submittedVersion ?? null, | ||
| publishedVersion: publishedVersion ?? null, | ||
| latestObservedVersion: submittedVersion ?? publishedVersion ?? anyVersion ?? null, | ||
| states, | ||
| takenDown: status.takenDown === true, | ||
| warned: status.warned === true, | ||
| expectedSubmitted, | ||
| expectedPublished, | ||
| pendingReview: hasPendingState && !hasFailureState && !published, | ||
| published, | ||
| failed: hasFailureState, | ||
| }; | ||
| } | ||
|
|
||
| function assertChromeWebStoreStatus(summary, { requirePublished }) { | ||
| if (summary.failed) { | ||
| if (summary.takenDown) { | ||
| throw new Error( | ||
| `Chrome Web Store item ${summary.extensionId} has been taken down for a policy violation.`, | ||
| ); | ||
| } | ||
|
|
||
| if (summary.warned) { | ||
| throw new Error( | ||
| `Chrome Web Store item ${summary.extensionId} has a policy warning that must be resolved.`, | ||
| ); | ||
| } | ||
|
|
||
| throw new Error( | ||
| `Chrome Web Store item ${summary.extensionId} has a failed/rejected state: ${summary.states.join(", ")}`, | ||
| ); | ||
| } | ||
|
|
||
| if (summary.expectedVersion && !summary.expectedSubmitted && !summary.expectedPublished) { | ||
| throw new Error( | ||
| `Chrome Web Store status does not show expected version ${summary.expectedVersion}. Latest observed version: ${summary.latestObservedVersion ?? "unknown"}.`, | ||
| ); | ||
| } | ||
|
|
||
| if (requirePublished && !summary.published) { | ||
| throw new Error( | ||
| `Chrome Web Store version ${summary.expectedVersion ?? "unknown"} is not published yet.`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| async function readPackageVersion(cwd) { | ||
| const packageJson = JSON.parse(await readFile(path.join(cwd, "package.json"), "utf8")); | ||
| if (!packageJson.version) throw new Error("package.json is missing version."); | ||
| return packageJson.version; | ||
| } | ||
|
|
||
| function distributionVersions(revisionStatus) { | ||
| if (!revisionStatus) return []; | ||
| return [ | ||
| revisionStatus.crxVersion, | ||
| ...(revisionStatus.distributionChannels ?? []).map((channel) => channel?.crxVersion), | ||
| ].filter(Boolean); | ||
| } | ||
|
|
||
| function revisionStates(revisionStatus) { | ||
| if (!revisionStatus) return []; | ||
| return [revisionStatus.itemState, revisionStatus.state, revisionStatus.reviewState]; | ||
| } | ||
|
|
||
| function collectValuesByKey(value, key, seen = new Set()) { | ||
| if (!value || typeof value !== "object" || seen.has(value)) return []; | ||
| seen.add(value); | ||
| const values = []; | ||
|
|
||
| if (Object.prototype.hasOwnProperty.call(value, key)) { | ||
| values.push(value[key]); | ||
| } | ||
|
|
||
| for (const child of Object.values(value)) { | ||
| if (Array.isArray(child)) { | ||
| for (const item of child) values.push(...collectValuesByKey(item, key, seen)); | ||
| } else if (child && typeof child === "object") { | ||
| values.push(...collectValuesByKey(child, key, seen)); | ||
| } | ||
| } | ||
|
|
||
| return values; | ||
| } | ||
|
|
||
| function firstString(values) { | ||
| return values.find((value) => typeof value === "string" && value.length > 0) ?? null; | ||
| } | ||
|
|
||
| function uniqueStrings(values) { | ||
| return Array.from(new Set(values.filter((value) => typeof value === "string" && value))); | ||
| } | ||
|
|
||
| function parseArgs(values) { | ||
| const parsed = {}; | ||
| for (let index = 0; index < values.length; index += 1) { | ||
| const key = values[index]; | ||
| if (!key?.startsWith("--")) throw new Error(`Unexpected argument: ${key}`); | ||
| const value = values[index + 1]; | ||
| if (!value || value.startsWith("--")) throw new Error(`Missing value for ${key}`); | ||
| parsed[toCamelCase(key.slice(2))] = value; | ||
| index += 1; | ||
| } | ||
| return parsed; | ||
| } | ||
|
|
||
| function parseOptionalBoolean(value, name) { | ||
| if (value === undefined || value === null || value === "") return undefined; | ||
| if (value === true || value === "true") return true; | ||
| if (value === false || value === "false") return false; | ||
| throw new Error(`Expected ${name} to be true or false, got ${value}.`); | ||
| } | ||
|
|
||
| function nonEmptyString(value) { | ||
| return typeof value === "string" && value.length > 0 ? value : undefined; | ||
| } | ||
|
|
||
| function toCamelCase(value) { | ||
| return value.replace(/-([a-z])/g, (_match, letter) => letter.toUpperCase()); | ||
| } | ||
|
|
||
| if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { | ||
| checkChromeWebStoreStatus().catch((error) => { | ||
| console.error(error.message); | ||
| process.exit(1); | ||
| }); | ||
| } | ||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.