@@ -3,56 +3,24 @@ package status
33import (
44 "context"
55 "regexp"
6- "strings"
76
87 "github.com/google/go-github/v47/github"
98 pacv1alpha1 "github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
109 "github.com/openshift-pipelines/pipelines-as-code/pkg/kubeinteraction"
10+ kstatus "github.com/openshift-pipelines/pipelines-as-code/pkg/kubeinteraction/status"
1111 "github.com/openshift-pipelines/pipelines-as-code/pkg/params"
1212 sortrepostatus "github.com/openshift-pipelines/pipelines-as-code/pkg/sort"
1313 tektonv1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
1414 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1515)
1616
17- var reasonMessageReplacementRegexp = regexp .MustCompile (`\(image: .*` )
18-
1917// snatched from prow
2018// https://github.com/kubernetes/test-infra/blob/3c8cbed65c421670a7d37239b8ffceb91e0eb16b/prow/spyglass/lenses/buildlog/lens.go#L95
2119var (
2220 ErorrRE = regexp .MustCompile (`timed out|ERROR:|(FAIL|Failure \[)\b|panic\b|^E\d{4} \d\d:\d\d:\d\d\.\d\d\d]` )
2321 defaultNumLinesOfLogsInContainersToGrabForErr = int64 (10 )
2422)
2523
26- // CollectTaskInfos collects all tasks information we are interested in.
27- // TODO: move to a better package than cli since used by watcher too
28- func CollectTaskInfos (ctx context.Context , cs * params.Run , pr tektonv1beta1.PipelineRun , numLines int64 ) map [string ]pacv1alpha1.TaskInfos {
29- failureReasons := map [string ]pacv1alpha1.TaskInfos {}
30- kinteract , _ := kubeinteraction .NewKubernetesInteraction (cs )
31- for _ , task := range pr .Status .TaskRuns {
32- if task .Status != nil && len (task .Status .Conditions ) > 0 && task .Status .Conditions [0 ].Reason == tektonv1beta1 .PipelineRunReasonFailed .String () {
33- ti := pacv1alpha1.TaskInfos {
34- Reason : reasonMessageReplacementRegexp .ReplaceAllString (task .Status .Conditions [0 ].Message , "" ),
35- }
36-
37- if kinteract != nil {
38- for _ , step := range task .Status .Steps {
39- if step .Terminated != nil && step .Terminated .ExitCode != 0 {
40- log , err := kinteract .GetPodLogs (ctx , pr .GetNamespace (), task .Status .PodName , step .ContainerName , numLines )
41- if err != nil {
42- cs .Clients .Log .Errorf ("cannot get pod logs: %w" , err )
43- continue
44- }
45- // see if a pattern match from errRe
46- ti .LogSnippet = strings .TrimSpace (log )
47- }
48- }
49- }
50- failureReasons [task .PipelineTaskName ] = ti
51- }
52- }
53- return failureReasons
54- }
55-
5624// RepositoryRunStatusRemoveSameSHA remove an existing status with the same
5725// SHA. This would come from repo pipelinerun_status. We don't want the doublons
5826// and we rather use the ones from the live PR on cluster.
@@ -68,7 +36,8 @@ func RepositoryRunStatusRemoveSameSHA(rs []pacv1alpha1.RepositoryRunStatus, live
6836}
6937
7038func convertPrStatusToRepositoryStatus (ctx context.Context , cs * params.Run , pr tektonv1beta1.PipelineRun , logurl string ) pacv1alpha1.RepositoryRunStatus {
71- failurereasons := CollectTaskInfos (ctx , cs , pr , defaultNumLinesOfLogsInContainersToGrabForErr )
39+ kinteract , _ := kubeinteraction .NewKubernetesInteraction (cs )
40+ failurereasons := kstatus .CollectFailedTasksLogSnippet (ctx , cs , kinteract , & pr , defaultNumLinesOfLogsInContainersToGrabForErr )
7241 prSHA := pr .GetLabels ()["pipelinesascode.tekton.dev/sha" ]
7342 return pacv1alpha1.RepositoryRunStatus {
7443 Status : pr .Status .Status ,
0 commit comments