Skip to content

Instantly share code, notes, and snippets.

@noxi515
Created January 19, 2020 14:17
Show Gist options
  • Save noxi515/9bc5749fccd04ebb6f459c2393e2162f to your computer and use it in GitHub Desktop.
Save noxi515/9bc5749fccd04ebb6f459c2393e2162f to your computer and use it in GitHub Desktop.
Azure Pipeline definition to update AutoRest C# client source code and create new pull request.
name: $(Date:yyyyMMdd).$(Rev:r)
pool:
vmImage: 'ubuntu-latest'
steps:
- checkout: self
clean: true
persistCredentials: true
- task: UseNode@1
inputs:
version: '12.x'
- task: UseDotNet@2
inputs:
packageType: sdk
useGlobalJson: true
- task: PowerShell@2
displayName: Download JSON File
inputs:
targetType: inline
pwsh: true
script: |
$url = "https://example.com/swagger/v1/swagger.json"
$fileName = "${env:AGENT_TEMPDIRECTORY}/${env:BUILD_BUILDNUMBER}_swagger.json"
Write-Output "URL: $url"
Write-Output "FileName: $fileName"
Write-Output "Execute HTTP Request"
Invoke-RestMethod -Uri $url -OutFile $fileName
Write-Output "HTTP Reuqest done."
- task: PowerShell@2
displayName: Update ClientLibrary Code
enabled: false
inputs:
targetType: inline
pwsh: true
script: |
$fileName = "${env:AGENT_TEMPDIRECTORY}/${env:BUILD_BUILDNUMBER}_swagger.json"
Write-Output "FileName: $fileName"
npx autorest --csharp --add-credentials --namespace="ApiClient.Hoge" --input-file="$fileName" --output="./src/ApiClient.Hoge/generated"
- task: PowerShell@2
displayName: Git Commit and Push
inputs:
targetType: inline
pwsh: true
script: |
$branchName = "pr/client-update/ApiClient.Hoge/${env:BUILD_BUILDNUMBER}"
Write-Output "BranchName: $branchName"
git config --global user.email "tzo.ppl@gmail.com"
git config --global user.name "noxi515"
git checkout -b $branchName
git add .
git commit -m "Updaet client library code"
git push origin $branchName
- task: PowerShell@2
displayName: Create PR
env:
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
inputs:
targetType: inline
pwsh: true
script: |
$body = @{
sourceRefName = "refs/heads/pr/client-update/ApiClient.Hoge/${env:BUILD_BUILDNUMBER}"
targetRefName = "refs/heads/master"
title = "Update client library (ApiClient.Hoge)"
description = ""
}
$jsonBody = ConvertTo-Json $body
$url = "${env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI}${env:SYSTEM_TEAMPROJECT}/_apis/git/repositories/${env:BUILD_REPOSITORY_NAME}/pullrequests?api-version=5.0"
$headers = @{
Authorization = "Bearer ${env:SYSTEM_ACCESSTOKEN}"
}
Write-Output "URL: $url"
Write-Output "Body: $jsonBody"
Invoke-RestMethod -Uri $url -Method Post -Headers $headers -Body $jsonBody -ContentType application/json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment