github.com/twelho/conform@v0.0.0-20231016230407-c25e9238598a/internal/policy/commit/check_gpg_signature.go (about)

     1  // This Source Code Form is subject to the terms of the Mozilla Public
     2  // License, v. 2.0. If a copy of the MPL was not distributed with this
     3  // file, You can obtain one at http://mozilla.org/MPL/2.0/.
     4  
     5  package commit
     6  
     7  import (
     8  	"github.com/pkg/errors"
     9  
    10  	"github.com/twelho/conform/internal/git"
    11  	"github.com/twelho/conform/internal/policy"
    12  )
    13  
    14  // GPGCheck ensures that the commit is cryptographically signed using GPG.
    15  type GPGCheck struct {
    16  	errors []error
    17  }
    18  
    19  // Name returns the name of the check.
    20  func (g GPGCheck) Name() string {
    21  	return "GPG"
    22  }
    23  
    24  // Message returns to check message.
    25  func (g GPGCheck) Message() string {
    26  	if len(g.errors) != 0 {
    27  		return g.errors[0].Error()
    28  	}
    29  
    30  	return "GPG signature found"
    31  }
    32  
    33  // Errors returns any violations of the check.
    34  func (g GPGCheck) Errors() []error {
    35  	return g.errors
    36  }
    37  
    38  // ValidateGPGSign checks the commit message for a GPG signature.
    39  func (c Commit) ValidateGPGSign(g *git.Git) policy.Check { //nolint:ireturn
    40  	check := &GPGCheck{}
    41  
    42  	ok, err := g.HasGPGSignature()
    43  	if err != nil {
    44  		check.errors = append(check.errors, err)
    45  
    46  		return check
    47  	}
    48  
    49  	if !ok {
    50  		check.errors = append(check.errors, errors.Errorf("Commit does not have a GPG signature"))
    51  
    52  		return check
    53  	}
    54  
    55  	return check
    56  }