Skip to content

Commit db53467

Browse files
authored
Merge pull request #40 from atlassian-labs/vbihun/compass-circleci-event-handler
Added example and instruction on how to setup CircleCI event handler
2 parents 83ac8bd + 36191b3 commit db53467

2 files changed

Lines changed: 149 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# How to use this
2+
Copy a script from config-example.yml file to your project's config to get events from your project.
3+
4+
Variables in the example:
5+
- **component-external-event-source-id** - get link to project CircleCI pipelines that you fill to Projects links in the Compass component (example: https://app.circleci.com/pipelines/github/test/potential-octo-giggle or https://app.circleci.com/pipelines/bitbucket/test/potential-octo-giggle). Copy string from `/pipelines` (example: `github/test/potential-octo-giggle` or `bitbucket/test/potential-octo-giggle`) and change github or bitbucket to gh or bb (example: `gh/test/potentian-octo-giggle` or `bb/test/potential-octo-giggle`). Add the final string to `externalEventSourceId` field in the config file;
6+
7+
- **name-of-your-site** - go to your site and copy the string before .atlassian.net/…. Example: `test-site.atlassian.net/compass` - you should copy only test-site. Add the final string to `name-of-your-site` field in the config file;
8+
9+
- **$USER-EMAIL** - your email of site on Atlassian. Add email to the CircleCI project’s variables. Path to the CircleCI project’s variables: *Projects → Your project’s settings → Environments Variables → Add. Name: USER-EMAIL*;
10+
11+
- **$TOKEN** - create an API token. Open the link (https://id.atlassian.com/manage-profile/security/api-tokens) and create a token. After that, add the Token to the CircleCI project’s variables. Path to the CircleCI project’s variables: *Projects → Your project’s settings → Environments Variables → Add; Name: TOKEN*;
12+
13+
- **$CLOUD-ID** - to get your `cloudId`, you should click on your avatar and go to the Profile. After loading the page in the URL, you should see at the end of the URL a query parameter - cloudId. Copy the cloudId and add it to the CircleCI project’s variables. Path to the CircleCI project’s variables: *Projects → Your project’s settings → Environments Variables → Add. Name: CLOUD-ID*;
14+
15+
- **$COMPONENT-ID** - copy component’s id in the component. Click on ```Copy component ID`. Add the Component ID to the CircleCI project’s variables. Path to the CircleCI project’s variables: *Projects → Your project’s settings → Environments Variables → Add; Name: COMPONENT-ID*;
16+
17+
## How to get all CircleCI components
18+
19+
You should use the GraphQL query:
20+
21+
```
22+
query searchComponents($cloudId: String!, $query: CompassSearchComponentQuery) {
23+
compass {
24+
searchComponents(cloudId: $cloudId, query: $query) {
25+
... on QueryError {
26+
identifier
27+
message
28+
}
29+
... on CompassSearchComponentConnection {
30+
pageInfo {
31+
hasNextPage
32+
endCursor
33+
}
34+
nodes {
35+
component {
36+
name
37+
id
38+
links {
39+
name
40+
type
41+
url
42+
}
43+
}
44+
}
45+
}
46+
}
47+
}
48+
}
49+
```
50+
and query variables:
51+
```
52+
{
53+
"cloudId": <your-cloud-id>,
54+
"query": {
55+
"first": 100,
56+
"fieldFilters": [
57+
{
58+
"name": "linkTypes",
59+
"filter": { "eq": "PROJECT" }
60+
}
61+
]
62+
}
63+
}
64+
```
65+
66+
To use this query, go to the link: `https://<your-site-name>.atlassian.net/gateway/api/graphql` and copy-paste the GraphQL query to the query editor and copy-paste variables to the Variables editor in the bottom of the page, and click the Run button. You will see all components with the Projects link type in your site. You can find a component that has a CircleCI link and open a link to set up the config file.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
jobs:
2+
build:
3+
docker:
4+
- image: <your-image>
5+
steps:
6+
- checkout
7+
8+
# Notify Compass on success
9+
- run:
10+
name: Notify Compass (SUCCESSFUL)
11+
when: on_success
12+
command: |
13+
curl \
14+
--request POST \
15+
--url "https://<name-of-your-site>.atlassian.net/gateway/api/compass/v1/events" \
16+
--user "$USER-EMAIL:$TOKEN" \
17+
--header "Accept: application/json" \
18+
--header "Content-Type: application/json" \
19+
--data "{
20+
\"cloudId\": \"$SITE-CLOUD-ID\",
21+
\"componentId\": \"$COMPONENT-ID\",
22+
\"event\": {
23+
\"build\": {
24+
\"displayName\": \"Build #$CIRCLE_BUILD_NUM on $CIRCLE_BRANCH\",
25+
\"description\": \"CircleCI build succeed\",
26+
\"url\": \"$CIRCLE_BUILD_URL\",
27+
\"lastUpdated\": \"$(date -u +'%Y-%m-%dT%H:%M:%SZ')\",
28+
\"externalEventSourceId\": \"<component-external-event-source-id>\",
29+
\"updateSequenceNumber\": $CIRCLE_BUILD_NUM,
30+
\"buildProperties\": {
31+
\"state\": \"SUCCESSFUL\",
32+
\"startedAt\": \"$(date -u -d @$CIRCLE_WORKFLOW_STARTED_AT +'%Y-%m-%dT%H:%M:%SZ' 2>/dev/null || date -u +'%Y-%m-%dT%H:%M:%SZ')\",
33+
\"completedAt\": \"$(date -u +'%Y-%m-%dT%H:%M:%SZ')\",
34+
\"pipeline\": {
35+
\"pipelineId\": \"$CIRCLE_WORKFLOW_ID\",
36+
\"url\": \"$CIRCLE_BUILD_URL\",
37+
\"displayName\": \"Workflow $CIRCLE_WORKFLOW_ID\"
38+
}
39+
}
40+
}
41+
}
42+
}"
43+
44+
# Notify Compass on failure
45+
- run:
46+
name: Notify Compass (FAILED)
47+
when: on_fail
48+
command: |
49+
curl \
50+
--request POST \
51+
--url "https://<name-of-your-site>.atlassian.net/gateway/api/compass/v1/events" \
52+
--user "$USER-EMAIL:$TOKEN" \
53+
--header "Accept: application/json" \
54+
--header "Content-Type: application/json" \
55+
--data "{
56+
\"cloudId\": \"$CLOUD-ID\",
57+
\"componentId\": \"$COMPONENT-ID\",
58+
\"event\": {
59+
\"build\": {
60+
\"displayName\": \"Build #$CIRCLE_BUILD_NUM on $CIRCLE_BRANCH\",
61+
\"description\": \"CircleCI build failed\",
62+
\"url\": \"$CIRCLE_BUILD_URL\",
63+
\"lastUpdated\": \"$(date -u +'%Y-%m-%dT%H:%M:%SZ')\",
64+
\"updateSequenceNumber\": $CIRCLE_BUILD_NUM,
65+
\"externalEventSourceId\": \"<component-external-event-source-id>\",
66+
\"buildProperties\": {
67+
\"state\": \"FAILED\",
68+
\"startedAt\": \"$(date -u -d @$CIRCLE_WORKFLOW_STARTED_AT +'%Y-%m-%dT%H:%M:%SZ' 2>/dev/null || date -u +'%Y-%m-%dT%H:%M:%SZ')\",
69+
\"completedAt\": \"$(date -u +'%Y-%m-%dT%H:%M:%SZ')\",
70+
\"pipeline\": {
71+
\"pipelineId\": \"$CIRCLE_WORKFLOW_ID\",
72+
\"url\": \"$CIRCLE_BUILD_URL\",
73+
\"displayName\": \"Workflow $CIRCLE_WORKFLOW_ID\"
74+
}
75+
}
76+
}
77+
}
78+
}"
79+
80+
workflows:
81+
build_workflow:
82+
jobs:
83+
- build

0 commit comments

Comments
 (0)