Skip to content

Commit

Permalink
Implement sign-off enforcement in Powershell (#75)
Browse files Browse the repository at this point in the history
I forgot to translate the sign-off enforcement in the pull request #74.
  • Loading branch information
mristin authored Jan 13, 2021
1 parent 10e4399 commit e46a3d7
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 1 deletion.
68 changes: 67 additions & 1 deletion local/powershell/OpinionatedCommitMessage.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,70 @@ function TestOKWithLinkDefinition
return $true
}

function TestOKSignedOff
{
$message = (
"Do something`n`nIt does something.`n" +
"Signed-off-by: Somebody <[email protected]>`n`n" +
"Signed-off is not necessarily the last line.`n`n" +
"And multiple sign-offs are possible!`n" +
"Signed-off-by: Somebody Else <[email protected]>`n"
)
$got = (powershell -File OpinionatedCommitMessage.ps1 -message $message -dontThrow)|Out-String

$nl = [Environment]::NewLine
$expected = "The message is OK.${nl}"

if ($got -ne $expected)
{
Write-Host "TestOKSignedOff: FAILED"
WriteExpectedGot -expected $expected -got $got
return $false
}

Write-Host "TestOKSignedOff: OK"
return $true
}

function TestFailSignedOff
{
$message = (
"Do something`n`n" +
"It does something.`n`n" +
"None of the following satisfy the sign-off:`n" +
"Signed-off-by Random Developer <[email protected]>`n" +
"signed-off-by: Random Developer <[email protected]>`n" +
"Signed-off-by: Random Developer <randomdeveloper.example.org>`n" +
"Signed-off-by: Random Developer ([email protected])`n" +
"Signed-off-by: Random Developer [email protected]`n" +
"Signed off by: Random Developer <[email protected]>`n" +
"Signed_off_by: Random Developer <[email protected]>`n" +
"Signed-off-by: Random Developer`n"
)

$got = (powershell `
-File OpinionatedCommitMessage.ps1 `
-message $message `
-enforceSignOff `
-dontThrow
)|Out-String

$nl = [Environment]::NewLine
$expected = (
"* The body does not contain any 'Signed-off-by: ' line. " +
"Did you sign off the commit with ``git commit --signoff``?${nl}"
)

if ($got -ne $expected)
{
Write-Host "TestFailSignedOff: FAILED"
WriteExpectedGot -expected $expected -got $got
return $false
}

Write-Host "TestFailSignedOff: OK"
return $true
}

function Main
{
Expand All @@ -428,7 +492,9 @@ function Main
$success = TestFailWithAllowOneLiners -and $success
$success = TestOKWithURLOnSeparateLine -and $success
$success = TestOKWithLinkDefinition -and $success

$success = TestOKSignedOff -and $success
$success = TestFailSignedOff -and $success

if(!$success)
{
throw "One or more unit tests failed."
Expand Down
35 changes: 35 additions & 0 deletions local/powershell/OpinionatedCommitMessage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ param(
[switch]
$allowOneLiners = $false,

[Parameter(HelpMessage = "If set, the commit must be signed off")]
[switch]
$enforceSignOff = $false,

[Parameter(HelpMessage = "If set, the script does not throw an exception on failed checks")]
[switch]
$dontThrow = $false
Expand Down Expand Up @@ -988,6 +992,32 @@ function CheckBody([string]$subject, [string[]] $bodyLines)
return $errors
}

$signedOffByRe = [Regex]::new('^\\s*Signed-off-by:\\s*[^<]+\\s*<[^@>, ]+@[^@>, ]+>\\s*$')

function CheckSignedOff([string[]]$bodyLines)
{
$errors = @()

$matches = 0
foreach($line in $bodyLines)
{
if ($signedOffByRe.IsMatch($line))
{
$matches += 1
}
}

if ($matches -eq 0)
{
$errors += (
"The body does not contain any 'Signed-off-by: ' line. " +
"Did you sign off the commit with ``git commit --signoff``?"
)
}

return $errors
}

function Check([string]$text, [hashtable]$verbs)
{
[string[]]$errors = @()
Expand Down Expand Up @@ -1043,6 +1073,11 @@ function Check([string]$text, [hashtable]$verbs)

$bodyLines = $trimmedLines |Select-Object -Skip 2
$errors = $errors + ( CheckBody -subject $subject -bodyLines $bodyLines)

if ($enforceSignOff)
{
$errors = $errors + ( CheckSignedOff -bodyLines $bodyLines)
}
}
}

Expand Down
35 changes: 35 additions & 0 deletions local/powershell/OpinionatedCommitMessage.ps1.template
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ param(
[switch]
$allowOneLiners = $false,

[Parameter(HelpMessage = "If set, the commit must be signed off")]
[switch]
$enforceSignOff = $false,

[Parameter(HelpMessage = "If set, the script does not throw an exception on failed checks")]
[switch]
$dontThrow = $false
Expand Down Expand Up @@ -247,6 +251,32 @@ function CheckBody([string]$subject, [string[]] $bodyLines)
return $errors
}

$signedOffByRe = [Regex]::new('^\\s*Signed-off-by:\\s*[^<]+\\s*<[^@>, ]+@[^@>, ]+>\\s*$')

function CheckSignedOff([string[]]$bodyLines)
{
$errors = @()

$matches = 0
foreach($line in $bodyLines)
{
if ($signedOffByRe.IsMatch($line))
{
$matches += 1
}
}

if ($matches -eq 0)
{
$errors += (
"The body does not contain any 'Signed-off-by: ' line. " +
"Did you sign off the commit with ``git commit --signoff``?"
)
}

return $errors
}

function Check([string]$text, [hashtable]$verbs)
{
[string[]]$errors = @()
Expand Down Expand Up @@ -302,6 +332,11 @@ function Check([string]$text, [hashtable]$verbs)

$bodyLines = $trimmedLines |Select-Object -Skip 2
$errors = $errors + ( CheckBody -subject $subject -bodyLines $bodyLines)

if ($enforceSignOff)
{
$errors = $errors + ( CheckSignedOff -bodyLines $bodyLines)
}
}
}

Expand Down

0 comments on commit e46a3d7

Please sign in to comment.