Skip to content

Commit

Permalink
Fix secret auto creation
Browse files Browse the repository at this point in the history
We were not adding the SecretAutoCreation in runinfo after parsing.

This part of code really need to get better tested
  • Loading branch information
chmouel committed Sep 16, 2021
1 parent d36bb71 commit d700611
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
3 changes: 1 addition & 2 deletions pkg/cmd/pipelineascode/pipelineascode.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ func parsePayload(ctx context.Context, cs *cli.Clients, opts *pacpkg.Options) (*
return nil, err
}

payloadinfo, err := cs.GithubClient.ParsePayload(ctx, cs.Log, opts.RunInfo.EventType,
opts.RunInfo.TriggerTarget, string(payloadB))
payloadinfo, err := cs.GithubClient.ParsePayload(ctx, cs.Log, opts.RunInfo, string(payloadB))
if err != nil {
return &webvcs.RunInfo{}, err
}
Expand Down
14 changes: 8 additions & 6 deletions pkg/webvcs/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,11 @@ func (v GithubVCS) populateCommitInfo(ctx context.Context, runinfo *RunInfo) err
}

// ParsePayload parse payload event
func (v GithubVCS) ParsePayload(ctx context.Context, log *zap.SugaredLogger, eventType, triggerTarget, payload string) (*RunInfo, error) {
// TODO: this piece of code is just plain silly
func (v GithubVCS) ParsePayload(ctx context.Context, log *zap.SugaredLogger, optRunInfo RunInfo, payload string) (*RunInfo, error) {
var runinfo RunInfo
payload = payloadFix(payload)
event, err := github.ParseWebHook(eventType, []byte(payloadFix(payload)))
event, err := github.ParseWebHook(optRunInfo.EventType, []byte(payloadFix(payload)))
if err != nil {
return &runinfo, err
}
Expand All @@ -202,7 +203,7 @@ func (v GithubVCS) ParsePayload(ctx context.Context, log *zap.SugaredLogger, eve

switch event := event.(type) {
case *github.CheckRunEvent:
if triggerTarget == "issue-recheck" {
if optRunInfo.TriggerTarget == "issue-recheck" {
runinfo, err = v.handleReRequestEvent(ctx, log, event)
if err != nil {
return &runinfo, err
Expand All @@ -224,7 +225,7 @@ func (v GithubVCS) ParsePayload(ctx context.Context, log *zap.SugaredLogger, eve
SHATitle: event.GetHeadCommit().GetMessage(),
Sender: event.GetSender().GetLogin(),
BaseBranch: event.GetRef(),
EventType: eventType,
EventType: optRunInfo.TriggerTarget,
}

runinfo.HeadBranch = runinfo.BaseBranch // in push events Head Branch is the same as Basebranch
Expand All @@ -238,7 +239,7 @@ func (v GithubVCS) ParsePayload(ctx context.Context, log *zap.SugaredLogger, eve
BaseBranch: event.GetPullRequest().Base.GetRef(),
HeadBranch: event.GetPullRequest().Head.GetRef(),
Sender: event.GetPullRequest().GetUser().GetLogin(),
EventType: eventType,
EventType: optRunInfo.EventType,
}
default:
return &runinfo, errors.New("this event is not supported")
Expand All @@ -250,7 +251,8 @@ func (v GithubVCS) ParsePayload(ctx context.Context, log *zap.SugaredLogger, eve
}

runinfo.Event = event
runinfo.TriggerTarget = triggerTarget
runinfo.TriggerTarget = optRunInfo.TriggerTarget
runinfo.SecretAutoCreation = optRunInfo.SecretAutoCreation
return &runinfo, nil
}

Expand Down
49 changes: 40 additions & 9 deletions pkg/webvcs/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ func TestPayLoadFix(t *testing.T) {
}

logger, _ := getLogger()
_, err = gvcs.ParsePayload(ctx, logger, "pull_request", "pull_request", string(b))
r := RunInfo{
EventType: "pull_request",
TriggerTarget: "pull_request",
}
_, err = gvcs.ParsePayload(ctx, logger, r, string(b))
// would bomb out on "assertion failed: error is not nil: invalid character
// '\n' in string literal" if we don't fix the payload
assert.NilError(t, err)
Expand Down Expand Up @@ -164,8 +168,11 @@ func TestParsePayloadRerequestFromPullRequest(t *testing.T) {
Client: fakeclient,
}
logger, observer := getLogger()
// TODO
runinfo, err := gvcs.ParsePayload(ctx, logger, "check_run", "issue-recheck", checkrunEvent)
r := RunInfo{
EventType: "check_run",
TriggerTarget: "issue-recheck",
}
runinfo, err := gvcs.ParsePayload(ctx, logger, r, checkrunEvent)
assert.NilError(t, err)

assert.Equal(t, prOwner, runinfo.Owner)
Expand Down Expand Up @@ -217,8 +224,12 @@ func TestParsePayloadRerequestFromPush(t *testing.T) {
_, _ = fmt.Fprint(w, `{"commit": {"message": "HELLO"}}`)
})

r := RunInfo{
EventType: "check_run",
TriggerTarget: "issue-recheck",
}
logger, _ := getLogger()
runinfo, err := gvcs.ParsePayload(ctx, logger, "check_run", "issue-recheck", checkrunEvent)
runinfo, err := gvcs.ParsePayload(ctx, logger, r, checkrunEvent)
assert.NilError(t, err)

assert.Equal(t, runinfo.EventType, "push")
Expand Down Expand Up @@ -272,7 +283,11 @@ func TestParsePayLoadRetest(t *testing.T) {
}

// TODO
runinfo, err := gvcs.ParsePayload(ctx, logger, "issue_comment", "issue_comment", issueEvent)
r := RunInfo{
EventType: "issue_comment",
TriggerTarget: "issue_comment",
}
runinfo, err := gvcs.ParsePayload(ctx, logger, r, issueEvent)
assert.NilError(t, err)
assert.Equal(t, prOwner, runinfo.Owner)
// Make sure the PR owner is the runinfo.Owner and not the issueSender
Expand All @@ -299,7 +314,11 @@ func TestParsePayload(t *testing.T) {
ctx, _ := rtesting.SetupFakeContext(t)
logger, _ := getLogger()

runinfo, err := gvcs.ParsePayload(ctx, logger, "pull_request", "pull_request", string(b))
r := RunInfo{
EventType: "pull_request",
TriggerTarget: "pull_request",
}
runinfo, err := gvcs.ParsePayload(ctx, logger, r, string(b))
assert.NilError(t, err)
assert.Assert(t, runinfo.BaseBranch == "master")
assert.Assert(t, runinfo.Owner == "chmouel")
Expand All @@ -312,23 +331,35 @@ func TestParsePayloadInvalid(t *testing.T) {
ctx, _ := rtesting.SetupFakeContext(t)
gvcs := NewGithubVCS("none", "")
logger, _ := getLogger()
_, err := gvcs.ParsePayload(ctx, logger, "pull_request", "pull_request", "hello moto")
r := RunInfo{
EventType: "pull_request",
TriggerTarget: "pull_request",
}
_, err := gvcs.ParsePayload(ctx, logger, r, "hello moto")
assert.ErrorContains(t, err, "invalid character")
}

func TestParsePayloadUnkownEvent(t *testing.T) {
ctx, _ := rtesting.SetupFakeContext(t)
gvcs := NewGithubVCS("none", "")
logger, _ := getLogger()
_, err := gvcs.ParsePayload(ctx, logger, "foo", "foo", "{\"hello\": \"moto\"}")
r := RunInfo{
EventType: "foo",
TriggerTarget: "foo",
}
_, err := gvcs.ParsePayload(ctx, logger, r, "{\"hello\": \"moto\"}")
assert.ErrorContains(t, err, "unknown X-Github-Event")
}

func TestParsePayCannotParse(t *testing.T) {
gvcs := NewGithubVCS("none", "")
ctx, _ := rtesting.SetupFakeContext(t)
logger, _ := getLogger()
_, err := gvcs.ParsePayload(ctx, logger, "gollum", "gollum", "{}")
r := RunInfo{
EventType: "gollum",
TriggerTarget: "gollum",
}
_, err := gvcs.ParsePayload(ctx, logger, r, "{}")
assert.Error(t, err, "this event is not supported")
}

Expand Down

0 comments on commit d700611

Please sign in to comment.