Skip to content

Commit

Permalink
Sup 2313/fix view (#285)
Browse files Browse the repository at this point in the history
* First work

* Patch non-script commands

* Format etc
  • Loading branch information
mcncl authored Jun 14, 2024
1 parent d5b588e commit 4d06f6c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 31 deletions.
4 changes: 2 additions & 2 deletions internal/build/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func BuildSummary(build *buildkite.Build) string {
buildCreator(build),
build.CreatedAt.UTC().Format(time.RFC1123Z))
hash := *build.Commit
if len(hash) >= 7 {
hash = hash[0:8]
if len(hash) > 0 {
hash = hash[0:]
}
commitDetails := fmt.Sprintf("Branch: %s / Commit: %s \n", *build.Branch, hash)
summary := lipgloss.JoinVertical(lipgloss.Top,
Expand Down
65 changes: 44 additions & 21 deletions internal/job/view.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,60 @@
package job

import (
"errors"
"fmt"
"log"
"time"

"github.com/buildkite/go-buildkite/v3/buildkite"
"github.com/charmbracelet/lipgloss"
)

func JobSummary(job *buildkite.Job) string {
jobName := getJobName(*job)
jobTotalTime, err := calculateTotalTime(job.StartedAt, job.FinishedAt)
if err != nil {
log.Fatal("Unable to calculate total job time", err)
}
type Job buildkite.Job

func JobSummary(job Job) string {
return job.Summarise()
}

func (j Job) Summarise() string {
var summary string
var jobState string
var jobName string
var jobDuration time.Duration

jobInfo := fmt.Sprintf("%s %s (%s)", renderJobState(*job.State), jobName, lipgloss.NewStyle().Foreground(lipgloss.Color("#5A5A5A")).Render(jobTotalTime.String()))
jobState = renderJobState(*j.State)

summary := lipgloss.JoinVertical(lipgloss.Top,
if j.getJobType() != "script" {
jobName = j.getJobType()
jobDuration = time.Duration(0)
} else {
jobName = j.getJobName()
jobDuration = calculateTotalTime(j.StartedAt, j.FinishedAt)
}

summary = lipgloss.JoinVertical(lipgloss.Top,
lipgloss.NewStyle().Align(lipgloss.Left).Padding(0, 1).Render(),
lipgloss.NewStyle().Bold(true).Padding(0, 1).Render(jobInfo),
lipgloss.JoinHorizontal(lipgloss.Left,
lipgloss.NewStyle().Bold(true).Padding(0, 1).Render(jobState),
lipgloss.NewStyle().Padding(0, 1).Render(jobName),
lipgloss.NewStyle().Padding(0, 1).Foreground(lipgloss.Color("#6c6c6c")).Render(jobDuration.String()),
),
)

return summary
}

func getJobName(job buildkite.Job) string {
func (j Job) getJobType() string {
return *j.Type
}

func (j Job) getJobName() string {
var jobName string

switch {
case job.Name != nil:
jobName = *job.Name
case job.Label != nil:
jobName = *job.Label
case j.Name != nil:
jobName = *j.Name
case j.Label != nil:
jobName = *j.Label
default:
jobName = *job.Command
jobName = *j.Command
}

return jobName
Expand All @@ -56,6 +75,10 @@ func renderJobState(state string) string {
stateIcon = "🚫"
case "canceling":
stateIcon = "🚫(cancelling...)"
case "blocked":
stateIcon = "🔒(Blocked)"
case "unblocked":
stateIcon = "🔓(Unblocked)"
default:
stateIcon = "❔"
}
Expand All @@ -78,10 +101,10 @@ func getJobStateColor(state string) lipgloss.Color {
return stateColor
}

func calculateTotalTime(startedAt, finishedAt *buildkite.Timestamp) (time.Duration, error) {
func calculateTotalTime(startedAt, finishedAt *buildkite.Timestamp) time.Duration {
if startedAt == nil || finishedAt == nil {
return 0, errors.New("both startedAt and finishedAt must be non-nil")
return 0
}

return finishedAt.Time.Sub(startedAt.Time), nil
return finishedAt.Time.Sub(startedAt.Time)
}
2 changes: 1 addition & 1 deletion pkg/cmd/build/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func download(build build.Build, f *factory.Factory) error {
return fmt.Errorf("could not get logs for job %s", *job.ID)
}

err = os.WriteFile(filepath.Join(directory, *job.ID), []byte(*log.Content), 0644)
err = os.WriteFile(filepath.Join(directory, *job.ID), []byte(*log.Content), 0o644)
if err != nil {
return err
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/cmd/build/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ func NewCmdBuildView(f *factory.Factory) *cobra.Command {
}

l := io.NewPendingCommand(func() tea.Msg {

b, _, err := f.RestAPIClient.Builds.Get(bld.Organization, bld.Pipeline, fmt.Sprint(bld.BuildNumber), &buildkite.BuildsListOptions{})
if err != nil {
return err
Expand All @@ -84,7 +83,10 @@ func NewCmdBuildView(f *factory.Factory) *cobra.Command {
if len(b.Jobs) > 0 {
summary += lipgloss.NewStyle().Bold(true).Padding(0, 1).Render("\nJobs")
for _, j := range b.Jobs {
summary += job.JobSummary(j)
bkJob := *j
if *bkJob.Type == "script" {
summary += job.JobSummary(job.Job(bkJob))
}
}
}
if len(buildArtifacts) > 0 {
Expand Down
6 changes: 1 addition & 5 deletions pkg/cmd/pipeline/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
)

func NewCmdPipelineCreate(f *factory.Factory) *cobra.Command {

cmd := cobra.Command{
DisableFlagsInUseLine: true,
Use: "create",
Expand Down Expand Up @@ -88,7 +87,6 @@ func getRepoURLS(f *factory.Factory) []string {

func createPipeline(client *buildkite.Client, org, pipelineName, description, repoURL string) error {
l := io.NewPendingCommand(func() tea.Msg {

createPipeline := buildkite.CreatePipeline{
Name: *buildkite.String(pipelineName),
Repository: *buildkite.String(repoURL),
Expand All @@ -97,9 +95,8 @@ func createPipeline(client *buildkite.Client, org, pipelineName, description, re
}

pipeline, resp, err := client.Pipelines.Create(org, &createPipeline)

if err != nil {
//return renderOutput(fmt.Sprintf("Unable to create pipeline.: %s", err.Error()))
// return renderOutput(fmt.Sprintf("Unable to create pipeline.: %s", err.Error()))
return io.PendingOutput(lipgloss.JoinVertical(lipgloss.Top,
lipgloss.NewStyle().Padding(1, 1).Render(fmt.Sprintf("Unable to create pipeline.: %s", err.Error()))))
}
Expand All @@ -116,7 +113,6 @@ func createPipeline(client *buildkite.Client, org, pipelineName, description, re

return io.PendingOutput(lipgloss.JoinVertical(lipgloss.Top,
lipgloss.NewStyle().Padding(1, 1).Render(fmt.Sprintf("Pipeline created: %s", *pipeline.WebURL))))

}, fmt.Sprintf("Creating new pipeline %s for %s", pipelineName, org))
p := tea.NewProgram(l)
_, err := p.Run()
Expand Down

0 comments on commit 4d06f6c

Please sign in to comment.