Skip to content

Commit

Permalink
feat: added parameters for workflow, dockerfile, repository and conta…
Browse files Browse the repository at this point in the history
…iner
  • Loading branch information
ArshiaBP committed Dec 22, 2024
1 parent c811992 commit 793fed3
Show file tree
Hide file tree
Showing 7 changed files with 287 additions and 97 deletions.
34 changes: 14 additions & 20 deletions pkg/sdk/es/resources_clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -7284,16 +7284,13 @@ func (p ArtifactDockerFilePaginator) NextPage(ctx context.Context) ([]ArtifactDo
}

var listArtifactDockerFileFilters = map[string]string{
"dockerfile_content": "Description.DockerfileContent",
"dockerfile_content_base64": "Description.DockerfileContentBase64",
"git_url": "Description.GitURL",
"html_url": "Description.HTMLURL",
"last_updated_at": "Description.LastUpdatedAt",
"name": "Description.Name",
"path": "Description.Path",
"repository": "Description.Repository",
"sha": "Description.Sha",
"uri": "Description.URI",
"git_url": "Description.GitURL",
"html_url": "Description.HTMLURL",
"name": "Description.Name",
"path": "Description.Path",
"repository": "Description.Repository",
"sha": "Description.Sha",
"uri": "Description.URI",
}

