From 6f94a9b0a37ee466c2fb457299c78de1bd371f95 Mon Sep 17 00:00:00 2001 From: Simon Heather <32168619+X-Guardian@users.noreply.github.com> Date: Tue, 2 May 2023 02:12:49 +0100 Subject: [PATCH] New/Set-GitHubRepository: Add Support for Merge Commit Title and Message Options (#385) Adds merge commit message and title parameters to the `New-GitHubRepository` and `Set-GitHubRepository` functions. Fixes #384 #### References - https://docs.github.com/en/rest/repos/repos#create-an-organization-repository - https://docs.github.com/en/rest/repos/repos#update-a-repository --- GitHubRepositories.ps1 | 107 ++++++++ Tests/GitHubRepositories.tests.ps1 | 399 +++++++++++++++++++++++++++++ 2 files changed, 506 insertions(+) diff --git a/GitHubRepositories.ps1 b/GitHubRepositories.ps1 index e6f387c8..403fddf5 100644 --- a/GitHubRepositories.ps1 +++ b/GitHubRepositories.ps1 @@ -11,6 +11,25 @@ GitHubRepositoryLanguageTypeName = 'GitHub.RepositoryLanguage' GitHubRepositoryTagTypeName = 'GitHub.RepositoryTag' GitHubRepositoryTeamPermissionTypeName = 'GitHub.RepositoryTeamPermission' + SquashMergeCommitTitleConversion = @{ + 'PRTitle' = 'PR_TITLE' + 'CommitOrPRTitle' = 'COMMIT_OR_PR_TITLE' + 'MergeMessage' = 'MERGE_MESSAGE' + } + SquashMergeCommitMessageConversion = @{ + 'PRBody' = 'PR_BODY' + 'CommitMessages' = 'COMMIT_MESSAGES' + 'Blank' = 'BLANK' + } + MergeCommitTitleConversion = @{ + 'PRTitle' = 'PR_TITLE' + 'MergeMessage' = 'MERGE_MESSAGE' + } + MergeCommitMessageConversion = @{ + 'PRTitle' = 'PR_TITLE' + 'PRBody' = 'PR_BODY' + 'Blank' = 'BLANK' + } }.GetEnumerator() | ForEach-Object { Set-Variable -Scope Script -Option ReadOnly -Name $_.Key -Value $_.Value } @@ -86,6 +105,34 @@ filter New-GitHubRepository By default, rebase-merge pull requests will be allowed. Specify this to disallow. + .PARAMETER SquashMergeCommitTitle + Specifies the default value for a squash merge commit title. This can be one of the + following values: + - 'PRTitle' - default to the pull request's title. + - 'CommitOrPRTitle' - default to the commit's title (if only one commit) or the pull + request's title (when more than one commit). + + .PARAMETER SquashMergeCommitMessage + Specifies the default value for a squash merge commit message. This can be one of the + following values: + - 'PRBody' - default to the pull request's body. + - 'CommitMessages' - default to the branch's commit messages. + - Blank - default to a blank commit message. + + .PARAMETER MergeCommitTitle + Specifies the default value for a merge commit title. This can be one of the + following values: + - 'PRTitle' - default to the pull request's title. + - 'MergeMessage' - default to the classic title for a merge message (e.g., Merge pull request + #123 from branch-name). + + .PARAMETER MergeCommitMessage + Specifies the default vaule for a merge commit message. This can be one of the + following values: + - 'PRTitle' - default to the pull request's title. + - 'PRBody' - default to the pull request's body. + - 'Blank' - default to a blank commit message. + .PARAMETER AllowAutoMerge Specifies whether to allow auto-merge on pull requests. @@ -172,6 +219,18 @@ filter New-GitHubRepository [switch] $DisallowRebaseMerge, + [ValidateSet('PRTitle', 'CommitOrPRTitle')] + [string] $SquashMergeCommitTitle, + + [ValidateSet('PRBody', 'CommitMessages', 'Blank')] + [string] $SquashMergeCommitMessage, + + [ValidateSet('PRTitle', 'MergeMessage')] + [string] $MergeCommitTitle, + + [ValidateSet('PRTitle', 'PRBody', 'Blank')] + [string] $MergeCommitMessage, + [switch] $AllowAutoMerge, [switch] $AllowUpdateBranch, @@ -222,6 +281,10 @@ filter New-GitHubRepository if ($PSBoundParameters.ContainsKey('DisallowSquashMerge')) { $hashBody['allow_squash_merge'] = (-not $DisallowSquashMerge.ToBool()) } if ($PSBoundParameters.ContainsKey('DisallowMergeCommit')) { $hashBody['allow_merge_commit'] = (-not $DisallowMergeCommit.ToBool()) } if ($PSBoundParameters.ContainsKey('DisallowRebaseMerge')) { $hashBody['allow_rebase_merge'] = (-not $DisallowRebaseMerge.ToBool()) } + if ($PSBoundParameters.ContainsKey('SquashMergeCommitTitle')) { $hashBody['squash_merge_commit_title'] = $script:SquashMergeCommitTitleConversion.$SquashMergeCommitTitle } + if ($PSBoundParameters.ContainsKey('SquashMergeCommitMessage')) { $hashBody['squash_merge_commit_message'] = $script:SquashMergeCommitMessageConversion.$SquashMergeCommitMessage } + if ($PSBoundParameters.ContainsKey('MergeCommitTitle')) { $hashBody['merge_commit_title'] = $script:MergeCommitTitleConversion.$MergeCommitTitle } + if ($PSBoundParameters.ContainsKey('MergeCommitMessage')) { $hashBody['merge_commit_message'] = $script:MergeCommitMessageConversion.$MergeCommitMessage } if ($PSBoundParameters.ContainsKey('AllowAutoMerge')) { $hashBody['allow_auto_merge'] = $AllowAutoMerge.ToBool() } if ($PSBoundParameters.ContainsKey('AllowUpdateBranch')) { $hashBody['allow_update_branch'] = $AllowUpdateBranch.ToBool() } if ($PSBoundParameters.ContainsKey('DeleteBranchOnMerge')) { $hashBody['delete_branch_on_merge'] = $DeleteBranchOnMerge.ToBool() } @@ -1082,6 +1145,34 @@ filter Set-GitHubRepository By default, rebase-merge pull requests will be allowed. Specify this to disallow. + .PARAMETER SquashMergeCommitTitle + Specifies the default value for a squash merge commit title. This can be one of the + following values: + - PRTitle - default to the pull request's title. + - CommitOrPRTitle - default to the commit's title (if only one commit) or the pull + request's title (when more than one commit). + + .PARAMETER SquashMergeCommitMessage + Specifies the default value for a squash merge commit message. This can be one of the + following values: + - PRBody - default to the pull request's body. + - CommitMessages - default to the branch's commit messages. + - Blank - default to a blank commit message. + + .PARAMETER MergeCommitTitle + Specifies the default value for a merge commit title. This can be one of the + following values: + - PRTitle - default to the pull request's title. + - MergeMessage - default to the classic title for a merge message (e.g., Merge pull request + #123 from branch-name). + + .PARAMETER MergeCommitMessage + Specifies the default vaule for a merge commit message. This can be one of the + following values: + - PRTitle - default to the pull request's title. + - PRBody - default to the pull request's body. + - Blank - default to a blank commit message. + .PARAMETER AllowAutoMerge Specifies whether to allow auto-merge on pull requests. @@ -1199,6 +1290,18 @@ filter Set-GitHubRepository [switch] $DisallowRebaseMerge, + [ValidateSet('PRTitle', 'CommitOrPRTitle')] + [string] $SquashMergeCommitTitle, + + [ValidateSet('PRBody', 'CommitMessages', 'Blank')] + [string] $SquashMergeCommitMessage, + + [ValidateSet('PRTitle', 'MergeMessage')] + [string] $MergeCommitTitle, + + [ValidateSet('PRTitle', 'PRBody', 'Blank')] + [string] $MergeCommitMessage, + [switch] $AllowAutoMerge, [switch] $AllowUpdateBranch, @@ -1250,6 +1353,10 @@ filter Set-GitHubRepository if ($PSBoundParameters.ContainsKey('DisallowSquashMerge')) { $hashBody['allow_squash_merge'] = (-not $DisallowSquashMerge.ToBool()) } if ($PSBoundParameters.ContainsKey('DisallowMergeCommit')) { $hashBody['allow_merge_commit'] = (-not $DisallowMergeCommit.ToBool()) } if ($PSBoundParameters.ContainsKey('DisallowRebaseMerge')) { $hashBody['allow_rebase_merge'] = (-not $DisallowRebaseMerge.ToBool()) } + if ($PSBoundParameters.ContainsKey('SquashMergeCommitTitle')) { $hashBody['squash_merge_commit_title'] = $script:SquashMergeCommitTitleConversion.$SquashMergeCommitTitle } + if ($PSBoundParameters.ContainsKey('SquashMergeCommitMessage')) { $hashBody['squash_merge_commit_message'] = $script:SquashMergeCommitMessageConversion.$SquashMergeCommitMessage } + if ($PSBoundParameters.ContainsKey('MergeCommitTitle')) { $hashBody['merge_commit_title'] = $script:MergeCommitTitleConversion.$MergeCommitTitle } + if ($PSBoundParameters.ContainsKey('MergeCommitMessage')) { $hashBody['merge_commit_message'] = $script:MergeCommitMessageConversion.$MergeCommitMessage } if ($PSBoundParameters.ContainsKey('AllowAutoMerge')) { $hashBody['allow_auto_merge'] = $AllowAutoMerge.ToBool() } if ($PSBoundParameters.ContainsKey('AllowUpdateBranch')) { $hashBody['allow_update_branch'] = $AllowUpdateBranch.ToBool() } if ($PSBoundParameters.ContainsKey('DeleteBranchOnMerge')) { $hashBody['delete_branch_on_merge'] = $DeleteBranchOnMerge.ToBool() } diff --git a/Tests/GitHubRepositories.tests.ps1 b/Tests/GitHubRepositories.tests.ps1 index 5c8fe9a1..ee477fd7 100644 --- a/Tests/GitHubRepositories.tests.ps1 +++ b/Tests/GitHubRepositories.tests.ps1 @@ -202,6 +202,230 @@ Describe 'GitHubRepositories\New-GitHubRepository' { } } + Context -Name 'When creating a repository with Squash Merge Commit PR Title' { + BeforeAll -ScriptBlock { + $repoName = ([Guid]::NewGuid().Guid) + $newGithubRepositoryParms = @{ + RepositoryName = $repoName + DisallowSquashMerge = $false + SquashMergeCommitTitle = 'PRTitle' + SquashMergeCommitMessage = 'Blank' + } + + $repo = New-GitHubRepository @newGithubRepositoryParms + } + + It 'Should return an object of the correct type' { + $repo | Should -BeOfType PSCustomObject + } + + It 'Should return the correct properties' { + $repo.name | Should -Be $repoName + $repo.allow_squash_merge | Should -BeTrue + $repo.squash_merge_commit_title | Should -Be 'PR_TITLE' + $repo.squash_merge_commit_message | Should -Be 'BLANK' + } + + AfterAll -ScriptBlock { + if ($repo) + { + Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + } + } + } + + Context -Name 'When creating a repository with Squash Merge Commit PR Title and Commit details' { + BeforeAll -ScriptBlock { + $repoName = ([Guid]::NewGuid().Guid) + $newGithubRepositoryParms = @{ + RepositoryName = $repoName + DisallowSquashMerge = $false + SquashMergeCommitTitle = 'PRTitle' + SquashMergeCommitMessage = 'CommitMessages' + } + + $repo = New-GitHubRepository @newGithubRepositoryParms + } + + It 'Should return an object of the correct type' { + $repo | Should -BeOfType PSCustomObject + } + + It 'Should return the correct properties' { + $repo.name | Should -Be $repoName + $repo.allow_squash_merge | Should -BeTrue + $repo.squash_merge_commit_title | Should -Be 'PR_TITLE' + $repo.squash_merge_commit_message | Should -Be 'COMMIT_MESSAGES' + } + + AfterAll -ScriptBlock { + if ($repo) + { + Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + } + } + } + + Context -Name 'When creating a repository with Squash Merge Commit PR Title and Description' { + BeforeAll -ScriptBlock { + $repoName = ([Guid]::NewGuid().Guid) + $newGithubRepositoryParms = @{ + RepositoryName = $repoName + DisallowSquashMerge = $false + SquashMergeCommitTitle = 'PRTitle' + SquashMergeCommitMessage = 'PRBody' + } + + $repo = New-GitHubRepository @newGithubRepositoryParms + } + + It 'Should return an object of the correct type' { + $repo | Should -BeOfType PSCustomObject + } + + It 'Should return the correct properties' { + $repo.name | Should -Be $repoName + $repo.allow_squash_merge | Should -BeTrue + $repo.squash_merge_commit_title | Should -Be 'PR_TITLE' + $repo.squash_merge_commit_message | Should -Be 'PR_BODY' + } + + AfterAll -ScriptBlock { + if ($repo) + { + Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + } + } + } + + Context -Name 'When creating a repository with Squash Merge Commit default message' { + BeforeAll -ScriptBlock { + $repoName = ([Guid]::NewGuid().Guid) + $newGithubRepositoryParms = @{ + RepositoryName = $repoName + DisallowSquashMerge = $false + SquashMergeCommitTitle = 'CommitOrPRTitle' + SquashMergeCommitMessage = 'CommitMessages' + } + + $repo = New-GitHubRepository @newGithubRepositoryParms + } + + It 'Should return an object of the correct type' { + $repo | Should -BeOfType PSCustomObject + } + + It 'Should return the correct properties' { + $repo.name | Should -Be $repoName + $repo.allow_squash_merge | Should -BeTrue + $repo.squash_merge_commit_title | Should -Be 'COMMIT_OR_PR_TITLE' + $repo.squash_merge_commit_message | Should -Be 'COMMIT_MESSAGES' + } + + AfterAll -ScriptBlock { + if ($repo) + { + Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + } + } + } + + Context -Name 'When creating a repository with Merge Commit PR Title' { + BeforeAll -ScriptBlock { + $repoName = ([Guid]::NewGuid().Guid) + $newGithubRepositoryParms = @{ + RepositoryName = $repoName + DisallowMergeCommit = $false + MergeCommitTitle = 'PRTitle' + MergeCommitMessage = 'Blank' + } + + $repo = New-GitHubRepository @newGithubRepositoryParms + } + + It 'Should return an object of the correct type' { + $repo | Should -BeOfType PSCustomObject + } + + It 'Should return the correct properties' { + $repo.name | Should -Be $repoName + $repo.allow_merge_commit | Should -BeTrue + $repo.merge_commit_title | Should -Be 'PR_TITLE' + $repo.merge_commit_message | Should -Be 'BLANK' + } + + AfterAll -ScriptBlock { + if ($repo) + { + Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + } + } + } + + Context -Name 'When creating a repository with Merge Commit PR Title and Description' { + BeforeAll -ScriptBlock { + $repoName = ([Guid]::NewGuid().Guid) + $newGithubRepositoryParms = @{ + RepositoryName = $repoName + DisallowMergeCommit = $false + MergeCommitTitle = 'PRTitle' + MergeCommitMessage = 'PRBody' + } + + $repo = New-GitHubRepository @newGithubRepositoryParms + } + + It 'Should return an object of the correct type' { + $repo | Should -BeOfType PSCustomObject + } + + It 'Should return the correct properties' { + $repo.name | Should -Be $repoName + $repo.allow_merge_commit | Should -BeTrue + $repo.merge_commit_title | Should -Be 'PR_TITLE' + $repo.merge_commit_message | Should -Be 'PR_BODY' + } + + AfterAll -ScriptBlock { + if ($repo) + { + Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + } + } + } + + Context -Name 'When creating a repository with Merge Commit default message' { + BeforeAll -ScriptBlock { + $repoName = ([Guid]::NewGuid().Guid) + $newGithubRepositoryParms = @{ + RepositoryName = $repoName + DisallowMergeCommit = $false + MergeCommitTitle = 'MergeMessage' + MergeCommitMessage = 'PRTitle' + } + + $repo = New-GitHubRepository @newGithubRepositoryParms + } + + It 'Should return an object of the correct type' { + $repo | Should -BeOfType PSCustomObject + } + + It 'Should return the correct properties' { + $repo.name | Should -Be $repoName + $repo.allow_merge_commit | Should -BeTrue + $repo.merge_commit_title | Should -Be 'MERGE_MESSAGE' + $repo.merge_commit_message | Should -Be 'PR_TITLE' + } + + AfterAll -ScriptBlock { + if ($repo) + { + Remove-GitHubRepository -Uri $repo.svn_url -Confirm:$false + } + } + } + Context -Name 'When a TeamID is specified' -Fixture { BeforeAll -ScriptBlock { $repoName = ([Guid]::NewGuid().Guid) @@ -858,6 +1082,181 @@ Describe 'GitHubRepositories\Set-GitHubRepository' { } } + Context -Name 'When updating a repository with Squash Merge Commit PR Title' { + BeforeAll -ScriptBlock { + $updateGithubRepositoryParms = @{ + OwnerName = $repo.owner.login + RepositoryName = $repoName + DisallowSquashMerge = $false + SquashMergeCommitTitle = 'PRTitle' + SquashMergeCommitMessage = 'Blank' + } + + $updatedRepo = Set-GitHubRepository @updateGithubRepositoryParms -PassThru + } + + It 'Should return an object of the correct type' { + $updatedRepo | Should -BeOfType PSCustomObject + } + + It 'Should return the correct properties' { + $updatedRepo.name | Should -Be $repoName + $updatedRepo.allow_squash_merge | Should -BeTrue + $updatedRepo.squash_merge_commit_title | Should -Be 'PR_TITLE' + $updatedRepo.squash_merge_commit_message | Should -Be 'BLANK' + } + } + + Context -Name 'When updating a repository with Squash Merge Commit PR Title and Commit details' { + BeforeAll -ScriptBlock { + $updateGithubRepositoryParms = @{ + OwnerName = $repo.owner.login + RepositoryName = $repoName + DisallowSquashMerge = $false + SquashMergeCommitTitle = 'PRTitle' + SquashMergeCommitMessage = 'CommitMessages' + } + + $updatedRepo = Set-GitHubRepository @updateGithubRepositoryParms -PassThru + } + + It 'Should return an object of the correct type' { + $updatedRepo | Should -BeOfType PSCustomObject + } + + It 'Should return the correct properties' { + $updatedRepo.name | Should -Be $repoName + $updatedRepo.allow_squash_merge | Should -BeTrue + $updatedRepo.squash_merge_commit_title | Should -Be 'PR_TITLE' + $updatedRepo.squash_merge_commit_message | Should -Be 'COMMIT_MESSAGES' + } + } + + Context -Name 'When updating a repository with Squash Merge Commit PR Title and Description' { + BeforeAll -ScriptBlock { + $updateGithubRepositoryParms = @{ + OwnerName = $repo.owner.login + RepositoryName = $repoName + DisallowSquashMerge = $false + SquashMergeCommitTitle = 'PRTitle' + SquashMergeCommitMessage = 'PRBody' + } + + $updatedRepo = Set-GitHubRepository @updateGithubRepositoryParms -PassThru + } + + It 'Should return an object of the correct type' { + $updatedRepo | Should -BeOfType PSCustomObject + } + + It 'Should return the correct properties' { + $updatedRepo.name | Should -Be $repoName + $updatedRepo.allow_squash_merge | Should -BeTrue + $updatedRepo.squash_merge_commit_title | Should -Be 'PR_TITLE' + $updatedRepo.squash_merge_commit_message | Should -Be 'PR_BODY' + } + } + + Context -Name 'When updating a repository with Squash Merge Commit default message' { + BeforeAll -ScriptBlock { + $updateGithubRepositoryParms = @{ + OwnerName = $repo.owner.login + RepositoryName = $repoName + DisallowSquashMerge = $false + SquashMergeCommitTitle = 'CommitOrPRTitle' + SquashMergeCommitMessage = 'CommitMessages' + } + + $updatedRepo = Set-GitHubRepository @updateGithubRepositoryParms -PassThru + } + + It 'Should return an object of the correct type' { + $updatedRepo | Should -BeOfType PSCustomObject + } + + It 'Should return the correct properties' { + $updatedRepo.name | Should -Be $repoName + $updatedRepo.allow_squash_merge | Should -BeTrue + $updatedRepo.squash_merge_commit_title | Should -Be 'COMMIT_OR_PR_TITLE' + $updatedRepo.squash_merge_commit_message | Should -Be 'COMMIT_MESSAGES' + } + } + + Context -Name 'When updating a repository with Merge Commit PR Title' { + BeforeAll -ScriptBlock { + $updateGithubRepositoryParms = @{ + OwnerName = $repo.owner.login + RepositoryName = $repoName + DisallowMergeCommit = $false + MergeCommitTitle = 'PRTitle' + MergeCommitMessage = 'Blank' + } + + $updatedRepo = Set-GitHubRepository @updateGithubRepositoryParms -PassThru + } + + It 'Should return an object of the correct type' { + $updatedRepo | Should -BeOfType PSCustomObject + } + + It 'Should return the correct properties' { + $updatedRepo.name | Should -Be $repoName + $updatedRepo.allow_merge_commit | Should -BeTrue + $updatedRepo.merge_commit_title | Should -Be 'PR_TITLE' + $updatedRepo.merge_commit_message | Should -Be 'BLANK' + } + } + + Context -Name 'When updating a repository with Merge Commit PR Title and Description' { + BeforeAll -ScriptBlock { + $updateGithubRepositoryParms = @{ + OwnerName = $repo.owner.login + RepositoryName = $repoName + DisallowMergeCommit = $false + MergeCommitTitle = 'PRTitle' + MergeCommitMessage = 'PRBody' + } + + $updatedRepo = Set-GitHubRepository @updateGithubRepositoryParms -PassThru + } + + It 'Should return an object of the correct type' { + $updatedRepo | Should -BeOfType PSCustomObject + } + + It 'Should return the correct properties' { + $updatedRepo.name | Should -Be $repoName + $updatedRepo.allow_merge_commit | Should -BeTrue + $updatedRepo.merge_commit_title | Should -Be 'PR_TITLE' + $updatedRepo.merge_commit_message | Should -Be 'PR_BODY' + } + } + + Context -Name 'When updating a repository with Merge Commit default message' { + BeforeAll -ScriptBlock { + $updateGithubRepositoryParms = @{ + OwnerName = $repo.owner.login + RepositoryName = $repoName + DisallowMergeCommit = $false + MergeCommitTitle = 'MergeMessage' + MergeCommitMessage = 'PRTitle' + } + + $updatedRepo = Set-GitHubRepository @updateGithubRepositoryParms -PassThru + } + + It 'Should return an object of the correct type' { + $updatedRepo | Should -BeOfType PSCustomObject + } + + It 'Should return the correct properties' { + $updatedRepo.name | Should -Be $repoName + $updatedRepo.allow_merge_commit | Should -BeTrue + $updatedRepo.merge_commit_title | Should -Be 'MERGE_MESSAGE' + $updatedRepo.merge_commit_message | Should -Be 'PR_TITLE' + } + } + Context -Name 'When updating a repository with the Archive setting' { BeforeAll -ScriptBlock { $updateGithubRepositoryParms = @{