diff --git a/.github/workflows/rerun.yml b/.github/workflows/rerun.yml index 6fbd174637..2317803530 100644 --- a/.github/workflows/rerun.yml +++ b/.github/workflows/rerun.yml @@ -72,3 +72,33 @@ jobs: if: always() run: | npx ts-node src/scripts/clean.ts + + - name: Switch M365 account + run: | + username='${{ env.M365_USERNAME_2 }}' + echo "M365_ACCOUNT_NAME=$username" >> $GITHUB_ENV + + - name: Clean resource + if: always() + run: | + npx ts-node src/scripts/clean.ts + + - name: Switch M365 account + run: | + username='${{ env.M365_USERNAME_3 }}' + echo "M365_ACCOUNT_NAME=$username" >> $GITHUB_ENV + + - name: Clean resource + if: always() + run: | + npx ts-node src/scripts/clean.ts + + - name: Switch M365 account + run: | + username='${{ env.M365_USERNAME_4 }}' + echo "M365_ACCOUNT_NAME=$username" >> $GITHUB_ENV + + - name: Clean resource + if: always() + run: | + npx ts-node src/scripts/clean.ts \ No newline at end of file diff --git a/packages/tests/src/scripts/clean.ts b/packages/tests/src/scripts/clean.ts index ce7dd7209e..a829e175ae 100644 --- a/packages/tests/src/scripts/clean.ts +++ b/packages/tests/src/scripts/clean.ts @@ -32,14 +32,88 @@ async function main() { const aadList = await cleanService.listAad(); if (aadList) { for (const aad of aadList) { - if ( - !aad.displayName?.startsWith("delete") - ) { - console.log(aad.displayName); - await cleanService.deleteAad(aad.id!); - } + if (!aad.displayName?.startsWith("delete")) { + console.log(aad.displayName); + await cleanService.deleteAad(aad.id!); + } + } + } + + console.log(`clean teams app (exclude ${excludePrefix})`); + const teamsUserId = await cleanService.getUserIdByName(Env.username); + const teamsAppList = await cleanService.listTeamsApp(teamsUserId); + if (teamsAppList) { + for (const app of teamsAppList) { + console.log(app?.teamsAppDefinition?.displayName); + try { + await cleanService.uninstallTeamsApp(teamsUserId, app?.id ?? ""); + } catch { + console.log( + `Failed to uninstall Teams App ${app?.teamsAppDefinition?.displayName}` + ); + } + } + } + + console.log(`clean app in app studio`); + const addStudioCleanService = await AppStudioCleanHelper.create( + Env.cleanTenantId, + Env.cleanClientId, + Env.username, + Env.password + ); + const appStudioAppList = await addStudioCleanService.getAppsInAppStudio(); + if (appStudioAppList) { + for (const app of appStudioAppList) { + console.log(app?.displayName); + try { + await addStudioCleanService.deleteAppInAppStudio(app?.appDefinitionId); + } catch { + console.log( + `Failed to delete Teams App ${app?.displayName} in App Studio` + ); + } } } + + let retry: boolean; + let count = 10; + const total = count + 1; + do { + retry = false; + console.log(`Start to try ${total - count} times`); + const m365TitleCleanService = await M365TitleCleanHelper.create( + Env.cleanTenantId, + "7ea7c24c-b1f6-4a20-9d11-9ae12e9e7ac0", + Env.username, + Env.password + ); + console.log(`clean M365 Titles (exclude ${excludePrefix})`); + try { + const acquisitions = await m365TitleCleanService.listAcquisitions(); + if (acquisitions) { + for (const acquisition of acquisitions) { + console.log(acquisition.titleDefinition.name); + console.log(acquisition.titleId); + const result = await m365TitleCleanService.unacquire( + acquisition.titleId + ); + if (!retry && result) { + retry = true; + } + } + } + } catch (e: any) { + console.log(`Get error: ${e.message}`); + retry = true; + if (count > 1) { + // Retry after a short time if getting "Rate limit is exceeded" + await delay(30 * 1000); + } + } + + count--; + } while (retry && count > 0); } main()