Skip to content

Commit

Permalink
fixes soft deletes with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 committed Jan 21, 2024
1 parent 7dddd0e commit 73cdd37
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 15 deletions.
8 changes: 8 additions & 0 deletions app/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#! /bin/bash

cd frontend
npm install
npm run build
cd ..

go build main.go
33 changes: 24 additions & 9 deletions app/models/coverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,23 +562,38 @@ func (c *Coverage) Create(
return &ret, err
}
func (c *Coverage) SoftDeleteCoverages(orgID int64, repoID int64, branches string) error {
// Split the branches string into a slice
branchList := strings.Split(branches, " ")

// Build the NOT IN clause with placeholders
placeholders := make([]string, len(branchList))
args := make([]interface{}, len(branchList)+2)

// Set the orgID and repoID at the beginning of the args slice
args[0] = orgID
args[1] = repoID

// Assign placeholders and arguments for branch names
for i, branch := range branchList {
placeholders[i] = "?" // Use "?" as the placeholder for each branch
args[i+2] = branch // +2 to account for orgID and repoID at the beginning
}
notInClause := strings.Join(placeholders, ", ")

query := `
UPDATE
coverages
SET
deleted_at = NOW()
WHERE
org_id = @org_id
org_id = ?
AND
repo_id = @repo_id
repo_id = ?
AND
branch_name NOT IN (@branches)
branch_name NOT IN (` + notInClause + `)
`
err := db.Db().Exec(
query,
sql.Named("org_id", orgID),
sql.Named("repo_id", repoID),
sql.Named("branches", branches)).
Error

// Execute the query
err := db.Db().Exec(query, args...).Error
return err
}
2 changes: 1 addition & 1 deletion app/pkg/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (p *PR) Get(req *PRRequest, types []models.Type) (string, error) {
}

