Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #73 from bancodobrasil/develop
Browse files Browse the repository at this point in the history
Version 1.8.0
  • Loading branch information
ralphg6 authored Dec 5, 2022
2 parents 127e868 + 976b481 commit 86d198b
Show file tree
Hide file tree
Showing 33 changed files with 4,679 additions and 448 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/dependent-issues.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Dependent Issues

on:
issues:
types:
- opened
- edited
- closed
- reopened
pull_request_target:
types:
- opened
- edited
- closed
- reopened
# Makes sure we always add status check for PRs. Useful only if
# this action is required to pass before merging. Otherwise, it
# can be removed.
- synchronize

# Schedule a daily check. Useful if you reference cross-repository
# issues or pull requests. Otherwise, it can be removed.
schedule:
- cron: '0 0 * * *'

jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: z0al/dependent-issues@v1
env:
# (Required) The token to use to make API calls to GitHub.
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# (Optional) The token to use to make API calls to GitHub for remote repos.
GITHUB_READ_TOKEN: ${{ secrets.GITHUB_READ_TOKEN }}

with:
# (Optional) The label to use to mark dependent issues
label: dependent

# (Optional) Enable checking for dependencies in issues.
# Enable by setting the value to "on". Default "off"
check_issues: off

# (Optional) Ignore dependabot PRs.
# Enable by setting the value to "on". Default "off"
ignore_dependabot: off

# (Optional) A comma-separated list of keywords. Default
# "depends on, blocked by"
keywords: depends on, blocked by

