Skip to content

Commit b2f40a7

Browse files
authored
Merge pull request #2124 from microsoftgraph/v2_example_script_importer
Updates examples import script to conform to v2 examples on Api reference docs
2 parents db86725 + 572724b commit b2f40a7

File tree

2 files changed

+16
-29
lines changed

2 files changed

+16
-29
lines changed

.azure-pipelines/weekly-examples-update.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ jobs:
5555
git status
5656
5757
- task: PowerShell@2
58-
displayName: 'Update Examples From API reference - V2'
58+
displayName: 'Import Examples From API reference - V2'
5959
continueOnError: false
6060
inputs:
6161
targetType: 'filePath'
6262
pwsh: true
63-
filePath: tools\ExamplesGenerator.ps1
63+
filePath: tools\ImportExamples.ps1
6464

6565
- task: PublishBuildArtifacts@1
6666
displayName: 'Publish Examples to be reviewed as artifact'

tools/ExamplesGenerator.ps1 renamed to tools/ImportExamples.ps1

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,10 @@ function Start-WebScrapping {
233233
)
234234
$ExampleFile = "$GraphProfilePath/$Command.md"
235235
$url = $ExternalDocUrl
236-
$DescriptionCommand = $Command
237-
if($GraphProfile -eq "beta"){
238-
$DescriptionCommand= $Command.Replace("-MgBeta", "-Mg")
236+
if($GraphProfile -eq "beta"){
237+
$url = $url.Replace("graph-rest-1.0","graph-rest-beta")
239238
}
240-
239+
$DescriptionCommand = $Command
241240
$Description = "This example shows how to use the $DescriptionCommand Cmdlet.`r`n`r`To learn about permissions for this resource, see the [permissions reference](/graph/permissions-reference)."
242241
$WebResponse = Invoke-WebRequest -Uri $url
243242
$HeaderList = New-Object -TypeName 'System.Collections.ArrayList';
@@ -264,7 +263,7 @@ function Start-WebScrapping {
264263

265264
}
266265

267-
Update-ExampleFile -GraphProfile $GraphProfile -HeaderList $HeaderList -ExampleList $ExampleList -ExampleFile $ExampleFile -Description $Description -Command $Command -ExternalDocUrl $ExternalDocUrl -UriPath $UriPath -Module $Module
266+
Update-ExampleFile -GraphProfile $GraphProfile -HeaderList $HeaderList -ExampleList $ExampleList -ExampleFile $ExampleFile -Description $Description -Command $Command -ExternalDocUrl $url -UriPath $UriPath -Module $Module
268267
}
269268

