Skip to content

Commit

Permalink
Copy some diverged changes over (#31)
Browse files Browse the repository at this point in the history
Co-authored-by: @lstout
  • Loading branch information
zoidyzoidzoid authored Oct 17, 2024
1 parent 664d0dc commit 5d2b616
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 53 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.18
FROM golang:1.23

WORKDIR /usr/src/app

Expand Down
3 changes: 1 addition & 2 deletions cmd/gitlab-honeycomb-buildevents/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"os"
"strconv"

"github.com/zoidbergwill/gitlab-honeycomb-buildevents-webhooks-sink/internal/hook"

"github.com/honeycombio/libhoney-go"
"github.com/spf13/cobra"
"github.com/zoidyzoidzoid/gitlab-honeycomb-buildevents-webhooks-sink/internal/hook"
)

// Version is the default value that should be overridden in the
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/zoidyzoidzoid/gitlab-honeycomb-buildevents-webhooks-sink

// +heroku goVersion 1.18
go 1.18
// +heroku goVersion 1.23
go 1.23

require (
github.com/honeycombio/libhoney-go v1.20.0
Expand Down
23 changes: 16 additions & 7 deletions internal/hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"github.com/honeycombio/libhoney-go"
"github.com/honeycombio/libhoney-go/transmission"
"github.com/zoidbergwill/gitlab-honeycomb-buildevents-webhooks-sink/internal/hook/types"
"github.com/zoidyzoidzoid/gitlab-honeycomb-buildevents-webhooks-sink/internal/hook/types"
)

