github.com/autonomy/conform@v0.1.0-alpha.16/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/autonomy/conform/internal/git"
     9  	"github.com/autonomy/conform/internal/policy"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  // GPGCheck ensures that the commit is cryptographically signed using GPG.
    14  type GPGCheck struct {
    15  	errors []error
    16  }
    17  
    18  // Name returns the name of the check.
    19  func (g GPGCheck) Name() string {
    20  	return "GPG"
    21  }
    22  
    23  // Message returns to check message.
    24  func (g GPGCheck) Message() string {
    25  	if len(g.errors) != 0 {
    26  		return g.errors[0].Error()
    27  	}
    28  	return "GPG signature found"
    29  }
    30  
    31  // Errors returns any violations of the check.
    32  func (g GPGCheck) Errors() []error {
    33  	return g.errors
    34  }
    35  
    36  // ValidateGPGSign checks the commit message for a GPG signature.
    37  func (c Commit) ValidateGPGSign(g *git.Git) policy.Check {
    38  	check := &GPGCheck{}
    39  
    40  	ok, err := g.HasGPGSignature()
    41  	if err != nil {
    42  		check.errors = append(check.errors, err)
    43  		return check
    44  	}
    45  
    46  	if ok {
    47  		return check
    48  	}
    49  
    50  	check.errors = append(check.errors, errors.Errorf("Commit does not have a GPG signature"))
    51  
    52  	return check
    53  }