# (Optional) A custom comment body. It supports `{{ dependencies }}` token.
comment: >
This PR/issue depends on:
{{ dependencies }}
By **[Dependent Issues](https://github.com/z0al/dependent-issues)** (🤖). Happy coding!
7 changes: 4 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ jobs:
- name: "Run golint"
if: ${{ matrix.go_version == env.LATEST_GO_VERSION && matrix.os == 'ubuntu-latest'}}
run: |
make lint
go install golang.org/x/lint/golint@latest
golint -set_exit_status $1 ./...
exit $1
- name: "Run gofmt"
Expand All @@ -47,5 +48,5 @@ jobs:
- name: Build
run: go build -v ./...

#- name: Test
# run: go test -v ./...
- name: Test
run: go test -v ./...
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ _data/
.json
.vscode

data/
data/

__debug_bin
16 changes: 13 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ lint:
@make run-on-our-code-directories ARGS="golint"

up-services:
docker-compose up -d adminer
docker compose up -d adminer

build:generate-swagger
go build -o api
Expand All @@ -15,8 +15,12 @@ up:up-services
run:build
./api

coverage:
go test ./... -coverprofile=coverage.out
go tool cover -html=coverage.out

test:
@make run-on-our-code-directories ARGS="go test -v"
@make run-on-our-code-directories ARGS="go test -v -coverprofile=coverage.out"

run-on-our-code-directories:
@echo "${ARGS} <our-code-directories>"
Expand All @@ -33,4 +37,10 @@ generate-mocks:

generate-swagger:
# Install swag on https://github.com/swaggo/swag
swag i
swag i

migrate-up:
MIGRATE=up make run

migrate-down:
MIGRATE=down make run
10 changes: 8 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@ import (
"github.com/spf13/viper"
)

//Config ...
// Config ...
type Config struct {
AllowOrigins string `mapstructure:"ALLOW_ORIGINS"`
Port string `mapstructure:"PORT"`
MysqlURI string `mapstructure:"FEATWS_API_MYSQL_URI"`
Migrate string `mapstructure:"MIGRATE"`
GitlabToken string `mapstructure:"FEATWS_API_GITLAB_TOKEN"`
GitlabURL string `mapstructure:"FEATWS_API_GITLAB_URL"`
GitlabNamespace string `mapstructure:"FEATWS_API_GITLAB_NAMESPACE"`
GitlabPrefix string `mapstructure:"FEATWS_API_GITLAB_PREFIX"`
GitlabDefaultBranch string `mapstructure:"FEATWS_API_GITLAB_DEFAULT_BRANCH"`
GitlabCIScript string `mapstructure:"FEATWS_API_GITLAB_CI_SCRIPT"`
ExternalHost string `mapstructure:"EXTERNAL_HOST"`
OpenAMURL string `mapstructure:"OPENAM_URL"`
AuthMode string `mapstructure:"FEATWS_API_AUTH_MODE"`
}

var config = &Config{}

//LoadConfig ...
// LoadConfig ...
func LoadConfig() (err error) {
viper.AddConfigPath("./")
viper.SetConfigFile(".env")
Expand All @@ -41,6 +44,9 @@ func LoadConfig() (err error) {
viper.SetDefault("FEATWS_API_GITLAB_DEFAULT_BRANCH", "main")
viper.SetDefault("FEATWS_API_GITLAB_CI_SCRIPT", "")
viper.SetDefault("EXTERNAL_HOST", "localhost:9007")
viper.SetDefault("MIGRATE", "")
viper.SetDefault("OPENAM_URL", "")
viper.SetDefault("FEATWS_API_AUTH_MODE", "none")

err = viper.ReadInConfig()
if err != nil {
Expand Down
26 changes: 26 additions & 0 deletions controllers/health_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package controllers_test

// func TestHealthLive(t *testing.T) {
// gin.SetMode(gin.TestMode)

// r := gin.Default()
// r.GET("/health/live", controllers.NewHealthController().HealthLiveHandler())

// req, err := http.NewRequest("GET", "/health/live", nil)
// if err != nil {
// t.Fatalf("Couldn't create request: %v\n", err)
// }

// w := httptest.NewRecorder()
// r.ServeHTTP(w, req)
// defer w.Result().Body.Close()

// mockUserResp := `{"goroutine-threshold": "OK"}`
// //Using testify

// assert.Nil(t, err)
// assert.Equal(t, http.StatusOK, w.Code)
// responseData, _ := ioutil.ReadAll(w.Body)
// assert.Equal(t, mockUserResp, string(responseData))

// }
11 changes: 3 additions & 8 deletions controllers/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@ import (
"github.com/gin-gonic/gin"
)

// func HomeHandler() gin.HandlerFunc {
// return func(c *gin.Context) {
// c.String(http.StatusOK, "FeatWS Resolver Works!!!")
// }

// }

// HomeHandler ...
func HomeHandler(c *gin.Context) {
c.String(http.StatusOK, "FeatWS API Works!!!")
c.JSON(http.StatusOK, gin.H{
"message": "FeatWS API Works!!!",
})
}
46 changes: 46 additions & 0 deletions controllers/home_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package controllers_test

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/bancodobrasil/featws-api/controllers"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)

// func TestHomeHandler(t *testing.T) {
// gin.SetMode(gin.TestMode)

// r := gin.Default()
// r.GET("/", controllers.HomeHandler)

// req, err := http.NewRequest("GET", "/", nil)
// if err != nil {
// t.Fatalf("Couldn't create request: %v\n", err)
// }

// w := httptest.NewRecorder()
// r.ServeHTTP(w, req)
// defer w.Result().Body.Close()

// mockUserResp := `{"message":"FeatWS API Works!!!"}`
// //Using testify

// assert.Nil(t, err)
// assert.Equal(t, http.StatusOK, w.Code)
// responseData, _ := ioutil.ReadAll(w.Body)
// assert.Equal(t, mockUserResp, string(responseData))

// }
func TestUnitHomeHandler(t *testing.T) {
gin.SetMode(gin.TestMode)

w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)

controllers.HomeHandler(c)
assert.Equal(t, http.StatusOK, w.Code)

}
33 changes: 25 additions & 8 deletions controllers/v1/rulesheets.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ func NewRulesheets(service services.Rulesheets) Rulesheets {
// @Failure 500 {object} responses.Error "Internal Server Error"
// @Failure default {object} responses.Error
// @Response 404 "Not Found"
// @Security ApiKeyAuth
// @Security Authentication Api Key
// @Security Authentication Bearer Token
// @Router /rulesheets [post]
func (rc *rulesheets) CreateRulesheet() gin.HandlerFunc {

return func(c *gin.Context) {

// Pass the context of gin Request
ctx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
ctx, cancel := context.WithTimeout(c.Request.Context(), 1000*time.Second)
var payload payloads.Rulesheet
defer cancel()

Expand Down Expand Up @@ -95,8 +96,10 @@ func (rc *rulesheets) CreateRulesheet() gin.HandlerFunc {
}

var response = responses.NewRulesheet(&dto)
//id := c.Query("id")
c.JSON(http.StatusCreated, response)
}

}

// GetRulesheets godoc
Expand All @@ -114,7 +117,8 @@ func (rc *rulesheets) CreateRulesheet() gin.HandlerFunc {
// @Failure 500 {object} responses.Error "Internal Server Error"
// @Failure default {object} responses.Error
// @Response 404 "Not Found"
// @Security ApiKeyAuth
// @Security Authentication Api Key
// @Security Authentication Bearer Token
// @Router /rulesheets/ [get]
func (rc *rulesheets) GetRulesheets() gin.HandlerFunc {

Expand Down Expand Up @@ -217,13 +221,14 @@ func (rc *rulesheets) GetRulesheets() gin.HandlerFunc {
// @Failure 500 {object} responses.Error "Internal Server Error"
// @Failure default {object} responses.Error
// @Response 404 "Not Found"
// @Security ApiKeyAuth
// @Security Authentication Api Key
// @Security Authentication Bearer Token
// @Router /rulesheets/{id} [get]
func (rc *rulesheets) GetRulesheet() gin.HandlerFunc {

return func(c *gin.Context) {

ctx, cancel := context.WithTimeout(c.Request.Context(), 100*time.Second)
ctx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
defer cancel()
id, exists := c.Params.Get("id")

Expand Down Expand Up @@ -269,7 +274,8 @@ func (rc *rulesheets) GetRulesheet() gin.HandlerFunc {
// @Failure 500 {object} responses.Error "Internal Server Error"
// @Failure default {object} responses.Error
// @Response 404 "Not Found"
// @Security ApiKeyAuth
// @Security Authentication Api Key
// @Security Authentication Bearer Token
// @Router /rulesheets/{id} [put]
func (rc *rulesheets) UpdateRulesheet() gin.HandlerFunc {

Expand All @@ -288,14 +294,15 @@ func (rc *rulesheets) UpdateRulesheet() gin.HandlerFunc {
return
}

_, err := rc.service.Get(ctx, id)
foudedEntity, err := rc.service.Get(ctx, id)
if err != nil {
c.String(http.StatusNotFound, "")
log.Errorf("You are trying to update a non existing record: %v", err)
return
}

var payload payloads.Rulesheet

// validate the request body
if err := c.BindJSON(&payload); err != nil {
c.JSON(http.StatusBadRequest, responses.Error{
Expand All @@ -314,6 +321,15 @@ func (rc *rulesheets) UpdateRulesheet() gin.HandlerFunc {
iid, _ := strconv.ParseUint(id, 10, 32)
payload.ID = uint(iid)

if payload.Slug != "" {
c.JSON(http.StatusBadRequest, responses.Error{
Error: "You can't update a slug already defined",
})
log.Errorf("You can't update a slug already defined: %v", err)
return
}
payload.Slug = foudedEntity.Slug

dto, err := dtos.NewRulesheetV1(payload)
if err != nil {
c.JSON(http.StatusInternalServerError, responses.Error{
Expand Down Expand Up @@ -356,7 +372,8 @@ func (rc *rulesheets) UpdateRulesheet() gin.HandlerFunc {
// @Failure 500 {object} responses.Error "Internal Server Error"
// @Failure default {object} responses.Error
// @Response 404 "Not Found"
// @Security ApiKeyAuth
// @Security Authentication Api Key
// @Security Authentication Bearer Token
// @Router /rulesheets/{id} [delete]
func (rc *rulesheets) DeleteRulesheet() gin.HandlerFunc {

Expand Down
Loading

0 comments on commit 86d198b

Please sign in to comment.