Skip to content

Commit 0d31450

Browse files
committed
Place copyright notifications in module
Update to use variables instead of duplicated strings Support authtoken since it's pretty easy to hit Github limits on API call Use Invoke-RestMethod consistently (rather than Invoke-WebRequest in places)
1 parent 253cf2d commit 0d31450

File tree

2 files changed

+53
-95
lines changed

2 files changed

+53
-95
lines changed

tools/Modules/RFC/RFC.psd1

+4-62
Original file line numberDiff line numberDiff line change
@@ -24,56 +24,28 @@ GUID = 'e05ba184-7bd8-4a16-9f41-93d7c7bd4ee6'
2424
Author = 'jimtru'
2525

2626
# Company or vendor of this module
27-
CompanyName = 'Unknown'
27+
CompanyName = 'Microsoft'
2828

2929
# Copyright statement for this module
30-
Copyright = '(c) jimtru. All rights reserved.'
30+
Copyright = 'Copyright (c) Microsoft Corporation. All rights reserved.'
3131

3232
# Description of the functionality provided by this module
3333
# Description = ''
3434

35-
# Minimum version of the PowerShell engine required by this module
36-
# PowerShellVersion = ''
37-
38-
# Name of the PowerShell host required by this module
39-
# PowerShellHostName = ''
40-
41-
# Minimum version of the PowerShell host required by this module
42-
# PowerShellHostVersion = ''
43-
44-
# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
45-
# DotNetFrameworkVersion = ''
46-
47-
# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
48-
# CLRVersion = ''
49-
50-
# Processor architecture (None, X86, Amd64) required by this module
51-
# ProcessorArchitecture = ''
52-
53-
# Modules that must be imported into the global environment prior to importing this module
54-
# RequiredModules = @()
55-
56-
# Assemblies that must be loaded prior to importing this module
57-
# RequiredAssemblies = @()
58-
59-
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
60-
# ScriptsToProcess = @()
6135

6236
# Type files (.ps1xml) to be loaded when importing this module
6337
TypesToProcess = @('RFC.Types.ps1xml')
6438

6539
# Format files (.ps1xml) to be loaded when importing this module
6640
FormatsToProcess = @('RFC.Formats.ps1xml')
6741

68-
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
69-
# NestedModules = @()
70-
7142
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
7243
FunctionsToExport = @(
7344
'Get-MaxRFCNumber', 'Get-MaxRFC', 'Get-PullRFCNumber', 'Get-RFCPullRequest',
7445
'Get-HighestPullRFCNumber', 'Get-NextRFCNumber', 'Get-NextRFCFileName', 'Get-GitFork',
7546
'Get-GitBranchesFromFork', 'Get-GitTreeFromBranch', 'Get-LastCommit', 'Get-RepoFileList',
76-
'Get-PR'
47+
'Get-PR',
48+
"Set-Header", "Get-Header", "Remove-Header"
7749
)
7850

7951
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
@@ -85,45 +57,15 @@ VariablesToExport = '*'
8557
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
8658
AliasesToExport = @()
8759

88-
# DSC resources to export from this module
89-
# DscResourcesToExport = @()
90-
91-
# List of all modules packaged with this module
92-
# ModuleList = @()
93-
94-
# List of all files packaged with this module
95-
# FileList = @()
96-
9760
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
9861
PrivateData = @{
9962

10063
PSData = @{
10164

102-
# Tags applied to this module. These help with module discovery in online galleries.
103-
# Tags = @()
104-
105-
# A URL to the license for this module.
106-
# LicenseUri = ''
107-
108-
# A URL to the main website for this project.
109-
# ProjectUri = ''
110-
111-
# A URL to an icon representing this module.
112-
# IconUri = ''
113-
114-
# ReleaseNotes of this module
115-
# ReleaseNotes = ''
116-
11765
} # End of PSData hashtable
11866

11967
} # End of PrivateData hashtable
12068

121-
# HelpInfo URI of this module
122-
# HelpInfoURI = ''
123-
124-
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
125-
# DefaultCommandPrefix = ''
126-
12769
}
12870

12971

tools/Modules/RFC/RFC.psm1

