Skip to content

Commit

Permalink
fix: workflow fails whenever there's asset with same build name attac…
Browse files Browse the repository at this point in the history
…hed on the draft (#208)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
kimlimjustin and lucasfernog authored Feb 20, 2022
1 parent 4d70258 commit 1205112
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changes/delete-assets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"action": patch
---

Delete assets from existing release, allowing running the action twice for the same version if an error happens.
2 changes: 1 addition & 1 deletion packages/action/dist/index.js

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions packages/action/src/create-release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@ interface Release {
htmlUrl: string
}

interface GitHubReleaseAsset {
id: number
name: string
state: string
size: number
browser_download_url: string
}

interface GitHubRelease {
id: number
upload_url: string
html_url: string
tag_name: string
assets: GitHubReleaseAsset[]
}

function allReleases(
Expand All @@ -25,6 +34,30 @@ function allReleases(
)
}


function getAssetPlatform(platform: string, fileName: string): string | null {
// macOS
if (
(fileName.includes(".app.tar.gz") || fileName.includes(".dmg")) &&
platform === "darwin"
) {
return 'darwin'
}

// Windows
if (fileName.includes('.msi') && platform === "win32") {
return 'win64'
}

// Linux
if ((fileName.includes('AppImage') || fileName.includes("deb")) && platform === "linux") {
return 'linux'
}

return null
}


export default async function createRelease(
tagName: string,
releaseName: string,
Expand Down Expand Up @@ -68,6 +101,19 @@ export default async function createRelease(
console.log(
`Found draft release with tag ${tagName} on the release list.`
)
// Remove all assets from the existing release
for (const asset of release.assets) {
if (getAssetPlatform(process.platform, asset.name)) {
console.log(
`Deleting asset ${asset.name} from the existing draft release`
)
await github.rest.repos.deleteReleaseAsset({
asset_id: asset.id,
owner,
repo,
})
}
}
break
}
}
Expand Down

0 comments on commit 1205112

Please sign in to comment.