Skip to content

Commit

Permalink
feat(cli): add support for tasks in Project CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rymuszka committed Aug 1, 2023
1 parent 32b1677 commit 6b145a6
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
22 changes: 21 additions & 1 deletion api/models/project_v1_alpha.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,27 @@ type Scheduler struct {
Status string `json:"status,omitempty" yaml:"status,omitempty"`
}

type Task struct {
Name string `json:"name"`
Recurring bool `json:"recurring"`
Id string `json:"id,omitempty"`
Branch string `json:"branch,omitempty"`
At string `json:"at,omitempty"`
PipelineFile string `json:"pipeline_file" yaml:"pipeline_file,omitempty"`
Status string `json:"status,omitempty" yaml:"status,omitempty"`
Parameters []TaskParameter `json:"parameters,omitempty" yaml:"parameters,omitempty"`
}

type TaskParameter struct {
Name string `json:"name"`
Required bool `json:"required"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
DefaultValue string `json:"default_value,omitempty" yaml:"default_value,omitempty"`
Options []string `json:"options,omitempty" yaml:"options,omitempty"`
}

type ForkedPullRequests struct {
AllowedSecrets []string `json:"allowed_secrets,omitempty" yaml:"allowed_secrets,omitempty"`
AllowedSecrets []string `json:"allowed_secrets,omitempty" yaml:"allowed_secrets,omitempty"`
AllowedContributors []string `json:"allowed_contributors,omitempty" yaml:"allowed_contributors,omitempty"`
}

Expand Down Expand Up @@ -56,6 +75,7 @@ type ProjectV1Alpha struct {
IntegrationType string `json:"integration_type" yaml:"integration_type"`
} `json:"repository,omitempty"`
Schedulers []Scheduler `json:"schedulers,omitempty" yaml:"schedulers,omitempty"`
Tasks []Task `json:"tasks,omitempty" yaml:"tasks,omitempty"`
CustomPermissions *bool `json:"custom_permissions,omitempty" yaml:"custom_permissions,omitempty"`
DebugPermissions []string `json:"debug_permissions,omitempty" yaml:"debug_permissions,omitempty"`
AttachPermissions []string `json:"attach_permissions,omitempty" yaml:"attach_permissions,omitempty"`
Expand Down
88 changes: 88 additions & 0 deletions cmd/edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,94 @@ func Test__EditProject__Response200(t *testing.T) {
assert.Equal(t, scheduler.PipelineFile, ".semaphore/cron.yml")
}

func Test__EditProject__WithTasks__Response200(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

dash := `{
"metadata":{
"name":"hello",
"id":"bb2ba294-d4b3-48bc-90a7-12dd56e9424b",
"description":"Just saying hi!"
},
"spec":{
"repository":{
"url":"[email protected]/renderextext/hello",
"run_on":["tags", "branches"],
"forked_pull_requests":{
"allowed_secrets":["foo"]
},
"pipeline_file": ""
},
"tasks":[
{
"name":"cron",
"id":"bb2ba294-d4b3-48bc-90a7-12dd56e9424c",
"recurring":false,
"branch":"master",
"pipeline_file":".semaphore/cron.yml",
"parameters":[
{
"name":"param1",
"required":true,
"description":"param1 description",
"default_value":"option1",
"options":["option1", "option2"]
}
]
}
]
}
}`

var received *models.ProjectV1Alpha

httpmock.RegisterResponder("GET", "https://org.semaphoretext.xyz/api/v1alpha/projects/hello",
func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(200, dash), nil
},
)

httpmock.RegisterResponder("PATCH", "https://org.semaphoretext.xyz/api/v1alpha/projects/bb2ba294-d4b3-48bc-90a7-12dd56e9424b",
func(req *http.Request) (*http.Response, error) {
body, _ := ioutil.ReadAll(req.Body)
received, _ = models.NewProjectV1AlphaFromJson(body)

return httpmock.NewStringResponse(200, string(body)), nil
},
)

RootCmd.SetArgs([]string{"edit", "project", "hello"})
RootCmd.Execute()

assert.Equal(t, received.Metadata.Name, "hello")
assert.Equal(t, received.Metadata.Description, "Just saying hi!")

repo := received.Spec.Repository

assert.Equal(t, repo.Url, "[email protected]/renderextext/hello")
assert.Equal(t, repo.RunOn, []string{"tags", "branches"})

forked_pull_requests := received.Spec.Repository.ForkedPullRequests

assert.Equal(t, forked_pull_requests.AllowedSecrets, []string{"foo"})

task := received.Spec.Tasks[0]

assert.Equal(t, task.Name, "cron")
assert.Equal(t, task.Branch, "master")
assert.Equal(t, task.Recurring, false)
assert.Equal(t, task.PipelineFile, ".semaphore/cron.yml")

task_parameter := task.Parameters[0]

assert.Equal(t, task_parameter.Name, "param1")
assert.Equal(t, task_parameter.Required, true)
assert.Equal(t, task_parameter.Description, "param1 description")
assert.Equal(t, task_parameter.DefaultValue, "option1")
assert.Equal(t, task_parameter.Options, []string{"option1", "option2"})
}

func Test__EditDeploymentTarget__Response200(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
Expand Down

0 comments on commit 6b145a6

Please sign in to comment.