Skip to content

Commit

Permalink
feat(actions) as composite
Browse files Browse the repository at this point in the history
  • Loading branch information
sassdawe committed Apr 17, 2024
1 parent 75fffe4 commit 984073c
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 5 deletions.
110 changes: 110 additions & 0 deletions SetSharingSettings.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@

[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string] $adminUrl,
[Parameter(Mandatory = $true)]
[string] $clientID,
[Parameter(Mandatory = $true)]
[string] $clientSecret.
[Parameter(Mandatory = $false)]
[string] $sharingCapabilityDisabled,
[Parameter(Mandatory = $false)]
[string] $sharingCapabilityExternalUserSharingOnly,
[Parameter(Mandatory = $false)]
[string] $sharingCapabilityExternalUserAndGuestSharing,
[Parameter(Mandatory = $false)]
[string] $sharingCapabilityExistingExternalUserSharingOnly
)

$ErrorActionPreference = "Stop";
$WarningPreference = "SilentlyContinue";

# ${{ inputs.Icons }}

$sharingCapabilityDisabledSites = if ( 'null' -ne $sharingCapabilityDisabled ) { $sharingCapabilityDisabled.Split(",").Trim().TrimEnd('/').ToLower() } else { @() };
"To Disable site count: $($sharingCapabilityDisabledSites.count)" | Write-Output;

$sharingCapabilityExternalUserSharingOnlySites = if ( 'null' -ne $sharingCapabilityExternalUserSharingOnly ) { $sharingCapabilityExternalUserSharingOnly.Split(",").Trim().TrimEnd('/').ToLower() } else { @() };
"To ExternalUserSharingOnly site count: $($sharingCapabilityExternalUserSharingOnlySites.count)" | Write-Output;

$sharingCapabilityExternalUserAndGuestSharingSites = if ( 'null' -ne $sharingCapabilityExternalUserAndGuestSharing ) { $sharingCapabilityExternalUserAndGuestSharing.Split(",").Trim().TrimEnd('/').ToLower() } else { @() };
"To ExternalAndGuestSharing site count: $($sharingCapabilityExternalUserAndGuestSharingSites.count)" | Write-Output;

$sharingCapabilityExistingExternalUserSharingOnlySites = if ( 'null' -ne $sharingCapabilityExistingExternalUserSharingOnly ) { $sharingCapabilityExistingExternalUserSharingOnly.Split(",").Trim().TrimEnd('/').ToLower() } else { @() };
"To ExistingExternalUserSharingOnly site count: $($sharingCapabilityExistingExternalUserSharingOnlySites.count)" | Write-Output;

$sharingOrderOfPrecedence = New-Object 'System.Collections.Specialized.OrderedDictionary';
$sharingOrderOfPrecedence.Add('ExternalUserAndGuestSharing', $sharingCapabilityExternalUserAndGuestSharingSites);
$sharingOrderOfPrecedence.Add('ExternalUserSharingOnly', $sharingCapabilityExternalUserSharingOnlySites);
$sharingOrderOfPrecedence.Add('ExistingExternalUserSharingOnly', $sharingCapabilityExistingExternalUserSharingOnlySites);
$sharingOrderOfPrecedence.Add('Disabled', $sharingCapabilityDisabledSites);

foreach ($sites in ($sharingOrderOfPrecedence.Values.GetEnumerator())) {
$sites.count | Write-Output;
}

# Now lets check that only one parameter contains 'ALLELSE', and make sure that one will get executed last.
$allCount = 0;

if ($sharingCapabilityDisabledSites -icontains 'ALLELSE') {
$allCount++; $allElse = 'Disabled';
$sharingOrderOfPrecedence.Remove('Disabled')
}
if ($sharingCapabilityExternalUserSharingOnlySites -icontains 'ALLELSE') {
$allCount++; $allElse = 'ExternalUserSharingOnly';
$sharingOrderOfPrecedence.Remove('ExternalUserSharingOnly')
}
if ($sharingCapabilityExternalUserAndGuestSharingSites -icontains 'ALLELSE') {
$allCount++; $allElse = 'ExternalUserAndGuestSharing';
$sharingOrderOfPrecedence.Remove('ExternalUserAndGuestSharing')
}
if ($sharingCapabilityExistingExternalUserSharingOnlySites -icontains 'ALLELSE') {
$allCount++; $allElse = 'ExistingExternalUserSharingOnly';
$sharingOrderOfPrecedence.Remove('ExistingExternalUserSharingOnly')
}

if ($allCount -le 1) {
Write-Output "✅ Only one parameter contains 'ALLELSE'";
Write-Output "🚩 All else: $allElse";
}
else {
Throw "More than one parameter contains 'ALLELSE' this is not allowed, make sure that maximum only one parameter contains 'ALLELSE'"
}

