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

Complete support for Releases API #177

Merged
merged 27 commits into from
Jul 20, 2020

Conversation

HowardWolosky
Copy link
Member

@HowardWolosky HowardWolosky commented May 22, 2020

Description

This completes the required work to support the set of Releases API's.

It adds the following functions:

  • New-GitHubRelease
  • Set-GitHubRelease
  • Remove-GitHubRelease
  • Get-GitHubReleaseAsset
  • New-GitHubReleaseAsset
  • Set-GitHubReleaseAsset
  • Remove-GitHubReleaseAsset

Invoke-GHRestMethod has been updated to be able to upload a file (via the new InFile parameter) and download a file (via the Save switch which will cause it to return back a FileInfo object of a temporary file which can then be renamed as seen fit by the caller).

This also adds formatters for GitHub.Release and GitHub.ReleaseAsset.

Positional Binding has been set as false for the three functions, and Position attributes added to the function's mandatory parameters.

Issues Fixed

Fixes #47
Fixes #110

References

GitHub Releases

Checklist

  • You actually ran the code that you just wrote, especially if you did just "one last quick change".
  • Comment-based help added/updated, including examples.
  • Static analysis is reporting back clean.
  • New/changed code adheres to our coding guidelines.
  • Changes to the manifest file follow the manifest guidance.
  • Unit tests were added/updated and are all passing. See testing guidelines.
  • Relevant usage examples have been added/updated in USAGE.md.
  • If desired, ensure your name is added to our Contributors list

@HowardWolosky HowardWolosky added api completeness This is basic API functionality that hasn't been implemented yet. api-repositories-releases Work to complete the API's defined here: https://developer.github.com/v3/repos/releases/ labels May 22, 2020
@HowardWolosky HowardWolosky force-pushed the releases branch 2 times, most recently from 82d5455 to bad95f4 Compare June 4, 2020 04:19
@rjmholt
Copy link
Contributor

rjmholt commented Jun 7, 2020

Ah, glad I saw this. Had begun adapting this to contribute here

GitHubCore.ps1 Outdated
@@ -130,6 +148,32 @@ function Invoke-GHRestMethod

Invoke-UpdateCheck

# Minor error checking around $InFile
if ((-not [String]::IsNullOrWhiteSpace($InFile)) -and ($Method -ne 'Post'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for using [string]::IsNullOrWhitespace() rather than $PSBoundParameter.ContainsKey()?

My thinking is that a [ValidateNotNullOrEmpty()] attribute will guarantee that some parameter value is passed, and checking $PSBoundParameters is a better way to distinguish whether the parameter was set (rather than an empty value being passed in)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair. I think my memory had flaked here on whether that could be used successfully on an optional parameter. I just verified that it would work fine. Will change.

function Invoke-NotNull {
    [cmdletbinding()]
    param(
        [ValidateNotNullOrEmpty()]
        [string] $Value
    )

    write-host "[$Value]"
}

Invoke-NotNull
[]
Invoke-NotNull -Value 'foo'
[foo]
Invoke-NotNull -Value $null
Invoke-NotNull : Cannot validate argument on parameter 'Value'.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scenario I have specifically in my head is something like:

New-GitHubRelease ... -Label $myLabel

in a case where I forgot to set $myLabel

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in a case where I forgot to set $myLabel

There is no Label for that function, so I'll assume your example was supposed to be -Description $myDescription.

Ah, so this is where it gets tricky. It's valid to have no description. I consider it a feature that earlier logic in your script might have determined that the description was $null and now you're just blindly passing it in. We'll take that input and just ignore it because [String]::IsNullOrEmpty. Is there a more consistent PowerShell philosophy in existing cmdlets where it doesn't work that way?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, so this is where it gets tricky. It's valid to have no description. I consider it a feature that earlier logic in your script might have determined that the description was $null and now you're just blindly passing it in.

Yeah, that's exactly why I'd use [ValidateNotNullOrEmpty()] on a non-mandatory parameter, to get this behaviour:

> function Test { param([ValidateNotNullOrEmpty()][Parameter()]$x) $x }

> Test

> Test -x $notSet
Test: Cannot validate argument on parameter 'x'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

> Test -x banana
banana

This way, it's perfectly valid to not specify the parameter, but if you do specify it, it shouldn't be the same as not specifying it.

The only issue there is that that means that more automated uses where the parameter is constructed, for example with splatting, have to be slightly more careful, since having a default value of null or empty string will cause a parameter binding failure.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, point is that it's totally philosophical, but just wanted to make sure it's a deliberate choice

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only issue there is that that means that more automated uses where the parameter is constructed, for example with splatting, have to be slightly more careful, since having a default value of null or empty string will cause a parameter binding failure.

That's the point I was trying to drive at. It seems like it's a positive feature to allow $null to be sent in and thus have the parameter ignored. But I'm just a single user, and so I am interested in hearing from others if they feel similarly or not.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think that's a totally valid point.

My personal usage, especially of a module like this, is a lot more interactive, and tends to involve something like executing lines that I've already written. But if I get them out of order and it quietly goes ahead, I have to go and fix it on GitHub (perhaps the lesson is that I should use -WhatIf...).

I don't feel that strongly about it. My real 2 cents are that it's less of a breaking change to turn it from an error into something that works later than to go the other way.

GitHubCore.ps1 Show resolved Hide resolved
GitHubCore.ps1 Show resolved Hide resolved
GitHubReleases.ps1 Outdated Show resolved Hide resolved
GitHubReleases.ps1 Outdated Show resolved Hide resolved
GitHubReleases.ps1 Show resolved Hide resolved
$Path = Resolve-UnverifiedPath -Path $Path
$file = Get-Item -Path $Path
$fileName = $file.Name
$fileNameEncoded = [System.Web.HTTPUtility]::UrlEncode($fileName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 880 to 1075
[Parameter(Mandatory)]
[ValidateScript({if (Test-Path -Path $_ -PathType Leaf) { $true } else { throw "$_ does not exist or is inaccessible." }})]
[string] $Path,

[string] $Label,

[string] $ContentType,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many releases have multiple assets. Given that this is a separate function, this could maybe implement ValueByPipelineProperty to allow sets of assets to be piped in

Comment on lines 108 to 109
$release = New-GitHubRelease -Uri $repo.svn_url -TagName $script:defaultTagName
$queried = Get-GitHubRelease -Uri $repo.svn_url -Release $release.id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think especially with the release of Pester 5, BeforeAll and AfterAll are highly recommended, rather than embedding setup and teardown directly in the testing block

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pester 5 support is being tracked by #194. Definitely desiring to have that support added. I haven't yet looked into what's involved for that though.

I agree with you that this setup work belongs in BeforeAll/AfterAll. At the time, I forgot about those and specifically wanted to avoid BeforeEach/AfterEach due to the added time from unnecessary duplicated setup that they would incur. Will look into moving setup/teardown as appropriate.

Thanks.

Tests/GitHubReleases.tests.ps1 Outdated Show resolved Hide resolved
@HowardWolosky
Copy link
Member Author

@rjmholt - Thanks for all the feedback on this. Much appreciated!

rjmholt
rjmholt previously approved these changes Jul 8, 2020
@HowardWolosky HowardWolosky marked this pull request as ready for review July 9, 2020 18:23
@HowardWolosky
Copy link
Member Author

/azp run PowerShellForGitHub-CI

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@HowardWolosky
Copy link
Member Author

/azp run PowerShellForGitHub-CI

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@HowardWolosky
Copy link
Member Author

/azp run PowerShellForGitHub-CI

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@HowardWolosky
Copy link
Member Author

/azp run PowerShellForGitHub-CI

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@HowardWolosky HowardWolosky force-pushed the releases branch 4 times, most recently from 52a001a to 86bba1f Compare July 18, 2020 19:08
@HowardWolosky
Copy link
Member Author

/azp run PowerShellForGitHub-CI

@HowardWolosky
Copy link
Member Author

/azp run PowerShellForGitHub-CI

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@HowardWolosky
Copy link
Member Author

/azp run PowerShellForGitHub-CI

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@HowardWolosky HowardWolosky merged commit 356af2f into microsoft:master Jul 20, 2020
@HowardWolosky HowardWolosky deleted the releases branch July 20, 2020 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api completeness This is basic API functionality that hasn't been implemented yet. api-repositories-releases Work to complete the API's defined here: https://developer.github.com/v3/repos/releases/
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GitHub Release support Enhance module by providing ability to query Release assets
2 participants