GitHub
In this example you are going to create a webhook integration between GitHub and Port, which will ingest pull request entities.
Port configurationโ
Create the following blueprint definition:
Pull request blueprint
{
"identifier": "pullRequest",
"description": "This blueprint represents a pull request in our software catalog",
"title": "Pull Request",
"icon": "Github",
"schema": {
"properties": {
"author": {
"type": "string"
},
"url": {
"type": "string",
"icon": "Github",
"format": "url"
}
},
"required": []
},
"mirrorProperties": {},
"calculationProperties": {},
"relations": {}
}
Create the following webhook configuration using Port UI:
Pull request webhook configuration
-
Basic details tab - fill the following details:
- Title :
Pull Request Mapper
; - Identifier :
pull_request_mapper
; - Description :
A webhook configuration for pull-request events from GitHub
; - Icon :
Github
;
- Title :
-
Integration configuration tab - fill the following JQ mapping:
[
{
"blueprint": "pullRequest",
"filter": ".headers.\"X-GitHub-Event\" == \"pull_request\"",
"entity": {
"identifier": ".body.pull_request.id | tostring",
"title": ".body.pull_request.title",
"properties": {
"author": ".body.pull_request.user.login",
"url": ".body.pull_request.html_url"
}
}
}
] -
Scroll down to Advanced settings and input the following details:
- Secret:
WEBHOOK_SECRET
; - Signature Header Name :
X-Hub-Signature-256
; - Signature Algorithm : Select
sha256
from dropdown option; - Signature Prefix :
sha256=
; - Request Identifier Path :
.headers.\"X-GitHub-Delivery\"
; - Click Save at the bottom of the page.
- Secret:
Create a webhook in GitHubโ
- Go to your desired organization/repository in GitHub;
- Select Settings;
- Select Webhooks;
- Click on Add webhook;
- Input the following details:
Payload URL
- enter the value of theurl
key you received after creating the webhook configuration;Content type
-application/json
;Secret
- enter the secret value you specified when creating the webhook;- Under "Which events would you like to trigger this webhook?" - select "Let me select individual events" and select Pull requests;
- Be sure to keep the "Active" checkbox checked.
- Click on Add webhook
In order to view the different payloads and events available in GitHub webhooks, look here
Done! any change you make to a pull request (open, close, edit, etc.) will trigger a webhook event that GitHub will send to the webhook URL provided by Port. Port will parse the events according to the mapping and update the catalog entities accordingly.
Let's Test Itโ
This section includes a sample webhook event sent from GitHub when a pull request is created. In addition, it includes the entity created from the event based on the webhook configuration provided in the previous section.
Payloadโ
Here is an example of the payload structure sent to the webhook URL when a GitHub pull request is opened:
Webhook event payload
{
"action": "opened",
"number": 15,
"pull_request": {
"url": "https://api.github.com/repos/username/My_Awesome_Python_App/pulls/15",
"id": 1395792349,
"node_id": "PR_kwDOEFWVvs5TMhnd",
"html_url": "https://github.com/username/My_Awesome_Python_App/pull/15",
"issue_url": "https://api.github.com/repos/username/My_Awesome_Python_App/issues/15",
"number": 15,
"state": "open",
"locked": false,
"title": "Update regex",
"user": {
"login": "username",
"id": 15999660,
"node_id": "MDQ6VXNlcjE1OTk5NjYw",
"url": "https://api.github.com/users/username"
},
"body": "Modifying event header",
"created_at": "2023-06-16T14:08:27Z",
"updated_at": "2023-06-16T14:08:27Z",
"closed_at": "None",
"merged_at": "None",
"assignees": [],
"requested_reviewers": [],
"requested_teams": [],
"labels": [],
"commits_url": "https://api.github.com/repos/username/My_Awesome_Python_App/pulls/15/commits",
"head": {
"label": "username:port",
"ref": "port",
"sha": "9bd151d8a6d6c3759e7fbdb5ba5ed82668021e77",
"user": {
"login": "username",
"id": 15999660,
"node_id": "MDQ6VXNlcjE1OTk5NjYw",
"avatar_url": "https://avatars.githubusercontent.com/u/15999660?v=4",
"html_url": "https://github.com/username",
"type": "User"
},
"repo": {
"id": 274044350,
"node_id": "MDEwOlJlcG9zaXRvcnkyNzQwNDQzNTA=",
"name": "My_Awesome_Python_App",
"full_name": "username/My_Awesome_Python_App",
"private": false,
"owner": {
"login": "username",
"id": 15999660,
"node_id": "MDQ6VXNlcjE1OTk5NjYw",
"url": "https://api.github.com/users/username"
},
"html_url": "https://github.com/username/My_Awesome_Python_App",
"description": "Repo description",
"fork": false,
"visibility": "public",
"forks": 0,
"open_issues": 11,
"watchers": 1,
"default_branch": "master"
}
}
}
}
Mapping Resultโ
The combination of the sample payload and the webhook configuration generates the following Port entity:
{
"identifier": "1395792349",
"title": "Update regex",
"blueprint": "pullRequest",
"properties": {
"author": "username",
"url": "https://github.com/username/My_Awesome_Python_App/pull/15"
},
"relations": {}
}