Skip to content

Commit

Permalink
test: cleanup other resources
Browse files Browse the repository at this point in the history
  • Loading branch information
hellyzh committed Jan 15, 2025
1 parent 82d13a9 commit 0b35d13
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 6 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/rerun.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
86 changes: 80 additions & 6 deletions packages/tests/src/scripts/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 0b35d13

Please sign in to comment.