Skip to content

Commit 854d429

Browse files
committed
feat: support contract_requiring_verification_published_webhook flow
1 parent b9ec979 commit 854d429

File tree

5 files changed

+78
-21
lines changed

5 files changed

+78
-21
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- uses: actions/checkout@v4
2121
- uses: actions/setup-go@v5
2222
with:
23-
go-version: '^1.14.1'
23+
go-version: '^1.22'
2424
- name: install pact-go shared libraries
2525
run: make install
2626
- name: Test
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: contract_requiring_verification_published
2+
3+
# This workflow leverages the https://docs.pact.io/pact_broker/webhooks#the-contract-requiring-verification-published-event webhook
4+
5+
on:
6+
repository_dispatch:
7+
types:
8+
- contract_requiring_verification_published
9+
workflow_dispatch:
10+
# inputs aren't available on push, so we set them explicitly in separate steps
11+
inputs:
12+
PACT_URL:
13+
description: URL of pact to verify
14+
required: true
15+
16+
env:
17+
PACT_BROKER_BASE_URL: https://test.pactflow.io
18+
PACT_BROKER_TOKEN: ${{ secrets.PACTFLOW_TOKEN_FOR_CI_CD_WORKSHOP }}
19+
PACT_BROKER_PUBLISH_VERIFICATION_RESULTS: true
20+
PACT_URL: ${{ github.event.client_payload.pact_url }}
21+
GIT_COMMIT: ${{ github.event.client_payload.sha }}
22+
GIT_BRANCH: ${{ github.event.client_payload.branch }}
23+
DESCRIPTION: ${{ github.event.client_payload.message }}
24+
25+
jobs:
26+
verify-contract-requiring-verification:
27+
runs-on: ubuntu-latest
28+
steps:
29+
- name: checkout default branch if user manually provides pact URL
30+
uses: actions/checkout@v4
31+
if: ${{ github.event.inputs.PACT_URL }}
32+
- name: checkout specific SHA if webhook providers pact URL
33+
uses: actions/checkout@v4
34+
if: ${{ github.event.client_payload.pact_url }}
35+
with:
36+
ref: ${{env.GIT_COMMIT}}
37+
- uses: actions/setup-go@v5
38+
with:
39+
go-version: '^1.22'
40+
- run: docker pull pactfoundation/pact-cli:latest
41+
- name: Install
42+
run: npm i
43+
- name: ${{env.DESCRIPTION}}
44+
run: make ci_webhook

.github/workflows/verify_changed_pact.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ jobs:
1717
verify-changed-pact:
1818
runs-on: ubuntu-latest
1919
steps:
20-
- uses: actions/checkout@v3
21-
- uses: actions/setup-go@v3
20+
- uses: actions/checkout@v4
21+
- uses: actions/setup-go@v5
2222
with:
23-
go-version: '^1.14.1'
23+
go-version: '^1.22'
2424
- name: Build
2525
run: GIT_BRANCH=${GITHUB_REF:11} make ci_webhook

Makefile

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PACTICIPANT := "pactflow-example-provider-golang"
2-
WEBHOOK_UUID := "c76b601e-d66a-4eb1-88a4-6ebc50c0df8b"
2+
CONTRACT_REQUIRING_VERIFICATION_PUBLISHED_WEBHOOK_UUID := "c76b601e-d66a-4eb1-88a4-6ebc50c0df8b"
33
PACT_CLI="docker run --rm -v ${PWD}:${PWD} -e PACT_BROKER_BASE_URL -e PACT_BROKER_TOKEN pactfoundation/pact-cli:latest"
44
PACT_GO_VERSION=2.0.8
55
PACT_DOWNLOAD_DIR=/tmp
@@ -87,21 +87,20 @@ create_github_token_secret:
8787

