Skip to content

Commit

Permalink
Merge pull request #74 from krancour/ci-cd
Browse files Browse the repository at this point in the history
rename ci/cd events
  • Loading branch information
krancour authored Jan 24, 2022
2 parents f7bd307 + 9874d22 commit 7cb02f1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 34 deletions.
44 changes: 22 additions & 22 deletions docs/CI_CD.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,29 +113,29 @@ can be readily understood.
Since script authors rarely, if ever, need to differentiate between a
`check_suite:requested` event and a `check_suite:rerequested` event, when
emitting either of these into Brigade's event bus, this gateway _also_ emits a
`ci_pipeline:requested` event. Apart from effectively collapsing two similarly
named and nearly identical events into one, the name `ci_pipeline:requested`
`ci:pipeline_requested` event. Apart from effectively collapsing two similarly
named and nearly identical events into one, the name `ci:pipeline_requested`
very clearly denotes exactly what any subscribed project's script should do to
handle such an event -- namely, run the CI pipeline. Better still, it eliminates
any potential confusion arising from questions like, "What even is a check
suite?" Since the gateway can handle such things all on its own, it is perhaps
better for Brigade's end-users not to get bogged down in such details and simply
focus on the fact that a `ci_pipeline:requested` event means CI should run.
focus on the fact that a `ci:pipeline_requested` event means CI should run.

Again, to help end-users avoid getting bogged down in the complexities of things
such as the GitHub Checks API, when emitting a `check_run:rerequested` event,
this gateway will _also_ emit a `ci_job:requested`. Again, this name clearly
this gateway will _also_ emit a `ci:job_requested`. Again, this name clearly
denotes exactly what any subscribed project's script should do to handle such an
event -- namely, run some discreet segment of the CI pipeline. As an added
convenience, `ci_job:requested` events have a `job` label that indicates _which_
convenience, `ci:job_requested` events have a `job` label that indicates _which_
specific job is to be re-run. This spares script authors from digging into the
event payload to make this determination.

Last, and primarily for consistency with the `ci_pipeline:requested` and
`ci_job:requested` names, when emitting a `release:published` event, this
gateway will _also_ emit a `cd_pipeline:requested` event. Once again, this name
Last, and primarily for consistency with the `ci:pipeline_requested` and
`ci:job_requested` names, when emitting a `release:published` event, this
gateway will _also_ emit a `cd:pipeline_requested` event. Once again, this name
clearly denotes exactly what any subscribed project's script should do in
response to such an event. As an added convenience, `cd_pipeline:requested` have
response to such an event. As an added convenience, `cd:pipeline_requested` have
a `release` label that indicates the release name. This spares script authors
from inferring this information themselves by digging into the event payload or
other event details.
Expand All @@ -151,9 +151,9 @@ To summarize, the existence of the custom events discussed in this section
means that script authors who are concerned only with CI/CD need only concern
themselves with the following three events:

* `ci_pipeline:requested`
* `ci_job:requested`
* `cd_pipeline:requested`
* `ci:pipeline_requested`
* `ci:job_requested`
* `cd:pipeline_requested`

## CI/CD Recipe

Expand All @@ -176,9 +176,9 @@ spec:
qualifiers:
repo: brigadecore/ci-cd-example
types:
- ci_pipeline:requested
- ci_job:requested
- cd_pipeline:requested
- ci:pipeline_requested
- ci:job_requested
- cd:pipeline_requested
workerTemplate:
git:
cloneURL: https://github.com/brigadecore/ci-cd-example.git
Expand All @@ -192,7 +192,7 @@ events to which we subscribed:
```typescript
import { events, Event, Job, ConcurrentGroup, Container } from "@brigadecore/brigadier"

events.on("brigade.sh/github", "ci_pipeline:requested", async event => {
events.on("brigade.sh/github", "ci:pipeline_requested", async event => {
// Chain some jobs together to implement CI. For example:
await new ConcurrentGroup(
// For brevity, we're omitting the definitions of each job.
Expand All @@ -203,7 +203,7 @@ events.on("brigade.sh/github", "ci_pipeline:requested", async event => {
).run()
})

events.on("brigade.sh/github", "cd_pipeline:requested", async event => {
events.on("brigade.sh/github", "cd:pipeline_requested", async event => {
// Chain some jobs together to implement CD. For example:
await new ConcurrentGroup(
// For brevity, we're omitting the definitions of each job.
Expand All @@ -217,15 +217,15 @@ events.on("brigade.sh/github", "cd_pipeline:requested", async event => {
events.process()
```

Unaccounted for in the first iteration of our script are `ci_job:requested`
Unaccounted for in the first iteration of our script are `ci:job_requested`
events which indicate that a specific job should be re-run. Modifying the
previous script slightly, we can account for such events. The strategy makes use
of a map of job factory functions indexed by name:

