This repository has been archived by the owner on Jan 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
logs.go
82 lines (69 loc) · 2.03 KB
/
logs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) 2015 Ableton AG, Berlin. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// Fragments of this file have been copied from the go-github (https://github.com/google/go-github)
// project, and is therefore licensed under the following copyright:
// Copyright 2013 The go-github AUTHORS. All rights reserved.
package travis
import (
"bytes"
"fmt"
"net/http"
)
// LogssService handles communication with the logs
// related methods of the Travis CI API.
type LogsService struct {
client *Client
}
// Log represents a Travis CI job log
type Log struct {
Id uint `json:"id,omitempty"`
JobId uint `json:"job_id,omitempty"`
Type string `json:"type,omitempty"`
Body string `json:"body,omitempty"`
}
// getLogResponse represents the response of a call
// to the Travis CI get log endpoint.
type getLogResponse struct {
Log Log `json:"log,omitempty"`
}
// Get fetches a log based on the provided id.
//
// Travis CI API docs: http://docs.travis-ci.com/api/#logs
func (ls *LogsService) Get(logId uint) (*Log, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("/logs/%d", logId), nil)
if err != nil {
return nil, nil, err
}
req, err := ls.client.NewRequest("GET", u, nil, nil)
if err != nil {
return nil, nil, err
}
var logResp getLogResponse
resp, err := ls.client.Do(req, &logResp)
if err != nil {
return nil, resp, err
}
return &logResp.Log, resp, err
}
// Get a job's log based on it's provided id.
//
// Travis CI API docs: http://docs.travis-ci.com/api/#logs
func (ls *LogsService) GetByJob(jobId uint) (*Log, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("/jobs/%d/log", jobId), nil)
if err != nil {
return nil, nil, err
}
req, err := ls.client.NewRequest("GET", u, nil, nil)
if err != nil {
return nil, nil, err
}
var plainText bytes.Buffer
resp, err := ls.client.Do(req, &plainText)
if err != nil {
return nil, resp, err
}
return &Log{JobId: jobId, Body: plainText.String()}, resp, err
}