Skip to content

Commit f8c6d41

Browse files
committed
feat(engagement): add close engagement in cli
1 parent 42e6ec7 commit f8c6d41

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

client/engagement.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package client
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
7+
"net/http"
68

79
"github.com/pngouin/defectdojo-cli/config"
810
)
@@ -28,6 +30,10 @@ type EngagementResponse struct {
2830
Engagement
2931
}
3032

33+
var (
34+
ErrCannotCloseEngagement = errors.New("cannot close engagement")
35+
)
36+
3137
const (
3238
NotStarted EngagementStatus = "Not Started"
3339
Blocked EngagementStatus = "Blocked"
@@ -80,3 +86,15 @@ func (ec EngagementClient) Get(id string) (EngagementResponse, error) {
8086
err = json.NewDecoder(resp.Body).Decode(&engagementResponse)
8187
return engagementResponse, err
8288
}
89+
90+
func (ec EngagementClient) Close(id string) (error) {
91+
path := fmt.Sprint(ec.baseEndpoint, id, "/close/")
92+
resp, err := ec.http.Post(path, nil)
93+
if err != nil {
94+
return err
95+
}
96+
if resp.StatusCode != http.StatusOK {
97+
return ErrCannotCloseEngagement
98+
}
99+
return nil
100+
}

cmd/engagement.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ var createEngagement = &cobra.Command{
2424
Run: create,
2525
}
2626

27+
var closeEngagement = &cobra.Command{
28+
Use: "close",
29+
Short: "close an engagement",
30+
Long: "close an engagement for a product",
31+
Run: close,
32+
}
33+
2734
func init() {
2835
createEngagement.PersistentFlags().StringP("name", "n", "CI/CD", "engagement name")
2936
createEngagement.PersistentFlags().StringP("description", "d", "CI/CD engagement", "engagement description")
@@ -33,7 +40,10 @@ func init() {
3340
createEngagement.PersistentFlags().StringP("branch", "b", "", "branch of the code repository")
3441
createEngagement.PersistentFlags().IntP("product", "p", -1, "product id")
3542

43+
closeEngagement.PersistentFlags().StringP("engagement", "e", "", "engagement id")
44+
3645
rootEngagement.AddCommand(createEngagement)
46+
rootEngagement.AddCommand(closeEngagement)
3747
cli.AddCommand(rootEngagement)
3848
}
3949

@@ -62,3 +72,13 @@ func create(cmd *cobra.Command, args []string) {
6272
data, _ := json.Marshal(resp)
6373
fmt.Println(string(data))
6474
}
75+
76+
func close(cmd *cobra.Command, args []string) {
77+
client := client.NewEngagementClient(config.Configuration)
78+
engagement := getFlagS(cmd, "engagement")
79+
err := client.Close(engagement)
80+
if err != nil {
81+
log.Fatalf("cannot close engagement %s: %v", engagement, err)
82+
}
83+
fmt.Println("deleted")
84+
}

0 commit comments

Comments
 (0)