```typescript
import { events, Event, Job, ConcurrentGroup, Container } from "@brigadecore/brigadier"

// A map of job factory functions indexed by name. When a ci_job:requested
// A map of job factory functions indexed by name. When a ci:job_requested
// event wants to re-run a single job, this allows us to easily find it.
const jobs: {[key: string]: (event: Event) => Job } = {}

Expand All @@ -239,7 +239,7 @@ jobs[testJob0Name] = testJob0

// ...

events.on("brigade.sh/github", "ci_pipeline:requested", async event => {
events.on("brigade.sh/github", "ci:pipeline_requested", async event => {
// Chain some jobs together to implement CI. For example:
await new ConcurrentGroup(
testJob0(event),
Expand All @@ -249,7 +249,7 @@ events.on("brigade.sh/github", "ci_pipeline:requested", async event => {
).run()
})

events.on("brigade.sh/github", "ci_job:requested", async event => {
events.on("brigade.sh/github", "ci:job_requested", async event => {
// Starting with Brigade/brigadier v2.2.0, the job name can be found in a
// label. Prior to that, an event's labels were not accessible via script.
const job = jobs[event.labels.job]
Expand All @@ -260,7 +260,7 @@ events.on("brigade.sh/github", "ci_job:requested", async event => {
throw new Error(`No job found with name: ${event.labels.job}`)
})

events.on("brigade.sh/github", "cd_pipeline:requested", async event => {
events.on("brigade.sh/github", "cd:pipeline_requested", async event => {
// Chain some jobs together to implement CD. For example:
await new ConcurrentGroup(
releaseJob0(event),
Expand Down
6 changes: 3 additions & 3 deletions docs/EVENT_REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ gateway and the corresponding event(s) that are emitted into Brigade's event bus
| Webhook | Scope | Possible Action Values | Event Type(s) Emitted |
|---------|-------|------------------------|-----------------------|
| [`check_run`](https://docs.github.com/en/github-ae@latest/developers/webhooks-and-events/webhook-events-and-payloads#check_run) | specific commit | <ul><li>`created`</li><li>`completed`</li><li>`rerequested`</li><li>`rerequested_action`</li></ul> | <ul><li>`check_run:created`</li><li>`check_run:completed`</li><li>`check_run:rerequested` + ⭐️&nbsp;&nbsp;`ci_job:requested`</li><li>`check_run:rerequested_action`</li></ul>
| [`check_suite`](https://docs.github.com/en/github-ae@latest/developers/webhooks-and-events/webhook-events-and-payloads#check_suite) | specific commit | <ul><li>`completed`</li><li>`requested`</li><li>`rerequested`</li></ul> | <ul><li>`check_suite:completed`</li><li>`check_suite:requested` + `ci_pipeline:requested`</li><li>`check_suite:rerequested` + ⭐️&nbsp;&nbsp;`ci_pipeline:requested`</li></ul>
| [`check_run`](https://docs.github.com/en/github-ae@latest/developers/webhooks-and-events/webhook-events-and-payloads#check_run) | specific commit | <ul><li>`created`</li><li>`completed`</li><li>`rerequested`</li><li>`rerequested_action`</li></ul> | <ul><li>`check_run:created`</li><li>`check_run:completed`</li><li>`check_run:rerequested` + ⭐️&nbsp;&nbsp;`ci:job_requested`</li><li>`check_run:rerequested_action`</li></ul>
| [`check_suite`](https://docs.github.com/en/github-ae@latest/developers/webhooks-and-events/webhook-events-and-payloads#check_suite) | specific commit | <ul><li>`completed`</li><li>`requested`</li><li>`rerequested`</li></ul> | <ul><li>`check_suite:completed`</li><li>`check_suite:requested` + `ci:pipeline_requested`</li><li>`check_suite:rerequested` + ⭐️&nbsp;&nbsp;`ci:pipeline_requested`</li></ul>
| [`create`](https://docs.github.com/en/github-ae@latest/developers/webhooks-and-events/webhook-events-and-payloads#create) | specific branch or tag || <ul><li>`create`</li></ul>
| [`deleted`](https://docs.github.com/en/github-ae@latest/developers/webhooks-and-events/webhook-events-and-payloads#delete) | specific branch or tag || <ul><li>`deleted`</li></ul>
| [`fork`](https://docs.github.com/en/github-ae@latest/developers/webhooks-and-events/webhook-events-and-payloads#fork) | specific repository || <ul><li>`fork`</li></ul>
Expand All @@ -98,7 +98,7 @@ gateway and the corresponding event(s) that are emitted into Brigade's event bus
| [`pull_request_review`](https://docs.github.com/en/github-ae@latest/developers/webhooks-and-events/webhook-events-and-payloads#pull_request_review) | specific commit | <ul><li>`submitted`</li><li>`edited`</li><li>`dismissed`</li></ul> | <ul><li>`pull_request_review:submitted`</li><li>`pull_request_review:edited`</li><li>`pull_request_review:dismissed`</li></ul>
| [`pull_request_review_comment`](https://docs.github.com/en/github-ae@latest/developers/webhooks-and-events/webhook-events-and-payloads#pull_request_review_comment) | specific commit | <ul><li>`created`</li><li>`edited`</li><li>`deleted`</li></ul> | <ul><li>`pull_request_review_comment:created`</li><li>`pull_request_review_comment:edited`</li><li>`pull_request_review_comment:deleted`</li></ul>
| [`push`](https://docs.github.com/en/github-ae@latest/developers/webhooks-and-events/webhook-events-and-payloads#push) | specific commit || <ul><li>`push`</li></ul>
| [`release`](https://docs.github.com/en/github-ae@latest/developers/webhooks-and-events/webhook-events-and-payloads#release) | specific repository | <ul><li>`published`</li><li>`unpublished`</li><li>`created`</li><li>`edited`</li><li>`deleted`</li><li>`prereleased`</li><li>`released`</li></ul> | <ul><li>`release:published` + ⭐️&nbsp;&nbsp;`cd_pipeline:requested`</li><li>`release:unpublished`</li><li>`release:created`</li><li>`release:edited`</li><li>`release:deleted`</li><li>`release:prereleased`</li><li>`release:released`</li></ul>
| [`release`](https://docs.github.com/en/github-ae@latest/developers/webhooks-and-events/webhook-events-and-payloads#release) | specific repository | <ul><li>`published`</li><li>`unpublished`</li><li>`created`</li><li>`edited`</li><li>`deleted`</li><li>`prereleased`</li><li>`released`</li></ul> | <ul><li>`release:published` + ⭐️&nbsp;&nbsp;`cd:pipeline_requested`</li><li>`release:unpublished`</li><li>`release:created`</li><li>`release:edited`</li><li>`release:deleted`</li><li>`release:prereleased`</li><li>`release:released`</li></ul>
| [`repository`](https://docs.github.com/en/github-ae@latest/developers/webhooks-and-events/webhook-events-and-payloads#repository) | specific repository | <ul><li>`created`</li><li>`deleted`</li><li>`archived`</li><li>`unarchived`</li><li>`anonymous_access_enabled`</li><li>`edited`</li><li>`renamed`</li><li>`transferred`</li><li>`publicized`</li><li>`privatized`</li></ul> | <ul><li>`repository:created`</li><li>`repository:deleted`</li><li>`repository:archived`</li><li>`repository:unarchived`</li><li>`repository:anonymous_access_enabled`</li><li>`repository:edited`</li><li>`repository:renamed`</li><li>`repository:transferred`</li><li>`repository:publicized`</li><li>`repository:privatized`</li></ul>
| [`status`](https://docs.github.com/en/github-ae@latest/developers/webhooks-and-events/webhook-events-and-payloads#status) | specific commit || <ul><li>`status`</li></ul>
| [`team_add`](https://docs.github.com/en/github-ae@latest/developers/webhooks-and-events/webhook-events-and-payloads#team_add) | specific repository || <ul><li>`team_add`</li></ul>
Expand Down
12 changes: 6 additions & 6 deletions receiver/internal/webhooks/ci_cd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ func (s *service) getCICDEvent(event core.Event) *core.Event {
// Where check_suite:requested and check_suite:rerequested are concerned, it's
// rare that any subscriber needs to differentiate between the two, so to
// simplify matters for most subscribers, we collapse those two cases into a
// ci_pipeline:requested event and we emit that in addition to the original
// ci:pipeline_requested event and we emit that in addition to the original
// check_suite:requested or check_suite:rerequested event.
case "check_suite:requested", "check_suite:rerequested":
ciCDEvent.Type = "ci_pipeline:requested"
// For consistency with the above, we emit a ci_job:requested event in
ciCDEvent.Type = "ci:pipeline_requested"
// For consistency with the above, we emit a ci:job_requested event in
// addition to the original check_run:rerequested event.
case "check_run:rerequested":
ciCDEvent.Type = "ci_job:requested"
// For consistency with the above, we emit a cd_pipeline:requested event in
ciCDEvent.Type = "ci:job_requested"
// For consistency with the above, we emit a cd:pipeline_requested event in
// addition to the original release:published event.
case "release:published":
ciCDEvent.Type = "cd_pipeline:requested"
ciCDEvent.Type = "cd:pipeline_requested"
default:
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions receiver/internal/webhooks/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func TestHandle(t *testing.T) {
)
event = events.Items[1]
require.Equal(t, "foo", event.ProjectID)
require.Equal(t, "ci_job:requested", event.Type)
require.Equal(t, "ci:job_requested", event.Type)
require.Equal(t, testQualifiers, event.Qualifiers)
require.Equal(
t,
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestHandle(t *testing.T) {
*event.Git,
)
event = events.Items[1]
require.Equal(t, "ci_pipeline:requested", event.Type)
require.Equal(t, "ci:pipeline_requested", event.Type)
require.Equal(t, testQualifiers, event.Qualifiers)
require.Equal(
t,
Expand Down Expand Up @@ -1232,7 +1232,7 @@ func TestHandle(t *testing.T) {
*event.Git,
)
event = events.Items[1]
require.Equal(t, "cd_pipeline:requested", event.Type)
require.Equal(t, "cd:pipeline_requested", event.Type)
require.Equal(
t,
core.GitDetails{
Expand Down

0 comments on commit 7cb02f1

Please sign in to comment.