8888
# NOTE: the github token secret must be created (either through the UI or using the
8989
# `create_github_token_secret` target) before the webhook is invoked.
90-
create_or_update_pact_changed_webhook:
90+
create_or_update_contract_requiring_verification_published_webhook:
9191
"${PACT_CLI}" \
9292
broker create-or-update-webhook \
9393
"https://api.github.com/repos/${GITHUB_REPO}/dispatches" \
9494
--header 'Content-Type: application/json' 'Accept: application/vnd.github.everest-preview+json' 'Authorization: Bearer $${user.githubToken}' \
9595
--request POST \
96-
--data '{ "event_type": "pact_changed", "client_payload": { "pact_url": "$${pactbroker.pactUrl}" } }' \
97-
--uuid ${PACT_CHANGED_WEBHOOK_UUID} \
98-
--consumer ${PACTICIPANT} \
99-
--contract-content-changed \
100-
--description "Pact content changed for ${PACTICIPANT}"
101-
102-
test_pact_changed_webhook:
103-
@curl -v -X POST ${PACT_BROKER_BASE_URL}/webhooks/${PACT_CHANGED_WEBHOOK_UUID}/execute -H "Authorization: Bearer ${PACT_BROKER_TOKEN}"
104-
96+
--data '{ "event_type": "contract_requiring_verification_published","client_payload": { "pact_url": "$${pactbroker.pactUrl}", "sha": "$${pactbroker.providerVersionNumber}", "branch":"$${pactbroker.providerVersionBranch}" , "message": "Verify changed pact for $${pactbroker.consumerName} version $${pactbroker.consumerVersionNumber} branch $${pactbroker.consumerVersionBranch} by $${pactbroker.providerVersionNumber} ($${pactbroker.providerVersionDescriptions})" } }' \
97+
--uuid ${CONTRACT_REQUIRING_VERIFICATION_PUBLISHED_WEBHOOK_UUID} \
98+
--provider ${PACTICIPANT} \
99+
--contract-requiring-verification-published \
100+
--description "contract_requiring_verification_published for ${PACTICIPANT}"
101+
102+
test_contract_requiring_verification_published_webhook:
103+
@curl -v -X POST ${PACT_BROKER_BASE_URL}/webhooks/${CONTRACT_REQUIRING_VERIFICATION_PUBLISHED_WEBHOOK_UUID}/execute -H "Authorization: Bearer ${PACT_BROKER_TOKEN}"
105104
## ======================
106105
## Misc
107106
## ======================

user_service_test.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,40 @@ func TestPactProvider(t *testing.T) {
2626
verifier := provider.NewVerifier()
2727

2828
// Verify the Provider - fetch pacts from PactFlow
29-
err := verifier.VerifyProvider(t, provider.VerifyRequest{
29+
verifyRequest := provider.VerifyRequest{
3030
Provider: "pactflow-example-provider-golang",
3131
ProviderBaseURL: fmt.Sprintf("http://127.0.0.1:%d", port),
32-
BrokerURL: fmt.Sprint(os.Getenv("PACT_BROKER_BASE_URL")),
33-
ConsumerVersionSelectors: getSelectors(),
3432
BrokerToken: os.Getenv("PACT_BROKER_TOKEN"),
3533
BrokerUsername: os.Getenv("PACT_BROKER_USERNAME"),
3634
BrokerPassword: os.Getenv("PACT_BROKER_PASSWORD"),
3735
PublishVerificationResults: envBool("PACT_BROKER_PUBLISH_VERIFICATION_RESULTS"),
3836
ProviderVersion: os.Getenv("GIT_COMMIT"),
3937
StateHandlers: stateHandlers,
40-
EnablePending: true,
41-
IncludeWIPPactsSince: parseDate("2024-01-01"),
4238
ProviderBranch: os.Getenv("GIT_BRANCH"),
43-
})
39+
}
40+
41+
if os.Getenv("PACT_URL") != "" {
42+
// For builds triggered by a 'contract_requiring_verification_published' webhook, verify the changed pact against latest of mainBranch and any version currently deployed to an environment
43+
// https://docs.pact.io/pact_broker/webhooks#using-webhooks-with-the-contract_requiring_verification_published-event
44+
// The URL will have been passed in from the webhook to the CI job.
45+
verifyRequest.PactFiles = []string{os.Getenv("PACT_URL")}
46+
} else {
47+
// For 'normal' provider builds, fetch the the latest version from the main branch of each consumer, as specified by
48+
// the consumer's mainBranch property and all the currently deployed and currently released and supported versions of each consumer.
49+
// https://docs.pact.io/pact_broker/advanced_topics/consumer_version_selectors
50+
verifyRequest.BrokerURL = fmt.Sprint(os.Getenv("PACT_BROKER_BASE_URL"))
51+
verifyRequest.ConsumerVersionSelectors = getSelectors()
52+
verifyRequest.EnablePending = true
53+
verifyRequest.IncludeWIPPactsSince = parseDate("2024-01-01")
54+
}
4455

56+
err := verifier.VerifyProvider(t, verifyRequest)
4557
if err != nil {
4658
t.Fatalf("%v", err)
4759
}
4860

61+
62+
4963
}
5064

5165
// Provider state handlers

0 commit comments

Comments
 (0)