Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(handler): HTTP Server handler and pull requests title linter #77

Merged
merged 6 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/pull-request-lint.yaml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 6 additions & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
},
}
)
Expand Down
2 changes: 1 addition & 1 deletion githooks/commit-msg
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ then
npm install -g @commitlint/config-conventional
fi

commitlint -g githooks/commitlint.config.js -V --edit "$1"
commitlint -g $(git config core.hooksPath)/commitlint.config.js -x $(npm root -g)/@commitlint/config-conventional -V --edit "$1"
4 changes: 1 addition & 3 deletions githooks/commitlint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
25 changes: 20 additions & 5 deletions internal/server/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,33 @@ 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{
v1alpha1.NewServer(),
}
)

// 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)

Expand All @@ -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
Expand Down
25 changes: 25 additions & 0 deletions internal/server/serve_test.go
Original file line number Diff line number Diff line change
@@ -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)

Expand Down