Skip to content

Commit 053d870

Browse files
committed
refactor: Move proto types conversion to separate package
1 parent b977639 commit 053d870

File tree

15 files changed

+445
-554
lines changed

15 files changed

+445
-554
lines changed

internal/gitlab/client.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
gitlab "github.com/xanzy/go-gitlab"
1212

13+
"github.com/cluttrdev/gitlab-exporter/internal/types"
1314
"github.com/cluttrdev/gitlab-exporter/protobuf/typespb"
1415
)
1516

@@ -156,9 +157,9 @@ func (c *Client) GetPipelineHierarchy(ctx context.Context, projectID int64, pipe
156157
},
157158
Pipeline: job.Pipeline,
158159
Name: secdat.Name,
159-
StartedAt: convertUnixSeconds(secdat.Start),
160-
FinishedAt: convertUnixSeconds(secdat.End),
161-
Duration: convertDuration(float64(secdat.End - secdat.Start)),
160+
StartedAt: types.ConvertUnixSeconds(secdat.Start),
161+
FinishedAt: types.ConvertUnixSeconds(secdat.End),
162+
Duration: types.ConvertDuration(float64(secdat.End - secdat.Start)),
162163
}
163164

164165
sections = append(sections, section)
@@ -176,7 +177,7 @@ func (c *Client) GetPipelineHierarchy(ctx context.Context, projectID int64, pipe
176177
Name: m.Name,
177178
Labels: convertLabels(m.Labels),
178179
Value: m.Value,
179-
Timestamp: convertUnixMilli(m.Timestamp),
180+
Timestamp: types.ConvertUnixMilli(m.Timestamp),
180181
}
181182
metrics = append(metrics, metric)
182183
}

internal/gitlab/gitlab.go

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ package gitlab
33
import (
44
"fmt"
55
"strconv"
6-
"time"
7-
8-
durationpb "google.golang.org/protobuf/types/known/durationpb"
9-
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
106
)
117

128
func Ptr[T any](v T) *T {
@@ -23,38 +19,3 @@ func parseID(id interface{}) (string, error) {
2319
return "", fmt.Errorf("invalid ID type %#v, the ID must be an int or a string", id)
2420
}
2521
}
26-
27-
func convertTime(t *time.Time) *timestamppb.Timestamp {
28-
if t == nil {
29-
return nil
30-
}
31-
return timestamppb.New(*t)
32-
}
33-
34-
func convertUnixSeconds(ts int64) *timestamppb.Timestamp {
35-
return &timestamppb.Timestamp{
36-
Seconds: ts,
37-
Nanos: 0,
38-
}
39-
}
40-
41-
func convertUnixMilli(ts int64) *timestamppb.Timestamp {
42-
const msPerSecond int64 = 1_000
43-
const nsPerMilli int64 = 1_000
44-
return &timestamppb.Timestamp{
45-
Seconds: ts / msPerSecond,
46-
Nanos: int32((ts % msPerSecond) * nsPerMilli),
47-
}
48-
}
49-
50-
func convertUnixNano(ts int64) *timestamppb.Timestamp {
51-
const nsPerSecond int64 = 1_000_000_000
52-
return &timestamppb.Timestamp{
53-
Seconds: ts / nsPerSecond,
54-
Nanos: int32(ts % nsPerSecond),
55-
}
56-
}
57-
58-
func convertDuration(d float64) *durationpb.Duration {
59-
return durationpb.New(time.Duration(d * float64(time.Second)))
60-
}

internal/gitlab/jobs.go

Lines changed: 3 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"context"
55

66
gitlab "github.com/xanzy/go-gitlab"
7-
"google.golang.org/protobuf/types/known/timestamppb"
87

8+
"github.com/cluttrdev/gitlab-exporter/internal/types"
99
"github.com/cluttrdev/gitlab-exporter/protobuf/typespb"
1010
)
1111