270269
function Update-ExampleFile {
@@ -282,9 +281,6 @@ function Update-ExampleFile {
282281
[string] $ExternalDocUrl = "https://learn.microsoft.com/en-us/graph/api/user-get?view=graph-rest-1.0&tabs=powershell"
283282
)
284283
$CommandPattern = $Command
285-
if($GraphProfile -eq "beta"){
286-
$CommandPattern = $Command.Replace("-MgBeta", "-Mg")
287-
}
288284
$Content = Get-Content -Path $ExampleFile
289285
$SearchText = "Example"
290286
$SearchTextForNewImports = "{{ Add description here }}"
@@ -306,7 +302,6 @@ function Update-ExampleFile {
306302
$ReplaceEverything = $True
307303
}
308304

309-
310305
$HeadCount = $HeaderList.Count
311306
$ExampleCount = $ExampleList.Count
312307
$WrongExamplesCount = 0;
@@ -320,14 +315,8 @@ function Update-ExampleFile {
320315
$CodeValue = $ExampleList[$d].Trim()
321316
if($CodeValue.Contains($CommandPattern)){
322317
$TitleValue = "### " + $HeaderList[$d].Trim()
323-
$Code = "``````powershell`r$CodeValue`r`n``````"
324-
318+
$Code = "``````powershell`r$CodeValue`r`n``````"
325319
$TotalText = "$TitleValue`r`n`n$Code`r`n$Description`r`n"
326-
if($GraphProfile -eq "beta"){
327-
#Replace examples to match the new beta naming convention
328-
$TotalText = $TotalText.Replace("-Mg", "-MgBeta")
329-
$TotalText = $TotalText.Replace("Microsoft.Graph", "Microsoft.Graph.Beta")
330-
}
331320
Add-Content -Path $ExampleFile -Value $TotalText
332321
$ContainsRightExamples = $True
333322
}else{
@@ -342,27 +331,23 @@ function Update-ExampleFile {
342331
$PatternToSearch = "Import-Module Microsoft.Graph.$Module"
343332
if(($Content | Select-String -pattern $SearchText) -and ($Content | Select-String -pattern "This example shows")){
344333
$ContainsPatternToSearch = $False
334+
if($GraphProfile -eq "beta"){
335+
$PatternToSearch = "Import-Module Microsoft.Graph.Beta.$Module"
336+
}
345337
foreach($List in $ExampleList){
346-
if($List.Contains($PatternToSearch)){
338+
if($List.Contains($PatternToSearch) -and $List.Contains($CommandPattern)){
347339
$ContainsPatternToSearch = $True
348340
}
349341
}
350342
if($ContainsPatternToSearch){
351-
Clear-Content $ExampleFile -Force
352-
343+
Clear-Content $ExampleFile -Force
353344
for ($d = 0; $d -lt $HeaderList.Count; $d++) {
354345
#We should only add the correct examples from external docs link
355346
if($ExampleList[$d].Contains($CommandPattern)){
356347
$CodeValue = $ExampleList[$d].Trim()
357348
$TitleValue = "### " + $HeaderList[$d].Trim()
358-
$Code = "``````powershell`r$CodeValue`r`n``````"
359-
349+
$Code = "``````powershell`r$CodeValue`r`n``````"
360350
$TotalText = "$TitleValue`r`n`n$Code`r`n$Description`r`n"
361-
if($GraphProfile -eq "beta"){
362-
#Replace examples to match the new beta naming convention
363-
$TotalText = $TotalText.Replace("-Mg", "-MgBeta")
364-
$TotalText = $TotalText.Replace("Microsoft.Graph", "Microsoft.Graph.Beta")
365-
}
366351
Add-Content -Path $ExampleFile -Value $TotalText
367352
}else{
368353
$SkippedExample++
@@ -373,10 +358,12 @@ function Update-ExampleFile {
373358

374359

375360
}else{
361+
if(-not($Content | Select-String -pattern $CommandPattern)){
376362
Clear-Content $ExampleFile -Force
377363
#Replace everything with boiler plate code
378364
$DefaultBoilerPlate = "### Example 1: {{ Add title here }}`r`n``````powershell`r`n PS C:\> {{ Add code here }}`r`n`n{{ Add output here }}`r`n```````n`n{{ Add description here }}`r`n`n### Example 2: {{ Add title here }}`r`n``````powershell`r`n PS C:\> {{ Add code here }}`r`n`n{{ Add output here }}`r`n```````n`n{{ Add description here }}`r`n`n"
379365
Add-Content -Path $ExampleFile -Value $DefaultBoilerPlate.Trim()
366+
}
380367
}
381368

382369
}
@@ -494,4 +481,4 @@ Start-Generator -ModulesToGenerate $ModulesToGenerate -GenerationMode "auto"
494481
#4. Test for beta updates from api reference
495482
#Start-Generator -GenerationMode "manual" -ManualExternalDocsUrl "https://docs.microsoft.com/graph/api/serviceprincipal-post-approleassignedto?view=graph-rest-beta" -GraphCommand "New-MgBetaServicePrincipalAppRoleAssignedTo" -GraphModule "Applications" -Profile "beta"
496483
#Write-Host -ForegroundColor Green "-------------Done-------------"
497-
#Start-Generator -GenerationMode "manual" -ManualExternalDocsUrl "https://docs.microsoft.com/graph/api/meetingattendancereport-get?view=graph-rest-1.0" -GraphCommand "Get-MgBetaCommunicationCallAudioRoutingGroup" -GraphModule "CloudCommunications" -Profile "beta"
484+
#Start-Generator -GenerationMode "manual" -ManualExternalDocsUrl "https://docs.microsoft.com/graph/api/accessreviewinstancedecisionitem-get?view=graph-rest-1.0" -GraphCommand "Get-MgBetaIdentityGovernanceAccessReviewDefinitionInstanceDecision" -GraphModule "Identity.Governance" -Profile "beta"

0 commit comments

Comments
 (0)