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

fix(api): do not clean requirements when job is disabled #5483

Merged
merged 2 commits into from
Oct 8, 2020
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
97 changes: 97 additions & 0 deletions engine/api/pipeline_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"bytes"
"encoding/json"
"github.com/stretchr/testify/require"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -308,3 +309,99 @@ func TestDeleteJobHandler(t *testing.T) {
assert.Equal(t, len(pipResult.Stages), 1)
assert.Equal(t, len(pipResult.Stages[0].Jobs), 0)
}

func TestUpdateDisabledJobHandler(t *testing.T) {
api, db, router := newTestAPI(t)

//1. Create admin user
u, pass := assets.InsertAdminUser(t, db)

//2. Create project
proj := assets.InsertTestProject(t, db, api.Cache, sdk.RandomString(10), sdk.RandomString(10))
test.NotNil(t, proj)

//3. Create Pipeline
pipelineKey := sdk.RandomString(10)
pip := &sdk.Pipeline{
Name: pipelineKey,
ProjectKey: proj.Key,
ProjectID: proj.ID,
}
test.NoError(t, pipeline.InsertPipeline(api.mustDB(), pip))

//4. Add Stage
stage := &sdk.Stage{
BuildOrder: 1,
Enabled: true,
Name: "Stage1",
PipelineID: pip.ID,
}
test.NoError(t, pipeline.InsertStage(api.mustDB(), stage))

//5. Prepare the request
job := &sdk.Job{
Enabled: true,
PipelineStageID: stage.ID,
Action: sdk.Action{
Enabled: true,
Name: "myJob",
Requirements: []sdk.Requirement{
{
Name: "req1",
Type: sdk.BinaryRequirement,
Value: "echo",
},
},
},
}
test.NoError(t, pipeline.InsertJob(api.mustDB(), job, stage.ID, pip))
assert.NotZero(t, job.PipelineActionID)
assert.NotZero(t, job.Action.ID)

// 6. Prepare the request
addJobRequest := sdk.Job{
Enabled: false,
PipelineStageID: stage.ID,
PipelineActionID: job.PipelineActionID,
Action: sdk.Action{
ID: job.Action.ID,
Name: "myJobUpdated",
Requirements: []sdk.Requirement{
{
Name: "req1",
Type: sdk.BinaryRequirement,
Value: "echo",
},
},
},
}
jsonBody, _ := json.Marshal(addJobRequest)
body := bytes.NewBuffer(jsonBody)

vars := map[string]string{
"permProjectKey": proj.Key,
"pipelineKey": pip.Name,
"stageID": strconv.FormatInt(stage.ID, 10),
"jobID": strconv.FormatInt(job.PipelineActionID, 10),
}

uri := router.GetRoute("PUT", api.updateJobHandler, vars)
test.NotEmpty(t, uri)

req, _ := http.NewRequest("PUT", uri, body)
assets.AuthentifyRequest(t, req, u, pass)

//7. Do the request
w := httptest.NewRecorder()
router.Mux.ServeHTTP(w, req)

require.Equal(t, 200, w.Code)
res, _ := ioutil.ReadAll(w.Body)
pipResult := &sdk.Pipeline{}
json.Unmarshal(res, &pipResult)
require.Equal(t, len(pipResult.Stages), 1)
require.Equal(t, len(pipResult.Stages[0].Jobs), 1)
require.Equal(t, pipResult.Stages[0].Jobs[0].Action.Name, addJobRequest.Action.Name)
require.Equal(t, 1, len(pipResult.Stages[0].Jobs[0].Action.Requirements))
require.Equal(t, "req1", pipResult.Stages[0].Jobs[0].Action.Requirements[0].Name)
}
4 changes: 0 additions & 4 deletions sdk/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,6 @@ func (a Action) IsValid() error {

// FlattenRequirements returns all requirements for an action and its children.
func (a *Action) FlattenRequirements() RequirementList {
if !a.Enabled {
return nil
}

rs := a.Requirements

// copy requirements from childs
Expand Down