Skip to content

Commit

Permalink
refactor: Move proto types conversion to separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
cluttrdev committed Aug 1, 2024
1 parent b977639 commit 053d870
Show file tree
Hide file tree
Showing 15 changed files with 445 additions and 554 deletions.
9 changes: 5 additions & 4 deletions internal/gitlab/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

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

"github.com/cluttrdev/gitlab-exporter/internal/types"
"github.com/cluttrdev/gitlab-exporter/protobuf/typespb"
)

Expand Down Expand Up @@ -156,9 +157,9 @@ func (c *Client) GetPipelineHierarchy(ctx context.Context, projectID int64, pipe
},
Pipeline: job.Pipeline,
Name: secdat.Name,
StartedAt: convertUnixSeconds(secdat.Start),
FinishedAt: convertUnixSeconds(secdat.End),
Duration: convertDuration(float64(secdat.End - secdat.Start)),
StartedAt: types.ConvertUnixSeconds(secdat.Start),
FinishedAt: types.ConvertUnixSeconds(secdat.End),
Duration: types.ConvertDuration(float64(secdat.End - secdat.Start)),
}

sections = append(sections, section)
Expand All @@ -176,7 +177,7 @@ func (c *Client) GetPipelineHierarchy(ctx context.Context, projectID int64, pipe
Name: m.Name,
Labels: convertLabels(m.Labels),
Value: m.Value,
Timestamp: convertUnixMilli(m.Timestamp),
Timestamp: types.ConvertUnixMilli(m.Timestamp),
}
metrics = append(metrics, metric)
}
Expand Down
39 changes: 0 additions & 39 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ package gitlab
import (
"fmt"
"strconv"
"time"

durationpb "google.golang.org/protobuf/types/known/durationpb"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)

func Ptr[T any](v T) *T {
Expand All @@ -23,38 +19,3 @@ func parseID(id interface{}) (string, error) {
return "", fmt.Errorf("invalid ID type %#v, the ID must be an int or a string", id)
}
}

func convertTime(t *time.Time) *timestamppb.Timestamp {
if t == nil {
return nil
}
return timestamppb.New(*t)
}

func convertUnixSeconds(ts int64) *timestamppb.Timestamp {
return &timestamppb.Timestamp{
Seconds: ts,
Nanos: 0,
}
}

func convertUnixMilli(ts int64) *timestamppb.Timestamp {
const msPerSecond int64 = 1_000
const nsPerMilli int64 = 1_000
return &timestamppb.Timestamp{
Seconds: ts / msPerSecond,
Nanos: int32((ts % msPerSecond) * nsPerMilli),
}
}

func convertUnixNano(ts int64) *timestamppb.Timestamp {
const nsPerSecond int64 = 1_000_000_000
return &timestamppb.Timestamp{
Seconds: ts / nsPerSecond,
Nanos: int32(ts % nsPerSecond),
}
}

func convertDuration(d float64) *durationpb.Duration {
return durationpb.New(time.Duration(d * float64(time.Second)))
}
134 changes: 3 additions & 131 deletions internal/gitlab/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"

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

"github.com/cluttrdev/gitlab-exporter/internal/types"
"github.com/cluttrdev/gitlab-exporter/protobuf/typespb"
)

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

for _, j := range jobs {
ch <- ListPipelineJobsResult{
Job: convertJob(j),
Job: types.ConvertJob(j),
}
}

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

for _, b := range bridges {
ch <- ListPipelineBridgesResult{
Bridge: convertBridge(b),
Bridge: types.ConvertBridge(b),
}
}

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

return ch
}

func convertJob(job *gitlab.Job) *typespb.Job {
artifacts := make([]*typespb.JobArtifacts, 0, len(job.Artifacts))
for _, a := range job.Artifacts {
artifacts = append(artifacts, &typespb.JobArtifacts{
Filename: a.Filename,
FileType: a.FileType,
FileFormat: a.FileFormat,
Size: int64(a.Size),
})
}

return &typespb.Job{
Id: int64(job.ID),
Name: job.Name,
Pipeline: &typespb.PipelineReference{
Id: int64(job.Pipeline.ID),
ProjectId: int64(job.Pipeline.ProjectID),
Ref: job.Pipeline.Ref,
Sha: job.Pipeline.Sha,
Status: job.Pipeline.Status,
},
Ref: job.Ref,
CreatedAt: convertTime(job.CreatedAt),
StartedAt: convertTime(job.StartedAt),
FinishedAt: convertTime(job.FinishedAt),
ErasedAt: convertTime(job.ErasedAt),
Duration: convertDuration(job.Duration),
QueuedDuration: convertDuration(job.QueuedDuration),
Coverage: job.Coverage,
Stage: job.Stage,
Status: job.Status,
AllowFailure: job.AllowFailure,
FailureReason: job.FailureReason,
Tag: job.Tag,
WebUrl: job.WebURL,
TagList: job.TagList,

Commit: convertCommit(job.Commit),
Project: convertProject(job.Project),
User: convertUser(job.User),

Runner: &typespb.JobRunner{
Id: int64(job.Runner.ID),
Name: job.Runner.Name,
Description: job.Runner.Description,
Active: job.Runner.Active,
IsShared: job.Runner.IsShared,
},

Artifacts: artifacts,
ArtifactsFile: &typespb.JobArtifactsFile{
Filename: job.ArtifactsFile.Filename,
Size: int64(job.ArtifactsFile.Size),
},
ArtifactsExpireAt: convertTime(job.ArtifactsExpireAt),
}
}

