Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Commit

Permalink
Add DeleteByJobId to LogsService
Browse files Browse the repository at this point in the history
  • Loading branch information
Shuhei Kitagawa committed May 2, 2019
1 parent 3d32728 commit 3136ed7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
23 changes: 23 additions & 0 deletions logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,26 @@ func (ls *LogsService) FindByJobId(ctx context.Context, jobId uint) (*Log, *http

return &log, resp, err
}

// DeleteByJobId fetches a job's log based on it's provided id.
//
// Travis CI API docs: https://developer.travis-ci.com/resource/log#delete
func (ls *LogsService) DeleteByJobId(ctx context.Context, jobId uint) (*Log, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("/job/%d/log", jobId), nil)
if err != nil {
return nil, nil, err
}

req, err := ls.client.NewRequest(http.MethodDelete, u, nil, nil)
if err != nil {
return nil, nil, err
}

var log Log
resp, err := ls.client.Do(ctx, req, &log)
if err != nil {
return nil, resp, err
}

return &log, resp, err
}
14 changes: 12 additions & 2 deletions logs_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,24 @@ import (
"testing"
)

func TestLogsService_Integration_FindByJob(t *testing.T) {
func TestLogsService_Integration_FindByJobId(t *testing.T) {
_, res, err := integrationClient.Logs.FindByJobId(context.TODO(), integrationJobId)

if err != nil {
t.Fatalf("unexpected error occured: %s", err)
}

if res.StatusCode != http.StatusOK {
t.Fatalf("#invalid http status: %s", res.Status)
t.Fatalf("invalid http status: %s", res.Status)
}
}

func TestLogsService_Integration_DeleteByJobId(t *testing.T) {
var id uint = 420907934

_, res, _ := integrationClient.Logs.DeleteByJobId(context.TODO(), id)

if res.StatusCode != http.StatusConflict {
t.Fatalf("invalid http status: %s", res.Status)
}
}
27 changes: 24 additions & 3 deletions logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"testing"
)

func TestLogsService_FindByJob(t *testing.T) {
func TestLogsService_FindByJobId(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

Expand All @@ -25,11 +25,32 @@ func TestLogsService_FindByJob(t *testing.T) {
log, _, err := client.Logs.FindByJobId(context.Background(), testJobId)

if err != nil {
t.Errorf("Log.FindByJob returned error: %v", err)
t.Errorf("Log.FindByJobId returned error: %v", err)
}

want := &Log{Id: 1, Content: "test"}
if !reflect.DeepEqual(log, want) {
t.Errorf("Log.FindByJob returned %+v, want %+v", log, want)
t.Errorf("Log.FindByJobId returned %+v, want %+v", log, want)
}
}

func TestLogsService_DeleteByJobId(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc(fmt.Sprintf("/job/%d/log", testJobId), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
fmt.Fprint(w, `{"id":1,"content":"Log removed by XXX at 2017-02-13 16:00:00 UTC"}`)
})

log, _, err := client.Logs.DeleteByJobId(context.Background(), testJobId)

if err != nil {
t.Errorf("Log.DeleteByJobId returned error: %v", err)
}

want := &Log{Id: 1, Content: "Log removed by XXX at 2017-02-13 16:00:00 UTC"}
if !reflect.DeepEqual(log, want) {
t.Errorf("Log.DeleteByJobId returned %+v, want %+v", log, want)
}
}

0 comments on commit 3136ed7

Please sign in to comment.