Skip to content

Commit

Permalink
fix: starting stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Claire.Nicholas authored and Claire.Nicholas committed Jan 15, 2024
1 parent 9d7cdb7 commit 8662409
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 3 deletions.
3 changes: 2 additions & 1 deletion api/build/list_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ func ListBuildsForOrg(c *gin.Context) {
// verify the event provided is a valid event type
if event != constants.EventComment && event != constants.EventDeploy &&
event != constants.EventPush && event != constants.EventPull &&
event != constants.EventTag && event != constants.EventSchedule {
event != constants.EventTag && event != constants.EventSchedule &&
event != constants.EventDelete {
retErr := fmt.Errorf("unable to process event %s: invalid event type provided", event)

util.HandleError(c, http.StatusBadRequest, retErr)
Expand Down
3 changes: 2 additions & 1 deletion api/build/list_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ func ListBuildsForRepo(c *gin.Context) {
// verify the event provided is a valid event type
if event != constants.EventComment && event != constants.EventDeploy &&
event != constants.EventPush && event != constants.EventPull &&
event != constants.EventTag && event != constants.EventSchedule {
event != constants.EventTag && event != constants.EventSchedule &&
event != constants.EventDelete {
retErr := fmt.Errorf("unable to process event %s: invalid event type provided", event)

util.HandleError(c, http.StatusBadRequest, retErr)
Expand Down
6 changes: 6 additions & 0 deletions api/repo/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,12 @@ func defaultAllowedEvents(sliceDefaults []string, maskDefaults int64) *library.E
comment.SetEdited(true)

events.SetComment(comment)
case constants.EventDelete:
delete := new(actions.Delete)

Check failure on line 382 in api/repo/create.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] api/repo/create.go#L382

redefines-builtin-id: redefinition of the built-in function delete (revive)
Raw output
api/repo/create.go:382:4: redefines-builtin-id: redefinition of the built-in function delete (revive)
			delete := new(actions.Delete)
			^
delete.SetBranch(true)
delete.SetTag(true)

events.SetDelete(delete)
}
}

Expand Down
4 changes: 4 additions & 0 deletions database/repo/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ func testEvents() *library.Events {
Schedule: &actions.Schedule{
Run: new(bool),
},
Delete: &actions.Delete{
Branch: new(bool),
Tag: new(bool),
},
}
}

Expand Down
4 changes: 4 additions & 0 deletions database/secret/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ func testEvents() *library.Events {
Schedule: &actions.Schedule{
Run: new(bool),
},
Delete: &actions.Delete{
Branch: new(bool),
Tag: new(bool),
},
}
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/drone/envsubst v1.0.3
github.com/gin-gonic/gin v1.9.1
github.com/go-playground/assert/v2 v2.2.0
github.com/go-vela/types v0.22.1-0.20240105182535-a91bd54636bc
github.com/go-vela/types v0.22.1-0.20240111192144-0d0e22367796
github.com/golang-jwt/jwt/v5 v5.1.0
github.com/google/go-cmp v0.6.0
github.com/google/go-github/v56 v56.0.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ github.com/go-test/deep v1.0.2 h1:onZX1rnHT3Wv6cqNgYyFOOlgVKJrksuCMCRvJStbMYw=
github.com/go-test/deep v1.0.2/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/go-vela/types v0.22.1-0.20240105182535-a91bd54636bc h1:S59SXYfqFTJeuIBdwoKQE/oFRPAFU/LuzHCq3mRXe3w=
github.com/go-vela/types v0.22.1-0.20240105182535-a91bd54636bc/go.mod h1:cax3mW1kVz/ioI8qltZE+wl9rOLgOPdwBIvCooL09e4=
github.com/go-vela/types v0.22.1-0.20240111192144-0d0e22367796 h1:fjma5QAoncoknvTOp2AL/qYDOt4liWkZS5+sUaCQKJw=
github.com/go-vela/types v0.22.1-0.20240111192144-0d0e22367796/go.mod h1:cax3mW1kVz/ioI8qltZE+wl9rOLgOPdwBIvCooL09e4=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
Expand Down
80 changes: 80 additions & 0 deletions scm/github/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ func (c *client) ProcessWebhook(ctx context.Context, request *http.Request) (*ty
case *github.IssueCommentEvent:
c.Logger.Tracef("issue comment")
return c.processIssueCommentEvent(h, event)
case *github.DeleteEvent:
c.Logger.Tracef("delete")
return c.processDeleteEvent(h, event)
case *github.RepositoryEvent:
c.Logger.Tracef("repository")
return c.processRepositoryEvent(h, event)
Expand Down Expand Up @@ -501,6 +504,83 @@ func (c *client) processRepositoryEvent(h *library.Hook, payload *github.Reposit
}, nil
}

// processDeleteEvent is a helper function to process the delete event.
func (c *client) processDeleteEvent(h *library.Hook, payload *github.DeleteEvent) (*types.Webhook, error) {
c.Logger.WithFields(logrus.Fields{
"org": payload.GetRepo().GetOwner().GetLogin(),
"repo": payload.GetRepo().GetName(),
}).Tracef("processing delete GitHub webhook for %s", payload.GetRepo().GetFullName())

repo := payload.GetRepo()

// convert payload to library repo
r := new(library.Repo)
r.SetOrg(repo.GetOwner().GetLogin())
r.SetName(repo.GetName())
r.SetFullName(repo.GetFullName())
r.SetLink(repo.GetHTMLURL())
r.SetClone(repo.GetCloneURL())
r.SetBranch(repo.GetDefaultBranch())
r.SetPrivate(repo.GetPrivate())
r.SetTopics(repo.Topics)

// convert payload to library build
b := new(library.Build)
b.SetEvent(constants.EventDelete)
b.SetClone(repo.GetCloneURL())
//b.SetSource(payload.GetHeadCommit().GetURL())
b.SetTitle(fmt.Sprintf("%s received from %s", constants.EventDelete, repo.GetHTMLURL()))
//b.SetMessage(payload.GetHeadCommit().GetMessage())
//b.SetCommit(payload.GetHeadCommit().GetID())
b.SetSender(payload.GetSender().GetLogin())
//b.SetAuthor(payload.GetDelete())
//b.SetEmail(payload.GetHeadCommit().GetAuthor().GetEmail())
b.SetBranch(strings.TrimPrefix(payload.GetRef(), "refs/heads/"))
b.SetRef(payload.GetRef())
//b.SetBaseRef(payload.GetBaseRef())

// update the hook object
h.SetBranch(b.GetBranch())
h.SetEvent(constants.EventDelete)
h.SetLink(
fmt.Sprintf("https://%s/%s/settings/hooks", h.GetHost(), r.GetFullName()),
)

// ensure the build author is set
//if len(b.GetAuthor()) == 0 {
// b.SetAuthor(payload.GetHeadCommit().GetCommitter().GetName())
//}

// ensure the build sender is set
//if len(b.GetSender()) == 0 {
// b.SetSender(payload.GetPusher().GetName())
//}

// ensure the build email is set
//if len(b.GetEmail()) == 0 {
// b.SetEmail(payload.GetHeadCommit().GetCommitter().GetEmail())
//}

// handle when push event is a tag
//if strings.HasPrefix(b.GetRef(), "refs/tags/") {
// set the proper event for the hook
// h.SetEvent(constants.EventTag)
// set the proper event for the build
// b.SetEvent(constants.EventTag)

// set the proper branch from the base ref
// if strings.HasPrefix(payload.GetBaseRef(), "refs/heads/") {
// b.SetBranch(strings.TrimPrefix(payload.GetBaseRef(), "refs/heads/"))
// }
//}

return &types.Webhook{
Hook: h,
Repo: r,
Build: b,
}, nil
}

// getDeliveryID gets the last 100 webhook deliveries for a repo and
// finds the matching delivery id with the source id in the hook.
func (c *client) getDeliveryID(ctx context.Context, ghClient *github.Client, r *library.Repo, h *library.Hook) (int64, error) {
Expand Down

0 comments on commit 8662409

Please sign in to comment.