diff --git a/AU/Plugins/PullRequest.ps1 b/AU/Plugins/PullRequest.ps1 new file mode 100644 index 0000000..b51cfff --- /dev/null +++ b/AU/Plugins/PullRequest.ps1 @@ -0,0 +1,91 @@ +<# +.SYNOPSIS + Creates a GitHub pull request for the updated packages. + +.DESCRIPTION + This plugin will open a GitHub pull request for the commits created by the Git plugin, which of course needs to run first. + It has options to add assignees and reviewers to the created pull request and, supports on-prem GitHub Enterprise Server. +#> +param( + $Info, + + # Git branch to create a GitHub pull request for + [string]$Branch, + + # Target Git branch for the GitHub pull request + [string]$BaseBranch = "master", + + # GitHub usernames to be added to the list of assignees + [string[]]$Assignees = @(), + + # GitHub usernames to be added to the list of reviewers + [string[]]$Reviewers = @(), + + # GitHub team slugs to be added to the list of reviewers + [string[]]$TeamReviewers = @(), + + # GitHub API base URL, overridable for GitHub Enterprise installations + [string]$GitHubAPI = "https://api.github.com", + + # Github ApiKey, create in GitHub profile -> Settings -> Personal access tokens -> Generate new token + [string]$ApiKey +) + +if ($Info.result.updated.Length -eq 0) { Write-Host "No package updated, skipping"; return } + +$ErrorActionPreference = "Stop" + +$origin = git config --get remote.origin.url +$originParts = $origin -split {$_ -eq "/" -or $_ -eq ":"} +$owner = $originParts[-2] +$repo = $originParts[-1] -replace "\.git$", "" + +#https://github.com/majkinetor/au/issues/142 +if ($PSVersionTable.PSVersion.major -ge 6) { + $AvailableTls = [enum]::GetValues('Net.SecurityProtocolType') | Where-Object { $_ -ge 'Tls' } # PowerShell 6+ does not support SSL3, so use TLS minimum + $AvailableTls.ForEach({[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor $_}) +} else { + [System.Net.ServicePointManager]::SecurityProtocol = 3072 -bor 768 -bor [System.Net.SecurityProtocolType]::Tls -bor [System.Net.SecurityProtocolType]::Ssl3 +} + +$data = @{ + title = git log -1 --pretty="%s" + head = $Branch + base = $BaseBranch +} +$params = @{ + ContentType = 'application/json' + Method = "POST" + Uri = "$GitHubAPI/repos/$owner/$repo/pulls" + Body = $data | ConvertTo-Json + UseBasicparsing = $true + Headers = @{ + 'Accept' = 'application/vnd.github.v3+json' + 'Authorization' = ('Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($ApiKey))) + } +} +$response = Invoke-WebRequest @params +$jsonResponse = $response.Content | ConvertFrom-Json +$prUrl = $jsonResponse.html_url +$prNumber = $jsonResponse.number + +if ($Assignees) { + $data = @{ + assignees = $Assignees + } + $params['Uri'] = "$GitHubAPI/repos/$owner/$repo/issues/$prNumber/assignees" + $params['Body'] = $data | ConvertTo-Json + $response = Invoke-WebRequest @params +} + +if ($Reviewers -or $TeamReviewers) { + $data = @{ + reviewers = $Reviewers + team_reviewers = $TeamReviewers + } + $params['Uri'] = "$GitHubAPI/repos/$owner/$repo/pulls/$prNumber/requested_reviewers" + $params['Body'] = $data | ConvertTo-Json + $response = Invoke-WebRequest @params +} + +Write-Host "Pull request sucessfully created: $prUrl" diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f6e77a..c04acb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - `Report`: - `Package_Source_Root_Url` param added to Markdown report type so links can be configured for non-Github users. ([#257](https://github.com/majkinetor/au/issues/257)) - `Package_Source_Branch` parameter added to Markdown report type to configure the branch name for the package source if not using master. ([#244](https://github.com/majkinetor/au/issues/244)) + - `PullRequest`: New plugin that creates a GitHub pull request for the updated packages. ([#269](https://github.com/majkinetor/au/pull/269)) ## 2021.7.18 diff --git a/Plugins.md b/Plugins.md index 696e32c..5b9831d 100644 --- a/Plugins.md +++ b/Plugins.md @@ -1,7 +1,7 @@ # Plugins -[Gist](#gist)   [Git](#git)   [GitLab](#gitlab)   [GitReleases](#gitreleases)   [Gitter](#gitter)   [History](#history)   [Mail](#mail)  [Report](#report)   [RunInfo](#runinfo)   [Snippet](#snippet) +[Gist](#gist)   [Git](#git)   [GitLab](#gitlab)   [GitReleases](#gitreleases)   [Gitter](#gitter)   [History](#history)   [Mail](#mail)  [PullRequest](#pullrequest) [Report](#report)   [RunInfo](#runinfo)   [Snippet](#snippet) --- @@ -74,6 +74,13 @@ This plugin requires Git plugin and that clone is done with adequate depth. * If you use Google mail for error notifications on a build server such as AppVeyor, Google may block authentication from unknown device. To receive those emails enable less secure apps - see [Allowing less secure apps to access your account](https://support.google.com/accounts/answer/6010255?hl=en). * If you do not want to use your private email for this, create a new Google account and redirect its messages to your private one. This wont affect you if you run the scripts from your own machine from which you usually access the email. +## [PullRequest](https://github.com/majkinetor/au/blob/master/AU/Plugins/PullRequest.ps1) + +**Create GitHub pull request for the updated packages**. + +The plugin will open a GitHub pull request for the commits created by the Git plugin, which of course needs to run first. +It has options to add assignees and reviewers to the created pull request and, supports on-prem GitHub Enterprise Server. + ## [Report](https://github.com/majkinetor/au/blob/master/AU/Plugins/Report.ps1) **Create different types of reports about the current run**.