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

Allow default team config project #47

Merged
merged 3 commits into from
Apr 12, 2024
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Those are the variables you have to use:
|PG_SSLMODE|One of these (disable,allow,prefer,require,verify-ca,verify-full)|disable|
|AWSSERVERCREDENTIALS_KEY|Parent key in the AWS Secret Manager to store server secrets|/vulcan/k8s/tracker/jira/|
|AWSSERVERCREDENTIALS_ENDPOINT|Optional AWS endpoint|http://locacalstack/|
|DEFAULT_TEAM_PROJECT|team id with a project that will be used as project for all the non explicit declared teams|
|AWS_REGION||eu-west-1|


Expand Down
5 changes: 3 additions & 2 deletions cmd/vulcan-tracker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ func main() {
ticketTrackerBuilder := &tracking.TTBuilder{}

a := api.New(ticketServer, ticketTrackerBuilder, db, api.Options{
MaxSize: cfg.API.MaxSize,
DefaultSize: cfg.API.DefaultSize,
DefaultTeamProject: cfg.API.DefaultTeamProject,
MaxSize: cfg.API.MaxSize,
DefaultSize: cfg.API.DefaultSize,
})

e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Expand Down
1 change: 1 addition & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
max_size = 100
default_size = 20
port = $PORT
default_team_project = "$DEFAULT_TEAM_PROJECT"

[log]
level = "$LOG_LEVEL"
Expand Down
2 changes: 2 additions & 0 deletions db/sql/V1.2__project_team_key.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE projects
ADD CONSTRAINT projects_team_id_key UNIQUE (team_id);
5 changes: 3 additions & 2 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ type API struct {

// Options represents size options for the API requests.
type Options struct {
MaxSize int
DefaultSize int
DefaultTeamProject string
MaxSize int
DefaultSize int
}

// New instantiates a new API.
Expand Down
13 changes: 11 additions & 2 deletions pkg/api/tracking.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package api
import (
"database/sql"
"errors"
"fmt"
"net/http"
"regexp"

Expand Down Expand Up @@ -87,6 +88,7 @@ func (api *API) GetTicket(c echo.Context) error {
// CreateTicket creates a ticket and returns a JSON containing the new ticket.
func (api *API) CreateTicket(c echo.Context) error {
teamID := c.Param("team_id")
projectTeamID := teamID
ticket := new(model.Ticket)

// Check if the team is an uuid
Expand All @@ -105,7 +107,14 @@ func (api *API) CreateTicket(c echo.Context) error {
// Get the server and the configuration for the teamID.
configuration, err := api.ticketServer.ProjectConfigByTeamID(teamID)
if err != nil {
return responseError(err)
if api.Options.DefaultTeamProject == "" {
return responseError(err)
}
configuration, err = api.ticketServer.ProjectConfigByTeamID(api.Options.DefaultTeamProject)
if err != nil {
return responseError(fmt.Errorf("unable to find config for default team %s: %w", api.Options.DefaultTeamProject, err))
}
projectTeamID = api.Options.DefaultTeamProject
}

// Retrieve the necessary values to create a ticket.
Expand All @@ -120,7 +129,7 @@ func (api *API) CreateTicket(c echo.Context) error {
}

// Get a ticket tracker client.
ttClient, err := api.ticketTrackerBuilder.GenerateTicketTrackerClient(api.ticketServer, teamID, c.Logger())
ttClient, err := api.ticketTrackerBuilder.GenerateTicketTrackerClient(api.ticketServer, projectTeamID, c.Logger())
if err != nil {
return responseError(err)
}
Expand Down
24 changes: 24 additions & 0 deletions pkg/api/tracking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,30 @@ func TestCreateTicket(t *testing.T) {
Message: "project not found",
},
},
{
name: "TeamDefault",
teamID: "c99fed50-c612-4f99-863f-6d3274e2b2b6",
ticket: model.Ticket{
Summary: "Summary TEST-2",
Description: "Description TEST-2",
FindingID: "12345678",
},
api: &API{
ticketServer: &mockTicketServer{
projects: projects,
},
ticketTrackerBuilder: &mockTicketTrackerBuilder{
tickets: tickets,
projects: projects,
},
storage: &mockStorage{},
Options: Options{
DefaultTeamProject: "80287cf6-db31-47f8-a9aa-792f214b1f88",
},
},
wantStatusCode: http.StatusOK,
wantTicket: "TEST-3",
},
}

for _, tt := range tests {
Expand Down
7 changes: 4 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ type Config struct {
}

type apiConfig struct {
MaxSize int `toml:"max_size"`
DefaultSize int `toml:"default_size"`
Port int `toml:"port"`
MaxSize int `toml:"max_size"`
DefaultSize int `toml:"default_size"`
Port int `toml:"port"`
DefaultTeamProject string `toml:"default_team_project"`
}

type logConfig struct {
Expand Down
10 changes: 8 additions & 2 deletions pkg/tracking/jira/tickets.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package jira

import (
"fmt"
"net/url"
"path"

"github.com/adevinta/vulcan-tracker/pkg/model"
)
Expand All @@ -32,7 +34,9 @@ func (tc *TrackerClient) GetTicket(id string) (model.Ticket, error) {
if err != nil {
return model.Ticket{}, err
}
ticket.URLTracker = fmt.Sprintf("%s/browse/%s", tc.URL, ticket.Key)
if ticket.URLTracker, err = url.JoinPath(tc.URL, path.Join("browse", ticket.Key)); err != nil {
return model.Ticket{}, err
}

return ticket, nil
}
Expand All @@ -46,7 +50,9 @@ func (tc *TrackerClient) CreateTicket(ticket model.Ticket) (model.Ticket, error)
return model.Ticket{}, err
}

createdTicket.URLTracker = fmt.Sprintf("%s/browse/%s", tc.URL, createdTicket.Key)
if createdTicket.URLTracker, err = url.JoinPath(tc.URL, path.Join("browse", createdTicket.Key)); err != nil {
return model.Ticket{}, err
}

return createdTicket, nil
}
3 changes: 2 additions & 1 deletion pkg/tracking/jira/tickets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func TestGetTicket(t *testing.T) {
tickets: tickets,
},
Logger: &mockLogger{},
URL: "https://www.example.com/jira/",
},
want: model.Ticket{
ID: "1000",
Expand All @@ -142,7 +143,7 @@ func TestGetTicket(t *testing.T) {
Status: ToDo,
TicketType: "Vulnerability",
Labels: []string{"Vulnerability"},
URLTracker: "/browse/TEST-1",
URLTracker: "https://www.example.com/jira/browse/TEST-1",
},
wantErr: nil,
},
Expand Down
Loading