type Listener struct {
Expand Down Expand Up @@ -48,7 +48,7 @@ func New(cfg Config) (*Listener, error) {
mux.HandleFunc("/", l.Home)

srv := &http.Server{
Addr: fmt.Sprintf(cfg.ListenAddr),
Addr: cfg.ListenAddr,
Handler: mux,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
Expand Down Expand Up @@ -129,6 +129,13 @@ func (l *Listener) HandleRequest(w http.ResponseWriter, r *http.Request) {
}
}

func SendEvent(e *libhoney.Event) {
err := e.Send()
if err != nil {
fmt.Printf("failed to send event: %s", err)
}
}

func (l *Listener) handlePipeline(p types.PipelineEventPayload) error {
if p.ObjectAttributes.Duration == 0 || p.ObjectAttributes.Status == "running" {
return nil
Expand All @@ -140,7 +147,7 @@ func (l *Listener) handlePipeline(p types.PipelineEventPayload) error {
return err
}

defer ev.Send()
defer SendEvent(ev)
buildURL := fmt.Sprintf("%s/-/pipelines/%d", p.Project.WebURL, p.ObjectAttributes.ID)
err = ev.Add(map[string]interface{}{
// Basic trace information
Expand Down Expand Up @@ -194,14 +201,14 @@ func (l *Listener) handleJob(j types.JobEventPayload) error {
return err
}

defer ev.Send()
defer SendEvent(ev)
err = ev.Add(map[string]interface{}{
// Basic trace information
"service_name": "job",
"trace.span_id": spanID,
"trace.trace_id": parentTraceID,
"trace.parent_id": parentTraceID,
"name": fmt.Sprintf(j.BuildName),
"name": j.BuildName,

// CI information
"ci_provider": "GitLab-CI",
Expand All @@ -210,12 +217,14 @@ func (l *Listener) handleJob(j types.JobEventPayload) error {
"build_id": j.BuildID,
"repo": j.Repository.Homepage,
// TODO: Something with job status
"status": j.BuildStatus,
"status": j.BuildStatus,
"queued_duration_ms": j.BuildQueuedDuration * 1000,
"queued_duration_min": j.BuildQueuedDuration / 60,

// Runner information
"ci_runner": j.Runner.Description,
"ci_runner_id": j.Runner.ID,
//"ci_runner_tags": strings.Join(j.Runner.Tags, ","),
// "ci_runner_tags": strings.Join(j.Runner.Tags, ","),

"duration_ms": j.BuildDuration * 1000,
})
Expand Down
8 changes: 6 additions & 2 deletions internal/hook/hook_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package hook

import (
"github.com/honeycombio/libhoney-go"
"testing"

"github.com/honeycombio/libhoney-go"
)

func Test_createEvent(t *testing.T) {
Expand All @@ -19,6 +20,9 @@ func Test_createEvent(t *testing.T) {
HookSecret: "",
HoneycombConfig: &config,
})
if err != nil {
t.Errorf("failed to create config: %s", err)
}
got, err := l.createEvent()
if err != nil {
t.Errorf("failed to create event: %s", err)
Expand All @@ -35,7 +39,7 @@ func Test_createEvent(t *testing.T) {
})
}

//func Test_HandlePipeline(t *testing.T) {
// func Test_HandlePipeline(t *testing.T) {
// defer libhoney.Close()
// var config libhoney.Config
// tests := []struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/hook/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"log"
"net/http"

"github.com/zoidbergwill/gitlab-honeycomb-buildevents-webhooks-sink/internal/hook/types"
"github.com/zoidyzoidzoid/gitlab-honeycomb-buildevents-webhooks-sink/internal/hook/types"
)

var (
Expand Down
47 changes: 24 additions & 23 deletions internal/hook/types/job.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
package types

// JobEventPayload contains the information for GitLab's Job status change
// JobEventPayload contains the information for GitLab's Job status change.
type JobEventPayload struct {
ObjectKind string `json:"object_kind"`
Ref string `json:"ref"`
Tag bool `json:"tag"`
BeforeSHA string `json:"before_sha"`
SHA string `json:"sha"`
BuildID int64 `json:"build_id"`
BuildName string `json:"build_name"`
BuildStage string `json:"build_stage"`
BuildStatus string `json:"build_status"`
BuildStartedAt GitLabTimestamp `json:"build_started_at,omitempty"`
BuildFinishedAt GitLabTimestamp `json:"build_finished_at,omitempty"`
BuildDuration float64 `json:"build_duration"`
BuildAllowFailure bool `json:"build_allow_failure"`
BuildFailureReason string `json:"build_failure_reason"`
PipelineID int64 `json:"pipeline_id"`
ProjectID int64 `json:"project_id"`
ProjectName string `json:"project_name"`
User User `json:"user"`
Commit BuildCommit `json:"commit"`
Repository Repository `json:"repository"`
Runner Runner `json:"runner"`
ObjectKind string `json:"object_kind"`
Ref string `json:"ref"`
Tag bool `json:"tag"`
BeforeSHA string `json:"before_sha"`
SHA string `json:"sha"`
BuildID int64 `json:"build_id"`
BuildName string `json:"build_name"`
BuildStage string `json:"build_stage"`
BuildStatus string `json:"build_status"`
BuildStartedAt GitLabTimestamp `json:"build_started_at,omitempty"`
BuildFinishedAt GitLabTimestamp `json:"build_finished_at,omitempty"`
BuildDuration float64 `json:"build_duration"`
BuildAllowFailure bool `json:"build_allow_failure"`
BuildFailureReason string `json:"build_failure_reason"`
BuildQueuedDuration float64 `json:"build_queued_duration"`
PipelineID int64 `json:"pipeline_id"`
ProjectID int64 `json:"project_id"`
ProjectName string `json:"project_name"`
User User `json:"user"`
Commit BuildCommit `json:"commit"`
Repository Repository `json:"repository"`
Runner Runner `json:"runner"`
}

// BuildCommit contains all of the GitLab build commit information
// BuildCommit contains all of the GitLab build commit information.
type BuildCommit struct {
ID int64 `json:"id"`
SHA string `json:"sha"`
Expand Down
8 changes: 4 additions & 4 deletions internal/hook/types/mergerequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package types

import "time"

// MergeRequest contains all the GitLab merge request information
// MergeRequest contains all the GitLab merge request information.
type MergeRequest struct {
ID int64 `json:"id"`
TargetBranch string `json:"target_branch"`
Expand All @@ -29,7 +29,7 @@ type MergeRequest struct {
URL string `json:"url"`
}

// Source contains all the GitLab source information
// Source contains all the GitLab source information.
type Source struct {
Name string `json:"name"`
Description string `json:"description"`
Expand All @@ -47,7 +47,7 @@ type Source struct {
HTTPURL string `json:"http_url"`
}

// Target contains all the GitLab target information
// Target contains all the GitLab target information.
type Target struct {
Name string `json:"name"`
Description string `json:"description"`
Expand All @@ -65,7 +65,7 @@ type Target struct {
HTTPURL string `json:"http_url"`
}

// LastCommit contains all the GitLab last commit information
// LastCommit contains all the GitLab last commit information.
type LastCommit struct {
ID string `json:"id"`
Message string `json:"message"`
Expand Down
10 changes: 5 additions & 5 deletions internal/hook/types/pipeline.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package types

// PipelineEventPayload contains the information for GitLab's pipeline status change event
// PipelineEventPayload contains the information for GitLab's pipeline status change event.
type PipelineEventPayload struct {
ObjectKind string `json:"object_kind"`
User User `json:"user"`
Expand All @@ -11,7 +11,7 @@ type PipelineEventPayload struct {
Builds []Build `json:"builds"`
}

// PipelineObjectAttributes contains pipeline specific GitLab object attributes information
// PipelineObjectAttributes contains pipeline specific GitLab object attributes information.
type PipelineObjectAttributes struct {
ID int64 `json:"id"`
Ref string `json:"ref"`
Expand All @@ -27,13 +27,13 @@ type PipelineObjectAttributes struct {
Variables []Variable `json:"variables"`
}

// Variable contains pipeline variables
// Variable contains pipeline variables.
type Variable struct {
Key string `json:"key"`
Value string `json:"value"`
}

// Build contains all of the GitLab Build information
// Build contains all of the GitLab Build information.
type Build struct {
ID int64 `json:"id"`
Stage string `json:"stage"`
Expand All @@ -49,7 +49,7 @@ type Build struct {
ArtifactsFile ArtifactsFile `json:"artifactsfile"`
}

// ArtifactsFile contains all of the GitLab artifact information
// ArtifactsFile contains all of the GitLab artifact information.
type ArtifactsFile struct {
Filename string `json:"filename"`
Size string `json:"size"`
Expand Down
12 changes: 6 additions & 6 deletions internal/hook/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"
)

// Commit contains all of the GitLab commit information
// Commit contains all of the GitLab commit information.
type Commit struct {
ID string `json:"id"`
Message string `json:"message"`
Expand All @@ -19,13 +19,13 @@ type Commit struct {
Removed []string `json:"removed"`
}

// Author contains all of the GitLab author information
// Author contains all of the GitLab author information.
type Author struct {
Name string `json:"name"`
Email string `json:"email"`
}

// Project contains all of the GitLab project information
// Project contains all of the GitLab project information.
type Project struct {
ID int64 `json:"id"`
Name string `json:"name"`
Expand All @@ -44,7 +44,7 @@ type Project struct {
HTTPURL string `json:"http_url"`
}

// Repository contains all of the GitLab repository information
// Repository contains all of the GitLab repository information.
type Repository struct {
Name string `json:"name"`
URL string `json:"url"`
Expand All @@ -55,7 +55,7 @@ type Repository struct {
VisibilityLevel int64 `json:"visibility_level"`
}

// User contains all of the GitLab user information
// User contains all of the GitLab user information.
type User struct {
ID int64 `json:"id"`
Name string `json:"name"`
Expand All @@ -64,7 +64,7 @@ type User struct {
Email string `json:"email"`
}

// Runner represents a runner agent
// Runner represents a runner agent.
type Runner struct {
ID int64 `json:"id"`
Description string `json:"description"`
Expand Down

0 comments on commit 5d2b616

Please sign in to comment.