Skip to content

Commit 1ec00df

Browse files
committed
Add getJenkinsBuild helper and use it for builds
Work around broken base URL by constructing Build.Base and polling it. Replace direct job.GetBuild calls with getJenkinsBuild and add error handling. Add errors and strconv imports.
1 parent 9217fc6 commit 1ec00df

3 files changed

Lines changed: 24 additions & 10 deletions

File tree

duration.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,30 @@ import "fmt"
66
// Returns the largest unit that makes sense: N days, N hours, N minutes, or N seconds
77
func formatDuration(milliseconds float64) string {
88
seconds := int64(milliseconds / 1000)
9-
9+
1010
if seconds < 60 {
1111
if seconds == 1 {
1212
return "1 second"
1313
}
1414
return fmt.Sprintf("%d seconds", seconds)
1515
}
16-
16+
1717
minutes := seconds / 60
1818
if minutes < 60 {
1919
if minutes == 1 {
2020
return "1 minute"
2121
}
2222
return fmt.Sprintf("%d minutes", minutes)
2323
}
24-
24+
2525
hours := minutes / 60
2626
if hours < 24 {
2727
if hours == 1 {
2828
return "1 hour"
2929
}
3030
return fmt.Sprintf("%d hours", hours)
3131
}
32-
32+
3333
days := hours / 24
3434
if days == 1 {
3535
return "1 day"

duration_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ func TestFormatDuration(t *testing.T) {
1616
{5000, "5 seconds"},
1717
{30000, "30 seconds"},
1818
{59000, "59 seconds"},
19-
19+
2020
// Minutes
2121
{60000, "1 minute"},
2222
{120000, "2 minutes"},
2323
{1800000, "30 minutes"},
2424
{3599000, "59 minutes"},
25-
25+
2626
// Hours
2727
{3600000, "1 hour"},
2828
{7200000, "2 hours"},
2929
{43200000, "12 hours"},
3030
{86399000, "23 hours"},
31-
31+
3232
// Days
3333
{86400000, "1 day"},
3434
{172800000, "2 days"},

main.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package main
22

33
import (
44
"context"
5+
"errors"
56
"flag"
67
"fmt"
78
"os"
89
"os/signal"
10+
"strconv"
911
"strings"
1012
"syscall"
1113

@@ -265,19 +267,31 @@ func getJob(ctx context.Context, jobName string) error {
265267
return nil
266268
}
267269

270+
func getJenkinsBuild(ctx context.Context, jobName string, job *gojenkins.Job, id int64) (*gojenkins.Build, error) {
271+
jobURL := "/job/" + jobName // hack for broken base URL
272+
build := gojenkins.Build{Jenkins: job.Jenkins, Job: job, Raw: new(gojenkins.BuildResponse), Depth: 1, Base: jobURL + "/" + strconv.FormatInt(id, 10)}
273+
status, err := build.Poll(ctx)
274+
if err != nil {
275+
return nil, err
276+
}
277+
if status == 200 {
278+
return &build, nil
279+
}
280+
return nil, errors.New(strconv.Itoa(status))
281+
}
282+
268283
// getBuild gets details of a specific build
269284
func getBuild(ctx context.Context, jobName, buildNumber string) error {
270285
job, err := jenkins.GetJob(ctx, jobName)
271286
if err != nil {
272287
return fmt.Errorf("failed to get job: %w", err)
273288
}
274-
275289
buildNum, err := parseBuildNumber(buildNumber)
276290
if err != nil {
277291
return err
278292
}
279293

280-
build, err := job.GetBuild(ctx, buildNum)
294+
build, err := getJenkinsBuild(ctx, jobName, job, buildNum)
281295
if err != nil {
282296
return fmt.Errorf("failed to get build: %w", err)
283297
}
@@ -298,7 +312,7 @@ func getBuildLog(ctx context.Context, jobName, buildNumber string) error {
298312
return err
299313
}
300314

301-
build, err := job.GetBuild(ctx, buildNum)
315+
build, err := getJenkinsBuild(ctx, jobName, job, buildNum)
302316
if err != nil {
303317
return fmt.Errorf("failed to get build: %w", err)
304318
}

0 commit comments

Comments
 (0)