Skip to content

Commit 3cb98dc

Browse files
committed
temp commit
1 parent eadcbf7 commit 3cb98dc

File tree

2 files changed

+84
-7
lines changed

2 files changed

+84
-7
lines changed

cli/pkg/spec/spec.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ func RunSpecManualCommand(
200200

201201
commentUpdater := comment_summary.NoopCommentUpdater{}
202202
// TODO: do not require conversion to gh service
203+
log.Printf("<========= DIGGER RUNNING IN MANUAL MODE =========>")
203204
allAppliesSuccess, _, err := digger.RunJobs(jobs, prService, orgService, lock, reporter, planStorage, policyChecker, commentUpdater, backendApi, spec.JobId, false, false, commentId, currentDir)
205+
log.Printf("<========= DIGGER COMPLETED =========>")
204206
if err != nil || allAppliesSuccess == false {
205207
usage.ReportErrorAndExit(spec.VCS.RepoOwner, "Terraform execution failed", 1)
206208
}

dgctl/cmd/exec.go

Lines changed: 82 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ func getRepoFullname() (string, error) {
7676
return repoFullname, nil
7777
}
7878

79+
func GetUrlContents(url string) (string, error) {
80+
resp, err := http.Get(url)
81+
if err != nil {
82+
return "", fmt.Errorf("%v", err)
83+
}
84+
defer resp.Body.Close()
85+
86+
body, err := io.ReadAll(resp.Body)
87+
if err != nil {
88+
return "", fmt.Errorf("%v", err)
89+
}
90+
91+
content := string(body)
92+
return content, nil
93+
}
94+
7995
func GetSpec(diggerUrl string, authToken string, command string, actor string, projectMarshalled string, diggerConfigMarshalled string, repoFullName string, defaultBanch string, prBranch string) ([]byte, error) {
8096
payload := spec.GetSpecPayload{
8197
Command: command,
@@ -132,32 +148,59 @@ func pushToBranch(prBranch string) error {
132148
return err
133149
}
134150

135-
func GetWorkflowIdAndUrlFromDiggerJobId(client *github.Client, repoOwner string, repoName string, diggerJobID string) (*int64, *string, error) {
151+
func GetWorkflowIdAndUrlFromDiggerJobId(client *github.Client, repoOwner string, repoName string, diggerJobID string) (*int64, *int64, *string, error) {
136152
timeFilter := time.Now().Add(-5 * time.Minute)
137153
runs, _, err := client.Actions.ListRepositoryWorkflowRuns(context.Background(), repoOwner, repoName, &github.ListWorkflowRunsOptions{
138154
Created: ">=" + timeFilter.Format(time.RFC3339),
139155
})
140156
if err != nil {
141-
return nil, nil, fmt.Errorf("error listing workflow runs %v", err)
157+
return nil, nil, nil, fmt.Errorf("error listing workflow runs %v", err)
142158
}
143159

144160
for _, workflowRun := range runs.WorkflowRuns {
145161
workflowjobs, _, err := client.Actions.ListWorkflowJobs(context.Background(), repoOwner, repoName, *workflowRun.ID, nil)
146162
if err != nil {
147-
return nil, nil, fmt.Errorf("error listing workflow jobs for run %v %v", workflowRun.ID, err)
163+
return nil, nil, nil, fmt.Errorf("error listing workflow jobs for run %v %v", workflowRun.ID, err)
148164
}
149165

150166
for _, workflowjob := range workflowjobs.Jobs {
151167
for _, step := range workflowjob.Steps {
168+
//log.Printf("workflowRun: %v workflowJob %v workflowjobName: %v workflowStep %v", *workflowRun.ID, *workflowjob.ID, *workflowjob.Name, *step.Name)
169+
152170
if strings.Contains(*step.Name, diggerJobID) {
153171
url := fmt.Sprintf("https://github.com/%v/%v/actions/runs/%v", repoOwner, repoName, *workflowRun.ID)
154-
return workflowRun.ID, &url, nil
172+
return workflowRun.ID, workflowjob.ID, &url, nil
155173
}
156174
}
157175
}
158176

159177
}
160-
return nil, nil, fmt.Errorf("workflow not found")
178+
return nil, nil, nil, fmt.Errorf("workflow not found")
179+
}
180+
181+
func cleanupDiggerOutput(output string) string {
182+
183+
startingDelimeter := "<========= DIGGER RUNNING IN MANUAL MODE =========>"
184+
endingDelimiter := "<========= DIGGER COMPLETED =========>"
185+
186+
startPos := 0
187+
endPos := len(output)
188+
// removes output of terraform -version command that terraform-exec executes on every run
189+
i := strings.Index(output, startingDelimeter)
190+
if i != -1 {
191+
startPos = i
192+
}
193+
194+
e := strings.Index(output, endingDelimiter)
195+
if e != -1 {
196+
endPos = e
197+
}
198+
199+
// This should not happen but in case we get here we avoid slice bounds out of range exception by resetting endPos
200+
if endPos <= startPos {
201+
endPos = len(output)
202+
}
203+
return output[startPos:endPos]
161204
}
162205

163206
// validateCmd represents the validate command
@@ -169,6 +212,12 @@ var execCmd = &cobra.Command{
169212
var execConfig execConfig
170213
viperExec.Unmarshal(&execConfig)
171214
log.Printf("%v - %v ", execConfig.Project, execConfig.Command)
215+
216+
if execConfig.Command != "digger plan" {
217+
log.Printf("ERROR: currently only 'digger plan' supported with exec command")
218+
os.Exit(1)
219+
}
220+
172221
config, _, _, err := digger_config.LoadDiggerConfig("./", true, nil)
173222
if err != nil {
174223
log.Printf("Invalid digger config file: %v. Exiting.", err)
@@ -266,16 +315,42 @@ var execCmd = &cobra.Command{
266315
repoOwner, repoName, _ := strings.Cut(repoFullname, "/")
267316
var logsUrl *string
268317
var runId *int64
318+
var jobId *int64
269319
for {
270-
runId, logsUrl, err = GetWorkflowIdAndUrlFromDiggerJobId(client, repoOwner, repoName, spec.JobId)
320+
runId, jobId, logsUrl, err = GetWorkflowIdAndUrlFromDiggerJobId(client, repoOwner, repoName, spec.JobId)
321+
log.Printf("=============")
271322
if err == nil {
272323
break
273324
}
274325
time.Sleep(time.Second * 1)
275326
}
276327

277-
log.Printf("logs url: %v runId %v", *logsUrl, *runId)
328+
log.Printf("waiting for logs to be available, you can view job in this url: %v runId %v", *logsUrl, *runId)
329+
log.Printf("......")
330+
331+
for {
332+
j, _, err := client.Actions.GetWorkflowJobByID(context.Background(), repoOwner, repoName, *jobId)
333+
if err != nil {
334+
log.Printf("GetWorkflowJobByID error: %v", err)
335+
}
336+
log.Printf("job status: %v", *j.Status)
337+
if *j.Status == "completed" {
338+
break
339+
}
340+
time.Sleep(time.Second * 1)
341+
}
342+
343+
logs, _, err := client.Actions.GetWorkflowJobLogs(context.Background(), repoOwner, repoName, *jobId, 1)
344+
345+
log.Printf("stereaming logs from: %v || %v", logs, err)
346+
logsContent, err := GetUrlContents(logs.String())
347+
348+
if err != nil {
349+
log.Printf("error while fetching logs: %v", err)
350+
os.Exit(1)
351+
}
278352

353+
log.Printf("logsContent is: %v", logsContent)
279354
},
280355
}
281356

0 commit comments

Comments
 (0)