Skip to content

Commit

Permalink
feat: added packages resources
Browse files Browse the repository at this point in the history
  • Loading branch information
ArshiaBP committed Dec 6, 2024
1 parent 6bf49e1 commit fe33be1
Show file tree
Hide file tree
Showing 10 changed files with 685 additions and 143 deletions.
141 changes: 0 additions & 141 deletions .github/workflows/build.yaml

This file was deleted.

2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,6 @@ github.com/oleiade/reflections v1.0.1 h1:D1XO3LVEYroYskEsoSiGItp9RUxG6jWnCVvrqH0
github.com/oleiade/reflections v1.0.1/go.mod h1:rdFxbxq4QXVZWj0F+e9jqjDkc7dbp97vkRixKo2JR60=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/opengovern/og-util v1.1.8 h1:Ij/sG074H6KtmkPuaNj1XjiwZvTp3DplFA/MLUzHBK4=
github.com/opengovern/og-util v1.1.8/go.mod h1:dyn8rhmxq59o1jnbgGfmcUvW7iB/eN6OxoTUUx6jEHA=
github.com/opengovern/og-util v1.1.9 h1:Pswy4yOEcUS9FZW/3ThTBNNtRdyhhGdW9oj/GJ5WpwY=
github.com/opengovern/og-util v1.1.9/go.mod h1:dyn8rhmxq59o1jnbgGfmcUvW7iB/eN6OxoTUUx6jEHA=
github.com/opengovern/opencomply v0.475.2 h1:JHs12+mhJb9HzFZYMLLgw079PRa+qLKntugvwWiCVL0=
Expand Down
110 changes: 110 additions & 0 deletions provider/describer/container_package.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package describer

import (
"context"
"encoding/json"
"fmt"
"github.com/opengovern/og-describer-github/pkg/sdk/models"
"github.com/opengovern/og-describer-github/provider/model"
"net/http"
"net/url"
"strconv"
"strings"
)