+49-33
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
13
$script:repoBase = "https://api.github.com/repos"
4+
$script:repositoryOwner = "PowerShell"
5+
$script:repositoryName = "PowerShell"
6+
$script:Headers = @{ }
7+
8+
$PSDefaultParameterValues['Invoke-RestMethod:Headers'] = $script:Headers
9+
10+
function Set-Header {
11+
param ( $token )
12+
$script:Headers = @{ Authorization = "Bearer $token" }
13+
$PSDefaultParameterValues['Invoke-RestMethod:Headers'] = $script:Headers
14+
}
15+
16+
function Remove-Header {
17+
if ( $PSDefaultParameterValues['Invoke-RestMethod:Headers'] ) {
18+
$PSDefaultParameterValues.Remove("Invoke-RestMethod:Headers")
19+
}
20+
}
21+
22+
function Get-Header {
23+
$PSDefaultParameterValues['Invoke-RestMethod:Headers']
24+
}
225

326
<#
427
.SYNOPSIS
@@ -16,9 +39,9 @@ PS> get-maxrfcnumber
1639
#>
1740

1841
function Get-MaxRFCNumber {
19-
$repoOwner = "PowerShell"
42+
# specify a new repoName, since it's specific to RFCs
2043
$repoName = "PowerShell-RFC"
21-
$repoFiles = Get-RepoFileList -repoOwner $repoOwner -repoName $repoName
44+
$repoFiles = Get-RepoFileList -repoOwner $script:repositoryOwner -repoName $repoName
2245
$pattern = "RFC(?<number>\d\d\d\d)-"
2346
$numbers = $repoFiles.Where({$_.path -cmatch $pattern}).Foreach({if ( $_ -match $pattern ) { [int]($matches.number)}}) | Sort-Object
2447
$numbers[-1]
@@ -44,8 +67,8 @@ General notes
4467
function Get-MaxRFC {
4568
$maxNumber = Get-MaxRFCNumber
4669
$rfcPattern = "RFC{0:0000}" -f $maxNumber
47-
$RFC = (Get-RepoFileList -repoOwner PowerShell -repoName PowerShell-RFC).Where({$_.path -cmatch $rfcPattern})
48-
$RFC | Add-Member -TypeName RepoFile -PassThru -MemberType NoteProperty -Name file_url -Value ("https://github.com/PowerShell/PowerShell-RFC/blob/master/" + $RFC.path)
70+
$RFC = (Get-RepoFileList -repoOwner $script:repositoryOwner -repoName PowerShell-RFC).Where({$_.path -cmatch $rfcPattern})
71+
$RFC | Add-Member -TypeName RepoFile -PassThru -MemberType NoteProperty -Name file_url -Value ("https://github.com/${script:repositoryOwner}/PowerShell-RFC/blob/master/" + $RFC.path)
4972
}
5073

5174
# get the RFCs referenced in PRs
@@ -73,12 +96,9 @@ function Get-PullRFCNumber {
7396
)
7497

7598
$rfcs = [System.Collections.ArrayList]::new()
76-
$page = 1
77-
do {
78-
$st = Invoke-RestMethod "${repoBase}/PowerShell/PowerShell-RFC/pulls?state=${State}&per_page=100&page=${page}"
79-
$st.Foreach({ if ( $_.Title -match "RFC(?<num>\d\d\d\d)" ) { $null = $rfcs.Add([int]$matches.num) } })
80-
$page++
81-
} while ( $st.Count -gt 1 )
99+
$url = "${repoBase}/${script:repositoryOwner}/PowerShell-RFC/pulls?state=${State}"
100+
$st = Invoke-RestMethod -FollowRelLink $url | Foreach-Object { $_ }
101+
$st.Foreach({ if ( $_.Title -match "RFC(?<num>\d\d\d\d)" ) { $null = $rfcs.Add([int]$matches.num) } })
82102
$rfcs.Sort()
83103
return $rfcs
84104
}
@@ -118,20 +138,17 @@ General notes
118138

119139
function Get-PR {
120140
param (
121-
[Parameter()]$repoOwner = "PowerShell",
122-
[Parameter()]$repoName = "PowerShell",
141+
[Parameter()]$repoOwner = $script:repositoryOwner,
142+
[Parameter()]$repoName = $script:repositoryName,
123143
[ValidateSet("open","closed","all")]
124144
[Parameter()]$State = "all",
125145
[Parameter()]$PageCount = [int]::maxvalue
126146
)
127147

128-
$page = 1
129-
do {
130-
$url = "${repoBase}/${repoOwner}/${repoName}/pulls?state=${State}&per_page=100&page=${page}"
131-
$st = Invoke-RestMethod $url
132-
$st.Foreach({$_.PSObject.TypeNames.Insert(0,"GitPullRequest");$_})
133-
$page++
134-
} while ( $st.Count -gt 1 -and $page -lt $PageCount + 1)
148+
$url = "${repoBase}/${repoOwner}/${repoName}/pulls?state=${State}"
149+
# get the data, and unspool the collections returned as a result of -FollowRelLink
150+
$st = Invoke-RestMethod -FollowRelLink $url | Foreach-Object { $_ }
151+
$st.Foreach({$_.PSObject.TypeNames.Insert(0,"GitPullRequest");$_})
135152

136153
}
137154

