Skip to content

Commit

Permalink
Merge branch 'main' into feat/dashboard/library-db-types
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrupper committed Jan 31, 2024
2 parents 0f8b556 + 1625a79 commit edf13cc
Show file tree
Hide file tree
Showing 37 changed files with 1,461 additions and 289 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@74483a38d39275f33fcff5f35b679b5ca4a26a99 # v2
uses: github/codeql-action/init@0b21cf2492b6b02c465a3e5d7c473717ad7721ba # v3
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -50,7 +50,7 @@ jobs:
# 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@74483a38d39275f33fcff5f35b679b5ca4a26a99 # v2
uses: github/codeql-action/autobuild@0b21cf2492b6b02c465a3e5d7c473717ad7721ba # v3

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -64,4 +64,4 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@74483a38d39275f33fcff5f35b679b5ca4a26a99 # v2
uses: github/codeql-action/analyze@0b21cf2492b6b02c465a3e5d7c473717ad7721ba # v3
17 changes: 17 additions & 0 deletions .github/workflows/pr-title-validate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# name of the action
name: validate PR title

# trigger on pull_request events of the opened & edited type.
on:
pull_request:
types: [ opened, synchronize, edited, reopened ]

# pipeline to execute
jobs:
validate:
runs-on: ubuntu-latest

steps:
- name: validate title
run: |
echo "${{ github.event.pull_request.title }}" | grep -Eq '^(feat|fix|chore|refactor|enhance|test|docs)(\(.*\)|):\s.+$' && (echo "Pass"; exit 0) || (echo "Incorrect Format. Please see https://go-vela.github.io/docs/community/contributing_guidelines/#development-workflow"; exit 1)
8 changes: 4 additions & 4 deletions .github/workflows/reviewdog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ jobs:
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4

- name: install go
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5
with:
# use version from go.mod file
go-version-file: 'go.mod'
cache: true
check-latest: true

- name: golangci-lint
uses: reviewdog/action-golangci-lint@94d61e3205b61acf4ddabfeb13c5f8a13eb4167b # v2
uses: reviewdog/action-golangci-lint@f016e796a429b33094d0da6e86af3fa5479afe65 # v2
with:
github_token: ${{ secrets.github_token }}
golangci_lint_flags: "--config=.golangci.yml"
Expand All @@ -39,15 +39,15 @@ jobs:
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4

- name: install go
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5
with:
# use version from go.mod file
go-version-file: 'go.mod'
cache: true
check-latest: true

- name: golangci-lint
uses: reviewdog/action-golangci-lint@94d61e3205b61acf4ddabfeb13c5f8a13eb4167b # v2
uses: reviewdog/action-golangci-lint@f016e796a429b33094d0da6e86af3fa5479afe65 # v2
with:
github_token: ${{ secrets.github_token }}
golangci_lint_flags: "--config=.golangci.yml"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4

- name: install go
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5
with:
# use version from go.mod file
go-version-file: 'go.mod'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4

- name: install go
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5
with:
# use version from go.mod file
go-version-file: 'go.mod'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4