func ListArtifactDockerFile(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
Expand Down Expand Up @@ -7357,16 +7354,13 @@ func ListArtifactDockerFile(ctx context.Context, d *plugin.QueryData, _ *plugin.
}

var getArtifactDockerFileFilters = map[string]string{
"dockerfile_content": "Description.DockerfileContent",
"dockerfile_content_base64": "Description.DockerfileContentBase64",
"git_url": "Description.GitURL",
"html_url": "Description.HTMLURL",
"last_updated_at": "Description.LastUpdatedAt",
"name": "Description.Name",
"path": "Description.Path",
"repository": "Description.Repository",
"sha": "Description.Sha",
"uri": "Description.URI",
"git_url": "Description.GitURL",
"html_url": "Description.HTMLURL",
"name": "Description.Name",
"path": "Description.Path",
"repository": "Description.Repository",
"sha": "Description.Sha",
"uri": "Description.URI",
}

func GetArtifactDockerFile(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
Expand Down
78 changes: 67 additions & 11 deletions provider/describer/action_workflow_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,84 @@ func GetAllWorkflowRuns(ctx context.Context, githubClient GitHubClient, organiza
if err != nil {
return nil, fmt.Errorf("error fetching repositories for workflow runs: %w", err)
}
log.Println(repositories)

sdk := newResilientSDK(githubClient.Token)

var values []models.Resource
for _, repo := range repositories {
// repo.Name should be the repository name field from the returned resources
repoValues, err := GetRepositoryWorkflowRuns(ctx, sdk, stream, organizationName, repo.Name)
if err != nil {
return nil, err
org := ctx.Value("organization")
orgName := org.(string)
if orgName != "" {
organizationName = orgName
}

repo := ctx.Value("repository")
repoName := repo.(string)

runNumberParam := ctx.Value("run_number")
runNumber := runNumberParam.(string)

if runNumber != "" {
runNumbers := parseRunNumberFlag(runNumber)

if repoName != "" {
var values []models.Resource
repoValues, err := GetRepositoryWorkflowRuns(ctx, sdk, stream, organizationName, repoName, runNumbers)
if err != nil {
return nil, err
}
values = append(values, repoValues...)
return values, nil
} else {
var values []models.Resource
for _, repo := range repositories {
// repo.Name should be the repository name field from the returned resources
repoValues, err := GetRepositoryWorkflowRuns(ctx, sdk, stream, organizationName, repo.Name, runNumbers)
if err != nil {
return nil, err
}
values = append(values, repoValues...)
}
return values, nil
}
} else {
if repoName != "" {
var values []models.Resource
repoValues, err := GetRepositoryWorkflowRuns(ctx, sdk, stream, organizationName, repoName, nil)
if err != nil {
return nil, err
}
values = append(values, repoValues...)
return values, nil
} else {
var values []models.Resource
for _, repo := range repositories {
// repo.Name should be the repository name field from the returned resources
repoValues, err := GetRepositoryWorkflowRuns(ctx, sdk, stream, organizationName, repo.Name, nil)
if err != nil {
return nil, err
}
values = append(values, repoValues...)
}
return values, nil
}
values = append(values, repoValues...)
}
return values, nil
}

func GetRepositoryWorkflowRuns(ctx context.Context, sdk *resilientbridge.ResilientBridge, stream *models.StreamSender, owner, repo string) ([]models.Resource, error) {
func GetRepositoryWorkflowRuns(ctx context.Context, sdk *resilientbridge.ResilientBridge, stream *models.StreamSender, owner, repo string, runNumbers []runNumberCriterion) ([]models.Resource, error) {
maxRuns := 50

runs, err := fetchWorkflowRuns(sdk, owner, repo, "", maxRuns)
if err != nil {
log.Fatalf("Error fetching workflow runs: %v", err)
return nil, fmt.Errorf("error fetching workflow runs: %v", err)
}

if runNumbers != nil {
if len(runNumbers) > 0 {
runs = filterRunsByNumber(runs, runNumbers)
if len(runs) == 0 {
log.Println("No runs found matching the specified run_number criteria.")
return []models.Resource{}, nil
}
}
}

var values []models.Resource
Expand Down
205 changes: 166 additions & 39 deletions provider/describer/artifact_dockerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ func ListArtifactDockerFiles(
organizationName string,
stream *models.StreamSender,
) ([]models.Resource, error) {

repositories, err := getRepositories(ctx, githubClient.RestClient, organizationName)
if err != nil {
return nil, fmt.Errorf("error fetching repositories for org %s: %w", organizationName, err)
}

sdk := resilientbridge.NewResilientBridge()
sdk.SetDebug(false)
sdk.RegisterProvider("github", adapters.NewGitHubAdapter(githubClient.Token), &resilientbridge.ProviderConfig{
Expand All @@ -42,6 +36,24 @@ func ListArtifactDockerFiles(
BaseBackoff: time.Second,
})

org := ctx.Value("organization")
orgName := org.(string)
if orgName != "" {
organizationName = orgName
}

repo := ctx.Value("repository")
repoName := repo.(string)
if repoName != "" {
repoFullName := fmt.Sprintf("%s/%s", organizationName, repoName)
return fetchRepositoryDockerfiles(sdk, repoFullName, stream)
}

repositories, err := getRepositories(ctx, githubClient.RestClient, organizationName)
if err != nil {
return nil, fmt.Errorf("error fetching repositories for org %s: %w", organizationName, err)
}

var allValues []models.Resource
totalCollected := 0
perPage := 100
Expand Down Expand Up @@ -103,10 +115,23 @@ func ListArtifactDockerFiles(
ID: dockerfileURI,
Name: item.Name,
Description: JSONAllFieldsMarshaller{
Value: map[string]interface{}{
"repo_full_name": item.Repository.FullName,
"path": item.Path,
"sha": item.Sha,
Value: model.ArtifactDockerFileDescription{
Sha: item.Sha,
Name: item.Name,
Path: item.Path,
GitURL: item.GitURL,
HTMLURL: item.HTMLURL,
URI: dockerfileURI,
Repository: map[string]interface{}{
"id": item.Repository.ID,
"node_id": item.Repository.NodeID,
"full_name": item.Repository.FullName,
"description": item.Repository.Description,
"html_url": item.Repository.HTMLURL,
"fork": item.Repository.Fork,
"private": item.Repository.Private,
"owner_login": item.Repository.Owner.Login,
},
},
},
}
Expand Down Expand Up @@ -143,6 +168,108 @@ func ListArtifactDockerFiles(
return allValues, nil
}

func fetchRepositoryDockerfiles(sdk *resilientbridge.ResilientBridge, repoFullName string, stream *models.StreamSender) ([]models.Resource, error) {
var allValues []models.Resource
totalCollected := 0
perPage := 100

queryParts := []string{
fmt.Sprintf("repo:%s", repoFullName),
"filename:Dockerfile",
}
finalQuery := strings.Join(queryParts, " ")

page := 1
for totalCollected < MAX_RESULTS {
q := url.QueryEscape(finalQuery)
searchEndpoint := fmt.Sprintf("/search/code?q=%s&per_page=%d&page=%d", q, perPage, page)

searchReq := &resilientbridge.NormalizedRequest{
Method: "GET",
Endpoint: searchEndpoint,
Headers: map[string]string{"Accept": "application/vnd.github+json"},
}

searchResp, err := sdk.Request("github", searchReq)
if err != nil {
log.Printf("Error searching code in %s: %v\n", repoFullName, err)
break
}

if searchResp.StatusCode >= 400 {
log.Printf("HTTP error %d searching code in %s: %s\n", searchResp.StatusCode, repoFullName, string(searchResp.Data))
break
}

var result model.CodeSearchResult
if err := json.Unmarshal(searchResp.Data, &result); err != nil {
log.Printf("Error parsing code search response for %s: %v\n", repoFullName, err)
break
}

// If no items returned, no more results
if len(result.Items) == 0 {
break
}

for _, item := range result.Items {
// Use item.Sha to link to a specific commit version of the file
dockerfileURI := fmt.Sprintf("https://github.com/%s/blob/%s/%s",
item.Repository.FullName,
item.Sha,
item.Path)

resource := models.Resource{
ID: dockerfileURI,
Name: item.Name,
Description: JSONAllFieldsMarshaller{
Value: model.ArtifactDockerFileDescription{
Sha: item.Sha,
Name: item.Name,
Path: item.Path,
GitURL: item.GitURL,
HTMLURL: item.HTMLURL,
URI: dockerfileURI,
Repository: map[string]interface{}{
"id": item.Repository.ID,
"node_id": item.Repository.NodeID,
"full_name": item.Repository.FullName,
"description": item.Repository.Description,
"html_url": item.Repository.HTMLURL,
"fork": item.Repository.Fork,
"private": item.Repository.Private,
"owner_login": item.Repository.Owner.Login,
},
},
},
}

totalCollected++
if stream != nil {
// Stream the resource
if err := (*stream)(resource); err != nil {
return nil, fmt.Errorf("error streaming resource: %w", err)
}
} else {
// Accumulate to return later
allValues = append(allValues, resource)
}

if totalCollected >= MAX_RESULTS {
break
}
}

if len(result.Items) < perPage {
// Fewer than perPage results means no more pages
break
}
page++
}

return allValues, nil
}

// GetDockerfile fetches the details and content of a single Dockerfile given the repo and file path.
// It returns a fully populated resource with Dockerfile content, line count checks, and last updated at info.
func GetDockerfile(ctx context.Context, githubClient GitHubClient, organizationName, repoFullName, filePath string, stream *models.StreamSender) (*models.Resource, error) {
Expand Down Expand Up @@ -194,41 +321,41 @@ func GetDockerfile(ctx context.Context, githubClient GitHubClient, organizationN
}

// Fetch last_updated_at via commits API
var lastUpdatedAt string
commitsEndpoint := fmt.Sprintf("/repos/%s/commits?path=%s&per_page=1", repoFullName, url.QueryEscape(filePath))
commitReq := &resilientbridge.NormalizedRequest{
Method: "GET",
Endpoint: commitsEndpoint,
Headers: map[string]string{"Accept": "application/vnd.github+json"},
}

commitResp, err := sdk.Request("github", commitReq)
if err == nil && commitResp.StatusCode < 400 {
var commits []model.CommitResponse
if json.Unmarshal(commitResp.Data, &commits) == nil && len(commits) > 0 {
if commits[0].Commit.Author.Date != "" {
lastUpdatedAt = commits[0].Commit.Author.Date
} else if commits[0].Commit.Committer.Date != "" {
lastUpdatedAt = commits[0].Commit.Committer.Date
}
}
}
//var lastUpdatedAt string
//commitsEndpoint := fmt.Sprintf("/repos/%s/commits?path=%s&per_page=1", repoFullName, url.QueryEscape(filePath))
//commitReq := &resilientbridge.NormalizedRequest{
// Method: "GET",
// Endpoint: commitsEndpoint,
// Headers: map[string]string{"Accept": "application/vnd.github+json"},
//}

//commitResp, err := sdk.Request("github", commitReq)
//if err == nil && commitResp.StatusCode < 400 {
// var commits []model.CommitResponse
// if json.Unmarshal(commitResp.Data, &commits) == nil && len(commits) > 0 {
// if commits[0].Commit.Author.Date != "" {
// lastUpdatedAt = commits[0].Commit.Author.Date
// } else if commits[0].Commit.Committer.Date != "" {
// lastUpdatedAt = commits[0].Commit.Committer.Date
// }
// }
//}

repoObj := map[string]interface{}{
"full_name": repoFullName,
}

output := model.ArtifactDockerFileDescription{
Sha: contentData.Sha,
Name: contentData.Name,
Path: contentData.Path,
LastUpdatedAt: lastUpdatedAt,
GitURL: contentData.GitURL,
HTMLURL: contentData.HTMLURL,
URI: contentData.HTMLURL,
DockerfileContent: fileContent,
DockerfileContentBase64: contentData.Content,
Repository: repoObj,
Sha: contentData.Sha,
Name: contentData.Name,
Path: contentData.Path,
//LastUpdatedAt: lastUpdatedAt,
GitURL: contentData.GitURL,
HTMLURL: contentData.HTMLURL,
URI: contentData.HTMLURL,
//DockerfileContent: fileContent,
//DockerfileContentBase64: contentData.Content,
Repository: repoObj,
}

value := models.Resource{
Expand Down
Loading

0 comments on commit 793fed3

Please sign in to comment.