forked from microsoft/dotnet-framework-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAzureDevOps.ps1
More file actions
74 lines (64 loc) · 2.14 KB
/
Copy pathAzureDevOps.ps1
File metadata and controls
74 lines (64 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env pwsh
# Lightweight wrapper for authenticated Azure DevOps REST API calls.
# Uses `az account get-access-token` for bearer token auth.
#
# Usage:
# . ./AzureDevOps.ps1
# $response = Invoke-AzDORestMethod -Organization myorg -Project myproject `
# -Endpoint "pipelines/42/runs" -Method POST -Body @{ resources = @{} }
$ErrorActionPreference = "Stop"
function Get-AzDOAccessToken {
<#
.SYNOPSIS
Returns a bearer token for Azure DevOps.
#>
# Well-known Entra ID application ID for Azure DevOps
$tokenJson = az account get-access-token --resource "499b84ac-1321-427f-aa17-267ca6975798" 2>&1
if ($LASTEXITCODE -ne 0) {
throw "Failed to get access token. Run 'az login' first. Output: $tokenJson"
}
$parsed = $tokenJson | ConvertFrom-Json
return $parsed.accessToken
}
function Invoke-AzDORestMethod {
<#
.SYNOPSIS
Calls an Azure DevOps REST API endpoint with automatic authentication.
.PARAMETER Organization
Azure DevOps organization name (not the full URL).
.PARAMETER Project
Azure DevOps project name.
.PARAMETER Endpoint
API path after _apis/ (e.g. "pipelines/42/runs", "build/builds").
.PARAMETER Method
HTTP method. Defaults to GET.
.PARAMETER Body
Request body as a hashtable. Automatically converted to JSON.
.PARAMETER ApiVersion
API version. Defaults to 7.1.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)][string] $Organization,
[Parameter(Mandatory)][string] $Project,
[Parameter(Mandatory)][string] $Endpoint,
[string] $Method = "GET",
[hashtable] $Body,
[string] $ApiVersion = "7.1"
)
$token = Get-AzDOAccessToken
$headers = @{
Authorization = "Bearer $token"
"Content-Type" = "application/json"
}
$uri = "https://dev.azure.com/$Organization/$Project/_apis/$($Endpoint)?api-version=$ApiVersion"
$params = @{
Uri = $uri
Headers = $headers
Method = $Method
}
if ($Body) {
$params.Body = $Body | ConvertTo-Json -Depth 10
}
return Invoke-RestMethod @params
}