From 15fe58b28dc7ce749d163fedfac022e9f3141dbd Mon Sep 17 00:00:00 2001 From: Tomasz Barwicki Date: Thu, 11 Jul 2024 15:41:12 +0200 Subject: [PATCH 1/7] chore: add reading openapi spec from defined location --- src/api-collector/main.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/api-collector/main.go b/src/api-collector/main.go index 677afbce..da7a3e61 100644 --- a/src/api-collector/main.go +++ b/src/api-collector/main.go @@ -35,8 +35,10 @@ import ( "gopkg.in/yaml.v2" ) -const API_DOCS_REPO = "api-hub" - +const ( + API_DOCS_REPO = "api-hub" + API_SPEC_PATH = "/docs/api/openAPI.yaml" +) type OpenAPIInfo struct { Version string `yaml:"version"` Title string `yaml:"title"` @@ -198,3 +200,13 @@ func downloadAPISpecs(repo string, specsUrls []string) []string { return downloadedSpecs } +func getAPISpecFromDir(ctx context.Context, client *github.Client, owner string, repo string) ([]byte, error) { + apiContent, _, _, err := client.Repositories.GetContents(ctx, owner, repo, API_SPEC_PATH, &github.RepositoryContentGetOptions{ + Ref: "main", + }) + if err != nil { + return []byte{}, fmt.Errorf("error getting file content: %v", err) + } + content, err := apiContent.GetContent() + return []byte(content), err +} From 9326052ebb09128bd075dd3a07fb10e9c7c55dee Mon Sep 17 00:00:00 2001 From: Tomasz Barwicki Date: Thu, 11 Jul 2024 15:43:34 +0200 Subject: [PATCH 2/7] chore: separate function for reading openapi spec from url --- src/api-collector/main.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/api-collector/main.go b/src/api-collector/main.go index da7a3e61..02d11f3a 100644 --- a/src/api-collector/main.go +++ b/src/api-collector/main.go @@ -210,3 +210,21 @@ func getAPISpecFromDir(ctx context.Context, client *github.Client, owner string, content, err := apiContent.GetContent() return []byte(content), err } + +func getAPISpecFromUrl(url string) ([]byte, error) { + resp, err := http.Get(url) + if err != nil { + return []byte{}, fmt.Errorf("error downloading API spec file: %v", err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return []byte{}, fmt.Errorf("bad HTTP status: %s", resp.Status) + + } + content, err := io.ReadAll(resp.Body) + if err != nil { + return []byte{}, fmt.Errorf("error reading HTTP response: %v", err) + } + return content, nil +} From 4bedb000cd5dcb7d015b8bb6a1d214203f7ddb51 Mon Sep 17 00:00:00 2001 From: Tomasz Barwicki Date: Thu, 11 Jul 2024 15:44:54 +0200 Subject: [PATCH 3/7] chore: detach saving api spec --- src/api-collector/main.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/api-collector/main.go b/src/api-collector/main.go index 02d11f3a..04425373 100644 --- a/src/api-collector/main.go +++ b/src/api-collector/main.go @@ -228,3 +228,22 @@ func getAPISpecFromUrl(url string) ([]byte, error) { } return content, nil } + +func saveAPISpec(content []byte, repo string) (string, error) { + var spec OpenAPISpec + err := yaml.Unmarshal([]byte(content), &spec) + if err != nil { + return "", fmt.Errorf("error parsing OpenAPI spec yaml format: %v", err) + } + dirPath := path.Join("docs", repo, spec.Info.Version) + err = os.MkdirAll(dirPath, os.ModePerm) + if err != nil { + return "", fmt.Errorf("error creating directory: %v", err) + } + filePath := path.Join(dirPath, "openAPI.yaml") + err = os.WriteFile(filePath, content, 0644) + if err != nil { + return "", fmt.Errorf("error saving OpenAPI spec content to file: %v", err) + } + return filePath, nil +} From 8569116b5b8d3f5e2762aa0ca69e35a46f1e8d0d Mon Sep 17 00:00:00 2001 From: Tomasz Barwicki Date: Fri, 12 Jul 2024 10:01:32 +0200 Subject: [PATCH 4/7] chore: refactor download function --- src/api-collector/main.go | 53 +++++++++++++++------------------------ 1 file changed, 20 insertions(+), 33 deletions(-) diff --git a/src/api-collector/main.go b/src/api-collector/main.go index 04425373..f2126bd8 100644 --- a/src/api-collector/main.go +++ b/src/api-collector/main.go @@ -155,47 +155,34 @@ func getAPISpecsUrls(ctx context.Context, client *github.Client, owner string, r return m.OpenApiSpecs, nil } -func downloadAPISpecs(repo string, specsUrls []string) []string { - var downloadedSpecs []string - for _, url := range specsUrls { - resp, err := http.Get(url) +func downloadAPISpecs(ctx context.Context, client *github.Client, owner string, repo string) []string { + content, err := getAPISpecFromDir(ctx, client, owner, repo) + if err == nil { + specPath, err := saveAPISpec(content, repo) if err != nil { - log.Printf("Error downloading API spec file: %v\n", err) - continue - } - defer resp.Body.Close() - - if resp.StatusCode != http.StatusOK { - log.Printf("Bad HTTP status: %s\n", resp.Status) - continue - } - content, err := io.ReadAll(resp.Body) - if err != nil { - log.Printf("Error reading http response: %v\n", err) - continue + log.Fatalf("%v\n", err) } - var spec OpenAPISpec - err = yaml.Unmarshal(content, &spec) - if err != nil { - log.Printf("Error parsing OpenAPI spec yaml format: %s\n", err) - continue - } - dirPath := path.Join("docs", repo, spec.Info.Version) - err = os.MkdirAll(dirPath, os.ModePerm) + return []string{specPath} + } + log.Printf("OpenAPI specs not found in the default location docs/api/openAPI.yaml. Proceeding with .tractusx metadata.") + var downloadedSpecs []string + specsUrls, err := getAPISpecsUrls(ctx, client, owner, repo) + if err != nil { + log.Printf("%v\n", err) + } + for _, url := range specsUrls { + specContent, err := getAPISpecFromUrl(url) if err != nil { - log.Printf("Error creating directory: %s\n", err) + log.Printf("%v\n",err) continue } - urlSplit := strings.Split(url, "/") - specName := urlSplit[len(urlSplit)-1] - filePath := path.Join(dirPath, specName) - err = os.WriteFile(filePath, content, 0644) + specPath, err := saveAPISpec(specContent, repo) if err != nil { - log.Printf("Error saving OpenAPI spec content to file: %s\n", err) + log.Printf("%v\n",err) continue } - downloadedSpecs = append(downloadedSpecs, filePath) - log.Printf("OpenAPI spec %s downloaded successfully\n", specName) + downloadedSpecs = append(downloadedSpecs, specPath) + log.Printf("OpenAPI spec saved successfully\n", specPath) } return downloadedSpecs } From 842d98a634a7a3efcc7fefb65933f9a29dab3644 Mon Sep 17 00:00:00 2001 From: Tomasz Barwicki Date: Fri, 12 Jul 2024 10:05:23 +0200 Subject: [PATCH 5/7] chore: amend main --- src/api-collector/main.go | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/api-collector/main.go b/src/api-collector/main.go index f2126bd8..cf4974f6 100644 --- a/src/api-collector/main.go +++ b/src/api-collector/main.go @@ -28,7 +28,6 @@ import ( "net/http" "os" "path" - "strings" "github.com/google/go-github/v61/github" "golang.org/x/oauth2" @@ -65,20 +64,14 @@ func main() { } for _, repo := range repos { log.Println("Scanning repo ", *repo.Name) - specsUrls, err := getAPISpecsUrls(ctx, client, owner, *repo.Name) - if err != nil { - log.Println(err) - continue - } - if specsUrls == nil { - log.Println("No OpenAPI specs found") - continue - } - - downloadedSpecs := downloadAPISpecs(*repo.Name, specsUrls) - log.Println("List of downloaded OpenAPI specs:") - for _, downloadedSpec := range downloadedSpecs { - log.Printf("- %s\n",downloadedSpec) + downloadedSpecs := downloadAPISpecs(ctx, client, owner, *repo.Name) + if len(downloadedSpecs) > 0 { + log.Println("List of downloaded OpenAPI specs:") + for _, downloadedSpec := range downloadedSpecs { + log.Printf("- %s\n",downloadedSpec) + } + } else { + log.Printf("No OpenAPI specs found in .tractusx metadata.") } } } From 5cd385dff12ff98a33408ccca8edd799bc90aa87 Mon Sep 17 00:00:00 2001 From: Tomasz Barwicki Date: Fri, 12 Jul 2024 10:19:27 +0200 Subject: [PATCH 6/7] chore: function rename --- src/api-collector/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api-collector/main.go b/src/api-collector/main.go index cf4974f6..7643c1c2 100644 --- a/src/api-collector/main.go +++ b/src/api-collector/main.go @@ -130,7 +130,7 @@ func getOrgRepos(ctx context.Context, gitOwner string, client *github.Client) ([ return allRepos, nil } -func getAPISpecsUrls(ctx context.Context, client *github.Client, owner string, repo string) ([]string, error) { +func getAPISpecsUrlsFromMetadata(ctx context.Context, client *github.Client, owner string, repo string) ([]string, error) { metadataFile, _, _, err := client.Repositories.GetContents(ctx, owner, repo, MetadataFilename, &github.RepositoryContentGetOptions{ Ref: "main", }) @@ -159,7 +159,7 @@ func downloadAPISpecs(ctx context.Context, client *github.Client, owner string, } log.Printf("OpenAPI specs not found in the default location docs/api/openAPI.yaml. Proceeding with .tractusx metadata.") var downloadedSpecs []string - specsUrls, err := getAPISpecsUrls(ctx, client, owner, repo) + specsUrls, err := getAPISpecsUrlsFromMetadata(ctx, client, owner, repo) if err != nil { log.Printf("%v\n", err) } From 343b66f1958b4d5ba49bc5007cb6d469f9a69e2b Mon Sep 17 00:00:00 2001 From: Tomasz Barwicki Date: Mon, 15 Jul 2024 10:30:27 +0200 Subject: [PATCH 7/7] chore: revert api checs and getAPISpecFromDir removal --- src/api-collector/main.go | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/src/api-collector/main.go b/src/api-collector/main.go index 7643c1c2..77a9f82d 100644 --- a/src/api-collector/main.go +++ b/src/api-collector/main.go @@ -149,20 +149,14 @@ func getAPISpecsUrlsFromMetadata(ctx context.Context, client *github.Client, own } func downloadAPISpecs(ctx context.Context, client *github.Client, owner string, repo string) []string { - content, err := getAPISpecFromDir(ctx, client, owner, repo) - if err == nil { - specPath, err := saveAPISpec(content, repo) - if err != nil { - log.Fatalf("%v\n", err) - } - return []string{specPath} - } - log.Printf("OpenAPI specs not found in the default location docs/api/openAPI.yaml. Proceeding with .tractusx metadata.") var downloadedSpecs []string specsUrls, err := getAPISpecsUrlsFromMetadata(ctx, client, owner, repo) if err != nil { log.Printf("%v\n", err) } + if len(specsUrls) == 0 { + specsUrls = append(specsUrls, fmt.Sprintf("https://raw.githubusercontent.com/%s/%s/main/%s", owner, repo, API_SPEC_PATH)) + } for _, url := range specsUrls { specContent, err := getAPISpecFromUrl(url) if err != nil { @@ -180,17 +174,6 @@ func downloadAPISpecs(ctx context.Context, client *github.Client, owner string, return downloadedSpecs } -func getAPISpecFromDir(ctx context.Context, client *github.Client, owner string, repo string) ([]byte, error) { - apiContent, _, _, err := client.Repositories.GetContents(ctx, owner, repo, API_SPEC_PATH, &github.RepositoryContentGetOptions{ - Ref: "main", - }) - if err != nil { - return []byte{}, fmt.Errorf("error getting file content: %v", err) - } - content, err := apiContent.GetContent() - return []byte(content), err -} - func getAPISpecFromUrl(url string) ([]byte, error) { resp, err := http.Get(url) if err != nil {