func GetContainerPackageList(ctx context.Context, githubClient GitHubClient, organizationName string, stream *models.StreamSender) ([]models.Resource, error) {
var packages []model.PackageResponse
var packagesResp model.PackageListResponse
var pagesRemaining = true
page := 1
baseURL := "https://api.github.com/orgs/"
client := http.DefaultClient
var values []models.Resource
for pagesRemaining {
params := url.Values{}
params.Set("package_type", "container")
params.Set("page", strconv.Itoa(page))
params.Set("per_page", "100")
finalURL := fmt.Sprintf("%s%s/packages?%s", baseURL, organizationName, params.Encode())
req, err := http.NewRequest("GET", finalURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", githubClient.Token))
req.Header.Set("X-GitHub-Api-Version", "2022-11-28")
resp, err := client.Do(req)
if err = json.NewDecoder(resp.Body).Decode(&packagesResp); err != nil {
return nil, err
}
packages = append(packages, packagesResp.Items...)
linkHeader := resp.Header.Get("Link")
pagesRemaining = strings.Contains(linkHeader, `rel="next"`)
if pagesRemaining {
page++
break
}
}
for _, packageData := range packages {
value := models.Resource{
ID: strconv.Itoa(packageData.ID),
Name: packageData.Name,
Description: JSONAllFieldsMarshaller{
Value: model.PackageDescription{
ID: strconv.Itoa(packageData.ID),
Name: packageData.Name,
URL: packageData.URL,
CreatedAt: packageData.CreatedAt,
UpdatedAt: packageData.UpdatedAt,
},
},
}
if stream != nil {
if err := (*stream)(value); err != nil {
return nil, err
}
} else {
values = append(values, value)
}
}

return values, nil
}

func GetContainerPackage(ctx context.Context, githubClient GitHubClient, organizationName string, repositoryName string, resourceID string, stream *models.StreamSender) (*models.Resource, error) {
var packageData model.PackageResponse
baseURL := "https://api.github.com/orgs/"
client := http.DefaultClient
finalURL := fmt.Sprintf("%s%s/packages/%s/%s", baseURL, organizationName, "container", resourceID)
req, err := http.NewRequest("GET", finalURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", githubClient.Token))
req.Header.Set("X-GitHub-Api-Version", "2022-11-28")
resp, err := client.Do(req)
if err = json.NewDecoder(resp.Body).Decode(&packageData); err != nil {
return nil, err
}
value := models.Resource{
ID: strconv.Itoa(packageData.ID),
Name: packageData.Name,
Description: JSONAllFieldsMarshaller{
Value: model.PackageDescription{
ID: strconv.Itoa(packageData.ID),
Name: packageData.Name,
URL: packageData.URL,
CreatedAt: packageData.CreatedAt,
UpdatedAt: packageData.UpdatedAt,
},
},
}
if stream != nil {
if err := (*stream)(value); err != nil {
return nil, err
}
}

return &value, nil
}
110 changes: 110 additions & 0 deletions provider/describer/maven_package.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package describer

import (
"context"
"encoding/json"
"fmt"
"github.com/opengovern/og-describer-github/pkg/sdk/models"
"github.com/opengovern/og-describer-github/provider/model"
"net/http"
"net/url"
"strconv"
"strings"
)

func GetMavenPackageList(ctx context.Context, githubClient GitHubClient, organizationName string, stream *models.StreamSender) ([]models.Resource, error) {
var packages []model.PackageResponse
var packagesResp model.PackageListResponse
var pagesRemaining = true
page := 1
baseURL := "https://api.github.com/orgs/"
client := http.DefaultClient
var values []models.Resource
for pagesRemaining {
params := url.Values{}
params.Set("package_type", "maven")
params.Set("page", strconv.Itoa(page))
params.Set("per_page", "100")
finalURL := fmt.Sprintf("%s%s/packages?%s", baseURL, organizationName, params.Encode())
req, err := http.NewRequest("GET", finalURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", githubClient.Token))
req.Header.Set("X-GitHub-Api-Version", "2022-11-28")
resp, err := client.Do(req)
if err = json.NewDecoder(resp.Body).Decode(&packagesResp); err != nil {
return nil, err
}
packages = append(packages, packagesResp.Items...)
linkHeader := resp.Header.Get("Link")
pagesRemaining = strings.Contains(linkHeader, `rel="next"`)
if pagesRemaining {
page++
break
}
}
for _, packageData := range packages {
value := models.Resource{
ID: strconv.Itoa(packageData.ID),
Name: packageData.Name,
Description: JSONAllFieldsMarshaller{
Value: model.PackageDescription{
ID: strconv.Itoa(packageData.ID),
Name: packageData.Name,
URL: packageData.URL,
CreatedAt: packageData.CreatedAt,
UpdatedAt: packageData.UpdatedAt,
},
},
}
if stream != nil {
if err := (*stream)(value); err != nil {
return nil, err
}
} else {
values = append(values, value)
}
}

return values, nil
}

func GetMavenPackage(ctx context.Context, githubClient GitHubClient, organizationName string, repositoryName string, resourceID string, stream *models.StreamSender) (*models.Resource, error) {
var packageData model.PackageResponse
baseURL := "https://api.github.com/orgs/"
client := http.DefaultClient
finalURL := fmt.Sprintf("%s%s/packages/%s/%s", baseURL, organizationName, "maven", resourceID)
req, err := http.NewRequest("GET", finalURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", githubClient.Token))
req.Header.Set("X-GitHub-Api-Version", "2022-11-28")
resp, err := client.Do(req)
if err = json.NewDecoder(resp.Body).Decode(&packageData); err != nil {
return nil, err
}
value := models.Resource{
ID: strconv.Itoa(packageData.ID),
Name: packageData.Name,
Description: JSONAllFieldsMarshaller{
Value: model.PackageDescription{
ID: strconv.Itoa(packageData.ID),
Name: packageData.Name,
URL: packageData.URL,
CreatedAt: packageData.CreatedAt,
UpdatedAt: packageData.UpdatedAt,
},
},
}
if stream != nil {
if err := (*stream)(value); err != nil {
return nil, err
}
}

return &value, nil
}
Loading

0 comments on commit fe33be1

Please sign in to comment.