Skip to content

Commit

Permalink
Create tutorial branch automation (#1075)
Browse files Browse the repository at this point in the history
* Create tutorial branch script and pipeline. (#1074)
  • Loading branch information
mitchdenny authored Oct 12, 2020
1 parent 49b1b81 commit b66d7d8
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
57 changes: 57 additions & 0 deletions eng/pipelines/setup-tutorial-branch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
pr: none
trigger: none

# This pipeline is used to trigger a script which creates a branch in the repository specified
# by the Repository parameter below. It creates a branch with the name feature/tutorial/<alias>
# and will be used in training to help training participants get a branch setup to merge their
# code into to simulate real world CI/CD usage without poluting the master branch.
#
# The Alias parameter forms part of the branch name above, and the GitReference parameter which
# has a default of refs/heads/feature/tutorial can be overriden if a different base branch is
# desired (this parameter may be removed in the future once our approach is more settled).

parameters:
- name: Repository
type: string
values:
- azure-sdk-for-net
- azure-sdk-for-java
- azure-sdk-for-python
- azure-sdk-for-js
- name: Alias
type: string
- name: GitReference
type: string
default: refs/heads/feature/tutorial/api-design/starter
values:
- refs/heads/feature/tutorial/api-design/starter
- refs/heads/feature/tutorial/api-design/complete
- refs/heads/feature/tutorial/implementation/starter
- refs/heads/feature/tutorial/implementation/complete
- refs/heads/feature/tutorial/engsys/starter
- refs/heads/feature/tutorial/engsys/complete
- refs/heads/feature/tutorial/quality/starter
- refs/heads/feature/tutorial/quality/complete
- refs/heads/feature/tutorial/xyz/starter
- refs/heads/feature/tutorial/xyz/complete

variables:
skipComponentGovernanceDetection: true

jobs:
- job: SetupTutorialBranch
pool:
vmImage: 'ubuntu-latest'
steps:
- task: PowerShell@2
displayName: 'Setup Tutorial Branch'
inputs:
targetType: filePath
filePath: "scripts/powershell/Setup-TutorialBranch.ps1"
arguments: >
-GitHubRepositoryOwner Azure
-GitHubRepositoryName ${{parameters.Repository}}
-GitReference ${{parameters.GitReference}}
-BranchName "feature/tutorial/${{parameters.Alias}}"
-GitHubPersonalAccessToken "$(azuresdk-github-pat)"
pwsh: true
91 changes: 91 additions & 0 deletions scripts/powershell/Setup-TutorialBranch.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
param (
[Parameter(Mandatory=$true)]
[ValidatePattern("^[\w\-]+$")]
[string]
$GitHubRepositoryOwner,

[Parameter(Mandatory=$true)]
[ValidatePattern("^[\w\-]+$")]
[string]
$GitHubRepositoryName,

[Parameter(Mandatory=$true)]
[ValidatePattern("^[\w\-\/]+$")]
[string]
$GitReference,

[Parameter(Mandatory=$true)]
[string]
[ValidatePattern("^[\w\-\/]+$")]
$BranchName,

[Parameter(Mandatory=$true)]
[string]
$GitHubPersonalAccessToken
)

$ErrorActionPreference = "Stop"

Write-Host "GitHubRepositoryOwner is: $GitHubRepositoryOwner"
Write-Host "GitHubRepositoryName is: $GitHubRepositoryName"
Write-Host "GitReference is: $GitReference"
Write-Host "BranchName is: $BranchName"

$secureGitHubPersonalAccessToken = ConvertTo-SecureString -String $GitHubPersonalAccessToken -AsPlainText -Force

$getUri = "https://api.github.com/repos/$GitHubRepositoryOwner/$GitHubRepositoryName/git/$GitReference"
Write-Host "Retrieving SHA for: $getUri"

$getResponse = Invoke-RestMethod `
-Method GET `
-Uri $getUri `
-Authentication Bearer `
-Token $secureGitHubPersonalAccessToken

$gitReferenceSha = $getResponse.object.sha
Write-Host "SHA for $GitReference is: $gitReferenceSha"

Write-Host "Creating/updating branch: $BranchName"

try {
$patchBody = @{
sha = $gitReferenceSha
force = $true
}
$patchBodyAsJson = ConvertTo-Json -InputObject $patchBody

$patchUri = "https://api.github.com/repos/$GitHubRepositoryOwner/$GitHubRepositoryName/git/refs/heads/$BranchName"
Write-Host "Attempting to update ref for: $patchUri"

$patchResponse = Invoke-RestMethod `
-Method PATCH `
-Uri $patchUri `
-Authentication Bearer `
-Token $secureGitHubPersonalAccessToken `
-Body $patchBodyAsJson `
-ContentType "application/json"

Write-Host "Successfully updated ref."
}
catch {
Write-Host "Failed to update branch, attempting to create."

$postBody = @{
ref = "refs/heads/$BranchName"
sha = $gitReferenceSha
}
$postBodyAsJson = ConvertTo-Json -InputObject $postBody

$postUri = "https://api.github.com/repos/$GitHubRepositoryOwner/$GitHubRepositoryName/git/refs"
Write-Host "Attempting to update ref for: $postUri"

$postResponse = Invoke-RestMethod `
-Method POST `
-Uri $postUri `
-Authentication Bearer `
-Token $secureGitHubPersonalAccessToken `
-Body $postBodyAsJson `
-ContentType "application/json"

Write-Host "Successfully created ref."
}

0 comments on commit b66d7d8

Please sign in to comment.