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

[Bug]: Will not delete old versions with max-versions-to-keep set to 1, if only 1 version exists. #307

Closed
1 task done
maforget opened this issue Oct 2, 2024 · 6 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@maforget
Copy link

maforget commented Oct 2, 2024

Is there an existing issue for this?

  • I have searched the existing issues

What happened?

First I am new to winget and still unsure about a couple of things. I was kind of forced to learn it, because someone submitted my program to it, without thinking that it would break the moment I made a new build and the installer checksum would change and not install anymore.

A contributor from the winget team did a PR to implement your action to my workflow to help automate things. They had put the max-versions-to-keep to 5. But since I am using a single url for a nightly version, keeping any other version than the last one is worthless since they would all point to the same link and any other would fail.

I also am using the commit sha as a version number, so I wanted to keep only 1 version to make sure that it would upgrade correctly (not certain about that one still).

When I ran my latest build it only deleted the oldest one and not both version that were already in the winget repo, I see now that it was ran on v2, and you stated to use main instead. I've changed that for the future.

But I was looking at your latest code to make sure and I believe I spotted a possible bug.

If ($Versions.Count + 1 -gt ${{ inputs.max-versions-to-keep }}) {
$VersionsToDelete = $Versions[(${{ inputs.max-versions-to-keep }} - 1)..($Versions.Count - 1)]
Write-Output "Versions to delete: $($VersionsToDelete -join ', ')"

Say that I have max-versions-to-keep = 1 and I have 2 versions already in the repo (I assume that $Versions.Count would not include the just created manifest). Then $VersionsToDelete = $Versions[0..1)], which would return both versions correctly removing both and keeping only the latest just created one.

But then the build after I would have only 1 version so it would result in $VersionsToDelete = $Versions[0..0)], which would return nothing and so delete nothing. So I would end up with 2 versions. Then on the subsequent run it would delete both and keep the latest, then 2, etc.

At least that is what I get when I test it in my powershell environment, not certain about running it via actions. Not going to test it out without any reasons.

So maybe it would be better to have something like (test code):

$Versions = 1, 2;
$max_versions_to_keep = 1;

If ($Versions.Count + 1 -gt $max_versions_to_keep ) {
  $VersionsToDelete = if ($Versions.Count -eq 1)  {$Versions[0]} else {$Versions[($max_versions_to_keep - 1)..($Versions.Count - 1)]}
  # $VersionsToDelete = $Versions[($max_versions_to_keep - 1)..($Versions.Count - 1)];
  Write-Output "Versions to delete: $($VersionsToDelete -join ', ')";
};

Please correct me if there is a reason or if I am mistaking anywhere.

@maforget maforget added bug Something isn't working help wanted Extra attention is needed labels Oct 2, 2024
@vedantmgoyal9
Copy link
Owner

Hi, I checked the latest code, and it seems to be working correctly. I think this is/might be a bug in v2, which does not use the code you've mentioned. Can you do a run with main branch and tell if it works or not?

@maforget
Copy link
Author

maforget commented Oct 7, 2024

I will report when I have an update to post. There was a problem with v2, but this is code from the main branch, which is why I mentioned it. I was checking it before doing an update.

@maforget
Copy link
Author

maforget commented Oct 12, 2024

Just pushed an update, it failed to delete the previous version. $VersionsToDelete returns n, and the Deleting version output message returns n also instead of (nightly-7635d6e).

https://github.com/maforget/ComicRackCE/actions/runs/11308262081/job/31450951821#step:3:200

Run # delete previous versions w.r.t. max-versions-to-keep (if any) 
  # delete previous versions w.r.t. max-versions-to-keep (if any) 
  $ToNatural = { [regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20) }) }
  $Versions = komac list-versions 'maforget.ComicRackCE.Nightly' --json | ConvertFrom-Json | Sort-Object $ToNatural -Descending
  $Reason = 'This version is older than what has been set in `max-versions-to-keep` by the publisher.'
  
  If ($Versions.Count + 1 -gt 1) {
    $VersionsToDelete = $Versions[(1 - 1)..($Versions.Count - 1)]
    Write-Output "Versions to delete: $($VersionsToDelete -join ', ')"
  
    ForEach ($Version in $VersionsToDelete) {
      Write-Output "Deleting version: $Version"
      komac remove 'maforget.ComicRackCE.Nightly' --version $Version --reason "$Reason" --submit
    }
  } Else {
    Write-Output "No versions to delete. All good :)"
  }
  shell: /usr/bin/pwsh -command ". '{0}'"
  env:
    KOMAC_FORK_OWNER: maforget
    KOMAC_CREATED_WITH: WinGet Releaser
    KOMAC_CREATED_WITH_URL: https://github.com/vedantmgoyal9/winget-releaser
    GITHUB_TOKEN: ***
Versions to delete: n
Deleting version: n
Packages should only be removed when necessary
Error: 
   0: maforget.ComicRackCE.Nightly version n does not exist in microsoft/winget-pkgs
Error: Process completed with exit code 1.

@maforget
Copy link
Author

And as I predicted on the following build, it deleted both remaining versions correctly.

When more than 1 version exists it works correctly, but not when there is only one.

vedantmgoyal9 added a commit that referenced this issue Nov 2, 2024
@vedantmgoyal9 vedantmgoyal9 removed their assignment Nov 2, 2024
@vedantmgoyal9
Copy link
Owner

@all-contributors please add @maforget for bug

Copy link
Contributor

@vedantmgoyal9

I've put up a pull request to add @maforget! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants