diff --git a/Posh-Github.psm1 b/Posh-Github.psm1 index 3ab948a..4d4811b 100644 --- a/Posh-Github.psm1 +++ b/Posh-Github.psm1 @@ -32,6 +32,65 @@ function GetRemotes return $remotes } +function FallBackToUserStyle($Owner, $Repository) +{ + # with no parameters specified, fall back to user style + if ([string]::IsNullOrEmpty($Owner) -or [string]::IsNullOrEmpty($Repository)) + { + if ([string]::IsNullOrEmpty($Env:GITHUB_OAUTH_TOKEN)) + { + throw ("Could not find valid repository to query for issues" + + " and no GITHUB_OAUTH_TOKEN was found ") + } + return $true + } + return $false +} + +function IsMissing($Value) +{ + [string]::IsNullOrEmpty($Value) +} + +function ResolveRepository() +{ + [CmdletBinding()] + param( + [Parameter(Mandatory = $false, Position=0)] + [string] + $Owner = $null, + + [Parameter(Mandatory = $false, Position=1)] + [string] + $Repository = $null + ) + + if ((IsMissing $Owner) -and (IsMissing $Repository)) + { + $remotes = GetRemotes + #first remote in order here wins! + 'upstream', 'origin' | + % { + if ([string]::IsNullOrEmpty($Owner) -and $remotes.$_) + { + $Owner = $remotes.$_.owner + $Repository = $remotes.$_.repository + Write-Host "Found $_ remote $Owner/$Repository" + } else { + $Owner = $null + $Repository = $null + } + } + } + elseif ($missingOwner -or $missingRepo) + { + throw "An Owner and Repository must be specified together" + } + + $Owner + $Repository +} + # Get-GitDirectory and Get-LocalOrParentPath are Posh-Git helpers function Get-GitDirectory { @@ -225,7 +284,7 @@ function Get-GitHubIssues { [CmdletBinding(DefaultParameterSetName='repo')] param( - [Parameter(Mandatory = $false, Position=0, ParameterSetName='repo')] + [Parameter(Mandatory = $false, Position=0, ParameterSetName='repo')] [string] $Owner = $null, @@ -285,42 +344,31 @@ function Get-GitHubIssues { 'repo' { - $missingOwner = [string]::IsNullOrEmpty($Owner) - $missingRepo = [string]::IsNullOrEmpty($Repository) - if ($missingOwner -and $missingRepo) + $repo = ResolveRepository $Owner $Repository + $Owner = $repo[0] + $Repository = $repo[1] + + if(FallBackToUserStyle $Owner $Repository) { - $remotes = GetRemotes - #first remote in order here wins! - 'upstream', 'origin' | - % { - if ([string]::IsNullOrEmpty($Owner) -and $remotes.$_) - { - $Owner = $remotes.$_.owner - $Repository = $remotes.$_.repository - Write-Host "Found $_ remote with owner $Owner" - } - } + return GetUserIssues $Filter.ToLower() $State.ToLower() $Labels ` + $Sort.ToLower() $Direction.ToLower() $PsBoundParameters.Since + } else { + echo "Repo-Style" + } - # with no parameters specified, fall back to user style - if ([string]::IsNullOrEmpty($Owner)) - { - if ([string]::IsNullOrEmpty($Env:GITHUB_OAUTH_TOKEN)) - { - throw ("Could not find valid repository to query for issues" + - " and no GITHUB_OAUTH_TOKEN was found ") - } + $missingOwner = [string]::IsNullOrEmpty($Owner) + $missingRepo = [string]::IsNullOrEmpty($Repository) - return GetUserIssues $Filter.ToLower() $State.ToLower() $Labels ` - $Sort.ToLower() $Direction.ToLower() $PsBoundParameters.Since - } - } - elseif ($missingOwner -or $missingRepo) + if ($missingOwner -or $missingRepo) { throw "An Owner and Repository must be specified together" } # accept null or lower case - if ($Milestone) { $Milestone = $Milestone.ToLower() } + if ($Milestone) { + $Milestone = $Milestone.ToLower() + } + GetRepoIssues $Owner $Repository $Milestone $State.ToLower() ` $Assignee $Creator $Mentioned $Labels $Sort.ToLower() ` $Direction.ToLower() $PsBoundParameters.Since @@ -1170,8 +1218,130 @@ function Clear-GitMergedBranches } } +function GetRefStatus($Owner, $Repository, $Ref) +{ + $uri = ("https://api.github.com/repos/$Owner/$Repository/statuses/$Ref" + + "?access_token=${Env:\GITHUB_OAUTH_TOKEN}") + + Invoke-RestMethod -Uri $uri +} + +function SetRefStatus($Owner, $Repository, $Ref, $State, $Description, $Context, $TargetUrl) +{ + $uri = ("https://api.github.com/repos/$Owner/$Repository/statuses/$Ref" + + "?access_token=${Env:\GITHUB_OAUTH_TOKEN}") + + if(IsMissing $Context) { + $Context = "default" + } + + $postData = @{ + state = $State + description = $Description + context = $Context + } + + if(-not(IsMissing $TargetUrl)) { + $postData.target_url = $TargetUrl + } + + $params = @{ + Uri = $uri; + Method = 'POST'; + ContentType = 'application/json' + Body = (ConvertTo-Json $postData -Compress) + } + + Invoke-RestMethod @params + +} + +function Get-GitHubStatus { + [CmdletBinding(DefaultParameterSetName='repo')] + param( + [Parameter(Mandatory = $false, Position=0)] + [string] + $Owner = "", + + [Parameter(Mandatory = $false, Position=1)] + [string] + $Repository = "", + + [Parameter(Mandatory = $false, Position=2)] + [string] + $Ref = $null + ) + + + $repo = ResolveRepository $Owner $Repository + $Owner = $repo[0] + $Repository = $repo[1] + + $missing = IsMissing $Repository + + if((IsMissing $Owner) -or (IsMissing $Repository)) { + throw ("Cound not find suitable Owner/Repository") + } elseif(IsMissing $Ref) { + throw ("Ref missing. Statuses are always related to a SHA, branch name or tag name") + } + + $response = GetRefStatus $Owner $Repository $Ref + + $response | ForEach { + Write-Host "$($_.updated_at) $($_.state) - $($_.description)" + } +} + +function Set-GitHubStatus { + [CmdletBinding(DefaultParameterSetName='repo')] + param( + [Parameter(Mandatory = $true, Position=0)] + [string] + $Owner = "", + + [Parameter(Mandatory = $true, Position=1)] + [string] + $Repository = "", + + [Parameter(Mandatory = $true, Position=2)] + [string] + $Ref = $null, + + [Parameter(Mandatory = $false, Position=3)] + [ValidateSet('pending', 'success', 'error', 'failure')] + $State = $null, + + [Parameter(Mandatory = $false, Position=4)] + [string] + $Description = $null, + + [Parameter(Mandatory = $false)] + [string] + $Context = "default", + + [Parameter(Mandatory = $false)] + [string] + $TargetUrl = $null + ) + + $repo = ResolveRepository $Owner $Repository + $Owner = $repo[0] + $Repository = $repo[1] + + $missing = IsMissing $Repository + + if((IsMissing $Owner) -or (IsMissing $Repository)) { + throw ("Cound not find suitable Owner/Repository") + } elseif(IsMissing $Ref) { + throw ("Ref missing. Statuses are always related to a SHA, branch name or tag name") + } + + SetRefStatus $Owner $Repository $Ref $State $Description $Context $TargetUrl +} + Export-ModuleMember -Function New-GitHubOAuthToken, New-GitHubPullRequest, Get-GitHubIssues, Get-GitHubEvents, Get-GitHubRepositories, Get-GitHubPullRequests, Set-GitHubUserName, Set-GitHubOrganization, Get-GitHubTeams, New-GitHubRepository, New-GitHubFork, - Clear-GitMergedBranches, Backup-GitHubRepositories + Clear-GitMergedBranches, Backup-GitHubRepositories, + Get-GitHubStatus, Set-GitHubStatus