Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance: any status, error wrapping, and fetch improvements #142

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@eb055d739abdc2e8de2e5f4ba1a8b246daa779aa # v3.26.0
uses: github/codeql-action/init@883d8588e56d1753a8a58c1c86e88976f0c23449 # v3.26.3
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -50,7 +50,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@eb055d739abdc2e8de2e5f4ba1a8b246daa779aa # v3.26.0
uses: github/codeql-action/autobuild@883d8588e56d1753a8a58c1c86e88976f0c23449 # v3.26.3

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -64,4 +64,4 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@eb055d739abdc2e8de2e5f4ba1a8b246daa779aa # v3.26.0
uses: github/codeql-action/analyze@883d8588e56d1753a8a58c1c86e88976f0c23449 # v3.26.3
1 change: 1 addition & 0 deletions cmd/vela-downstream/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (b *Build) Validate() error {
constants.StatusPending,
constants.StatusRunning,
constants.StatusSuccess,
"any",
}

// iterate through the build statuses provided
Expand Down
2 changes: 2 additions & 0 deletions cmd/vela-downstream/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type Config struct {
Server string
// user token to authenticate with the Vela server
Token string
// depth of builds search in downstream repo
Depth int
// the app name utilizing this config
AppName string
// the app version utilizing this config
Expand Down
8 changes: 8 additions & 0 deletions cmd/vela-downstream/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ func main() {
Name: "config.token",
Usage: "user token to authenticate with the Vela server",
},
&cli.IntFlag{
EnvVars: []string{"PARAMETER_DEPTH", "DOWNSTREAM_DEPTH"},
FilePath: "/vela/parameters/downstream/depth,/vela/secrets/downstream/depth",
Name: "config.depth",
Usage: "number of builds to search for downstream repositories",
Value: 50,
},

// Repo Flags

Expand Down Expand Up @@ -192,6 +199,7 @@ func run(c *cli.Context) error {
Config: &Config{
Server: c.String("config.server"),
Token: c.String("config.token"),
Depth: c.Int("config.depth"),
AppName: c.App.Name,
AppVersion: c.App.Version,
},
Expand Down
57 changes: 23 additions & 34 deletions cmd/vela-downstream/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
"time"

"github.com/go-vela/sdk-go/vela"
"github.com/go-vela/server/api/types"
api "github.com/go-vela/server/api/types"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)

Expand All @@ -34,7 +33,7 @@
return err
}

rBMap := make(map[*library.Repo]int)
rBMap := make(map[*api.Repo]int)

// parse list of repos to trigger builds on
repos, err := p.Repo.Parse(p.Build.Branch)
Expand All @@ -45,9 +44,9 @@
// iterate through each repo from provided configuration
for _, repo := range repos {
// create new build type to store last successful build
build := types.Build{}
build := api.Build{}

logrus.Infof("listing last 500 builds for %s", repo.GetFullName())
logrus.Infof("searching last %d %s builds with branch %s for %s", p.Config.Depth, p.Build.Event, repo.GetBranch(), repo.GetFullName())

// create options for listing builds
//
Expand All @@ -60,30 +59,38 @@
// set the default starting page for options
Page: 1,
// set the max per page for options
PerPage: 100,
PerPage: 10,
},
}

// create new slice of builds to store API results
builds := []types.Build{}

