2018-08-29 11:13:22 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#!/usr/bin/env node
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-30 17:55:27 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import { Octokit } from '@octokit/rest';
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import * as chalk from 'chalk';
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import { createGitHubTokenStrategy } from './github-token';
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import { ELECTRON_ORG, ELECTRON_REPO, ElectronReleaseRepo, NIGHTLY_REPO } from './types';
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-26 00:12:11 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								const pass = chalk.green('✓');
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								const fail = chalk.red('✗');
							 | 
						
					
						
							
								
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-30 17:55:27 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								async function deleteDraft (releaseId: string, targetRepo: ElectronReleaseRepo) {
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-23 09:39:54 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  const octokit = new Octokit({
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    authStrategy: createGitHubTokenStrategy(targetRepo)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  });
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  try {
							 | 
						
					
						
							
								
									
										
										
										
											2019-01-08 12:05:58 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const result = await octokit.repos.getRelease({
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-30 17:55:27 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      owner: ELECTRON_ORG,
							 | 
						
					
						
							
								
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      repo: targetRepo,
							 | 
						
					
						
							
								
									
										
										
										
											2019-01-08 12:05:58 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      release_id: parseInt(releaseId, 10)
							 | 
						
					
						
							
								
									
										
										
										
											2020-03-20 13:28:31 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    });
							 | 
						
					
						
							
								
									
										
										
										
											2019-01-04 11:26:14 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    if (!result.data.draft) {
							 | 
						
					
						
							
								
									
										
										
										
											2020-03-20 13:28:31 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      console.log(`${fail} published releases cannot be deleted.`);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      return false;
							 | 
						
					
						
							
								
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    } else {
							 | 
						
					
						
							
								
									
										
										
										
											2019-01-08 12:05:58 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      await octokit.repos.deleteRelease({
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-30 17:55:27 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        owner: ELECTRON_ORG,
							 | 
						
					
						
							
								
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        repo: targetRepo,
							 | 
						
					
						
							
								
									
										
										
										
											2019-01-04 11:26:14 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        release_id: result.data.id
							 | 
						
					
						
							
								
									
										
										
										
											2020-03-20 13:28:31 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      });
							 | 
						
					
						
							
								
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							
								
									
										
										
										
											2020-03-20 13:28:31 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    console.log(`${pass} successfully deleted draft with id ${releaseId} from ${targetRepo}`);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    return true;
							 | 
						
					
						
							
								
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  } catch (err) {
							 | 
						
					
						
							
								
									
										
										
										
											2020-03-20 13:28:31 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    console.error(`${fail} couldn't delete draft with id ${releaseId} from ${targetRepo}: `, err);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    return false;
							 | 
						
					
						
							
								
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-30 17:55:27 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								async function deleteTag (tag: string, targetRepo: ElectronReleaseRepo) {
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-23 09:39:54 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  const octokit = new Octokit({
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    authStrategy: createGitHubTokenStrategy(targetRepo)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  });
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  try {
							 | 
						
					
						
							
								
									
										
										
										
											2019-01-08 12:05:58 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    await octokit.git.deleteRef({
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-30 17:55:27 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      owner: ELECTRON_ORG,
							 | 
						
					
						
							
								
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      repo: targetRepo,
							 | 
						
					
						
							
								
									
										
										
										
											2019-01-10 13:42:42 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      ref: `tags/${tag}`
							 | 
						
					
						
							
								
									
										
										
										
											2020-03-20 13:28:31 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    });
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    console.log(`${pass} successfully deleted tag ${tag} from ${targetRepo}`);
							 | 
						
					
						
							
								
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  } catch (err) {
							 | 
						
					
						
							
								
									
										
										
										
											2020-03-20 13:28:31 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    console.log(`${fail} couldn't delete tag ${tag} from ${targetRepo}: `, err);
							 | 
						
					
						
							
								
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-10-01 13:51:40 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								type CleanOptions = {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  releaseID?: string;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  tag: string;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								export async function cleanReleaseArtifacts ({ releaseID, tag }: CleanOptions) {
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-30 17:55:27 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  const releaseId = releaseID && releaseID.length > 0 ? releaseID : null;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  const isNightly = tag.includes('nightly');
							 | 
						
					
						
							
								
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-11-30 09:58:09 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  if (releaseId) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    if (isNightly) {
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-30 17:55:27 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      await deleteDraft(releaseId, NIGHTLY_REPO);
							 | 
						
					
						
							
								
									
										
										
										
											2019-01-18 14:00:15 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2020-03-10 03:13:10 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      // We only need to delete the Electron tag since the
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      // nightly tag is only created at publish-time.
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-30 17:55:27 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      await deleteTag(tag, ELECTRON_REPO);
							 | 
						
					
						
							
								
									
										
										
										
											2018-11-30 09:58:09 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    } else {
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-30 17:55:27 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      const deletedElectronDraft = await deleteDraft(releaseId, ELECTRON_REPO);
							 | 
						
					
						
							
								
									
										
										
										
											2018-11-30 09:58:09 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      // don't delete tag unless draft deleted successfully
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      if (deletedElectronDraft) {
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-30 17:55:27 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        await deleteTag(tag, ELECTRON_REPO);
							 | 
						
					
						
							
								
									
										
										
										
											2018-11-30 09:58:09 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							
								
									
										
										
										
											2019-01-18 14:00:15 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  } else {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    await Promise.all([
							 | 
						
					
						
							
								
									
										
										
										
											2024-09-30 17:55:27 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      deleteTag(tag, ELECTRON_REPO),
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      deleteTag(tag, NIGHTLY_REPO)
							 | 
						
					
						
							
								
									
										
										
										
											2020-03-20 13:28:31 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    ]);
							 | 
						
					
						
							
								
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2020-03-20 13:28:31 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  console.log(`${pass} failed release artifact cleanup complete`);
							 | 
						
					
						
							
								
									
										
										
										
											2018-08-29 11:13:22 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 |