Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions .github/workflows/auto-azp-run.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Auto Trigger ADO Pipeline for Azure Members
run-name: Auto trigger ADO pipeline for PR by Azure member

on:
pull_request:
types: [opened, synchronize]

jobs:
check-and-trigger:
runs-on: ubuntu-latest
permissions:
pull-requests: write
contents: read
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
sparse-checkout: |
tools/GitHubOrgMember

- name: Get PR author and check Azure org membership
shell: pwsh
env:
PR_AUTHOR: ${{ github.event.pull_request.user.login }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
Write-Host "Checking if PR author '$env:PR_AUTHOR' is an Azure organization member..."

# Use the existing script to check membership
$membershipResult = & "./tools/GitHubOrgMember/Check-AzureOrgMembership.ps1" -Username $env:PR_AUTHOR -Quiet

Write-Host "Membership check result:"
Write-Host "Username: $($membershipResult.Username)"
Write-Host "Organization: $($membershipResult.Organization)"
Write-Host "IsMember: $($membershipResult.IsMember)"
Write-Host "Status: $($membershipResult.Status)"

if ($membershipResult.ErrorMessage) {
Write-Host "Error: $($membershipResult.ErrorMessage)"
}

# Set output for next step
echo "IS_AZURE_MEMBER=$($membershipResult.IsMember)" >> $env:GITHUB_ENV
echo "MEMBERSHIP_STATUS=$($membershipResult.Status)" >> $env:GITHUB_ENV

- name: Trigger ADO pipeline for Azure member
if: env.IS_AZURE_MEMBER == 'True'
shell: pwsh
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
try {
Write-Host "PR author is an Azure member. Proceeding to trigger ADO pipeline..."

# Comment "/azp run" on the PR to trigger ADO pipeline
Write-Host "Commenting '/azp run' on PR #$env:PR_NUMBER to trigger ADO pipeline..."
gh pr comment $env:PR_NUMBER --body "/azp run"

Write-Host "✅ Successfully triggered ADO pipeline for PR #$env:PR_NUMBER"
}
catch {
Write-Error "❌ Failed to trigger ADO pipeline: $($_.Exception.Message)"
exit 1
}
125 changes: 125 additions & 0 deletions tools/GitHubOrgMember/Check-AzureOrgMembership.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Check if a GitHub user is a member of the Azure organization.

.DESCRIPTION
This script uses the GitHub CLI to check if a specified user is a member of the Azure GitHub organization.
Returns a structured PowerShell object with membership details.

.PARAMETER Username
The GitHub username to check.

.PARAMETER Organization
The GitHub organization to check membership for. Defaults to "Azure".

.PARAMETER Quiet
Suppress console output and only return the object.

.OUTPUTS
PSCustomObject with the following properties:
- Username: The checked username
- Organization: The organization that was checked
- IsMember: Boolean indicating if user is a public member
- Status: Detailed status (PublicMember, NotMember, PrivateMember, UserNotFound, Error)
- ErrorMessage: Error details if Status is Error
- CheckedAt: Timestamp of when the check was performed

.EXAMPLE
$result = .\Check-AzureOrgMembership.ps1 "octocat"
if ($result.IsMember) { Write-Host "User is a member!" }

.EXAMPLE
.\Check-AzureOrgMembership.ps1 "octocat" -Quiet | ConvertTo-Json
#>

[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0)]
[ValidateNotNullOrEmpty()]
[string]$Username,

[Parameter()]
[string]$Organization = "Azure",

[Parameter()]
[switch]$Quiet
)

function Write-ConditionalOutput {
param([string]$Message, [string]$ForegroundColor = "White")
if (-not $Quiet) {
Write-Host $Message -ForegroundColor $ForegroundColor
}
}

# Initialize result object
$result = [PSCustomObject]@{
Username = $Username
Organization = $Organization
IsMember = $false
Status = "Unknown"
ErrorMessage = $null
CheckedAt = Get-Date
}

try {
# Check if GitHub CLI is available
if (-not (Get-Command gh -ErrorAction SilentlyContinue)) {
$result.Status = "Error"
$result.ErrorMessage = "GitHub CLI (gh) is not installed. Install from: https://cli.github.com/"
Write-ConditionalOutput "❌ GitHub CLI not found" "Red"
return $result
}

# Check if authenticated
$null = gh auth status 2>&1
if ($LASTEXITCODE -ne 0) {
$result.Status = "Error"
$result.ErrorMessage = "GitHub CLI is not authenticated. Run 'gh auth login' first."
Write-ConditionalOutput "❌ GitHub CLI not authenticated" "Red"
return $result
}

Write-ConditionalOutput "🔍 Checking if '$Username' is a member of '$Organization' organization..." "Yellow"

# Check organization membership using GitHub API
gh api "orgs/$Organization/members/$Username" --silent 2>$null
$apiExitCode = $LASTEXITCODE

if ($apiExitCode -eq 0) {
# User is a public member
$result.IsMember = $true
$result.Status = "PublicMember"
Write-ConditionalOutput "✅ $Username is a PUBLIC member of the $Organization organization!" "Green"
}
elseif ($apiExitCode -eq 1) {
# Exit code 1 typically means 404 - could be not a member or private membership
# Check if user exists
gh api "users/$Username" --silent 2>$null
if ($LASTEXITCODE -eq 0) {
$result.IsMember = $false
$result.Status = "NotMemberOrPrivate"
Write-ConditionalOutput "❌ $Username is either not a member of $Organization organization or has private membership." "Red"
}
else {
$result.Status = "UserNotFound"
$result.ErrorMessage = "User '$Username' was not found on GitHub."
Write-ConditionalOutput "❌ User '$Username' was not found on GitHub." "Red"
}
}
else {
# Unexpected error
$result.Status = "Error"
$result.ErrorMessage = "Unexpected error occurred (GitHub API exit code: $apiExitCode)"
Write-ConditionalOutput "❌ Unexpected error checking membership (exit code: $apiExitCode)" "Red"
}
}
catch {
$result.Status = "Error"
$result.ErrorMessage = "Exception occurred: $($_.Exception.Message)"
Write-ConditionalOutput "❌ Error: $($_.Exception.Message)" "Red"
}

# Return the result object
return $result
Loading
Loading