Skip to content

Commit

Permalink
set common logic in func
Browse files Browse the repository at this point in the history
  • Loading branch information
motatoes committed Jan 9, 2025
1 parent 72cb2fb commit 0f9b7cf
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
3 changes: 1 addition & 2 deletions cli/pkg/digger/digger.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ func RunJobs(jobs []orchestrator.Job, prService ci.PullRequestService, orgServic
}

currentJob := jobs[0]
repoNameForBackendReporting := strings.ReplaceAll(currentJob.Namespace, "/", "-")
projectNameForBackendReporting := currentJob.ProjectName
// TODO: handle the apply result summary as well to report it to backend. Possibly reporting changed resources as well
// Some kind of generic terraform operation summary might need to be introduced
Expand All @@ -143,7 +142,7 @@ func RunJobs(jobs []orchestrator.Job, prService ci.PullRequestService, orgServic
prNumber := *currentJob.PullRequestNumber

iacUtils := iac_utils.GetIacUtilsIacType(currentJob.IacType())
batchResult, err := backendApi.ReportProjectJobStatus(repoNameForBackendReporting, projectNameForBackendReporting, jobId, "succeeded", time.Now(), &summary, "", jobPrCommentUrl, terraformOutput, iacUtils)
batchResult, err := backendApi.ReportProjectJobStatus(currentJob.Namespace, projectNameForBackendReporting, jobId, "succeeded", time.Now(), &summary, "", jobPrCommentUrl, terraformOutput, iacUtils)
if err != nil {
log.Printf("error reporting Job status: %v.\n", err)
return false, false, fmt.Errorf("error while running command: %v", err)
Expand Down
9 changes: 3 additions & 6 deletions cli/pkg/spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ import (
"log"
"os"
"os/exec"
"strings"
"time"
)

func reportError(spec spec.Spec, backendApi backend2.Api, message string, err error) {
log.Printf(message)
repoNameForBackendReporting := strings.ReplaceAll(spec.VCS.RepoName, "/", "-")
_, reportingError := backendApi.ReportProjectJobStatus(repoNameForBackendReporting, spec.Job.ProjectName, spec.JobId, "failed", time.Now(), nil, "", "", "", nil)
_, reportingError := backendApi.ReportProjectJobStatus(spec.VCS.RepoName, spec.Job.ProjectName, spec.JobId, "failed", time.Now(), nil, "", "", "", nil)
if reportingError != nil {
usage.ReportErrorAndExit(spec.VCS.RepoOwner, fmt.Sprintf("Failed to run commands. %v", err), 5)
}
Expand Down Expand Up @@ -139,8 +137,7 @@ func RunSpec(

jobs := []scheduler.Job{job}

repoNameForBackendReporting := strings.ReplaceAll(spec.VCS.RepoName, "/", "-")
_, err = backendApi.ReportProjectJobStatus(repoNameForBackendReporting, spec.Job.ProjectName, spec.JobId, "started", time.Now(), nil, "", "", "", nil)
_, err = backendApi.ReportProjectJobStatus(spec.VCS.RepoName, spec.Job.ProjectName, spec.JobId, "started", time.Now(), nil, "", "", "", nil)
if err != nil {
message := fmt.Sprintf("Failed to report jobSpec status to backend. Exiting. %v", err)
reportError(spec, backendApi, message, err)
Expand All @@ -161,7 +158,7 @@ func RunSpec(
reportTerraformOutput := spec.Reporter.ReportTerraformOutput
allAppliesSuccess, _, err := digger.RunJobs(jobs, prService, orgService, lock, reporter, planStorage, policyChecker, commentUpdater, backendApi, spec.JobId, true, reportTerraformOutput, commentId, currentDir)
if !allAppliesSuccess || err != nil {
serializedBatch, reportingError := backendApi.ReportProjectJobStatus(repoNameForBackendReporting, spec.Job.ProjectName, spec.JobId, "failed", time.Now(), nil, "", "", "", nil)
serializedBatch, reportingError := backendApi.ReportProjectJobStatus(spec.VCS.RepoName, spec.Job.ProjectName, spec.JobId, "failed", time.Now(), nil, "", "", "", nil)
if reportingError != nil {
message := fmt.Sprintf("Failed run commands. %v", err)
reportError(spec, backendApi, message, err)
Expand Down
6 changes: 4 additions & 2 deletions libs/backendapi/diggerapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"os"
"path"
"path/filepath"
"strings"
"time"
)

Expand Down Expand Up @@ -129,7 +130,8 @@ func (d DiggerApi) ReportProjectRun(namespace string, projectName string, starte
return nil
}

func (d DiggerApi) ReportProjectJobStatus(repo string, projectName string, jobId string, status string, timestamp time.Time, summary *iac_utils.IacSummary, planJson string, PrCommentUrl string, terraformOutput string, iacUtils iac_utils.IacUtils) (*scheduler.SerializedBatch, error) {
func (d DiggerApi) ReportProjectJobStatus(repoFullName string, projectName string, jobId string, status string, timestamp time.Time, summary *iac_utils.IacSummary, planJson string, PrCommentUrl string, terraformOutput string, iacUtils iac_utils.IacUtils) (*scheduler.SerializedBatch, error) {
repoNameForBackendReporting := strings.ReplaceAll(repoFullName, "/", "-")
u, err := url.Parse(d.DiggerHost)
if err != nil {
log.Fatalf("Not able to parse digger cloud url: %v", err)
Expand All @@ -152,7 +154,7 @@ func (d DiggerApi) ReportProjectJobStatus(repo string, projectName string, jobId
}
}

u.Path = filepath.Join(u.Path, "repos", repo, "projects", projectName, "jobs", jobId, "set-status")
u.Path = filepath.Join(u.Path, "repos", repoNameForBackendReporting, "projects", projectName, "jobs", jobId, "set-status")
request := map[string]interface{}{
"status": status,
"timestamp": timestamp,
Expand Down

0 comments on commit 0f9b7cf

Please sign in to comment.