@@ -41,7 +41,7 @@ func (c *Client) ListPipelineJobs(ctx context.Context, projectID int64, pipeline
4141

4242
for _, j := range jobs {
4343
ch <- ListPipelineJobsResult{
44-
Job: convertJob(j),
44+
Job: types.ConvertJob(j),
4545
}
4646
}
4747

@@ -87,7 +87,7 @@ func (c *Client) ListPipelineBridges(ctx context.Context, projectID int64, pipel
8787

8888
for _, b := range bridges {
8989
ch <- ListPipelineBridgesResult{
90-
Bridge: convertBridge(b),
90+
Bridge: types.ConvertBridge(b),
9191
}
9292
}
9393

@@ -101,131 +101,3 @@ func (c *Client) ListPipelineBridges(ctx context.Context, projectID int64, pipel
101101

102102
return ch
103103
}
104-
105-
func convertJob(job *gitlab.Job) *typespb.Job {
106-
artifacts := make([]*typespb.JobArtifacts, 0, len(job.Artifacts))
107-
for _, a := range job.Artifacts {
108-
artifacts = append(artifacts, &typespb.JobArtifacts{
109-
Filename: a.Filename,
110-
FileType: a.FileType,
111-
FileFormat: a.FileFormat,
112-
Size: int64(a.Size),
113-
})
114-
}
115-
116-
return &typespb.Job{
117-
Id: int64(job.ID),
118-
Name: job.Name,
119-
Pipeline: &typespb.PipelineReference{
120-
Id: int64(job.Pipeline.ID),
121-
ProjectId: int64(job.Pipeline.ProjectID),
122-
Ref: job.Pipeline.Ref,
123-
Sha: job.Pipeline.Sha,
124-
Status: job.Pipeline.Status,
125-
},
126-
Ref: job.Ref,
127-
CreatedAt: convertTime(job.CreatedAt),
128-
StartedAt: convertTime(job.StartedAt),
129-
FinishedAt: convertTime(job.FinishedAt),
130-
ErasedAt: convertTime(job.ErasedAt),
131-
Duration: convertDuration(job.Duration),
132-
QueuedDuration: convertDuration(job.QueuedDuration),
133-
Coverage: job.Coverage,
134-
Stage: job.Stage,
135-
Status: job.Status,
136-
AllowFailure: job.AllowFailure,
137-
FailureReason: job.FailureReason,
138-
Tag: job.Tag,
139-
WebUrl: job.WebURL,
140-
TagList: job.TagList,
141-
142-
Commit: convertCommit(job.Commit),
143-
Project: convertProject(job.Project),
144-
User: convertUser(job.User),
145-
146-
Runner: &typespb.JobRunner{
147-
Id: int64(job.Runner.ID),
148-
Name: job.Runner.Name,
149-
Description: job.Runner.Description,
150-
Active: job.Runner.Active,
151-
IsShared: job.Runner.IsShared,
152-
},
153-
154-
Artifacts: artifacts,
155-
ArtifactsFile: &typespb.JobArtifactsFile{
156-
Filename: job.ArtifactsFile.Filename,
157-
Size: int64(job.ArtifactsFile.Size),
158-
},
159-
ArtifactsExpireAt: convertTime(job.ArtifactsExpireAt),
160-
}
161-
}
162-
163-
func convertCommit(commit *gitlab.Commit) *typespb.Commit {
164-
var status string
165-
if commit.Status != nil {
166-
status = string(*commit.Status)
167-
}
168-
return &typespb.Commit{
169-
Id: commit.ID,
170-
ShortId: commit.ShortID,
171-
ParentIds: commit.ParentIDs,
172-
ProjectId: int64(commit.ProjectID),
173-
AuthorName: commit.AuthorName,
174-
AuthorEmail: commit.AuthorEmail,
175-
AuthoredDate: convertTime(commit.AuthoredDate),
176-
CommitterName: commit.CommitterName,
177-
CommitterEmail: commit.CommitterEmail,
178-
CommittedDate: convertTime(commit.CommittedDate),
179-
CreatedAt: convertTime(commit.CreatedAt),
180-
Title: commit.Title,
181-
Message: commit.Message,
182-
Trailers: commit.Trailers,
183-
Stats: convertCommitStats(commit.Stats),
184-
Status: status,
185-
WebUrl: commit.WebURL,
186-
}
187-
}
188-
189-
func convertCommitStats(stats *gitlab.CommitStats) *typespb.CommitStats {
190-
if stats == nil {
191-
return nil
192-
}
193-
return &typespb.CommitStats{
194-
Additions: int64(stats.Additions),
195-
Deletions: int64(stats.Deletions),
196-
Total: int64(stats.Total),
197-
}
198-
}
199-
200-
func convertBridge(bridge *gitlab.Bridge) *typespb.Bridge {
201-
// account for downstream pipeline creation failures
202-
downstreamPipeline := &typespb.PipelineInfo{
203-
CreatedAt: &timestamppb.Timestamp{},
204-
UpdatedAt: &timestamppb.Timestamp{},
205-
}
206-
if bridge.DownstreamPipeline != nil {
207-
downstreamPipeline = convertPipelineInfo(bridge.DownstreamPipeline)
208-
}
209-
return &typespb.Bridge{
210-
// Commit: ConvertCommit(bridge.Commit),
211-
Id: int64(bridge.ID),
212-
Name: bridge.Name,
213-
Pipeline: convertPipelineInfo(&bridge.Pipeline),
214-
Ref: bridge.Ref,
215-
CreatedAt: convertTime(bridge.CreatedAt),
216-
StartedAt: convertTime(bridge.StartedAt),
217-
FinishedAt: convertTime(bridge.FinishedAt),
218-
ErasedAt: convertTime(bridge.ErasedAt),
219-
Duration: convertDuration(bridge.Duration),
220-
QueuedDuration: convertDuration(bridge.QueuedDuration),
221-
Coverage: bridge.Coverage,
222-
Stage: bridge.Stage,
223-
Status: bridge.Status,
224-
AllowFailure: bridge.AllowFailure,
225-
FailureReason: bridge.FailureReason,
226-
Tag: bridge.Tag,
227-
WebUrl: bridge.WebURL,
228-
// User: ConvertUser(bridge.User),
229-
DownstreamPipeline: downstreamPipeline,
230-
}
231-
}

internal/gitlab/pipelines.go

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package gitlab
33
import (
44
"context"
55
"fmt"
6-
"strconv"
76

87
gitlab "github.com/xanzy/go-gitlab"
98

9+
"github.com/cluttrdev/gitlab-exporter/internal/types"
1010
"github.com/cluttrdev/gitlab-exporter/protobuf/typespb"
1111
)
1212

@@ -37,7 +37,7 @@ func (c *Client) ListProjectPipelines(ctx context.Context, projectID int64, opt
3737

3838
for _, pi := range ps {
3939
out <- ListProjectPipelinesResult{
40-
Pipeline: convertPipelineInfo(pi),
40+
Pipeline: types.ConvertPipelineInfo(pi),
4141
}
4242
}
4343

@@ -58,56 +58,5 @@ func (c *Client) GetPipeline(ctx context.Context, projectID int64, pipelineID in
5858
if err != nil {
5959
return nil, fmt.Errorf("error getting pipeline: %w", err)
6060
}
61-
return convertPipeline(pipeline), nil
62-
}
63-
64-
func convertPipelineInfo(pipeline *gitlab.PipelineInfo) *typespb.PipelineInfo {
65-
if pipeline == nil {
66-
return nil
67-
}
68-
return &typespb.PipelineInfo{
69-
Id: int64(pipeline.ID),
70-
Iid: int64(pipeline.IID),
71-
ProjectId: int64(pipeline.ProjectID),
72-
Status: pipeline.Status,
73-
Source: pipeline.Source,
74-
Ref: pipeline.Ref,
75-
Sha: pipeline.SHA,
76-
WebUrl: pipeline.WebURL,
77-
CreatedAt: convertTime(pipeline.CreatedAt),
78-
UpdatedAt: convertTime(pipeline.UpdatedAt),
79-
}
80-
}
81-
82-
func convertPipeline(pipeline *gitlab.Pipeline) *typespb.Pipeline {
83-
return &typespb.Pipeline{
84-
Id: int64(pipeline.ID),
85-
Iid: int64(pipeline.IID),
86-
ProjectId: int64(pipeline.ProjectID),
87-
Status: pipeline.Status,
88-
Source: pipeline.Source,
89-
Ref: pipeline.Ref,
90-
Sha: pipeline.SHA,
91-
BeforeSha: pipeline.BeforeSHA,
92-
Tag: pipeline.Tag,
93-
YamlErrors: pipeline.YamlErrors,
94-
CreatedAt: convertTime(pipeline.CreatedAt),
95-
UpdatedAt: convertTime(pipeline.UpdatedAt),
96-
StartedAt: convertTime(pipeline.StartedAt),
97-
FinishedAt: convertTime(pipeline.FinishedAt),
98-
CommittedAt: convertTime(pipeline.CommittedAt),
99-
Duration: convertDuration(float64(pipeline.Duration)),
100-
QueuedDuration: convertDuration(float64(pipeline.QueuedDuration)),
101-
Coverage: convertCoverage(pipeline.Coverage),
102-
WebUrl: pipeline.WebURL,
103-
User: convertBasicUser(pipeline.User),
104-
}
105-
}
106-
107-
func convertCoverage(coverage string) float64 {
108-
cov, err := strconv.ParseFloat(coverage, 64)
109-
if err != nil {
110-
cov = 0.0
111-
}
112-
return cov
61+
return types.ConvertPipeline(pipeline), nil
11362
}

0 commit comments

Comments
 (0)