diff --git a/.github/workflows/pull-request-lint.yaml b/.github/workflows/pull-request-lint.yaml new file mode 100644 index 0000000..5a01427 --- /dev/null +++ b/.github/workflows/pull-request-lint.yaml @@ -0,0 +1,21 @@ +on: + pull_request: + types: + - opened + - edited + - ready_for_review + +jobs: + lint_title: + name: Lint pull request title + runs-on: ubuntu-latest + if: github.event_name == 'pull_request' && !contains(fromJson('["skip-commit-lint"]'), github.event.pull_request.labels) + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Install Dependencies + run: npm install @commitlint/cli @commitlint/config-conventional + - uses: JulienKode/pull-request-name-linter-action@v0.5.0 + with: + configuration-path: githooks/commitlint.config.js \ No newline at end of file diff --git a/cmd/serve.go b/cmd/serve.go index 08f844b..3f5f4d8 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -42,7 +42,12 @@ var ( log.Fatal().Err(err).Msg("invalid configuration") } - log.Fatal().Err(server.Serve(*flagPort)).Msg("Error during server start") + srv, err := server.NewServer(*flagPort) + if err != nil { + log.Fatal().Err(err).Msg("failed to create server") + } + + log.Fatal().Err(srv.Serve()).Msg("Error during server start") }, } ) diff --git a/githooks/commit-msg b/githooks/commit-msg index 98540a5..c61f475 100755 --- a/githooks/commit-msg +++ b/githooks/commit-msg @@ -17,4 +17,4 @@ then npm install -g @commitlint/config-conventional fi -commitlint -g githooks/commitlint.config.js -V --edit "$1" \ No newline at end of file +commitlint -g $(git config core.hooksPath)/commitlint.config.js -x $(npm root -g)/@commitlint/config-conventional -V --edit "$1" \ No newline at end of file diff --git a/githooks/commitlint.config.js b/githooks/commitlint.config.js index eefe3b4..9eb8bf2 100644 --- a/githooks/commitlint.config.js +++ b/githooks/commitlint.config.js @@ -33,15 +33,13 @@ const Configuration = { ]], 'scope-case': [2, 'always', 'lowerCase'], 'scope-enum': [2, 'always', [ - 'github/actions', - 'github/codeowners', - 'github', 'handler', 'security', 'formatting', 'storage', 'configuration', 'go', + 'github', 'git' ]], 'scope-empty': [1, 'never'], diff --git a/internal/server/serve.go b/internal/server/serve.go index fda2fdf..4370606 100644 --- a/internal/server/serve.go +++ b/internal/server/serve.go @@ -19,6 +19,10 @@ type APIVersion interface { WebhookHandler() http.HandlerFunc } +type Server struct { + *http.Server +} + var ( // apiVersions is a list of supported API versions by the server apiVersions = []APIVersion{ @@ -26,13 +30,22 @@ var ( } ) -// Serve the proxy server on the given port for all supported API versions -func Serve(port int) error { +// NewServer create a new server instance with the given port +func NewServer(port int) (*Server, error) { if !validPort(port) { - return fmt.Errorf("invalid port") + return nil, fmt.Errorf("invalid port") } - log.Info().Msgf("Listening on port %d", port) + return &Server{ + Server: &http.Server{ + Addr: fmt.Sprintf(":%d", port), + Handler: nil, + }, + }, nil +} + +// Serve the proxy server on the given port for all supported API versions +func (s *Server) Serve() error { router := newRouter() router.Use(loggingMiddleware) @@ -41,7 +54,9 @@ func Serve(port int) error { router.Handle("/metrics", promhttp.Handler()).Name("metrics") } - return http.ListenAndServe(fmt.Sprintf(":%d", port), router) + s.Handler = router + log.Info().Msgf("Listening on %s", s.Addr) + return s.ListenAndServe() } // newRouter returns a new router with all the routes diff --git a/internal/server/serve_test.go b/internal/server/serve_test.go index 79f4579..3d7b384 100644 --- a/internal/server/serve_test.go +++ b/internal/server/serve_test.go @@ -1,11 +1,36 @@ package server import ( + "context" + "net/http" "testing" + "time" "github.com/stretchr/testify/assert" ) +func Test_NewServer(t *testing.T) { + srv, err := NewServer(8080) + assert.NoError(t, err) + assert.NotNil(t, srv) + + srv, err = NewServer(0) + assert.Error(t, err) + assert.Nil(t, srv) +} + +func Test_Serve(t *testing.T) { + srv, err := NewServer(38081) + assert.NoError(t, err) + + go func() { + time.Sleep(2 * time.Second) + assert.ErrorIs(t, srv.Shutdown(context.Background()), http.ErrServerClosed) + }() + + assert.ErrorIs(t, srv.Serve(), http.ErrServerClosed) +} + func Test_validPort(t *testing.T) { assert := assert.New(t)