- name: install go
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5
with:
# use version from go.mod file
go-version-file: 'go.mod'
Expand Down
6 changes: 6 additions & 0 deletions constants/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ const (

// ActionTransferred defines the action for transferring repository ownership.
ActionTransferred = "transferred"

// ActionBranch defines the action for deleting a branch.
ActionBranch = "branch"

// ActionTag defines the action for deleting a tag.
ActionTag = "tag"
)
3 changes: 3 additions & 0 deletions constants/allow_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ const (
AllowDeployCreate
AllowCommentCreate
AllowCommentEdit
AllowSchedule
AllowPushDeleteBranch
AllowPushDeleteTag
)
3 changes: 3 additions & 0 deletions constants/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const (
// EventComment defines the event type for comments added to a pull request.
EventComment = "comment"

// EventDelete defines the event type for build and repo delete events.
EventDelete = "delete"

// EventDeploy defines the event type for build and repo deployment events.
EventDeploy = "deployment"

Expand Down
3 changes: 3 additions & 0 deletions constants/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const (
// TableDashboard defines the table type for the database dashboards table.
TableDashboard = "dashboards"

// TableDeployment defines the table type for the database deployments table.
TableDeployment = "deployments"

// TableHook defines the table type for the database hooks table.
TableHook = "hooks"

Expand Down
8 changes: 8 additions & 0 deletions database/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Build struct {
Started sql.NullInt64 `sql:"started"`
Finished sql.NullInt64 `sql:"finished"`
Deploy sql.NullString `sql:"deploy"`
DeployNumber sql.NullInt64 `sql:"deploy_number"`
DeployPayload raw.StringSliceMap `sql:"deploy_payload" gorm:"type:varchar(2000)"`
Clone sql.NullString `sql:"clone"`
Source sql.NullString `sql:"source"`
Expand Down Expand Up @@ -170,6 +171,11 @@ func (b *Build) Nullify() *Build {
b.Deploy.Valid = false
}

// check if the DeployNumber field should be false
if b.DeployNumber.Int64 == 0 {
b.Deploy.Valid = false
}

// check if the Clone field should be false
if len(b.Clone.String) == 0 {
b.Clone.Valid = false
Expand Down Expand Up @@ -282,6 +288,7 @@ func (b *Build) ToLibrary() *library.Build {
build.SetStarted(b.Started.Int64)
build.SetFinished(b.Finished.Int64)
build.SetDeploy(b.Deploy.String)
build.SetDeployNumber(b.DeployNumber.Int64)
build.SetDeployPayload(b.DeployPayload)
build.SetClone(b.Clone.String)
build.SetSource(b.Source.String)
Expand Down Expand Up @@ -365,6 +372,7 @@ func BuildFromLibrary(b *library.Build) *Build {
Started: sql.NullInt64{Int64: b.GetStarted(), Valid: true},
Finished: sql.NullInt64{Int64: b.GetFinished(), Valid: true},
Deploy: sql.NullString{String: b.GetDeploy(), Valid: true},
DeployNumber: sql.NullInt64{Int64: b.GetDeployNumber(), Valid: true},
DeployPayload: b.GetDeployPayload(),
Clone: sql.NullString{String: b.GetClone(), Valid: true},
Source: sql.NullString{String: b.GetSource(), Valid: true},
Expand Down
4 changes: 4 additions & 0 deletions database/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func TestDatabase_Build_Nullify(t *testing.T) {
Started: sql.NullInt64{Int64: 0, Valid: false},
Finished: sql.NullInt64{Int64: 0, Valid: false},
Deploy: sql.NullString{String: "", Valid: false},
DeployNumber: sql.NullInt64{Int64: 0, Valid: false},
DeployPayload: nil,
Clone: sql.NullString{String: "", Valid: false},
Source: sql.NullString{String: "", Valid: false},
Expand Down Expand Up @@ -121,6 +122,7 @@ func TestDatabase_Build_ToLibrary(t *testing.T) {
want.SetStarted(1563474078)
want.SetFinished(1563474079)
want.SetDeploy("")
want.SetDeployNumber(0)
want.SetDeployPayload(nil)
want.SetClone("https://github.com/github/octocat.git")
want.SetSource("https://github.com/github/octocat/48afb5bdc41ad69bf22588491333f7cf71135163")
Expand Down Expand Up @@ -212,6 +214,7 @@ func TestDatabase_BuildFromLibrary(t *testing.T) {
b.SetStarted(1563474078)
b.SetFinished(1563474079)
b.SetDeploy("")
b.SetDeployNumber(0)
b.SetDeployPayload(nil)
b.SetClone("https://github.com/github/octocat.git")
b.SetSource("https://github.com/github/octocat/48afb5bdc41ad69bf22588491333f7cf71135163")
Expand Down Expand Up @@ -273,6 +276,7 @@ func testBuild() *Build {
Started: sql.NullInt64{Int64: 1563474078, Valid: true},
Finished: sql.NullInt64{Int64: 1563474079, Valid: true},
Deploy: sql.NullString{String: "", Valid: false},
DeployNumber: sql.NullInt64{Int64: 0, Valid: true},
DeployPayload: raw.StringSliceMap{"foo": "test1", "bar": "test2"},
Clone: sql.NullString{String: "https://github.com/github/octocat.git", Valid: true},
Source: sql.NullString{String: "https://github.com/github/octocat/48afb5bdc41ad69bf22588491333f7cf71135163", Valid: true},
Expand Down
183 changes: 183 additions & 0 deletions database/deployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
// SPDX-License-Identifier: Apache-2.0

package database

import (
"database/sql"
"errors"
"fmt"

"github.com/go-vela/types/library"
"github.com/go-vela/types/raw"
"github.com/lib/pq"
)

var (
// ErrEmptyDeploymentNumber defines the error type when a
// Deployment type has an empty Number field provided.
ErrEmptyDeploymentNumber = errors.New("empty deployment number provided")

// ErrEmptyDeploymentRepoID defines the error type when a
// Deployment type has an empty RepoID field provided.
ErrEmptyDeploymentRepoID = errors.New("empty deployment repo_id provided")
)

// Deployment is the database representation of a deployment for a repo.
type Deployment struct {
ID sql.NullInt64 `sql:"id"`
Number sql.NullInt64 `sql:"number"`
RepoID sql.NullInt64 `sql:"repo_id"`
URL sql.NullString `sql:"url"`
Commit sql.NullString `sql:"commit"`
Ref sql.NullString `sql:"ref"`
Task sql.NullString `sql:"task"`
Target sql.NullString `sql:"target"`
Description sql.NullString `sql:"description"`
Payload raw.StringSliceMap `sql:"payload"`
CreatedAt sql.NullInt64 `sql:"created_at"`
CreatedBy sql.NullString `sql:"created_by"`
Builds pq.StringArray `sql:"builds" gorm:"type:varchar(50)"`
}

// Nullify ensures the valid flag for
// the sql.Null types are properly set.
//
// When a field within the Deployment type is the zero
// value for the field, the valid flag is set to
// false causing it to be NULL in the database.
func (d *Deployment) Nullify() *Deployment {
if d == nil {
return nil
}

// check if the ID field should be false
if d.ID.Int64 == 0 {
d.ID.Valid = false
}

// check if the Number field should be false
if d.Number.Int64 == 0 {
d.Number.Valid = false
}

// check if the RepoID field should be false
if d.RepoID.Int64 == 0 {
d.RepoID.Valid = false
}

// check if the URL field should be false
if len(d.URL.String) == 0 {
d.URL.Valid = false
}

// check if the Commit field should be false
if len(d.Commit.String) == 0 {
d.Commit.Valid = false
}

// check if the Ref field should be false
if len(d.Ref.String) == 0 {
d.Ref.Valid = false
}

// check if the Task field should be false
if len(d.Task.String) == 0 {
d.Task.Valid = false
}

// check if the Target field should be false
if len(d.Target.String) == 0 {
d.Target.Valid = false
}

// check if the Description field should be false
if len(d.Description.String) == 0 {
d.Description.Valid = false
}

// check if the CreatedAt field should be false
if d.CreatedAt.Int64 == 0 {
d.CreatedAt.Valid = false
}

// check if the CreatedBy field should be false
if len(d.CreatedBy.String) == 0 {
d.CreatedBy.Valid = false
}

return d
}

// ToLibrary converts the Deployment type
// to a library Deployment type.
func (d *Deployment) ToLibrary(builds []*library.Build) *library.Deployment {
deployment := new(library.Deployment)

deployment.SetID(d.ID.Int64)
deployment.SetNumber(d.Number.Int64)
deployment.SetRepoID(d.RepoID.Int64)
deployment.SetURL(d.URL.String)
deployment.SetCommit(d.Commit.String)
deployment.SetRef(d.Ref.String)
deployment.SetTask(d.Task.String)
deployment.SetTarget(d.Target.String)
deployment.SetDescription(d.Description.String)
deployment.SetPayload(d.Payload)
deployment.SetCreatedAt(d.CreatedAt.Int64)
deployment.SetCreatedBy(d.CreatedBy.String)
deployment.SetBuilds(builds)

return deployment
}

// Validate verifies the necessary fields for
// the Deployment type are populated correctly.
func (d *Deployment) Validate() error {
// verify the RepoID field is populated
if d.RepoID.Int64 <= 0 {
return ErrEmptyDeploymentRepoID
}

// verify the Number field is populated
if d.Number.Int64 <= 0 {
return ErrEmptyDeploymentNumber
}

// ensure that all Deployment string fields
// that can be returned as JSON are sanitized
// to avoid unsafe HTML content
d.Commit = sql.NullString{String: sanitize(d.Commit.String), Valid: d.Commit.Valid}
d.Ref = sql.NullString{String: sanitize(d.Ref.String), Valid: d.Ref.Valid}
d.Task = sql.NullString{String: sanitize(d.Task.String), Valid: d.Task.Valid}
d.Target = sql.NullString{String: sanitize(d.Target.String), Valid: d.Target.Valid}
d.Description = sql.NullString{String: sanitize(d.Description.String), Valid: d.Description.Valid}

return nil
}

// DeploymentFromLibrary converts the library Deployment type
// to a database Deployment type.
func DeploymentFromLibrary(d *library.Deployment) *Deployment {
buildIDs := []string{}
for _, build := range d.GetBuilds() {
buildIDs = append(buildIDs, fmt.Sprint(build.GetID()))
}

deployment := &Deployment{
ID: sql.NullInt64{Int64: d.GetID(), Valid: true},
Number: sql.NullInt64{Int64: d.GetNumber(), Valid: true},
RepoID: sql.NullInt64{Int64: d.GetRepoID(), Valid: true},
URL: sql.NullString{String: d.GetURL(), Valid: true},
Commit: sql.NullString{String: d.GetCommit(), Valid: true},
Ref: sql.NullString{String: d.GetRef(), Valid: true},
Task: sql.NullString{String: d.GetTask(), Valid: true},
Target: sql.NullString{String: d.GetTarget(), Valid: true},
Description: sql.NullString{String: d.GetDescription(), Valid: true},
Payload: d.GetPayload(),
CreatedAt: sql.NullInt64{Int64: d.GetCreatedAt(), Valid: true},
CreatedBy: sql.NullString{String: d.GetCreatedBy(), Valid: true},
Builds: buildIDs,
}

return deployment.Nullify()
}
Loading

0 comments on commit edf13cc

Please sign in to comment.