Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support contract_requiring_verification_published_webhook flow #14

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '^1.14.1'
go-version: '^1.22'
- name: install pact-go shared libraries
run: make install
- name: Test
44 changes: 44 additions & 0 deletions .github/workflows/contract_requiring_verification_published.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: contract_requiring_verification_published

# This workflow leverages the https://docs.pact.io/pact_broker/webhooks#the-contract-requiring-verification-published-event webhook

on:
repository_dispatch:
types:
- contract_requiring_verification_published
workflow_dispatch:
# inputs aren't available on push, so we set them explicitly in separate steps
inputs:
PACT_URL:
description: URL of pact to verify
required: true

env:
PACT_BROKER_BASE_URL: https://test.pactflow.io
PACT_BROKER_TOKEN: ${{ secrets.PACTFLOW_TOKEN_FOR_CI_CD_WORKSHOP }}
PACT_BROKER_PUBLISH_VERIFICATION_RESULTS: true
PACT_URL: ${{ github.event.client_payload.pact_url }}
GIT_COMMIT: ${{ github.event.client_payload.sha }}
GIT_BRANCH: ${{ github.event.client_payload.branch }}
DESCRIPTION: ${{ github.event.client_payload.message }}

jobs:
verify-contract-requiring-verification:
runs-on: ubuntu-latest
steps:
- name: checkout default branch if user manually provides pact URL
uses: actions/checkout@v4
if: ${{ github.event.inputs.PACT_URL }}
- name: checkout specific SHA if webhook providers pact URL
uses: actions/checkout@v4
if: ${{ github.event.client_payload.pact_url }}
with:
ref: ${{env.GIT_COMMIT}}
- uses: actions/setup-go@v5
with:
go-version: '^1.22'
- run: docker pull pactfoundation/pact-cli:latest
- name: Install
run: npm i
- name: ${{env.DESCRIPTION}}
run: make ci_webhook
6 changes: 3 additions & 3 deletions .github/workflows/verify_changed_pact.yml
Original file line number Diff line number Diff line change
@@ -17,9 +17,9 @@ jobs:
verify-changed-pact:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '^1.14.1'
go-version: '^1.22'
- name: Build
run: GIT_BRANCH=${GITHUB_REF:11} make ci_webhook
21 changes: 10 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PACTICIPANT := "pactflow-example-provider-golang"
WEBHOOK_UUID := "c76b601e-d66a-4eb1-88a4-6ebc50c0df8b"
CONTRACT_REQUIRING_VERIFICATION_PUBLISHED_WEBHOOK_UUID := "c76b601e-d66a-4eb1-88a4-6ebc50c0df8b"
PACT_CLI="docker run --rm -v ${PWD}:${PWD} -e PACT_BROKER_BASE_URL -e PACT_BROKER_TOKEN pactfoundation/pact-cli:latest"
PACT_GO_VERSION=2.0.8
PACT_DOWNLOAD_DIR=/tmp
@@ -87,21 +87,20 @@ create_github_token_secret:

# NOTE: the github token secret must be created (either through the UI or using the
# `create_github_token_secret` target) before the webhook is invoked.
create_or_update_pact_changed_webhook:
create_or_update_contract_requiring_verification_published_webhook:
"${PACT_CLI}" \
broker create-or-update-webhook \
"https://api.github.com/repos/${GITHUB_REPO}/dispatches" \
--header 'Content-Type: application/json' 'Accept: application/vnd.github.everest-preview+json' 'Authorization: Bearer $${user.githubToken}' \
--request POST \
--data '{ "event_type": "pact_changed", "client_payload": { "pact_url": "$${pactbroker.pactUrl}" } }' \
--uuid ${PACT_CHANGED_WEBHOOK_UUID} \
--consumer ${PACTICIPANT} \
--contract-content-changed \
--description "Pact content changed for ${PACTICIPANT}"

test_pact_changed_webhook:
@curl -v -X POST ${PACT_BROKER_BASE_URL}/webhooks/${PACT_CHANGED_WEBHOOK_UUID}/execute -H "Authorization: Bearer ${PACT_BROKER_TOKEN}"

--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})" } }' \
--uuid ${CONTRACT_REQUIRING_VERIFICATION_PUBLISHED_WEBHOOK_UUID} \
--provider ${PACTICIPANT} \
--contract-requiring-verification-published \
--description "contract_requiring_verification_published for ${PACTICIPANT}"

test_contract_requiring_verification_published_webhook:
@curl -v -X POST ${PACT_BROKER_BASE_URL}/webhooks/${CONTRACT_REQUIRING_VERIFICATION_PUBLISHED_WEBHOOK_UUID}/execute -H "Authorization: Bearer ${PACT_BROKER_TOKEN}"
## ======================
## Misc
## ======================
26 changes: 20 additions & 6 deletions user_service_test.go
Original file line number Diff line number Diff line change
@@ -26,26 +26,40 @@ func TestPactProvider(t *testing.T) {
verifier := provider.NewVerifier()

// Verify the Provider - fetch pacts from PactFlow
err := verifier.VerifyProvider(t, provider.VerifyRequest{
verifyRequest := provider.VerifyRequest{
Provider: "pactflow-example-provider-golang",
ProviderBaseURL: fmt.Sprintf("http://127.0.0.1:%d", port),
BrokerURL: fmt.Sprint(os.Getenv("PACT_BROKER_BASE_URL")),
ConsumerVersionSelectors: getSelectors(),
BrokerToken: os.Getenv("PACT_BROKER_TOKEN"),
BrokerUsername: os.Getenv("PACT_BROKER_USERNAME"),
BrokerPassword: os.Getenv("PACT_BROKER_PASSWORD"),
PublishVerificationResults: envBool("PACT_BROKER_PUBLISH_VERIFICATION_RESULTS"),
ProviderVersion: os.Getenv("GIT_COMMIT"),
StateHandlers: stateHandlers,
EnablePending: true,
IncludeWIPPactsSince: parseDate("2024-01-01"),
ProviderBranch: os.Getenv("GIT_BRANCH"),
})
}

if os.Getenv("PACT_URL") != "" {
// 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
// https://docs.pact.io/pact_broker/webhooks#using-webhooks-with-the-contract_requiring_verification_published-event
// The URL will have been passed in from the webhook to the CI job.
verifyRequest.PactFiles = []string{os.Getenv("PACT_URL")}
} else {
// For 'normal' provider builds, fetch the the latest version from the main branch of each consumer, as specified by
// the consumer's mainBranch property and all the currently deployed and currently released and supported versions of each consumer.
// https://docs.pact.io/pact_broker/advanced_topics/consumer_version_selectors
verifyRequest.BrokerURL = fmt.Sprint(os.Getenv("PACT_BROKER_BASE_URL"))
verifyRequest.ConsumerVersionSelectors = getSelectors()
verifyRequest.EnablePending = true
verifyRequest.IncludeWIPPactsSince = parseDate("2024-01-01")
}

err := verifier.VerifyProvider(t, verifyRequest)
if err != nil {
t.Fatalf("%v", err)
}



}

// Provider state handlers