Skip to content

Instantly share code, notes, and snippets.

@rajbos
Created November 3, 2020 08:17
Show Gist options
  • Save rajbos/c4ff9619b9da7dd7f9062d69e0d364e5 to your computer and use it in GitHub Desktop.
Save rajbos/c4ff9619b9da7dd7f9062d69e0d364e5 to your computer and use it in GitHub Desktop.
# collection of GitLab api calls
$baseUrl = "https://private.gitlab.host/api/v4"
function GetToken{
$token = $env:GitLabToken
if ($null -ne $token) {
Write-Host "Found token as environment variable [GitLabToken]"
}
if ($null -eq $token) {
# try to load gitlab token from GitLab pipeline runner
$token = $CI_JOB_TOKEN
if ($null -ne $token) {
Write-Host "Found token from [CI_JOB_TOKEN]"
}
else {
Write-Error "Cannot find GitLab token to use!"
throw
}
}
return $token
}
$accesToken = GetToken
if ($null -eq $accesToken) {
Write-Error "Cannot find access token to use for GitLab. Specify env:GitLabToken"
return
}
function GetFromGitLab{
param(
[string] $url
)
$urlToCall = "$baseUrl/$url"
try {
$response = Invoke-RestMethod -Uri $urlToCall -Headers @{"PRIVATE-TOKEN" = $accesToken} -ContentType "application/json" -Method Get
return $response
}
catch {
Write-Host "Error $_"
}
}
function PostToGitLab{
param(
[string] $url,
[object] $body
)
$urlToCall = "$baseUrl/$url"
try {
$response = Invoke-RestMethod -Uri $urlToCall -Headers @{"PRIVATE-TOKEN" = $accesToken} -Body $body -ContentType "application/json" -Method Post
return $response
}
catch {
Write-Host "Error $_"
}
}
# returns the first matching project with the given searchName
function GetProjectByName{
param(
[string] $searchName
)
$url = "projects?simple=true&search=$searchName"
$response = GetFromGitLab -url $url
return $response[0]
}
function GetMergeRequests {
param (
[string] $projectId,
[string] $state = "opened"
)
$url = "projects/$projectId/merge_requests?state=$state"
$response = GetFromGitLab -url $url
return $response
}
function CreateNewMergeRequest{
param (
[string] $projectId,
[string] $sourceBranch,
[string] $targetBranch,
[string] $title
)
$url = "projects/$projectId/merge_requests"
$bodyObject = @{
source_branch = $sourceBranch
target_branch = $targetBranch
title = $title
}
$body = (ConvertTo-Json $bodyObject)
$response = PostToGitLab -url $url -Body $body
return $response
}
function CreateNewMergeRequestIfNoOpenOnes {
param (
[string] $projectId,
[string] $sourceBranchPrefix,
[string] $sourceBranch,
[string] $targetBranch,
[string] $title
)
$mergeRequests = GetMergeRequests -projectId $projectId
$existingBranchOpen = $false
foreach ($mr in $mergeRequests | Where-Object {$_.target_branch -eq $targetBranch}) {
if ($mr.source_branch.StartsWith($sourceBranchPrefix)) {
Write-Host "Found an existing MR: [$($mr.web_url)]"
$existingBranchOpen = $true
break
}
else {
Write-Host "Found an MR that doesn't match the search prefix [$sourceBranchPrefix]: [$($mr.source_branch)]"
}
}
if ($existingBranchOpen) {
Write-Host "Halting execution. Merge the open source branch first"
return
}
$mr = CreateNewMergeRequest -projectId $projectId -sourceBranch $sourceBranch -targetBranch $targetBranch -title $title
if ($mr) {
Write-Host "Created new Merge Request with url [$($mr.web_url)]"
}
}
function ExampleCalls {
$sourceBranch = "ISODATE-nuget-updates"
$sourceBranchPrefix = "ISODATE"
CreateNewMergeRequestIfNoOpenOnes -projectId 327 -sourceBranchPrefix $sourceBranchPrefix -sourceBranch $sourceBranch `
-targetBranch "main" -title "Bumping NuGet versions"
}
# global variables we use
$branchPrefix = "nuget-updates"
$gitLabProjectId = 327 # hard coded for now since GitLab project search is weird
function ExecuteUpdates {
param (
[string] $gitLabProjectId
)
git --version
git config user.email "CI-Pipeline@some-domain.com"
git config user.name "CI Pipeline"
# setting token for auth
git remote rm gitlab # remove the branch if already exists
git remote add gitlab https://xx:$($env:GitLabToken)@private.gitlab.host/path/to/project/repo.git
git remote -v
git pull
# install nukeeper
dotnet tool update nukeeper --tool-path .
# get update info
$updates = .\nukeeper inspect --outputformat csv
# since the update info is in csv, we'll need to search
$updatesFound = $false
foreach ($row in $updates) {
if ($row.IndexOf("possible updates") -gt -1) {
Write-Host "Found updates row [$row]";
if ($row.IndexOf("0 possible updates") -gt -1) {
Write-Host "There are no upates"
}
else {
Write-Host "There are updates"
$updatesFound = $true
break
}
}
}
if ($updatesFound) {
$branchName = CreateNewBranch
UpdatePackages
CommitAndPushBranch -branchName $branchName
CreateMergeRequest -branchName $branchName -branchPrefix $branchPrefix -gitLabProjectId $gitLabProjectId
}
else {
Write-Host "Found no updates"
}
}
function UpdatePackages {
.\nukeeper update
}
function CreateNewBranch {
# todo: create new branch with git
$ISODATE = (Get-Date -UFormat '+%Y%m%d')
$branchName = "$branchPrefix/$ISODATE"
git checkout -b $branchName
return $branchName
}
function CommitAndPushBranch {
param (
[string] $branchName
)
git add .
git commit -m "NuGet dependencies updated"
git push --set-upstream gitlab $branchName
}
function CreateMergeRequest {
param(
[string] $gitLabProjectId,
[string] $branchName,
[string] $targetBranch = "main",
[string] $branchPrefix
)
# get gitlab functions
. .\GitLab.ps1
$sourceBranch = $branchName
$sourceBranchPrefix = $branchPrefix
CreateNewMergeRequestIfNoOpenOnes -projectId $gitLabProjectId `
-sourceBranchPrefix $sourceBranchPrefix `
-sourceBranch $sourceBranch `
-targetBranch "main" `
-title "Bumping NuGet versions"
}
ExecuteUpdates -gitLabProjectId $gitLabProjectId
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment