Skip to content

Commit efa7152

Browse files
committed
BUG/MINOR: check if whole message is properly formatted
between subject and body of a message we need to have and empty line
1 parent a27bada commit efa7152

File tree

5 files changed

+89
-395
lines changed

5 files changed

+89
-395
lines changed

.github/workflows/actions.yaml

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@ jobs:
55
name: Go lint
66
runs-on: ubuntu-latest
77
steps:
8-
- uses: actions/checkout@v2
8+
- uses: actions/checkout@v4
99
- name: golangci-lint
10-
uses: golangci/golangci-lint-action@v2
10+
uses: golangci/golangci-lint-action@v3
1111
go_build:
1212
name: Go build
1313
runs-on: ubuntu-latest
1414
needs: ["go_lint"]
1515
steps:
1616
- name: Check out code into the Go module directory
17-
uses: actions/checkout@v2
18-
- name: Set up Go 1.17
19-
uses: actions/setup-go@v2
17+
uses: actions/checkout@v4
18+
- name: Set up Go
19+
uses: actions/setup-go@v4
2020
with:
21-
go-version: 1.17
21+
go-version-file: 'go.mod'
22+
check-latest: true
2223
id: go
2324
- name: Get dependencies
2425
run: |
@@ -31,10 +32,12 @@ jobs:
3132
runs-on: ubuntu-latest
3233
needs: ["go_build"]
3334
steps:
34-
- uses: actions/checkout@v2
35-
- uses: actions/setup-go@v2
35+
- uses: actions/checkout@v4
36+
- name: Set up Go
37+
uses: actions/setup-go@v4
3638
with:
37-
go-version: 1.17
39+
go-version-file: 'go.mod'
40+
check-latest: true
3841
id: go
3942
- name: Get dependencies
4043
run: |
@@ -48,13 +51,14 @@ jobs:
4851
runs-on: ubuntu-latest
4952
needs: ["go_build"]
5053
steps:
51-
- uses: actions/checkout@v2
54+
- uses: actions/checkout@v4
5255
with:
5356
fetch-depth: 0
54-
- name: Set up Go 1.17
55-
uses: actions/setup-go@v2
57+
- name: Set up Go
58+
uses: actions/setup-go@v4
5659
with:
57-
go-version: 1.17
60+
go-version-file: 'go.mod'
61+
check-latest: true
5862
- name: check-commit
5963
run: go build -o check && ./check
6064
env:

.github/workflows/build_on_trag.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ jobs:
2323
contents: read
2424

2525
steps:
26-
- uses: actions/checkout@v2
26+
- uses: actions/checkout@v4
2727

2828
- name: Build image
2929
run: docker build . --file Dockerfile --tag $IMAGE_NAME --label "runnumber=${GITHUB_RUN_ID}"
3030

3131
- name: Log in to registry
32-
# This is where you will update the PAT to GITHUB_TOKEN
32+
# This is where you will update the PATH to GITHUB_TOKEN
3333
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
3434

3535
- name: Push image

check.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/hex"
66
"errors"
77
"fmt"
8-
"io/ioutil"
98
"log"
109
"os"
1110
"path"
@@ -15,7 +14,7 @@ import (
1514
"unicode"
1615
"unicode/utf8"
1716

18-
"github.com/google/go-github/v35/github"
17+
"github.com/google/go-github/v56/github"
1918

2019
"github.com/xanzy/go-gitlab"
2120
"golang.org/x/oauth2"
@@ -244,7 +243,7 @@ func LoadCommitPolicy(filename string) (CommitPolicyConfig, error) {
244243

245244
var config string
246245

247-
if data, err := ioutil.ReadFile(filename); err != nil {
246+
if data, err := os.ReadFile(filename); err != nil {
248247
log.Printf("warning: using built-in fallback configuration with HAProxy defaults (%s)", err)
249248

250249
config = defaultConf
@@ -297,8 +296,18 @@ func getGithubCommitSubjects() ([]string, error) {
297296

298297
subjects := []string{}
299298
for _, c := range commits {
300-
l := strings.SplitN(c.Commit.GetMessage(), "\n", 2)
299+
l := strings.SplitN(c.Commit.GetMessage(), "\n", 3)
300+
hash := c.Commit.GetSHA()
301+
if len(hash) > 8 {
302+
hash = hash[:8]
303+
}
304+
if len(l) > 1 {
305+
if l[1] != "" {
306+
return nil, fmt.Errorf("empty line between subject and body is required: %s %s", hash, l[0])
307+
}
308+
}
301309
if len(l) > 0 {
310+
log.Printf("detected message %s from commit %s", l[0], hash)
302311
subjects = append(subjects, l[0])
303312
}
304313
}
@@ -335,8 +344,15 @@ func getGitlabCommitSubjects() ([]string, error) {
335344

336345
subjects := []string{}
337346
for _, c := range commits {
338-
l := strings.SplitN(c.Message, "\n", 2)
347+
l := strings.SplitN(c.Message, "\n", 3)
348+
hash := c.ShortID
339349
if len(l) > 0 {
350+
if len(l) > 1 {
351+
if l[1] != "" {
352+
return nil, fmt.Errorf("empty line between subject and body is required: %s %s", hash, l[0])
353+
}
354+
}
355+
log.Printf("detected message %s from commit %s", l[0], hash)
340356
subjects = append(subjects, l[0])
341357
}
342358
}

go.mod

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
module check-commit
22

3-
go 1.15
3+
go 1.21
44

55
require (
6-
github.com/google/go-github/v35 v35.0.0
7-
github.com/xanzy/go-gitlab v0.48.0
8-
golang.org/x/oauth2 v0.0.0-20210413134643-5e61552d6c78
6+
github.com/google/go-github/v56 v56.0.0
7+
github.com/xanzy/go-gitlab v0.93.2
8+
golang.org/x/oauth2 v0.13.0
99
gopkg.in/yaml.v2 v2.4.0
1010
)
11+
12+
require (
13+
github.com/golang/protobuf v1.5.3 // indirect
14+
github.com/google/go-querystring v1.1.0 // indirect
15+
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
16+
github.com/hashicorp/go-retryablehttp v0.7.2 // indirect
17+
golang.org/x/net v0.16.0 // indirect
18+
golang.org/x/time v0.3.0 // indirect
19+
google.golang.org/appengine v1.6.7 // indirect
20+
google.golang.org/protobuf v1.31.0 // indirect
21+
)

0 commit comments

Comments
 (0)