Import-Module ".\Modules\PnP.PowerShell\2.4.0\PnP.PowerShell.psd1" -Force;
Connect-PnPOnline -Url ${adminUrl} -ClientId ${clientID} -ClientSecret ${clientSecret};
Get-PnPTenantSite | Format-Table Url, Template, LocaleId, SharingCapability | Write-Output;

$sitesAsSet = New-Object System.Collections.Generic.HashSet[string]
Get-PnPTenantSite | ForEach-Object { $null = $sitesAsSet.add( $_.url.ToLower().TrimEnd('/') ) }
Write-Output "All sites count: $($sitesAsSet.count)";

Write-Output "🚀 Start to update the sharing capability for the sites";

foreach ($sharingCapability in $sharingOrderOfPrecedence.GetEnumerator()) {
$sharingCapabilityName = $sharingCapability.Key;
$sites = $sharingCapability.Value;
if ($null -ne $sites -and $sites.Count -gt 0) {
Write-Output "🚀 Start to update the sharing capability for the sites with sharing capability: $sharingCapabilityName";
foreach ($site in $sites) {
Write-Output "🚀🚀 Start to update the sharing capability for the site: $site";
Set-PnPTenantSite -Url $site -SharingCapability $sharingCapabilityName -ErrorAction Continue;
$null = $sitesAsSet.Remove($site);
Write-Output "✅ Sharing capability updated for the site: $site";
}
}
}

if ($allCount -eq 1) {
# Get All sites, and filter out the sites that are already updated.
Write-Output "Remaining sites to update: $($sitesAsSet.count)";
foreach ($site in $sitesAsSet) {
Write-Output "🚀🚀 Start to update the sharing capability for the site: $site";
Set-PnPTenantSite -Url $site -SharingCapability $allElse -ErrorAction Continue;
Write-Output "✅ Sharing capability updated for the site: $site";
}
}

Write-Output "✅ All sites are updated successfully";
Get-PnPTenantSite | Format-Table Url, Template, LocaleId, SharingCapability | Write-Output;
50 changes: 45 additions & 5 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
name: 'PnP PowerShell Sharing Settings as Code'

description: 'Define and enforce SharePoint Online sharing settings with GitHub Actions.'

branding:
icon: 'briefcase'
color: 'blue'

inputs:
SHAREPOINT_ADMIN_URL:
description: 'URL of the SharePoint Admin Site'
Expand All @@ -26,9 +32,43 @@ inputs:
description: 'Comma separated list of URLS where to enable sharing capability for existing external users only. Or "null" or "ALLELSE".'
required: false
default: 'null'

runs:
using: 'node20'
main: 'dist/index.js'
branding:
icon: 'briefcase'
color: 'blue'
using: "composite"
steps:
- shell: pwsh
id: SetSharingCapability
run: |
Write-Output "::group::My Awesome Function!"
Write-Output "::notice File=SetSharingSettings.ps1::Running script to enforce sharing settings!"
Write-Output "::endgroup::"
try {
# splat parameters
$inputParam = @{
AdminUrl = ${{ inputs.SHAREPOINT_ADMIN_URL }}
ClientId = ${{ inputs.CLIENT_ID }}
ClientSecret = ${{ inputs.CLIENT_SECRET }}
SharingCapabilityDisabled = ${{ inputs.SHARINGCAPABILITY_DISABLED_SITES }}
SharingCapabilityExternalUserSharingOnly = ${{ inputs.SHARINGCAPABILITY_EXTERNALUSERSHARINGONLY_SITES }}
SharingCapabilityExternalUserAndGuestSharing = ${{ inputs.SHARINGCAPABILITY_EXTERNALUSERANDGUESTSHARING_SITES }}
SharingCapabilityExistingExternalUserSharingOnly = ${{ inputs.SHARINGCAPABILITY_EXISTINGEXTERNALUSERSHARINGONLY_SITES }}
}
Write-Output "::Group::Trying to run stuff"
$res = & $env:GITHUB_ACTION_PATH\SetSharingSettings.ps1 @inputParam
# Write to standard output
# Write-Output "::notice::Result: $($res -join ',')"
Write-Output "::endgroup::"
## Set environment variables - works in the same job
# "IconsOutput=$($res -join ',')" >> $env:GITHUB_ENV
## Set output variables
#"IconResult=$($res -join ',')" >> $env:GITHUB_OUTPUT
}
catch {
Write-Output "::error file=SetSharingSettings.ps1::Failed to run!"
}

0 comments on commit 984073c

Please sign in to comment.