Skip to content

Commit

Permalink
internal/ci: move commit check script to a separate file with tests
Browse files Browse the repository at this point in the history
This allows us to test that the current shell logic works as designed.
We will soon rewrite this logic into Go, for reasons of portability
as well as being able to parse markdown to detect username mentions.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: Iccfbc1c23a7a0a5de0d8e769af1e13c4a9957821
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1198207
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Paul Jolly <paul@myitcv.io>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
  • Loading branch information
mvdan committed Jul 23, 2024
1 parent af83dad commit daf98a0
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 75 deletions.
38 changes: 1 addition & 37 deletions .github/workflows/trybot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,43 +103,7 @@ jobs:
run: go clean -testcache
- if: (matrix.go-version == '1.23.0-rc.2' && matrix.runner == 'ubuntu-22.04')
name: Early git and code sanity checks
run: |-
# Ensure that commit messages have a blank second line.
# We know that a commit message must be longer than a single
# line because each commit must be signed-off.
if git log --format=%B -n 1 HEAD | sed -n '2{/^$/{q1}}'; then
echo "second line of commit message must be blank"
exit 1
fi
# All authors, including co-authors, must have a signed-off trailer by email.
# Note that trailers are in the form "Name <email>", so grab the email with sed.
# For now, we require the sorted lists of author and signer emails to match.
# Note that this also fails if a commit isn't signed-off at all.
#
# In Gerrit we already enable a form of this via https://gerrit-review.googlesource.com/Documentation/project-configuration.html#require-signed-off-by,
# but it does not support co-authors nor can it be used when testing GitHub PRs.
commit_authors="$(
{
git log -1 --pretty='%ae'
git log -1 --pretty='%(trailers:key=Co-authored-by,valueonly)' | sed -ne 's/.* <\(.*\)>/\1/p'
} | sort -u
)"
commit_signers="$(
{
git log -1 --pretty='%(trailers:key=Signed-off-by,valueonly)' | sed -ne 's/.* <\(.*\)>/\1/p'
} | sort -u
)"
if [[ "${commit_authors}" != "${commit_signers}" ]]; then
echo "Error: commit author email addresses do not match signed-off-by trailers"
echo
echo "Authors:"
echo "${commit_authors}"
echo
echo "Signers:"
echo "${commit_signers}"
exit 1
fi
run: ./internal/ci/checks/commit.sh
- if: (matrix.go-version == '1.23.0-rc.2' && matrix.runner == 'ubuntu-22.04')
name: Generate
run: go generate ./...
Expand Down
39 changes: 1 addition & 38 deletions internal/ci/base/github.cue
Original file line number Diff line number Diff line change
Expand Up @@ -100,44 +100,7 @@ checkoutCode: {

earlyChecks: json.#step & {
name: "Early git and code sanity checks"
run: #"""
# Ensure that commit messages have a blank second line.
# We know that a commit message must be longer than a single
# line because each commit must be signed-off.
if git log --format=%B -n 1 HEAD | sed -n '2{/^$/{q1}}'; then
echo "second line of commit message must be blank"
exit 1
fi
# All authors, including co-authors, must have a signed-off trailer by email.
# Note that trailers are in the form "Name <email>", so grab the email with sed.
# For now, we require the sorted lists of author and signer emails to match.
# Note that this also fails if a commit isn't signed-off at all.
#
# In Gerrit we already enable a form of this via https://gerrit-review.googlesource.com/Documentation/project-configuration.html#require-signed-off-by,
# but it does not support co-authors nor can it be used when testing GitHub PRs.
commit_authors="$(
{
git log -1 --pretty='%ae'
git log -1 --pretty='%(trailers:key=Co-authored-by,valueonly)' | sed -ne 's/.* <\(.*\)>/\1/p'
} | sort -u
)"
commit_signers="$(
{
git log -1 --pretty='%(trailers:key=Signed-off-by,valueonly)' | sed -ne 's/.* <\(.*\)>/\1/p'
} | sort -u
)"
if [[ "${commit_authors}" != "${commit_signers}" ]]; then
echo "Error: commit author email addresses do not match signed-off-by trailers"
echo
echo "Authors:"
echo "${commit_authors}"
echo
echo "Signers:"
echo "${commit_signers}"
exit 1
fi
"""#
run: "./internal/ci/checks/commit.sh"
}

curlGitHubAPI: {
Expand Down
38 changes: 38 additions & 0 deletions internal/ci/checks/commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

# Ensure that commit messages have a blank second line.
# We know that a commit message must be longer than a single
# line because each commit must be signed-off.
if git log --format=%B -n 1 HEAD | sed -n '2{/^$/{q1}}'; then
echo "second line of commit message must be blank"
exit 1
fi

# All authors, including co-authors, must have a signed-off trailer by email.
# Note that trailers are in the form "Name <email>", so grab the email with sed.
# For now, we require the sorted lists of author and signer emails to match.
# Note that this also fails if a commit isn't signed-off at all.
#
# In Gerrit we already enable a form of this via https://gerrit-review.googlesource.com/Documentation/project-configuration.html#require-signed-off-by,
# but it does not support co-authors nor can it be used when testing GitHub PRs.
commit_authors="$(
{
git log -1 --pretty='%ae'
git log -1 --pretty='%(trailers:key=Co-authored-by,valueonly)' | sed -ne 's/.* <\(.*\)>/\1/p'
} | sort -u
)"
commit_signers="$(
{
git log -1 --pretty='%(trailers:key=Signed-off-by,valueonly)' | sed -ne 's/.* <\(.*\)>/\1/p'
} | sort -u
)"
if [[ "${commit_authors}" != "${commit_signers}" ]]; then
echo "Error: commit author email addresses do not match signed-off-by trailers"
echo
echo "Authors:"
echo "${commit_authors}"
echo
echo "Signers:"
echo "${commit_signers}"
exit 1
fi
93 changes: 93 additions & 0 deletions internal/ci/checks/commit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2024 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"io/fs"
"os/exec"
"path/filepath"
"runtime"
"testing"

"github.com/go-quicktest/qt"
"golang.org/x/tools/txtar"
)

func TestCommits(t *testing.T) {
// We are removing the dependency on bash very soon.
if _, err := exec.LookPath("bash"); err != nil {
t.Skipf("cannot find bash: %v", err)
}
if runtime.GOOS != "linux" {
t.Skipf("running only on Linux as others may ship older Bash")
}

scriptPath, err := filepath.Abs("commit.sh")
qt.Assert(t, qt.IsNil(err))

archive, err := txtar.ParseFile("testdata/checks.txtar")
qt.Assert(t, qt.IsNil(err))
archiveFS, err := txtar.FS(archive)
qt.Assert(t, qt.IsNil(err))

setupCommit := func(t *testing.T, name string) string {
commit, err := fs.ReadFile(archiveFS, name)
qt.Assert(t, qt.IsNil(err))

t.Logf("commit:\n%s", commit)

dir := t.TempDir()
mustRunCmd(t, dir, "git", "init")
mustRunCmd(t, dir, "git",
"-c", "user.email=cueckoo@gmail.com",
"-c", "user.name=cueckoo",
"commit", "--allow-empty", "-m", string(commit),
)
return dir
}

passFiles, err := fs.Glob(archiveFS, "pass-*")
qt.Assert(t, qt.IsNil(err))
for _, name := range passFiles {
t.Run(name, func(t *testing.T) {
dir := setupCommit(t, name)
cmd := exec.Command("bash", scriptPath)
cmd.Dir = dir
data, err := cmd.CombinedOutput()
t.Logf("error: %v", err)
qt.Assert(t, qt.IsNil(err), qt.Commentf("output: %q", data))
})
}

failFiles, err := fs.Glob(archiveFS, "fail-*")
qt.Assert(t, qt.IsNil(err))
for _, name := range failFiles {
t.Run(name, func(t *testing.T) {
dir := setupCommit(t, name)
cmd := exec.Command("bash", scriptPath)
cmd.Dir = dir
err = cmd.Run()
t.Logf("error: %v", err)
qt.Assert(t, qt.IsNotNil(err))
})
}
}

func mustRunCmd(t *testing.T, dir string, exe string, args ...string) {
cmd := exec.Command(exe, args...)
cmd.Dir = dir
data, err := cmd.CombinedOutput()
qt.Assert(t, qt.IsNil(err), qt.Commentf("output: %q", data))
}
44 changes: 44 additions & 0 deletions internal/ci/checks/testdata/checks.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# The commit author is cueckoo@gmail.com.

-- pass-short --
this is a one-line commit message

Signed-off-by: cueckoo <cueckoo@gmail.com>
-- pass-long --
this message is very long

So it needs many more paragraphs
to explain what it is doing.

Fixes #123.

Signed-off-by: cueckoo <cueckoo@gmail.com>
-- pass-co-author --
this is a collaborative commit

Signed-off-by: cueckoo <cueckoo@gmail.com>
Co-authored-by: collaborator <collaborator@corp.com>
Signed-off-by: collaborator <collaborator@corp.com>
-- pass-co-author-repeated --
this is a collaborative commit with repeats

Repeated trailers can happen due to human error, or when a commit
is taken over from a previous author, and they are harmless.

Co-authored-by: cueckoo <cueckoo@gmail.com>
Signed-off-by: cueckoo <cueckoo@gmail.com>
Co-authored-by: collaborator <collaborator@corp.com>
Signed-off-by: collaborator <collaborator@corp.com>
Signed-off-by: collaborator <collaborator@corp.com>

-- fail-no-empty --
This message forgot a title
which is followed by an empty line.

Signed-off-by: cueckoo <cueckoo@gmail.com>
-- fail-no-signoff --
this message lacks a signed-off-by trailer
-- fail-different-signoff --
this message is signed off by a different person

Signed-off-by: Other Developer <other.developer@corp.com>

0 comments on commit daf98a0

Please sign in to comment.