Skip to content

Commit 730c6ab

Browse files
Merge pull request #2451 from intuit/force-release
Fix uploading conflicting canary assets
2 parents 10ab4f9 + 6a1d713 commit 730c6ab

File tree

2 files changed

+48
-32
lines changed

2 files changed

+48
-32
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
329329

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

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

334334
### Adding a Contributor
335335

plugins/upload-assets/src/index.ts

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ export default class UploadAssetsPlugin implements IPlugin {
149149
dryRun = false,
150150
id?: string
151151
) {
152+
const releases = Array.isArray(release) ? release : [release];
152153
const assets = await glob(this.options.assets);
153154

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

165-
const responses = await Promise.all(
166+
const assetUploadRequests = await Promise.all(
166167
assets.map(async (asset) => {
167-
if (!auto.git) {
168-
return;
169-
}
170-
171168
const file = await readFile(asset);
172169
const stats = await stat(asset);
173170
const type = await FileType.fromBuffer(file);
174171

175172
const DEFAULT_BASE_URL = "https://api.github.com";
176-
const baseUrl = auto.git.options.baseUrl || DEFAULT_BASE_URL;
173+
const baseUrl = auto.git!.options.baseUrl || DEFAULT_BASE_URL;
177174
const fileName = path.basename(asset);
178175
const extension = path.extname(fileName);
179176
const options: RestEndpointMethodTypes["repos"]["uploadReleaseAsset"]["parameters"] = {
@@ -185,8 +182,8 @@ export default class UploadAssetsPlugin implements IPlugin {
185182
? fileName.replace(extension, `-${id}${extension}`)
186183
: `${fileName}-${id}`
187184
: fileName,
188-
owner: auto.git.options.owner,
189-
repo: auto.git.options.repo,
185+
owner: auto.git!.options.owner,
186+
repo: auto.git!.options.repo,
190187
headers: {
191188
"content-length": stats.size,
192189
"content-type": type ? type.mime : "application/octet-stream",
@@ -198,34 +195,53 @@ export default class UploadAssetsPlugin implements IPlugin {
198195
options.baseUrl = `${origin}/api/uploads`;
199196
}
200197

201-
const assetResponses: AssetResponse[] = [];
198+
return options;
199+
})
200+
);
202201

203-
// Multiple releases were made
204-
if (Array.isArray(release)) {
205-
await Promise.all(
206-
release.map(async (r) => {
207-
const {
208-
data: releaseAsset,
209-
} = await auto.git!.github.repos.uploadReleaseAsset({
210-
...options,
211-
release_id: r.data.id,
212-
});
213-
214-
assetResponses.push(releaseAsset);
215-
})
216-
);
217-
} else {
218-
const {
219-
data: releaseAsset,
220-
} = await auto.git.github.repos.uploadReleaseAsset({
221-
...options,
202+
const assetNames = assetUploadRequests.map((o) => o.name);
203+
await Promise.all(
204+
releases.map(async (release) => {
205+
const assetsInRelease = await auto.git!.github.paginate(
206+
auto.git!.github.repos.listReleaseAssets,
207+
{
208+
owner: auto.git!.options.owner,
209+
repo: auto.git!.options.repo,
222210
release_id: release.data.id,
223-
});
211+
}
212+
);
224213

225-
assetResponses.push(releaseAsset);
214+
for (const asset of assetsInRelease) {
215+
if (assetNames.includes(asset.name)) {
216+
// eslint-disable-next-line no-await-in-loop
217+
await auto.git!.github.repos.deleteReleaseAsset({
218+
owner: auto.git!.options.owner,
219+
repo: auto.git!.options.repo,
220+
asset_id: asset.id,
221+
});
222+
}
226223
}
224+
})
225+
);
226+
227+
const responses = await Promise.all(
228+
assetUploadRequests.map(async (options) => {
229+
const assetResponses: AssetResponse[] = [];
230+
231+
await Promise.all(
232+
releases.map(async (r) => {
233+
const {
234+
data: releaseAsset,
235+
} = await auto.git!.github.repos.uploadReleaseAsset({
236+
...options,
237+
release_id: r.data.id,
238+
});
239+
240+
assetResponses.push(releaseAsset);
241+
})
242+
);
227243

228-
auto.logger.log.success(`Uploaded asset: ${asset}`);
244+
auto.logger.log.success(`Uploaded asset: ${options.name}`);
229245
return assetResponses;
230246
})
231247
);

0 commit comments

Comments
 (0)