Skip to content

Commit 68cfc99

Browse files
authored
PMM-7 migrate CI from Travis to GitHub Actions (#66)
* set up formatter and license checker * set up github actions * use relative bin directory for tools * use gofumpt in place of go fmt * set up mysql in CI * reconfigure mysql for ubuntu * stop before configuring * drop mysql configuration * reduce log output * use correct input * remove travis config * remove leading zeroes
1 parent 57d7b21 commit 68cfc99

File tree

15 files changed

+1433
-131
lines changed

15 files changed

+1433
-131
lines changed

.github/check-license.go

Lines changed: 114 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//go:build tools
2+
// +build tools
3+
14
/*
25
Copyright (c) 2019, Percona LLC.
36
All rights reserved.
@@ -28,59 +31,140 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2831
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2932
*/
3033

31-
// +build ignore
32-
33-
// check-license checks that AGPL license header in all files matches header in this file.
34+
// check-license checks that the license header in all files matches the copyright text below.
3435
package main
3536

3637
import (
37-
"bufio"
3838
"flag"
3939
"fmt"
4040
"io"
4141
"log"
4242
"os"
4343
"path/filepath"
4444
"regexp"
45-
"runtime"
4645
)
4746

48-
func getHeader() string {
49-
_, file, _, ok := runtime.Caller(0)
50-
if !ok {
51-
panic("runtime.Caller(0) failed")
52-
}
53-
f, err := os.Open(file)
47+
var (
48+
generatedHeader = regexp.MustCompile(`^// Code generated .* DO NOT EDIT\.`)
49+
50+
copyrightText = `/*
51+
Copyright (c) 2019, Percona LLC.
52+
All rights reserved.
53+
54+
Redistribution and use in source and binary forms, with or without
55+
modification, are permitted provided that the following conditions are met:
56+
57+
* Redistributions of source code must retain the above copyright notice, this
58+
list of conditions and the following disclaimer.
59+
60+
* Redistributions in binary form must reproduce the above copyright notice,
61+
this list of conditions and the following disclaimer in the documentation
62+
and/or other materials provided with the distribution.
63+
64+
* Neither the name of the copyright holder nor the names of its
65+
contributors may be used to endorse or promote products derived from
66+
this software without specific prior written permission.
67+
68+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
69+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
70+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
71+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
72+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
73+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
74+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
75+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
76+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
77+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
78+
*/
79+
`
80+
81+
copyrightPattern = regexp.MustCompile(`^/\*
82+
Copyright \(c\) 20\d{2}, Percona LLC\.
83+
All rights reserved\.
84+
85+
Redistribution and use in source and binary forms, with or without
86+
modification, are permitted provided that the following conditions are met:
87+
88+
\* Redistributions of source code must retain the above copyright notice, this
89+
{2}list of conditions and the following disclaimer.
90+
91+
\* Redistributions in binary form must reproduce the above copyright notice,
92+
{2}this list of conditions and the following disclaimer in the documentation
93+
{2}and/or other materials provided with the distribution.
94+
95+
\* Neither the name of the copyright holder nor the names of its
96+
{2}contributors may be used to endorse or promote products derived from
97+
{2}this software without specific prior written permission.
98+
99+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
100+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
101+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
102+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
103+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
104+
DAMAGES \(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
105+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION\) HOWEVER
106+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
107+
OR TORT \(INCLUDING NEGLIGENCE OR OTHERWISE\) ARISING IN ANY WAY OUT OF THE USE
108+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE\.
109+
\*/
110+
`,
111+
)
112+
)
113+
114+
func checkHeader(path string) bool {
115+
f, err := os.Open(path)
54116
if err != nil {
55117
log.Fatal(err)
56118
}
57119
defer f.Close()
58120

59-
var header string
60-
s := bufio.NewScanner(f)
61-
for s.Scan() {
62-
if s.Text() == "" {
63-
break
64-
}
65-
header += s.Text() + "\n"
121+
actual := make([]byte, len(copyrightText))
122+
_, err = io.ReadFull(f, actual)
123+
if err == io.ErrUnexpectedEOF {
124+
err = nil // some files are shorter than license header
66125
}
67-
header += "\n"
68-
if err := s.Err(); err != nil {
69-
log.Fatal(err)
126+
if err != nil {
127+
log.Printf("%s - %s", path, err)
128+
return false
70129
}
71-
return header
72-
}
73130

74-
var generatedHeader = regexp.MustCompile(`^// Code generated .* DO NOT EDIT\.`)
131+
if string(copyrightText) == string(actual) {
132+
return true
133+
}
134+
135+
if generatedHeader.Match(actual) {
136+
return true
137+
}
75138

76-
func checkHeader(path string, header string) bool {
139+
if !copyrightPattern.Match(actual) {
140+
if !checkBuildIgnoreHeader(path) {
141+
log.Print(path)
142+
return false
143+
}
144+
return true
145+
}
146+
147+
return true
148+
}
149+
150+
func checkBuildIgnoreHeader(path string) bool {
77151
f, err := os.Open(path)
78152
if err != nil {
79153
log.Fatal(err)
80154
}
81155
defer f.Close()
82156

83-
actual := make([]byte, len(header))
157+
headerPattern := regexp.MustCompile(`^//go:build tools
158+
// \+build tools
159+
160+
` + copyrightPattern.String())
161+
162+
headerText := `//go:build tools
163+
// +build tools
164+
165+
` + copyrightText
166+
167+
actual := make([]byte, len(headerText))
84168
_, err = io.ReadFull(f, actual)
85169
if err == io.ErrUnexpectedEOF {
86170
err = nil // some files are shorter than license header
@@ -90,14 +174,15 @@ func checkHeader(path string, header string) bool {
90174
return false
91175
}
92176

93-
if generatedHeader.Match(actual) {
177+
if string(headerText) == string(actual) {
94178
return true
95179
}
96180

97-
if header != string(actual) {
181+
if !headerPattern.Match(actual) {
98182
log.Print(path)
99183
return false
100184
}
185+
101186
return true
102187
}
103188

@@ -109,8 +194,6 @@ func main() {
109194
}
110195
flag.Parse()
111196

112-
header := getHeader()
113-
114197
ok := true
115198
filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
116199
if err != nil {
@@ -126,7 +209,7 @@ func main() {
126209
}
127210

128211
if filepath.Ext(info.Name()) == ".go" {
129-
if !checkHeader(path, header) {
212+
if !checkHeader(path) {
130213
ok = false
131214
}
132215
}

.github/workflows/go.yml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: Go
2+
3+
on:
4+
schedule:
5+
# run every Sunday
6+
- cron: '0 13 * * 0'
7+
push:
8+
branches:
9+
- main
10+
tags:
11+
- v[0-9]+.[0-9]+.[0-9]+*
12+
pull_request:
13+
14+
permissions:
15+
contents: read
16+
17+
jobs:
18+
build:
19+
name: Build
20+
21+
runs-on: ubuntu-latest
22+
23+
steps:
24+
- name: Install MySQL
25+
run: |
26+
sudo apt remove --purge mysql* -y
27+
sudo apt autoremove -y
28+
sudo apt autoclean
29+
30+
sudo DEBIAN_FRONTEND=noninteractive apt update
31+
sudo DEBIAN_FRONTEND=noninteractive apt install -y mysql-server-8.0
32+
33+
sudo systemctl start mysql.service
34+
sudo systemctl status mysql.service
35+
36+
- name: Checkout code
37+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
38+
39+
- name: Set up Go
40+
uses: actions/setup-go@0aaccfd150d50ccaeb58ebd88d36e91967a5f35b # v5.4.0
41+
with:
42+
go-version-file: ${{ github.workspace }}/go.mod
43+
44+
45+
- name: Install development tools
46+
run: make init
47+
48+
- name: Run checks/formatting
49+
run: |
50+
make check format
51+
git diff --exit-code
52+
53+
- name: Run tests with code coverage
54+
run: |
55+
go clean -testcache
56+
make test-cover
57+
make test-race
58+
59+
- name: Upload coverage results
60+
uses: codecov/codecov-action@0565863a31f2c772f9f0395002a31e3f06189574 # v5.4.0
61+
with:
62+
files: cover.out
63+
flags: agent
64+
fail_ci_if_error: false
65+
66+
- name: Run debug commands on failure
67+
if: ${{ failure() }}
68+
run: |
69+
env | sort
70+
go env | sort
71+
git status

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
coverage.out
22
*.swp
3+
bin/
4+

.travis.yml

Lines changed: 0 additions & 60 deletions
This file was deleted.

Makefile

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@ help: ## Display this help message.
33
@grep '^[a-zA-Z]' $(MAKEFILE_LIST) | \
44
awk -F ':.*?## ' 'NF==2 {printf " %-26s%s\n", $$1, $$2}'
55

6-
init: ## Installs tools to $GOPATH/bin (which is expected to be in $PATH).
7-
curl https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin
8-
9-
go install ./vendor/golang.org/x/tools/cmd/goimports
10-
11-
go test -i ./...
12-
go test -race -i ./...
6+
init: ## Installs development tools.
7+
rm -rf bin
8+
cd tools && go generate -x -tags=tools
139

1410
TEST_FLAGS ?= -timeout=20s
1511

@@ -31,7 +27,7 @@ check: ## Run required checkers and linters.
3127
FILES = $(shell find . -type f -name '*.go' -not -path "./vendor/*")
3228

3329
format: ## Format source code.
34-
gofmt -w -s $(FILES)
35-
goimports -local github.com/percona/go-mysql -l -w $(FILES)
30+
bin/gofumpt -l -w .
31+
bin/goimports -local github.com/percona/go-mysql -l -w $(FILES)
3632

3733
.PHONY: test

dsn/dsn.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,8 @@ const (
6868
HiddenPassword = "***"
6969
)
7070

71-
var (
72-
// ErrNoSocket is returned when GetSocketFromProcessLists can't locate socket.
73-
ErrNoSocket = errors.New("cannot auto-detect MySQL socket")
74-
)
71+
// ErrNoSocket is returned when GetSocketFromProcessLists can't locate socket.
72+
var ErrNoSocket = errors.New("cannot auto-detect MySQL socket")
7573

7674
func (dsn DSN) AutoDetect(ctx context.Context) (DSN, error) {
7775
defaults, err := Defaults(dsn.DefaultsFile)

0 commit comments

Comments
 (0)