-
Notifications
You must be signed in to change notification settings - Fork 30
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
dependencies update + more test codes + sample middleware code #26
Open
pilinux
wants to merge
9
commits into
onrik:main
Choose a base branch
from
pilinux:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
560b822
ci: added test files
pilinux 2c50815
bumped dependencies
pilinux 05024f0
bump golang.org/x/term from 0.14.0 to 0.15.0
pilinux 927045c
`onrik/logrus` -> `pilinux/logrus`
pilinux ce27bab
bumped dependencies
pilinux dfa7111
ci: github actions
pilinux 0a71a3b
ci: explicitly set go v1.20.14
pilinux fd46dfb
ci: upload coverage report during `push` event
pilinux 7b3afff
chore: bumped dependencies
pilinux File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Documentation for all configuration options: | ||
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates | ||
|
||
version: 2 | ||
updates: | ||
- package-ecosystem: "gomod" | ||
directory: "/" | ||
schedule: | ||
interval: "daily" | ||
# Allow up to 10 open pull requests for dependencies | ||
open-pull-requests-limit: 10 | ||
|
||
- package-ecosystem: github-actions | ||
directory: / | ||
schedule: | ||
interval: daily |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# For most projects, this workflow file will not need changing; you simply need | ||
# to commit it to your repository. | ||
# | ||
# You may wish to alter this file to override the set of languages analyzed, | ||
# or to provide custom queries or build logic. | ||
# | ||
# ******** NOTE ******** | ||
# We have attempted to detect the languages in your repository. Please check | ||
# the `language` matrix defined below to confirm you have the correct set of | ||
# supported CodeQL languages. | ||
# | ||
name: "CodeQL" | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
# The branches below must be a subset of the branches above | ||
branches: | ||
- main | ||
schedule: | ||
- cron: "19 7 * * 2" | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
CodeQL: | ||
name: Analyze | ||
runs-on: ubuntu-latest | ||
permissions: | ||
actions: read | ||
contents: read | ||
security-events: write | ||
|
||
strategy: | ||
fail-fast: false | ||
matrix: | ||
language: ["go"] | ||
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] | ||
# Learn more: | ||
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed | ||
|
||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
# Initializes the CodeQL tools for scanning. | ||
- name: Initialize CodeQL | ||
uses: github/codeql-action/init@v3 | ||
with: | ||
languages: ${{ matrix.language }} | ||
# If you wish to specify custom queries, you can do so here or in a config file. | ||
# By default, queries listed here will override any specified in a config file. | ||
# Prefix the list here with "+" to use these queries and those in the config file. | ||
# queries: ./path/to/local/query, your-org/your-repo/queries@main | ||
|
||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java). | ||
# If this step fails, then you should remove it and run the build manually (see below) | ||
- name: Autobuild | ||
uses: github/codeql-action/autobuild@v3 | ||
|
||
# ℹ️ Command-line programs to run using the OS shell. | ||
# 📚 https://git.io/JvXDl | ||
|
||
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines | ||
# and modify them (or add more) to build your code if your project | ||
# uses a compiled language | ||
|
||
#- run: | | ||
# make bootstrap | ||
# make release | ||
|
||
- name: Perform CodeQL Analysis | ||
uses: github/codeql-action/analyze@v3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Dependency Review Action | ||
# | ||
# This Action will scan dependency manifest files that change as part of a Pull Request, | ||
# surfacing known-vulnerable versions of the packages declared or updated in the PR. | ||
# Once installed, if the workflow run is marked as required, | ||
# PRs introducing known-vulnerable packages will be blocked from merging. | ||
# | ||
# Source repository: https://github.com/actions/dependency-review-action | ||
name: "Dependency Review" | ||
on: [pull_request] | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
dependency-review: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: "Checkout Repository" | ||
uses: actions/checkout@v4 | ||
|
||
- name: "Dependency Review" | ||
uses: actions/dependency-review-action@v4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
name: Go | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
actions: read | ||
contents: read | ||
|
||
steps: | ||
- name: Setup Go | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: 1.22.5 | ||
- run: go version | ||
|
||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Examine source code for Linux AMD | ||
run: GOOS=linux GOARCH=amd64 go vet -v ./... | ||
|
||
- name: Examine source code for MacOS AMD | ||
run: GOOS=darwin GOARCH=amd64 go vet -v ./... | ||
|
||
- name: Examine source code for Windows AMD | ||
run: GOOS=windows GOARCH=amd64 go vet -v ./... | ||
|
||
- name: Examine source code for Linux ARM | ||
run: GOOS=linux GOARCH=arm64 go vet -v ./... | ||
|
||
- name: Examine source code for MacOS ARM | ||
run: GOOS=darwin GOARCH=arm64 go vet -v ./... | ||
|
||
- name: Examine source code for Windows ARM | ||
run: GOOS=windows GOARCH=arm64 go vet -v ./... | ||
|
||
- name: Gosec security scanner | ||
uses: securego/gosec@master | ||
with: | ||
args: ./... | ||
|
||
- name: Test | ||
if: github.event_name == 'push' | ||
run: go test -v -cover -coverprofile=coverage.txt -covermode=atomic ./... | ||
|
||
- name: Upload coverage reports to Codecov | ||
if: github.event_name == 'push' | ||
uses: codecov/codecov-action@v4 | ||
with: | ||
token: ${{ secrets.CODECOV_TOKEN }} | ||
slug: pilinux/logrus |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
name: golangci-lint | ||
on: | ||
push: | ||
tags: | ||
- v* | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
permissions: | ||
contents: read | ||
# Optional: allow read access to pull request. Use with `only-new-issues` option. | ||
pull-requests: read | ||
|
||
jobs: | ||
golangci: | ||
name: lint | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Setup Go | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: 1.22.5 | ||
- run: go version | ||
|
||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: golangci-lint | ||
uses: golangci/golangci-lint-action@v6 | ||
with: | ||
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version | ||
version: v1.59.1 | ||
|
||
# Optional: working directory, useful for monorepos | ||
# working-directory: somedir | ||
|
||
# Optional: golangci-lint command line arguments. | ||
args: --timeout 5m --verbose | ||
|
||
# Optional: show only new issues if it's a pull request. The default value is `false`. | ||
only-new-issues: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package example | ||
|
||
// github.com/pilinux/gorest | ||
// The MIT License (MIT) | ||
// Copyright (c) 2023 pilinux | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/pilinux/logrus/sentry" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// SentryCapture - capture errors and forward to sentry.io | ||
func SentryCapture(sentryDsn string) gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
// Automatic recovery from panic | ||
defer func() { | ||
if r := recover(); r != nil { | ||
log.Error("panic msg: middleware -> sentry panicked") | ||
} | ||
}() | ||
|
||
sentryHook, err := sentry.NewHook(sentry.Options{ | ||
Dsn: sentryDsn, | ||
Debug: true, | ||
Environment: "development", | ||
Release: "1.0.0", | ||
}) | ||
if err != nil { | ||
// middleware -> sentry NewHook failed | ||
c.AbortWithStatusJSON(http.StatusInternalServerError, "internal server error") | ||
return | ||
} | ||
sentryHook.AddTag("method", c.Request.Method) | ||
sentryHook.AddTag("path", c.Request.URL.Path) | ||
sentryHook.AddTag("host", c.Request.Host) | ||
sentryHook.AddTag("remote.addr", c.Request.RemoteAddr) | ||
sentryHook.AddTag("user.agent", c.Request.UserAgent()) | ||
defer sentryHook.Flush() | ||
|
||
log.AddHook(sentryHook) | ||
|
||
c.Next() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package example | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func TestSentryCapture(t *testing.T) { | ||
// set up a gin router and handler | ||
gin.SetMode(gin.TestMode) | ||
router := gin.New() | ||
err := router.SetTrustedProxies(nil) | ||
if err != nil { | ||
t.Errorf("failed to set trusted proxies to nil") | ||
} | ||
router.TrustedPlatform = "X-Real-Ip" | ||
|
||
// register middleware with valid sentry dsn | ||
sentryDSN := os.Getenv("TEST_SENTRY_DSN") | ||
router.Use(SentryCapture(sentryDSN)) | ||
|
||
// define test route | ||
router.GET("/", func(c *gin.Context) { | ||
// send log to sentry for testing | ||
log.Info("testing sentry integration") | ||
c.Status(http.StatusOK) | ||
}) | ||
|
||
// perform request and get response | ||
req, err := http.NewRequest("GET", "/", nil) | ||
if err != nil { | ||
t.Errorf("failed to create an HTTP request") | ||
return | ||
} | ||
req.Header.Set("User-Agent", "Test-User-Agent") | ||
res := httptest.NewRecorder() | ||
|
||
router.ServeHTTP(res, req) | ||
|
||
// check response | ||
if res.Code != http.StatusOK { | ||
t.Errorf("expected response code %v, got '%v'", http.StatusOK, res.Code) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like it's redundant.