-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommit.go
219 lines (164 loc) · 5.79 KB
/
commit.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2024 qbee.io
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package client
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"go.qbee.io/client/types"
)
// Commit represents a single commit in the system.
type Commit struct {
// ID is the unique identifier of the commit.
ID string `json:"-"`
// SHA is the pseudo-digest of the commit.
SHA string `json:"sha"`
// Message is the commit message.
Message string `json:"message"`
// Type is the type of the commit.
Type string `json:"type"`
// Labels is the list of labels of the commit.
Labels []string `json:"labels"`
// Changes is the list of changes' SHA that are part of the commit.
Changes []string `json:"changes"`
// UserID is the ID of the user who created the commit.
UserID string `json:"user_id"`
// User contains the user who created the commit.
// This field is populated by the API handlers.
User *UserBaseInfo `json:"user,omitempty"`
// Created is the timestamp when the commit was created.
Created types.Timestamp `json:"created"`
// Updated is the timestamp when the commit was last updated.
Updated types.Timestamp `json:"updated"`
}
// CommitRequest is a request to commit uncommitted changes.
type CommitRequest struct {
// Action must be always set to "commit".
Action string `json:"action"`
// Message describing changes in the commit.
Message string `json:"message"`
}
const commitAction = "commit"
// CommitConfiguration commits uncommitted changes with provided message.
func (cli *Client) CommitConfiguration(ctx context.Context, message string) (*Commit, error) {
const path = "/api/v2/commit"
request := CommitRequest{
Action: commitAction,
Message: message,
}
auditCommit := new(Commit)
err := cli.Call(ctx, http.MethodPost, path, request, auditCommit)
if err != nil {
return nil, err
}
return auditCommit, nil
}
// CommitExtended is a commit with complete changes objects.
type CommitExtended struct {
Commit
// Changes is the list of changes' SHA that are part of the commit.
Changes []Change `json:"changes"`
}
// GetCommit returns a single commit by its SHA.
func (cli *Client) GetCommit(ctx context.Context, sha string) (*CommitExtended, error) {
path := "/api/v2/commit/" + sha
commit := new(CommitExtended)
if err := cli.Call(ctx, http.MethodGet, path, nil, commit); err != nil {
return nil, err
}
return commit, nil
}
// CommitListSearch defines search parameters for CommitListQuery.
type CommitListSearch struct {
// CommitSHA - full string or substring with at least 6 characters.
CommitSHA string `json:"commit_sha,omitempty"`
// Message - commit message to search for (substring match).
Message string `json:"message,omitempty"`
// UserID - user ID to search for (exact match).
UserID string `json:"user_id,omitempty"`
// Committer - name or surname of the user (substring match)
Committer string `json:"committer,omitempty"`
// ChangeSHA - full string or substring with at least 6 characters
ChangeSHA string `json:"change_sha,omitempty"`
// LabelsInclude - array of labels which MUST be present (exact match)
LabelsInclude []string `json:"labels_include,omitempty"`
// LabelsExclude - array of labels which MUST NOT be present (exact match)
LabelsExclude []string `json:"labels_exclude,omitempty"`
// StartDate - start date UTC, format: YYYY-MM-DD hh:ii:ss
StartDate string `json:"start_date,omitempty"`
// EndDate - end date UTC, format: YYYY-MM-DD hh:ii:ss
EndDate string `json:"end_date,omitempty"`
}
// CommitListQuery defines query parameters for ChangeList.
type CommitListQuery struct {
Search CommitListSearch
// SortField defines field used to sort, 'created' by default.
// Supported sort fields:
// - user_id
// - type
// - message
// - created
Sort string
// SortDirection defines sort direction, 'desc' by default.
SortDirection string
// Limit defines maximum number of records in result, default 30, max 1000
Limit int
// Offset defines offset of the first record in result, default 0
Offset int
}
// String returns string representation of CommitListQuery which can be used as query string.
func (q CommitListQuery) String() (string, error) {
values := make(url.Values)
searchQueryJSON, err := json.Marshal(q.Search)
if err != nil {
return "", err
}
values.Set("search", string(searchQueryJSON))
if q.Sort != "" {
values.Set("sort_field", q.Sort)
}
if q.SortDirection != "" {
values.Set("sort_direction", q.SortDirection)
}
if q.Limit != 0 {
values.Set("items_per_page", fmt.Sprintf("%d", q.Limit))
}
if q.Offset != 0 {
values.Set("offset", fmt.Sprintf("%d", q.Offset))
}
return values.Encode(), nil
}
// CommitsList represents a slice of commits matched by the query.
// As well as the total number of commits matched by the query.
type CommitsList struct {
Commits []*CommitExtended `json:"items"`
Total int `json:"total"`
}
// ListCommits returns a list of commits based on provided query.
func (cli *Client) ListCommits(ctx context.Context, query CommitListQuery) (*CommitsList, error) {
queryString, err := query.String()
if err != nil {
return nil, err
}
path := "/api/v2/commitlist?" + queryString
response := new(CommitsList)
if err = cli.Call(ctx, http.MethodGet, path, nil, response); err != nil {
return nil, err
}
return response, nil
}