Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
Add Pull Request plugin
Browse files Browse the repository at this point in the history
The plugin adds support for automatically creating a GitHub pull
request for the updated packages.
  • Loading branch information
easybe committed Sep 2, 2022
1 parent d6fa494 commit 54fe588
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
92 changes: 92 additions & 0 deletions AU/Plugins/PullRequest.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<#
.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"
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 8 additions & 1 deletion Plugins.md
Original file line number Diff line number Diff line change
@@ -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)

---

Expand Down Expand Up @@ -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**.
Expand Down

0 comments on commit 54fe588

Please sign in to comment.