Skip to content

Commit

Permalink
added parallel processing
Browse files Browse the repository at this point in the history
Signed-off-by: Dipankar Das <[email protected]>
  • Loading branch information
dipankardas011 committed Jun 12, 2024
1 parent a264aa7 commit b01c5d2
Showing 1 changed file with 51 additions and 27 deletions.
78 changes: 51 additions & 27 deletions scripts/repo-variable-writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"os"
"strings"
"sync"
"time"
)

Expand All @@ -28,9 +29,7 @@ type StatePersistentFactory interface {
}

const (
LocProjMeta string = "../projects/project.json"
FullRepoName string = "dipankardas011/test-vars"
// FullRepoName string = "cncf-tags/green-reviews-tooling"
LocProjMeta string = "../projects/project.json"
)

func toPtr[T any](v T) *T {
Expand Down Expand Up @@ -66,7 +65,6 @@ func loadProjectMetadata(locProj string) (resp *Projects, err error) {
return
}

// TODO: add go routine to fetch versions simultenously
func fetchLatestRelease(org, proj string) (*string, error) {
url := fmt.Sprintf(
"https://api.github.com/repos/%s/%s/releases/latest", org, proj)
Expand Down Expand Up @@ -99,7 +97,8 @@ func fetchLatestRelease(org, proj string) (*string, error) {
}

type GithubVariable struct {
gh_token string
gh_token string
fullRepoName string
}

func NewGithubVariable() (*GithubVariable, error) {
Expand All @@ -109,14 +108,14 @@ func NewGithubVariable() (*GithubVariable, error) {
if !ok || len(v) == 0 {
return nil, fmt.Errorf("the environment variable for github pat is missing")
}
return &GithubVariable{gh_token: v}, nil
return &GithubVariable{gh_token: v, fullRepoName: "cncf-tags/green-reviews-tooling"}, nil
}

func (obj *GithubVariable) genUrlAndHeaders(proj string) (*string, map[string]string, error) {
variableURI := strings.ToLower(proj) + "_version"
url := fmt.Sprintf(
"https://api.github.com/repos/%s/actions/variables/%s",
FullRepoName,
obj.fullRepoName,
variableURI,
)

Expand Down Expand Up @@ -217,33 +216,58 @@ func handleProjectsUpdater() {
os.Exit(1)
}

wg := new(sync.WaitGroup)
wg.Add(len(projects.Projects))

errChan := make(chan error, len(projects.Projects))

for _, project := range projects.Projects {
latestRelease, err := fetchLatestRelease(
project.Organization,
project.Name,
)
if err != nil {
slog.Error("Failed: fetchLatestRelease", "Reason", err)
os.Exit(1)
}

storedRelease, err := storage.Read(project.Name)
if err != nil {
slog.Error("Failed: readProjectVariable", "Reason", err)
os.Exit(1)
}
go func(proj Project) {
defer wg.Done()

latestRelease, err := fetchLatestRelease(
proj.Organization,
proj.Name,
)
if err != nil {
errChan <- err
return
}

if strings.Compare(*storedRelease, *latestRelease) != 0 {
slog.Info("Release Versions", "Proj", project.Name, "Org", project.Organization, "Latest", *latestRelease, "Current", *storedRelease)
if err := storage.Write(project.Name, *latestRelease); err != nil {
slog.Error("Failed: writeProjectVariable", "Reason", err)
os.Exit(1)
storedRelease, err := storage.Read(proj.Name)
if err != nil {
errChan <- err
return
}

} else {
slog.Info("Already in Latest version", "Proj", project.Name, "Org", project.Organization, "Ver", *latestRelease)
if strings.Compare(*storedRelease, *latestRelease) != 0 {
slog.Info("Release Versions", "Proj", proj.Name, "Org", proj.Organization, "Latest", *latestRelease, "Current", *storedRelease)
if err := storage.Write(proj.Name, *latestRelease); err != nil {
errChan <- err
return
}
slog.Info("Updated to Latest version", "Proj", proj.Name, "Org", proj.Organization, "Ver", *latestRelease)

} else {
slog.Info("Already in Latest version", "Proj", proj.Name, "Org", proj.Organization, "Ver", *latestRelease)
}
}(project)
}

wg.Wait()
close(errChan)

hadFailures := false
for err := range errChan {
if err != nil {
hadFailures = true
slog.Error("Failed", "Reason", err)
}
}
if hadFailures {
os.Exit(1)
}
}

func main() {
Expand Down

0 comments on commit b01c5d2

Please sign in to comment.