Skip to content

Commit

Permalink
Merge pull request #2451 from intuit/force-release
Browse files Browse the repository at this point in the history
Fix uploading conflicting canary assets
  • Loading branch information
hipstersmoothie authored Apr 4, 2024
2 parents 10ab4f9 + 6a1d713 commit 730c6ab
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!
This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification, contributions of any kind welcome!

### Adding a Contributor

Expand Down
78 changes: 47 additions & 31 deletions plugins/upload-assets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export default class UploadAssetsPlugin implements IPlugin {
dryRun = false,
id?: string
) {
const releases = Array.isArray(release) ? release : [release];
const assets = await glob(this.options.assets);

auto.logger.log.info(endent`
Expand All @@ -162,18 +163,14 @@ export default class UploadAssetsPlugin implements IPlugin {
return [];
}

const responses = await Promise.all(
const assetUploadRequests = await Promise.all(
assets.map(async (asset) => {
if (!auto.git) {
return;
}

const file = await readFile(asset);
const stats = await stat(asset);
const type = await FileType.fromBuffer(file);

const DEFAULT_BASE_URL = "https://api.github.com";
const baseUrl = auto.git.options.baseUrl || DEFAULT_BASE_URL;
const baseUrl = auto.git!.options.baseUrl || DEFAULT_BASE_URL;
const fileName = path.basename(asset);
const extension = path.extname(fileName);
const options: RestEndpointMethodTypes["repos"]["uploadReleaseAsset"]["parameters"] = {
Expand All @@ -185,8 +182,8 @@ export default class UploadAssetsPlugin implements IPlugin {
? fileName.replace(extension, `-${id}${extension}`)
: `${fileName}-${id}`
: fileName,
owner: auto.git.options.owner,
repo: auto.git.options.repo,
owner: auto.git!.options.owner,
repo: auto.git!.options.repo,
headers: {
"content-length": stats.size,
"content-type": type ? type.mime : "application/octet-stream",
Expand All @@ -198,34 +195,53 @@ export default class UploadAssetsPlugin implements IPlugin {
options.baseUrl = `${origin}/api/uploads`;
}

const assetResponses: AssetResponse[] = [];
return options;
})
);

// Multiple releases were made
if (Array.isArray(release)) {
await Promise.all(
release.map(async (r) => {
const {
data: releaseAsset,
} = await auto.git!.github.repos.uploadReleaseAsset({
...options,
release_id: r.data.id,
});

assetResponses.push(releaseAsset);
})
);
} else {
const {
data: releaseAsset,
} = await auto.git.github.repos.uploadReleaseAsset({
...options,
const assetNames = assetUploadRequests.map((o) => o.name);
await Promise.all(
releases.map(async (release) => {
const assetsInRelease = await auto.git!.github.paginate(
auto.git!.github.repos.listReleaseAssets,
{
owner: auto.git!.options.owner,
repo: auto.git!.options.repo,
release_id: release.data.id,
});
}
);

assetResponses.push(releaseAsset);
for (const asset of assetsInRelease) {
if (assetNames.includes(asset.name)) {
// eslint-disable-next-line no-await-in-loop
await auto.git!.github.repos.deleteReleaseAsset({
owner: auto.git!.options.owner,
repo: auto.git!.options.repo,
asset_id: asset.id,
});
}
}
})
);

const responses = await Promise.all(
assetUploadRequests.map(async (options) => {
const assetResponses: AssetResponse[] = [];

await Promise.all(
releases.map(async (r) => {
const {
data: releaseAsset,
} = await auto.git!.github.repos.uploadReleaseAsset({
...options,
release_id: r.data.id,
});

assetResponses.push(releaseAsset);
})
);

auto.logger.log.success(`Uploaded asset: ${asset}`);
auto.logger.log.success(`Uploaded asset: ${options.name}`);
return assetResponses;
})
);
Expand Down

0 comments on commit 730c6ab

Please sign in to comment.