mirror of
https://github.com/actions/versions-package-tools.git
synced 2025-12-10 03:13:23 +00:00
Move helpers to the separate repository
This commit is contained in:
106
github/create-pull-request.ps1
Normal file
106
github/create-pull-request.ps1
Normal file
@@ -0,0 +1,106 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Create commit with all unstaged changes in repository and create pull-request
|
||||
|
||||
.PARAMETER RepositoryOwner
|
||||
Required parameter. The organization which tool repository belongs
|
||||
.PARAMETER RepositoryName
|
||||
Optional parameter. The name of tool repository
|
||||
.PARAMETER AccessToken
|
||||
Required parameter. PAT Token to authorize
|
||||
.PARAMETER BranchName
|
||||
Required parameter. The name of branch where changes will be pushed
|
||||
.PARAMETER CommitMessage
|
||||
Required parameter. The commit message to push changes
|
||||
.PARAMETER PullRequestTitle
|
||||
Required parameter. The title of pull-request
|
||||
.PARAMETER PullRequestBody
|
||||
Required parameter. The description of pull-request
|
||||
#>
|
||||
param (
|
||||
[Parameter(Mandatory)] [string] $RepositoryOwner,
|
||||
[Parameter(Mandatory)] [string] $RepositoryName,
|
||||
[Parameter(Mandatory)] [string] $AccessToken,
|
||||
[Parameter(Mandatory)] [string] $BranchName,
|
||||
[Parameter(Mandatory)] [string] $CommitMessage,
|
||||
[Parameter(Mandatory)] [string] $PullRequestTitle,
|
||||
[Parameter(Mandatory)] [string] $PullRequestBody
|
||||
)
|
||||
|
||||
Import-Module (Join-Path $PSScriptRoot "github-api.psm1")
|
||||
Import-Module (Join-Path $PSScriptRoot "git.psm1")
|
||||
|
||||
function Update-PullRequest {
|
||||
Param (
|
||||
[Parameter(Mandatory=$true)]
|
||||
[object] $GitHubApi,
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string] $Title,
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string] $Body,
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string] $BranchName,
|
||||
[Parameter(Mandatory=$true)]
|
||||
[object] $PullRequest
|
||||
)
|
||||
|
||||
$updatedPullRequest = $GitHubApi.UpdatePullRequest($Title, $Body, $BranchName, $PullRequest.number)
|
||||
|
||||
if (($updatedPullRequest -eq $null) -or ($updatedPullRequest.html_url -eq $null)) {
|
||||
Write-Host "##vso[task.logissue type=error;] Unexpected error occurs while updating pull request."
|
||||
exit 1
|
||||
}
|
||||
Write-host "##[section] Pull request updated: $($updatedPullRequest.html_url)"
|
||||
}
|
||||
|
||||
function Create-PullRequest {
|
||||
Param (
|
||||
[Parameter(Mandatory=$true)]
|
||||
[object] $GitHubApi,
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string] $Title,
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string] $Body,
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string] $BranchName
|
||||
)
|
||||
|
||||
$createdPullRequest = $GitHubApi.CreateNewPullRequest($Title, $Body, $BranchName)
|
||||
|
||||
if (($createdPullRequest -eq $null) -or ($createdPullRequest.html_url -eq $null)) {
|
||||
Write-Host "##vso[task.logissue type=error;] Unexpected error occurs while creating pull request."
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-host "##[section] Pull request created: $($createdPullRequest.html_url)"
|
||||
}
|
||||
|
||||
Write-Host "Configure local git preferences"
|
||||
Git-ConfigureUser -Name "Service account" -Email "no-reply@microsoft.com"
|
||||
|
||||
Write-Host "Create branch: $BranchName"
|
||||
Git-CreateBranch -Name $BranchName
|
||||
|
||||
Write-Host "Create commit"
|
||||
Git-CommitAllChanges -Message $CommitMessage
|
||||
|
||||
Write-Host "Push branch: $BranchName"
|
||||
Git-PushBranch -Name $BranchName -Force $true
|
||||
|
||||
$gitHubApi = Get-GitHubApi -AccountName $RepositoryOwner -ProjectName $RepositoryName -AccessToken $AccessToken
|
||||
$pullRequest = $gitHubApi.GetPullRequest($BranchName, $RepositoryOwner)
|
||||
|
||||
if ($pullRequest.Count -gt 0) {
|
||||
Write-Host "Update pull request"
|
||||
Update-PullRequest -GitHubApi $gitHubApi `
|
||||
-Title $PullRequestTitle `
|
||||
-Body $PullRequestBody `
|
||||
-BranchName $BranchName `
|
||||
-PullRequest $pullRequest[0]
|
||||
} else {
|
||||
Write-Host "Create pull request"
|
||||
Create-PullRequest -GitHubApi $gitHubApi `
|
||||
-Title $PullRequestTitle `
|
||||
-Body $PullRequestBody `
|
||||
-BranchName $BranchName
|
||||
}
|
||||
81
github/git.psm1
Normal file
81
github/git.psm1
Normal file
@@ -0,0 +1,81 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Configure git credentials to use with commits
|
||||
#>
|
||||
function Git-ConfigureUser {
|
||||
Param (
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string] $Name,
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string] $Email
|
||||
)
|
||||
|
||||
git config --global user.name $Name | Out-Host
|
||||
git config --global user.email $Email | Out-Host
|
||||
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-Host "##vso[task.logissue type=error;] Unexpected failure occurs while configuring git preferences."
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Create new branch
|
||||
#>
|
||||
function Git-CreateBranch {
|
||||
Param (
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string] $Name
|
||||
)
|
||||
|
||||
git checkout -b $Name | Out-Host
|
||||
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-Host "##vso[task.logissue type=error;] Unexpected failure occurs while creating new branch: $Name."
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Commit all staged and unstaged changes
|
||||
#>
|
||||
function Git-CommitAllChanges {
|
||||
Param (
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string] $Message
|
||||
)
|
||||
|
||||
git add -A | Out-Host
|
||||
git commit -m "$Message" | Out-Host
|
||||
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-Host "##vso[task.logissue type=error;] Unexpected failure occurs while commiting changes."
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Push branch to remote repository
|
||||
#>
|
||||
function Git-PushBranch {
|
||||
Param (
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string] $Name,
|
||||
[Parameter(Mandatory=$true)]
|
||||
[boolean] $Force
|
||||
)
|
||||
|
||||
if ($Force) {
|
||||
git push --set-upstream origin $Name --force | Out-Host
|
||||
} else {
|
||||
git push --set-upstream origin $Name | Out-Host
|
||||
}
|
||||
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-Host "##vso[task.logissue type=error;] Unexpected failure occurs while pushing changes."
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
109
github/github-api.psm1
Normal file
109
github/github-api.psm1
Normal file
@@ -0,0 +1,109 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
The module that contains a bunch of methods to interact with GitHub API V3
|
||||
#>
|
||||
class GitHubApi
|
||||
{
|
||||
[string] $BaseUrl
|
||||
[string] $RepoOwner
|
||||
[object] $AuthHeader
|
||||
|
||||
GitHubApi(
|
||||
[string] $AccountName,
|
||||
[string] $ProjectName,
|
||||
[string] $AccessToken
|
||||
) {
|
||||
$this.BaseUrl = $this.BuildBaseUrl($AccountName, $ProjectName)
|
||||
$this.AuthHeader = $this.BuildAuth($AccessToken)
|
||||
}
|
||||
|
||||
[object] hidden BuildAuth([string]$AccessToken) {
|
||||
if ([string]::IsNullOrEmpty($AccessToken)) {
|
||||
return $null
|
||||
}
|
||||
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("'':${AccessToken}"))
|
||||
return @{
|
||||
Authorization = "Basic ${base64AuthInfo}"
|
||||
}
|
||||
}
|
||||
|
||||
[string] hidden BuildBaseUrl([string]$RepositoryOwner, [string]$RepositoryName) {
|
||||
return "https://api.github.com/repos/$RepositoryOwner/$RepositoryName"
|
||||
}
|
||||
|
||||
[object] CreateNewPullRequest([string]$Title, [string]$Body, [string]$BranchName){
|
||||
$requestBody = @{
|
||||
title = $Title
|
||||
body = $Body
|
||||
head = $BranchName
|
||||
base = "master"
|
||||
} | ConvertTo-Json
|
||||
|
||||
$url = "pulls"
|
||||
return $this.InvokeRestMethod($url, 'Post', $null, $requestBody)
|
||||
}
|
||||
|
||||
[object] GetPullRequest([string]$BranchName, [string]$RepositoryOwner){
|
||||
$url = "pulls"
|
||||
return $this.InvokeRestMethod($url, 'GET', "head=${RepositoryOwner}:$BranchName&base=master", $null)
|
||||
}
|
||||
|
||||
[object] UpdatePullRequest([string]$Title, [string]$Body, [string]$BranchName, [string]$PullRequestNumber){
|
||||
$requestBody = @{
|
||||
title = $Title
|
||||
body = $Body
|
||||
head = $BranchName
|
||||
base = "master"
|
||||
} | ConvertTo-Json
|
||||
|
||||
$url = "pulls/$PullRequestNumber"
|
||||
return $this.InvokeRestMethod($url, 'Post', $null, $requestBody)
|
||||
}
|
||||
|
||||
[object] GetGitHubReleases(){
|
||||
$url = "releases"
|
||||
return $this.InvokeRestMethod($url, 'GET', $null, $null)
|
||||
}
|
||||
|
||||
[string] hidden BuildUrl([string]$Url, [string]$RequestParams) {
|
||||
if ([string]::IsNullOrEmpty($RequestParams)) {
|
||||
return "$($this.BaseUrl)/$($Url)"
|
||||
} else {
|
||||
return "$($this.BaseUrl)/$($Url)?$($RequestParams)"
|
||||
}
|
||||
}
|
||||
|
||||
[object] hidden InvokeRestMethod(
|
||||
[string] $Url,
|
||||
[string] $Method,
|
||||
[string] $RequestParams,
|
||||
[string] $Body
|
||||
) {
|
||||
$requestUrl = $this.BuildUrl($Url, $RequestParams)
|
||||
$params = @{
|
||||
Method = $Method
|
||||
ContentType = "application/json"
|
||||
Uri = $requestUrl
|
||||
Headers = @{}
|
||||
}
|
||||
if ($this.AuthHeader) {
|
||||
$params.Headers += $this.AuthHeader
|
||||
}
|
||||
if (![string]::IsNullOrEmpty($Body)) {
|
||||
$params.Body = $Body
|
||||
}
|
||||
|
||||
return Invoke-RestMethod @params
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function Get-GitHubApi {
|
||||
param (
|
||||
[string] $AccountName,
|
||||
[string] $ProjectName,
|
||||
[string] $AccessToken
|
||||
)
|
||||
|
||||
return [GitHubApi]::New($AccountName, $ProjectName, $AccessToken)
|
||||
}
|
||||
Reference in New Issue
Block a user