mdText.PlainText("")
readmeLink := fmt.Sprintf("%s://%s/README.md?org=%s&repo=%s&branch=%s",
readmeLink := fmt.Sprintf("%s://%s/readme?org=%s&repo=%s&branch=%s",
req.scheme, req.host, req.Org, req.Repo, req.Branch)
mdText.PlainTextf(md.Link("Add to Readme", readmeLink))

Expand Down
7 changes: 5 additions & 2 deletions app/pkg/readme.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ func NewReadme() *Readme {
return &Readme{}
}

func (r *Readme) Get(req *ReadmeRequest) (string, error) {
func (r *Readme) GetTypes(req *ReadmeRequest) ([]models.Type, error) {
types, err := r.typeModel.GetTypesFor(req.Org, req.Repo)
if err != nil {
return "", err
return nil, err
}
return types, nil
}
func (r *Readme) Get(req *ReadmeRequest, types []models.Type) (string, error) {
mdText := md.NewMarkdown(os.Stdout)

mdText.H1("CoverItUp Report").
Expand Down
14 changes: 13 additions & 1 deletion app/pkg/readme_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,21 @@ func (h *ReadmeHandler) Get(c echo.Context) error {
return echo.NewHTTPError(http.StatusUnprocessableEntity, err)
}
defaults.SetDefaults(req)
msgs, err := ValidateRequest(req)
if err != nil {
return echo.NewHTTPError(http.StatusUnprocessableEntity, msgs)
}

req.host = c.Request().Host
req.scheme = c.Scheme()
str, err := h.Readme.Get(req)
types, err := h.Readme.GetTypes(req)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
if len(types) == 0 {
return c.String(http.StatusNotFound, "no types found for this org/repo")
}
str, err := h.Readme.Get(req, types)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
Expand Down
111 changes: 111 additions & 0 deletions app/pkg/readme_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package pkg

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

"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)

func TestReadmeNotOk(t *testing.T) {

BeforeEach()
defer AfterEach()
e := echo.New()
e.GET("/readme", func(c echo.Context) error {
return NewReadmeHandler().Get(c)
})

server := httptest.NewServer(e)
defer server.Close()

type TestCase struct {
Name string
Query string
Status int
}
testCases := []TestCase{
{
Name: "404",
Query: `?org=org&repo=repo&branch=branch&type=type`,
Status: http.StatusNotFound,
},
{
Name: "422",
Query: `?repo=repo&branch=branch&type=type`,
Status: http.StatusUnprocessableEntity,
},
{
Name: "422",
Query: `?org=org&branch=branch&type=type`,
Status: http.StatusUnprocessableEntity,
},
}

for _, tc := range testCases {
url := server.URL + "/readme" + tc.Query
resp, err := http.Get(url)

assert.NoError(t, err)
assert.Equal(t, tc.Status, resp.StatusCode)
}
}
func TestReadmeOK(t *testing.T) {

BeforeEach()
defer AfterEach()
e := echo.New()
e.GET("/readme", func(c echo.Context) error {
return NewReadmeHandler().Get(c)
})

server := httptest.NewServer(e)
defer server.Close()

type TestCase struct {
Name string
PreUploadRequest *UploadRequest
Query string
Status int
}
testCases := []TestCase{
{
Name: "200",
PreUploadRequest: &UploadRequest{
Org: "org",
Repo: "repo",
Type: "type",
Branch: "branch",
Commit: "commit",
},
Query: `?org=org&repo=repo&branch=branch&type=type`,
Status: http.StatusOK,
},
{
Name: "200",
PreUploadRequest: &UploadRequest{
Org: "org",
Repo: "repo",
Type: "type",
Branch: "branch",
Commit: "commit",
Score: "1999",
},
Query: `?org=org&repo=repo&branch=branch&type=type`,
Status: http.StatusOK,
},
}

for _, tc := range testCases {
c, err := NewUpload().Post(tc.PreUploadRequest)
assert.NotNil(t, c)
assert.NoError(t, err)
url := server.URL + "/readme" + tc.Query
resp, err := http.Get(url)

assert.NoError(t, err)
assert.Equal(t, tc.Status, resp.StatusCode)
}
}
4 changes: 2 additions & 2 deletions app/pkg/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Disallow: /chart
Disallow: /badge
Disallow: /destroy
Disallow: /pr
Disallow: /README.md`
Disallow: /readme`
)

func SetupRoutes(e *echo.Echo, baseURL string, publicDir embed.FS, favicon embed.FS) {
Expand All @@ -48,7 +48,7 @@ func SetupRoutes(e *echo.Echo, baseURL string, publicDir embed.FS, favicon embed
e.POST(baseURL+"destroy", NewDestroyHandler().Post, HasAuthorizationHeader())

// /README.md to return markdown for embedings of badge and charts
e.GET(baseURL+"README.md", NewReadmeHandler().Get)
e.GET(baseURL+"readme", NewReadmeHandler().Get)

// /badge to return badges
e.GET(baseURL+"badge", NewBadgeHandler().Get)
Expand Down
1 change: 1 addition & 0 deletions app/pkg/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (c *Upload) Post(req *UploadRequest) (*models.Coverage, error) {
}
}

req.Branches += " " + req.Branch
err = c.coverage.SoftDeleteCoverages(o.ID, r.ID, req.Branches)
if err != nil {
return nil, err
Expand Down
44 changes: 44 additions & 0 deletions app/pkg/upload_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http/httptest"
"testing"

"github.com/kevincobain2000/action-coveritup/models"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -87,3 +88,46 @@ func TestPostUploadOK(t *testing.T) {
assert.Equal(t, tc.Status, resp.StatusCode)
}
}
func TestPostCRUD(t *testing.T) {

BeforeEach()
defer AfterEach()
e := echo.New()
e.POST("/upload", func(c echo.Context) error {
return NewUploadHandler().Post(c)
})

server := httptest.NewServer(e)
defer server.Close()

type TestCase struct {
Name string
Body []byte
Status int
}
testCases := []TestCase{
{
Name: "200",
Body: []byte(`{"org":"org","repo":"repo","user": "user", "branch":"branch1","type":"type","commit":"commit", "branches": "master develop", "pr_num": "1234"}`),
Status: http.StatusOK,
},
{
Name: "200",
Body: []byte(`{"org":"org","repo":"repo","user": "user", "branch":"branch2","type":"type","commit":"commit", "branches": "branch1 master develop", "pr_num": "1234"}`),
Status: http.StatusOK,
},
}

for _, tc := range testCases {
url := server.URL + "/upload"
resp, err := http.Post(url, "application/json", bytes.NewBuffer(tc.Body))

assert.NoError(t, err)
assert.Equal(t, tc.Status, resp.StatusCode)
}

cm := models.Coverage{}
ret, err := cm.GetAllBranches("org", "repo", "type")
assert.NoError(t, err)
assert.Equal(t, []string{"branch1", "branch2"}, ret)
}

0 comments on commit 73cdd37

Please sign in to comment.