Skip to content

Commit

Permalink
Add SHA vcs viewer url
Browse files Browse the repository at this point in the history
Add vcs url for SHA in PipelineRun created in annotations as
pipelinesascode.tekton.dev/sha-url and in repository status as sha_url.

Closes #192

Signed-off-by: Chmouel Boudjnah <[email protected]>
  • Loading branch information
chmouel committed Aug 9, 2021
1 parent 177c504 commit 49f6aaa
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pkg/apis/pipelinesascode/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
7 changes: 7 additions & 0 deletions pkg/cmd/pipelineascode/pipelineascode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down Expand Up @@ -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",
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/repository/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/repository/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
},
Expand Down Expand Up @@ -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"),
},
},
Expand All @@ -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"),
},
{
Expand All @@ -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"),
},
},
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/repository/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
}
Expand Down Expand Up @@ -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"),
},
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/pipelineascode/pipelineascode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down Expand Up @@ -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,
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/webvcs/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type RunInfo struct {
Owner string
Repository string
SHA string
SHAURL string
Sender string
TriggerTarget string
URL string
Expand Down Expand Up @@ -158,16 +159,32 @@ 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? 🤔
runinfo.Sender = pr.GetUser().GetLogin()
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
Expand Down Expand Up @@ -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,
Expand All @@ -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")
}
Expand Down
14 changes: 13 additions & 1 deletion pkg/webvcs/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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,
}
Expand Down

0 comments on commit 49f6aaa

Please sign in to comment.