Skip to content

Commit

Permalink
Add error checking to job url generator. (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
nishkrishnan authored Oct 13, 2021
1 parent ff71852 commit c2c42f9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 17 deletions.
18 changes: 11 additions & 7 deletions server/handlers/mocks/mock_project_job_url_generator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions server/handlers/mocks/mock_project_status_updater.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions server/handlers/project_command_output_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type AsyncProjectCommandOutputHandler struct {

// ProjectJobURLGenerator generates urls to view project's progress.
type ProjectJobURLGenerator interface {
GenerateProjectJobURL(p models.ProjectCommandContext) string
GenerateProjectJobURL(p models.ProjectCommandContext) (string, error)
}

//go:generate pegomock generate -m --use-experimental-model-gen --package mocks -o mocks/mock_project_status_updater.go ProjectStatusUpdater
Expand Down Expand Up @@ -126,7 +126,11 @@ func (p *AsyncProjectCommandOutputHandler) Clear(ctx models.ProjectCommandContex
}

func (p *AsyncProjectCommandOutputHandler) SetJobURLWithStatus(ctx models.ProjectCommandContext, cmdName models.CommandName, status models.CommitStatus) error {
url := p.projectJobURLGenerator.GenerateProjectJobURL(ctx)
url, err := p.projectJobURLGenerator.GenerateProjectJobURL(ctx)

if err != nil {
return err
}
return p.projectStatusUpdater.UpdateProject(ctx, cmdName, status, url)
}

Expand Down
21 changes: 20 additions & 1 deletion server/handlers/project_command_output_handler_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers_test

import (
"errors"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -225,12 +226,30 @@ func TestProjectCommandOutputHandler(t *testing.T) {
logger,
)

When(projectJobURLGenerator.GenerateProjectJobURL(matchers.EqModelsProjectCommandContext(ctx))).ThenReturn("url-to-project-jobs")
When(projectJobURLGenerator.GenerateProjectJobURL(matchers.EqModelsProjectCommandContext(ctx))).ThenReturn("url-to-project-jobs", nil)
err := prjCmdOutputHandler.SetJobURLWithStatus(ctx, models.PlanCommand, models.PendingCommitStatus)
Ok(t, err)

projectStatusUpdater.VerifyWasCalledOnce().UpdateProject(ctx, models.PlanCommand, models.PendingCommitStatus, "url-to-project-jobs")
})

t.Run("update project status with project jobs url error", func(t *testing.T) {
RegisterMockTestingT(t)
logger := logging.NewNoopLogger(t)
prjCmdOutputChan := make(chan *models.ProjectCmdOutputLine)
projectStatusUpdater := mocks.NewMockProjectStatusUpdater()
projectJobURLGenerator := mocks.NewMockProjectJobURLGenerator()
prjCmdOutputHandler := handlers.NewAsyncProjectCommandOutputHandler(
prjCmdOutputChan,
projectStatusUpdater,
projectJobURLGenerator,
logger,
)

When(projectJobURLGenerator.GenerateProjectJobURL(matchers.EqModelsProjectCommandContext(ctx))).ThenReturn("url-to-project-jobs", errors.New("some error"))
err := prjCmdOutputHandler.SetJobURLWithStatus(ctx, models.PlanCommand, models.PendingCommitStatus)
assert.Error(t, err)
})
}

func TestFeatureAwareOutputHandler(t *testing.T) {
Expand Down
12 changes: 9 additions & 3 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/url"

"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/events/models"
)

Expand Down Expand Up @@ -38,14 +39,19 @@ func (r *Router) GenerateLockURL(lockID string) string {
return r.AtlantisURL.String() + lockURL.String()
}

func (r *Router) GenerateProjectJobURL(ctx models.ProjectCommandContext) string {
func (r *Router) GenerateProjectJobURL(ctx models.ProjectCommandContext) (string, error) {
pull := ctx.Pull

jobURL, _ := r.Underlying.Get(r.ProjectJobsViewRouteName).URL(
jobURL, err := r.Underlying.Get(r.ProjectJobsViewRouteName).URL(
"org", pull.BaseRepo.Owner,
"repo", pull.BaseRepo.Name,
"pull", fmt.Sprintf("%d", pull.Num),
"project", ctx.ProjectName,
)
return r.AtlantisURL.String() + jobURL.String()

if err != nil {
return "", errors.Wrapf(err, "creating job url for %s/%d/%s", pull.BaseRepo.FullName, pull.Num, ctx.ProjectName)
}

return r.AtlantisURL.String() + jobURL.String(), nil
}

0 comments on commit c2c42f9

Please sign in to comment.