// loop to capture *ALL* the builds
for {
// send API call to capture a list of builds for the repo
//
// https://pkg.go.dev/github.com/go-vela/sdk-go/vela#BuildService.GetAll
b, resp, err := client.Build.GetAll(repo.GetOrg(), repo.GetName(), opts)
builds, resp, err := client.Build.GetAll(repo.GetOrg(), repo.GetName(), opts)
if err != nil {
return fmt.Errorf("unable to list builds for %s: %w", repo.GetFullName(), err)
}

// add the results to the list of builds
builds = append(builds, *b...)
// iterate through list of builds for the repo
for _, b := range *builds {
// check if the build branch, event and status match
if contains(p.Build.Status, b.GetStatus()) || contains(p.Build.Status, "any") {
// update the build object to the current build
build = b

logrus.Infof("found %s build %s/%d on branch %s with status %s", p.Build.Event, repo.GetFullName(), build.GetNumber(), repo.GetBranch(), build.GetStatus())

// break out of the loop
break
}
}

// break the loop if there is no more results
// to page through or after 5 pages of results
// to page through or after 50 pages of results
// giving us up to a total of 500 builds
if resp.NextPage == 0 || resp.NextPage > 5 {
if resp.NextPage == 0 || resp.NextPage > 50 {
break
}

Expand All @@ -92,24 +99,6 @@
opts.ListOptions.Page = resp.NextPage
}

logrus.Debugf("searching for latest %s build on branch %s with status %s",
p.Build.Event,
repo.GetBranch(),
p.Build.Status,
)

// iterate through list of builds for the repo
for _, b := range builds {
// check if the build branch, event and status match
if contains(p.Build.Status, b.GetStatus()) {
// update the build object to the current build
build = b

// break out of the loop
break
}
}

// check if we found a build to restart
if build.GetNumber() == 0 {
msg := fmt.Sprintf("no %s build on branch %s with status %s found for %s",
Expand All @@ -125,7 +114,7 @@
continue
}

return fmt.Errorf(msg)

Check failure on line 117 in cmd/vela-downstream/plugin.go

View workflow job for this annotation

GitHub Actions / diff-review

printf: non-constant format string in call to fmt.Errorf (govet)

Check failure on line 117 in cmd/vela-downstream/plugin.go

View workflow job for this annotation

GitHub Actions / full-review

printf: non-constant format string in call to fmt.Errorf (govet)

Check failure on line 117 in cmd/vela-downstream/plugin.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] cmd/vela-downstream/plugin.go#L117

printf: non-constant format string in call to fmt.Errorf (govet)
Raw output
cmd/vela-downstream/plugin.go:117:22: printf: non-constant format string in call to fmt.Errorf (govet)
			return fmt.Errorf(msg)
			                  ^
}

logrus.Infof("restarting build %s/%d", repo.GetFullName(), build.GetNumber())
Expand All @@ -135,7 +124,7 @@
// https://pkg.go.dev/github.com/go-vela/sdk-go/vela#BuildService.Restart
b, _, err := client.Build.Restart(repo.GetOrg(), repo.GetName(), build.GetNumber())
if err != nil {
return fmt.Errorf("unable to restart build %s/%d", repo.GetFullName(), build.GetNumber())
return fmt.Errorf("unable to restart build %s/%d: %w", repo.GetFullName(), build.GetNumber(), err)
}

// set map value for status checking
Expand All @@ -159,7 +148,7 @@

// Report is a plugin method that checks the build statuses of all the builds kicked off from the plugin.
// It will continue to check the statuses on 30 second intervals until the timeout is reached.
func (p *Plugin) Report(client *vela.Client, rBMap map[*library.Repo]int) error {
func (p *Plugin) Report(client *vela.Client, rBMap map[*api.Repo]int) error {
logrus.Info("waiting for 30 seconds to check status of downstream builds...")
// sleep to allow for all restart processing
time.Sleep(30 * time.Second)
Expand Down
8 changes: 4 additions & 4 deletions cmd/vela-downstream/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"strings"

"github.com/go-vela/types/library"
api "github.com/go-vela/server/api/types"

"github.com/sirupsen/logrus"
)
Expand All @@ -18,17 +18,17 @@ type Repo struct {
}

// Parse verifies the Repo is properly configured.
func (r *Repo) Parse(branch string) ([]*library.Repo, error) {
func (r *Repo) Parse(branch string) ([]*api.Repo, error) {
logrus.Trace("parsing repos from provided configuration")

// create new repos type to store parsed repos
repos := []*library.Repo{}
repos := []*api.Repo{}

for _, name := range r.Names {
logrus.Tracef("parsing repo %s", name)

// create new repo type to store parsed repo information
repo := new(library.Repo)
repo := new(api.Repo)

// split the repo on / to account for org/repo as input
parts := strings.Split(name, "/")
Expand Down
8 changes: 4 additions & 4 deletions cmd/vela-downstream/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"reflect"
"testing"

"github.com/go-vela/types/library"
api "github.com/go-vela/server/api/types"
)

func TestDownstream_Repo_Parse(t *testing.T) {
Expand All @@ -15,19 +15,19 @@ func TestDownstream_Repo_Parse(t *testing.T) {
Names: []string{"go-vela/hello-world@test", "go-vela/hello-world"},
}

r1 := new(library.Repo)
r1 := new(api.Repo)
r1.SetOrg("go-vela")
r1.SetName("hello-world")
r1.SetFullName("go-vela/hello-world")
r1.SetBranch("test")

r2 := new(library.Repo)
r2 := new(api.Repo)
r2.SetOrg("go-vela")
r2.SetName("hello-world")
r2.SetFullName("go-vela/hello-world")
r2.SetBranch("main")

want := []*library.Repo{r1, r2}
want := []*api.Repo{r1, r2}

// run test
got, err := r.Parse("main")
Expand Down