diff --git a/pkg/apis/pipelinesascode/v1alpha1/types.go b/pkg/apis/pipelinesascode/v1alpha1/types.go index df8581ae3..5257738b0 100644 --- a/pkg/apis/pipelinesascode/v1alpha1/types.go +++ b/pkg/apis/pipelinesascode/v1alpha1/types.go @@ -36,6 +36,10 @@ type RepositoryRunStatus struct { // +optional SHA *string `json:"sha,omitempty"` + // SHA the URL of the SHA to view it + // +optional + SHAURL *string `json:"sha_url,omitempty"` + // Title is the title of the commit SHA that has been tested // +optional Title *string `json:"title,omitempty"` diff --git a/pkg/cmd/pipelineascode/pipelineascode_test.go b/pkg/cmd/pipelineascode/pipelineascode_test.go index f5f3388e1..3a1f24ac7 100644 --- a/pkg/cmd/pipelineascode/pipelineascode_test.go +++ b/pkg/cmd/pipelineascode/pipelineascode_test.go @@ -48,6 +48,9 @@ func TestRunWrapPR(t *testing.T) { mux.HandleFunc("/repos/chmouel/scratchmyback/commits/ref", func(w http.ResponseWriter, r *http.Request) { _, _ = fmt.Fprint(w, `{"commit": {"message": "HELLO"}}`) }) + mux.HandleFunc("/repos/chmouel/scratchmyback/git/commits/ref", func(w http.ResponseWriter, r *http.Request) { + _, _ = fmt.Fprint(w, `{"commit": {"message": "HELLO"}}`) + }) mux.HandleFunc(fmt.Sprintf("/repos/chmouel/scratchmyback/check-runs/%d", checkid), func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, `{"id": %d}`, checkid) }) @@ -189,6 +192,10 @@ func TestRunWrap(t *testing.T) { mux.HandleFunc("/repos/chmouel/scratchmyback/commits/ref", func(w http.ResponseWriter, r *http.Request) { _, _ = fmt.Fprint(w, `{"commit": {"message": "HELLO"}}`) }) + mux.HandleFunc("/repos/chmouel/scratchmyback/git/commits/ref", func(w http.ResponseWriter, r *http.Request) { + _, _ = fmt.Fprint(w, `{"commit": {"message": "HELLO"}}`) + }) + kinteract := &kitesthelper.KinterfaceTest{ ConsoleURL: "https://console.url", } diff --git a/pkg/cmd/repository/describe.go b/pkg/cmd/repository/describe.go index 44f1263fe..ddee0a057 100644 --- a/pkg/cmd/repository/describe.go +++ b/pkg/cmd/repository/describe.go @@ -37,7 +37,7 @@ const ( {{ $.ColorScheme.Bold "PipelineRun" }}: {{ $status.PipelineRunName }} {{ $.ColorScheme.Bold "Status" }}: {{ $.ColorScheme.ColorStatus (index $status.Status.Conditions 0).Reason }} -{{ $.ColorScheme.Bold "Commit" }}: {{.Repository.Spec.URL}}/commit/{{ shortSHA $status.SHA }} +{{ $.ColorScheme.Bold "Commit" }}: {{ $status.SHAURL }} {{ $.ColorScheme.Bold "Commit Title" }}: {{ $status.Title }} {{ $.ColorScheme.Bold "StartTime" }}: {{ formatTime $status.StartTime $.Clock }} {{ $.ColorScheme.Bold "Duration" }}: {{ formatDuration $status.StartTime $status.CompletionTime }} diff --git a/pkg/cmd/repository/describe_test.go b/pkg/cmd/repository/describe_test.go index fbe7292de..df4d96e13 100644 --- a/pkg/cmd/repository/describe_test.go +++ b/pkg/cmd/repository/describe_test.go @@ -66,6 +66,7 @@ func TestDescribe(t *testing.T) { StartTime: &metav1.Time{Time: cw.Now().Add(-16 * time.Minute)}, CompletionTime: &metav1.Time{Time: cw.Now().Add(-15 * time.Minute)}, SHA: github.String("SHA"), + SHAURL: github.String("https://anurl.com/commit/SHA"), Title: github.String("A title"), }, }, @@ -93,6 +94,7 @@ func TestDescribe(t *testing.T) { StartTime: &metav1.Time{Time: cw.Now().Add(-16 * time.Minute)}, CompletionTime: &metav1.Time{Time: cw.Now().Add(-15 * time.Minute)}, SHA: github.String("SHA"), + SHAURL: github.String("https://anurl.com/commit/SHA"), Title: github.String("A title"), }, }, @@ -118,6 +120,7 @@ func TestDescribe(t *testing.T) { StartTime: &metav1.Time{Time: cw.Now().Add(-16 * time.Minute)}, CompletionTime: &metav1.Time{Time: cw.Now().Add(-15 * time.Minute)}, SHA: github.String("SHA"), + SHAURL: github.String("https://anurl.com/commit/SHA"), Title: github.String("A title"), }, { @@ -132,6 +135,7 @@ func TestDescribe(t *testing.T) { StartTime: &metav1.Time{Time: cw.Now().Add(-18 * time.Minute)}, CompletionTime: &metav1.Time{Time: cw.Now().Add(-17 * time.Minute)}, SHA: github.String("SHA2"), + SHAURL: github.String("https://anurl.com/commit/SHA2"), Title: github.String("Another Update"), }, }, diff --git a/pkg/cmd/repository/root_test.go b/pkg/cmd/repository/root_test.go index 08a53444b..972cb3d48 100644 --- a/pkg/cmd/repository/root_test.go +++ b/pkg/cmd/repository/root_test.go @@ -66,6 +66,7 @@ func TestCommands(t *testing.T) { StartTime: &metav1.Time{Time: cw.Now().Add(-16 * time.Minute)}, CompletionTime: &metav1.Time{Time: cw.Now().Add(-15 * time.Minute)}, SHA: github.String("SHA"), + SHAURL: github.String("https://anurl.com/repo/owner/commit/SHA"), Title: github.String("A title"), }, } @@ -132,6 +133,7 @@ func TESSS(t *testing.T) { StartTime: &metav1.Time{Time: cw.Now().Add(-16 * time.Minute)}, CompletionTime: &metav1.Time{Time: cw.Now().Add(-15 * time.Minute)}, SHA: github.String("SHA"), + SHAURL: github.String("https://anurl.com/repo/owner/commit/SHA"), Title: github.String("A title"), }, } diff --git a/pkg/pipelineascode/pipelineascode.go b/pkg/pipelineascode/pipelineascode.go index 0642c84b4..1d94c781d 100644 --- a/pkg/pipelineascode/pipelineascode.go +++ b/pkg/pipelineascode/pipelineascode.go @@ -164,6 +164,7 @@ func Run(ctx context.Context, cs *cli.Clients, k8int cli.KubeInteractionIntf, ru } pipelineRun.Annotations["pipelinesascode.tekton.dev/sha-title"] = runinfo.SHATitle + pipelineRun.Annotations["pipelinesascode.tekton.dev/sha-url"] = runinfo.SHAURL // Create the actual pipeline pr, err := cs.Tekton.TektonV1beta1().PipelineRuns(repo.Spec.Namespace).Create(ctx, pipelineRun, metav1.CreateOptions{}) @@ -217,6 +218,7 @@ func Run(ctx context.Context, cs *cli.Clients, k8int cli.KubeInteractionIntf, ru StartTime: newPr.Status.StartTime, CompletionTime: newPr.Status.CompletionTime, SHA: &runinfo.SHA, + SHAURL: &runinfo.SHAURL, Title: &runinfo.SHATitle, LogURL: &consoleURL, } diff --git a/pkg/webvcs/github.go b/pkg/webvcs/github.go index ae9331f35..5efaecc54 100644 --- a/pkg/webvcs/github.go +++ b/pkg/webvcs/github.go @@ -33,6 +33,7 @@ type RunInfo struct { Owner string Repository string SHA string + SHAURL string Sender string TriggerTarget string URL string @@ -158,6 +159,7 @@ func (v GithubVCS) getPullRequest(ctx context.Context, runinfo RunInfo, prNumber runinfo.DefaultBranch = pr.GetBase().GetRepo().GetDefaultBranch() runinfo.URL = pr.GetBase().GetRepo().GetHTMLURL() runinfo.SHA = pr.GetHead().GetSHA() + runinfo.SHAURL = fmt.Sprintf("%s/commit/%s", pr.GetHTMLURL(), pr.GetHead().GetSHA()) // TODO: Maybe if we wanted to allow rerequest from non approved user we // would use the CheckRun Sender instead of the rerequest sender, could it // be a room for abuse? 🤔 @@ -165,9 +167,24 @@ func (v GithubVCS) getPullRequest(ctx context.Context, runinfo RunInfo, prNumber runinfo.HeadBranch = pr.GetHead().GetRef() runinfo.BaseBranch = pr.GetBase().GetRef() runinfo.EventType = "pull_request" + err = v.populateCommitInfo(ctx, &runinfo) + if err != nil { + return RunInfo{}, err + } return runinfo, nil } +// populateCommitInfo get info on a commit in runinfo +func (v GithubVCS) populateCommitInfo(ctx context.Context, runinfo *RunInfo) error { + commit, _, err := v.Client.Git.GetCommit(ctx, runinfo.Owner, runinfo.Repository, runinfo.SHA) + if err != nil { + return err + } + + runinfo.SHAURL = commit.GetHTMLURL() + return nil +} + // ParsePayload parse payload event func (v GithubVCS) ParsePayload(ctx context.Context, log *zap.SugaredLogger, eventType, triggerTarget, payload string) (*RunInfo, error) { var runinfo RunInfo @@ -202,6 +219,7 @@ func (v GithubVCS) ParsePayload(ctx context.Context, log *zap.SugaredLogger, eve URL: event.GetRepo().GetHTMLURL(), SHA: event.GetHeadCommit().GetID(), SHATitle: event.GetHeadCommit().GetMessage(), + SHAURL: event.GetHeadCommit().GetURL(), Sender: event.GetSender().GetLogin(), BaseBranch: event.GetRef(), EventType: eventType, @@ -220,6 +238,10 @@ func (v GithubVCS) ParsePayload(ctx context.Context, log *zap.SugaredLogger, eve Sender: event.GetPullRequest().GetUser().GetLogin(), EventType: eventType, } + err := v.populateCommitInfo(ctx, &runinfo) + if err != nil { + return nil, err + } default: return &runinfo, errors.New("this event is not supported") } diff --git a/pkg/webvcs/github_test.go b/pkg/webvcs/github_test.go index 4d874f60e..d60a59b58 100644 --- a/pkg/webvcs/github_test.go +++ b/pkg/webvcs/github_test.go @@ -122,6 +122,10 @@ func TestPayLoadFix(t *testing.T) { mux.HandleFunc("/repos/repo/owner/commits/SHA", func(w http.ResponseWriter, r *http.Request) { _, _ = fmt.Fprint(w, `{"commit": {"message": "HELLO"}}`) }) + mux.HandleFunc("/repos/repo/owner/git/commits/SHA", func(w http.ResponseWriter, r *http.Request) { + _, _ = fmt.Fprint(w, `{"commit": {"message": "HELLO"}}`) + }) + gvcs := GithubVCS{ Client: fakeclient, } @@ -152,6 +156,9 @@ func TestParsePayloadRerequestFromPullRequest(t *testing.T) { mux.HandleFunc("/repos/owner/repo/commits/"+sha, func(w http.ResponseWriter, r *http.Request) { _, _ = fmt.Fprint(w, `{"commit": {"message": "HELLO"}}`) }) + mux.HandleFunc("/repos/owner/repo/git/commits/"+sha, func(w http.ResponseWriter, r *http.Request) { + _, _ = fmt.Fprint(w, `{"commit": {"message": "HELLO"}}`) + }) ctx, _ := rtesting.SetupFakeContext(t) gvcs := GithubVCS{ Client: fakeclient, @@ -234,7 +241,9 @@ func TestParsePayLoadRetest(t *testing.T) { mux.HandleFunc("/repos/user1/pipelines/commits/"+sha, func(w http.ResponseWriter, r *http.Request) { _, _ = fmt.Fprint(w, `{"commit": {"message": "HELLO"}}`) }) - + mux.HandleFunc("/repos/user1/pipelines/git/commits/"+sha, func(w http.ResponseWriter, r *http.Request) { + _, _ = fmt.Fprint(w, `{"commit": {"message": "HELLO"}}`) + }) issueEvent := fmt.Sprintf(`{ "sender": { "login": "%s" @@ -277,6 +286,9 @@ func TestParsePayload(t *testing.T) { mux.HandleFunc("/repos/chmouel/scratchpad/commits/cc8334de8e056317d18bd00c2588c3f7c95af294", func(w http.ResponseWriter, r *http.Request) { _, _ = fmt.Fprint(w, `{"commit": {"message": "HELLO"}}`) }) + mux.HandleFunc("/repos/chmouel/scratchpad/git/commits/cc8334de8e056317d18bd00c2588c3f7c95af294", func(w http.ResponseWriter, r *http.Request) { + _, _ = fmt.Fprint(w, `{"commit": {"message": "HELLO"}}`) + }) gvcs := GithubVCS{ Client: fakeclient, }