Skip to content
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

Fix uploading conflicting canary assets #2451

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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