Skip to content

Commit

Permalink
created a interface for any other storage driver later
Browse files Browse the repository at this point in the history
Signed-off-by: Dipankar Das <[email protected]>
  • Loading branch information
dipankardas011 committed Jun 11, 2024
1 parent 4da30f6 commit 5a43d62
Showing 1 changed file with 48 additions and 29 deletions.
77 changes: 48 additions & 29 deletions scripts/repo-variable-writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,21 @@ type Projects struct {
Projects []Project `json:"projects"`
}

type StatePersistentFactory interface {
Write(projectName, variableValue string) error
Read(projectName string) (*string, error)
}

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

func toPtr[T any](v T) *T {
return &v
}

func genUrlAndHeaders(proj string) (*string, map[string]string, error) {
v, ok := os.LookupEnv(EnvGithubPat)
if !ok || len(v) == 0 {
return nil, nil, fmt.Errorf("the environment variable for github pat is missing")
}
variableURI := strings.ToLower(proj) + "_version"
url := fmt.Sprintf(
"https://api.github.com/repos/%s/actions/variables/%s",
FullRepoName,
variableURI,
)

return toPtr[string](url),
map[string]string{
"Accept": "application/vnd.github+json",
"Authorization": "Bearer " + v,
"X-GitHub-Api-Version": "2022-11-28",
}, nil
}

func handleHTTPCall(method string, url string, timeOut time.Duration,
body io.Reader, headers map[string]string) (*http.Response, error) {

Expand Down Expand Up @@ -114,9 +98,38 @@ func fetchLatestRelease(org, proj string) (*string, error) {
return toPtr[string](release.TagName), nil
}

// TODO: Create a interface for the Project Variable state store
func writeProjectVariable(proj, value string) error {
url, header, err := genUrlAndHeaders(proj)
type GithubVariable struct {
gh_token string
}

func NewGithubVariable() (*GithubVariable, error) {
const EnvGithubPat string = "GH_TOKEN"

v, ok := os.LookupEnv(EnvGithubPat)
if !ok || len(v) == 0 {
return nil, fmt.Errorf("the environment variable for github pat is missing")
}
return &GithubVariable{gh_token: v}, 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,
variableURI,
)

return toPtr[string](url),
map[string]string{
"Accept": "application/vnd.github+json",
"Authorization": "Bearer " + obj.gh_token,
"X-GitHub-Api-Version": "2022-11-28",
}, nil
}

func (obj *GithubVariable) Write(proj, value string) error {
url, header, err := obj.genUrlAndHeaders(proj)
if err != nil {
return err
}
Expand Down Expand Up @@ -156,9 +169,8 @@ func writeProjectVariable(proj, value string) error {
return nil
}

// TODO: Create a interface for the Project Variable state store
func readProjectVariable(proj string) (*string, error) {
url, header, err := genUrlAndHeaders(proj)
func (obj *GithubVariable) Read(proj string) (*string, error) {
url, header, err := obj.genUrlAndHeaders(proj)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -198,6 +210,13 @@ func handleProjectsUpdater() {
}
slog.Info("Got projects metadata", "Projects", projects)

var storage StatePersistentFactory
storage, err = NewGithubVariable()
if err != nil {
slog.Error("Failed: initialize storage", "Reason", err)
os.Exit(1)
}

for _, project := range projects.Projects {
latestRelease, err := fetchLatestRelease(
project.Organization,
Expand All @@ -208,15 +227,15 @@ func handleProjectsUpdater() {
os.Exit(1)
}

storedRelease, err := readProjectVariable(project.Name)
storedRelease, err := storage.Read(project.Name)
if err != nil {
slog.Error("Failed: readProjectVariable", "Reason", err)
os.Exit(1)
}

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

0 comments on commit 5a43d62

Please sign in to comment.