Skip to content

Commit

Permalink
refactor: improved the method names
Browse files Browse the repository at this point in the history
Signed-off-by: Dipankar Das <[email protected]>
  • Loading branch information
dipankardas011 committed Jun 15, 2024
1 parent 36d49eb commit d28ab12
Showing 1 changed file with 20 additions and 30 deletions.
50 changes: 20 additions & 30 deletions scripts/repo-variable-writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,15 @@ func NewGithubVariable() (*GithubVariable, error) {
return &GithubVariable{githubToken: v, fullRepoName: "cncf-tags/green-reviews-tooling"}, nil
}

func (obj *GithubVariable) genUrlAndHeaders(proj string) (*string, map[string]string, error) {
variableURI := strings.ToLower(proj) + "_version"
func (obj *GithubVariable) genVariableName(projName string) string {
return strings.ToLower(projName + "_version")
}

func (obj *GithubVariable) genUrlAndHeadersForRepoVars(projName string) (*string, map[string]string, error) {
url := fmt.Sprintf(
"https://api.github.com/repos/%s/actions/variables/%s",
obj.fullRepoName,
variableURI,
obj.genVariableName(projName),
)

return toPtr[string](url),
Expand All @@ -123,20 +126,18 @@ func (obj *GithubVariable) genUrlAndHeaders(proj string) (*string, map[string]st
}, nil
}

func (obj *GithubVariable) Update(proj, value string) error {
url, header, err := obj.genUrlAndHeaders(proj)
func (obj *GithubVariable) UpdateRepoVariable(projName, newVersion string) error {
url, header, err := obj.genUrlAndHeadersForRepoVars(projName)
if err != nil {
return err
}

variable := strings.ToLower(proj) + "_version"

newVariableData := struct {
Name string `json:"name"`
Val string `json:"value"`
}{
Name: strings.ToUpper(variable),
Val: value,
Name: strings.ToUpper(obj.genVariableName(projName)),
Val: newVersion,
}

var _newVariableData bytes.Buffer
Expand Down Expand Up @@ -164,8 +165,8 @@ func (obj *GithubVariable) Update(proj, value string) error {
return nil
}

func (obj *GithubVariable) Read(proj string) (*string, error) {
url, header, err := obj.genUrlAndHeaders(proj)
func (obj *GithubVariable) ReadRepoVariable(proj string) (*string, error) {
url, header, err := obj.genUrlAndHeadersForRepoVars(proj)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -197,13 +198,11 @@ func (obj *GithubVariable) Read(proj string) (*string, error) {
return toPtr[string](variableData.VariableValue), nil
}

func main() {

type UpdatedProjects struct {
ProjNames []string `json:"proj_names"`
}
func (obj *GithubVariable) UpdatedProjectDispatcher(proj Project, version string) error {
return nil
}

updatedProjects := UpdatedProjects{}
func main() {

projects, err := loadProjectMetadata(LocProjMeta)
if err != nil {
Expand Down Expand Up @@ -237,21 +236,22 @@ func main() {
return
}

storedRelease, err := storage.Read(proj.Name)
storedRelease, err := storage.ReadRepoVariable(proj.Name)
if err != nil {
errChan <- err
return
}

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

// TODO(dipankar)!: call project workflow_dispatch

} else {
slog.Info("Already in Latest version", "Proj", proj.Name, "Org", proj.Organization, "Ver", *latestRelease)
}
Expand All @@ -271,14 +271,4 @@ func main() {
if hadFailures {
os.Exit(1)
}

if v, err := json.Marshal(updatedProjects); err != nil {
slog.Error("failed to serialize the updated projects list", "Reason", err)
os.Exit(1)
} else {
if err := os.WriteFile(LocWriteUpdatedProjects, v, 0400); err != nil {
slog.Error("Failed to write the updated projects to a file", "Reason", err)
os.Exit(1)
}
}
}

0 comments on commit d28ab12

Please sign in to comment.