func convertCommit(commit *gitlab.Commit) *typespb.Commit {
var status string
if commit.Status != nil {
status = string(*commit.Status)
}
return &typespb.Commit{
Id: commit.ID,
ShortId: commit.ShortID,
ParentIds: commit.ParentIDs,
ProjectId: int64(commit.ProjectID),
AuthorName: commit.AuthorName,
AuthorEmail: commit.AuthorEmail,
AuthoredDate: convertTime(commit.AuthoredDate),
CommitterName: commit.CommitterName,
CommitterEmail: commit.CommitterEmail,
CommittedDate: convertTime(commit.CommittedDate),
CreatedAt: convertTime(commit.CreatedAt),
Title: commit.Title,
Message: commit.Message,
Trailers: commit.Trailers,
Stats: convertCommitStats(commit.Stats),
Status: status,
WebUrl: commit.WebURL,
}
}

func convertCommitStats(stats *gitlab.CommitStats) *typespb.CommitStats {
if stats == nil {
return nil
}
return &typespb.CommitStats{
Additions: int64(stats.Additions),
Deletions: int64(stats.Deletions),
Total: int64(stats.Total),
}
}

func convertBridge(bridge *gitlab.Bridge) *typespb.Bridge {
// account for downstream pipeline creation failures
downstreamPipeline := &typespb.PipelineInfo{
CreatedAt: &timestamppb.Timestamp{},
UpdatedAt: &timestamppb.Timestamp{},
}
if bridge.DownstreamPipeline != nil {
downstreamPipeline = convertPipelineInfo(bridge.DownstreamPipeline)
}
return &typespb.Bridge{
// Commit: ConvertCommit(bridge.Commit),
Id: int64(bridge.ID),
Name: bridge.Name,
Pipeline: convertPipelineInfo(&bridge.Pipeline),
Ref: bridge.Ref,
CreatedAt: convertTime(bridge.CreatedAt),
StartedAt: convertTime(bridge.StartedAt),
FinishedAt: convertTime(bridge.FinishedAt),
ErasedAt: convertTime(bridge.ErasedAt),
Duration: convertDuration(bridge.Duration),
QueuedDuration: convertDuration(bridge.QueuedDuration),
Coverage: bridge.Coverage,
Stage: bridge.Stage,
Status: bridge.Status,
AllowFailure: bridge.AllowFailure,
FailureReason: bridge.FailureReason,
Tag: bridge.Tag,
WebUrl: bridge.WebURL,
// User: ConvertUser(bridge.User),
DownstreamPipeline: downstreamPipeline,
}
}
57 changes: 3 additions & 54 deletions internal/gitlab/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package gitlab
import (
"context"
"fmt"
"strconv"

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

"github.com/cluttrdev/gitlab-exporter/internal/types"
"github.com/cluttrdev/gitlab-exporter/protobuf/typespb"
)

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

for _, pi := range ps {
out <- ListProjectPipelinesResult{
Pipeline: convertPipelineInfo(pi),
Pipeline: types.ConvertPipelineInfo(pi),
}
}

Expand All @@ -58,56 +58,5 @@ func (c *Client) GetPipeline(ctx context.Context, projectID int64, pipelineID in
if err != nil {
return nil, fmt.Errorf("error getting pipeline: %w", err)
}
return convertPipeline(pipeline), nil
}

func convertPipelineInfo(pipeline *gitlab.PipelineInfo) *typespb.PipelineInfo {
if pipeline == nil {
return nil
}
return &typespb.PipelineInfo{
Id: int64(pipeline.ID),
Iid: int64(pipeline.IID),
ProjectId: int64(pipeline.ProjectID),
Status: pipeline.Status,
Source: pipeline.Source,
Ref: pipeline.Ref,
Sha: pipeline.SHA,
WebUrl: pipeline.WebURL,
CreatedAt: convertTime(pipeline.CreatedAt),
UpdatedAt: convertTime(pipeline.UpdatedAt),
}
}

func convertPipeline(pipeline *gitlab.Pipeline) *typespb.Pipeline {
return &typespb.Pipeline{
Id: int64(pipeline.ID),
Iid: int64(pipeline.IID),
ProjectId: int64(pipeline.ProjectID),
Status: pipeline.Status,
Source: pipeline.Source,
Ref: pipeline.Ref,
Sha: pipeline.SHA,
BeforeSha: pipeline.BeforeSHA,
Tag: pipeline.Tag,
YamlErrors: pipeline.YamlErrors,
CreatedAt: convertTime(pipeline.CreatedAt),
UpdatedAt: convertTime(pipeline.UpdatedAt),
StartedAt: convertTime(pipeline.StartedAt),
FinishedAt: convertTime(pipeline.FinishedAt),
CommittedAt: convertTime(pipeline.CommittedAt),
Duration: convertDuration(float64(pipeline.Duration)),
QueuedDuration: convertDuration(float64(pipeline.QueuedDuration)),
Coverage: convertCoverage(pipeline.Coverage),
WebUrl: pipeline.WebURL,
User: convertBasicUser(pipeline.User),
}
}

func convertCoverage(coverage string) float64 {
cov, err := strconv.ParseFloat(coverage, 64)
if err != nil {
cov = 0.0
}
return cov
return types.ConvertPipeline(pipeline), nil
}
Loading

0 comments on commit 053d870

Please sign in to comment.