@@ -165,6 +182,7 @@ function Get-RFCPullRequest {
165182
[Parameter()]$State = "open"
166183
)
167184

185+
# specific to RFC repo
168186
Get-PR -repoName "PowerShell-RFC" -State $State
169187
}
170188

@@ -234,22 +252,22 @@ pushed_at html_url
234252
function Get-GitFork {
235253
[CmdletBinding(DefaultParameterSetName="owner")]
236254
param (
237-
[Parameter(ParameterSetName="owner")]$repoOwner = "PowerShell", $repoName = "PowerShell" ,
255+
[Parameter(ParameterSetName="owner")]$repoOwner = $script:repositoryOwner,
256+
[Parameter(ParameterSetName="owner")]$repoName = $script:repositoryName,
238257
[Parameter(ParameterSetName="fork")]$forkUrl
239258
)
240259
if ( ! $forkUrl ) {
241260
$forkUrl = "${repoBase}/${repoOwner}/${repoName}/forks"
242261
}
243262
try {
244263
# if we have a problem bail
245-
$result = Invoke-WebRequest "${forkUrl}?per_page=100"
264+
$result = Invoke-RestMethod -FollowRelLink "${forkUrl}" | Foreach-Object { $_ }
246265
}
247266
catch {
248-
Write-Warning "Could not get data from $forkUrl"
267+
Write-Warning "Could not get data from $forkUrl ($_)"
249268
return
250269
}
251-
$forks = $result.Content | ConvertFrom-Json
252-
foreach ( $fork in $forks ) {
270+
foreach ( $fork in $result ) {
253271
if ( $fork.forks -ne 0 ) {
254272
Get-GitFork -forkUrl $fork.forks_url
255273
}
@@ -280,7 +298,7 @@ General notes
280298
function Get-GitBranchesFromFork {
281299
param ( $Fork )
282300
$branchurl = $Fork.branches_url -replace "{/branch}$"
283-
$branchInfo = (Invoke-WebRequest $branchurl).Content | ConvertFrom-Json
301+
$branchInfo = Invoke-RestMethod $branchurl
284302
$branchInfo.Foreach({$_.psobject.typenames.insert(0,"GitBranchInfo");$_})
285303
}
286304

@@ -307,12 +325,11 @@ General notes
307325

308326
function Get-LastCommit
309327
{
310-
param ( $repoOwner = "PowerShell", $repoName = "PowerShell" )
311-
#$d = "{0:YYYY}-{0:MM}-{0:DD}T{0:HH}:{0:mm}:{0:ss}Z" -f [datetime]::now()
328+
param ( $repoOwner = $script:repositoryOwner, $repoName = $script:repositoryName )
329+
312330
$repo ="${repoBase}/${repoOwner}/${repoName}/commits"
313-
$r = invoke-webrequest $repo
314-
$rContent = $r.content | ConvertFrom-Json
315-
$lastCommit = $rContent | Sort-Object {$_.commit.committer.date}|Select-Object -Last 1
331+
$result = Invoke-RestMethod $repo
332+
$lastCommit = $result | Sort-Object {$_.commit.committer.date}|Select-Object -Last 1
316333
return $lastCommit.sha
317334
}
318335

@@ -347,13 +364,12 @@ General notes
347364

348365
function Get-RepoFileList
349366
{
350-
param ( $commit, $repoOwner = "PowerShell", $repoName = "PowerShell" )
367+
param ( $commit, $repoOwner = $script:repositoryOwner, $repoName = $script:repositoryName )
351368
if ( ! $commit ) {
352369
$commit = Get-LastCommit -repoOwner $repoOwner -repoName $repoName
353370
}
354371
$repo = "${repoBase}/${repoOwner}/${repoName}/git/trees/${commit}?recursive=1"
355-
$r = invoke-webrequest $repo
356-
$rContent = $r.content | ConvertFrom-Json
372+
$rContent = Invoke-RestMethod $repo
357373
if ( $rContent.truncated ) {
358374
#Get-RepoFileListFromTree $commit
359375
}

0 commit comments

Comments
 (0)