Skip to content

Instantly share code, notes, and snippets.

@h1bay1
Created April 23, 2018 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save h1bay1/2ca9fbabd610e237071a017f9504d141 to your computer and use it in GitHub Desktop.
Save h1bay1/2ca9fbabd610e237071a017f9504d141 to your computer and use it in GitHub Desktop.
The Final Product
Import-Module PSSlack
$checkoutPath = 'C:\Temp'
$repoUri = 'https://github.com/githubtraining/hellogitworld.git'
$repoFolder = 'hellogitworld'
$branchMatch = '*'
$defaultBranch = 'master'
$pushChanges = $false
$success = ":check2:"
$failed = ":x-cross:"
$mergeText = "*Merge Successful*"
$conflictsText = "*Found Conflicts*"
$slackApiKey = '12345'
$slackChannel = '#testchannel'
Push-Location
if(!(Test-Path -Path $checkoutPath)){
New-Item -ItemType directory -Path $checkoutPath
}
Set-Location $checkoutPath
if(!(Test-Path -Path "$checkoutPath\$repoFolder")){
Write-Host("Cloning $repoUri into $checkoutPath\$repoFolder")
git clone $repoUri $repoFolder 2>&1 | write-host
}
Set-Location $repoFolder
$resultList = @{}
$branches = git for-each-ref --format='%(refname:short)' refs/remotes/origin
foreach($branch in $branches){
$branchName = $branch.Substring(7, $branch.Length - 7)
if($branchName -like $branchMatch -And !($branchName -eq $defaultBranch)){
Write-Host("Checked out $fixedBranch") -ForegroundColor Green
git checkout "$branchName" 2>&1 | write-host
# pull any current branch changes
git pull 2>&1 | write-host
# merge in remote
git pull origin $defaultBranch 2>&1 | write-host
$workingDirectory = git status --porcelain
if($workingDirectory -eq $null){
# tree is clean
if($pushChanges)
{
git push 2>&1 | write-host
}
$resultList.add($branchName, $success)
Write-Host("$branchName successfully updated with changes from $DefaultBranch") -ForegroundColor Green
}
else {
# not clean
$resultList.add($branchName, $failed )
Write-Warning("Pull failed. Found conflicts.")
git clean -fdx 2>&1 | write-host
git reset --hard 2>&1 | write-host
}
# switch off so we can delete the copy we have
git checkout $defaultBranch 2>&1 | write-host
git branch -D $branchName 2>&1 | write-host
# reset working directory
git reset --hard 2>&1 | write-host
}
}
$slackMessage = "*Merge Master into branches that match `<$branchMatch>`* $nl"
$lastCommitMessage = git log -1 --pretty=%B
$lastCommitHash = git log --pretty=format:'%H' -n 1
$slackMessage += "The last commit was _<$lastCommitMessage>_ $nl $repoUri/commit/$lastCommitHash $nl"
if($pushChanges -eq $false){
$slackMessage += "``ReadOnly mode - no changes will be pushed`` $nl"
}
$groupedResults = $resultList.GetEnumerator() | Group-Object value
$groupedResults | Sort-Object -Descending | ForEach-Object{
if($_.Group[0].value -eq $success)
{
$slackMessage += $mergeText + $nl
}
else {
$slackMessage += $conflictsText + $nl
}
$_.Group | ForEach-Object {
$message = ""
if($_.Value -eq $success) {
$message = ">$($_.value) _$($_.key)_"
}
else {
$message = ">$($_.value) *$($_.key)*"
}
$slackMessage += "$message $nl"
}
}
Send-SlackMessage -IconEmoji :thegreatmerger: -Username 'The Great Merger' -Token $slackApiKey -Channel $slackChannel -Parse full -Text $slackMessage
Pop-Location
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment