Skip to content

Commit 2add248

Browse files
feat: automatic trigger of test environment
1 parent d2f5bb4 commit 2add248

File tree

2 files changed

+167
-2
lines changed

2 files changed

+167
-2
lines changed

.gitlab/ci-visibility-tests.yml

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,64 @@
1+
check-ci-visibility-label:
2+
stage: publish
3+
image: registry.ddbuild.io/images/dd-octo-sts-ci-base:2025.06-1
4+
tags: [ "arch:amd64" ]
5+
needs: [ publish-artifacts-to-s3 ]
6+
id_tokens:
7+
DDOCTOSTS_ID_TOKEN:
8+
aud: dd-octo-sts
9+
rules:
10+
- if: '$POPULATE_CACHE'
11+
when: never
12+
- if: '$CI_COMMIT_BRANCH && $CI_COMMIT_BRANCH !~ /^(master|release\/)/'
13+
when: on_success
14+
- when: never
15+
before_script:
16+
- dd-octo-sts version
17+
- dd-octo-sts debug --scope DataDog/dd-trace-java --policy self.gitlab.github-access.read
18+
- dd-octo-sts token --scope DataDog/dd-trace-java --policy self.gitlab.github-access.read > github-token.txt
19+
- gh auth login --with-token < github-token.txt
20+
script:
21+
- |
22+
# Install authanywhere - Required for PR comment
23+
wget -q binaries.ddbuild.io/dd-source/authanywhere/LATEST/authanywhere-linux-amd64
24+
chmod +x authanywhere-linux-amd64
25+
26+
# Source utility functions
27+
source .gitlab/ci_visibility_utils.sh
28+
29+
# Get PR number
30+
if ! PR_NUMBER=$(get_pr_number "${CI_COMMIT_BRANCH}"); then
31+
echo "No open PR found for branch ${CI_COMMIT_BRANCH}"
32+
exit 1
33+
fi
34+
35+
echo "Found PR #${PR_NUMBER}"
36+
37+
# Check if PR has the CI visibility label
38+
if pr_has_label "$PR_NUMBER" "comp: ci visibility"; then
39+
echo "PR #${PR_NUMBER} detected as CI Visibility PR"
40+
write_pr_comment "$PR_NUMBER" "Test Optimization - Test Environment" "Triggering test environment. Check [pipeline](https://gitlab.ddbuild.io/DataDog/apm-reliability/test-environment/-/pipelines?ref=main&source=pipeline) for test results."
41+
exit 0
42+
else
43+
echo "PR #${PR_NUMBER} not a CI Visibility PR, ignoring trigger"
44+
exit 1
45+
fi
46+
after_script:
47+
- dd-octo-sts revoke -t $(cat github-token.txt) || true
48+
allow_failure: true
49+
retry:
50+
max: 2
51+
when: always
52+
153
run-ci-visibility-test-environment:
254
stage: ci-visibility-tests
3-
when: manual
4-
needs: []
55+
needs:
56+
- job: check-ci-visibility-label
57+
rules:
58+
- if: '$POPULATE_CACHE'
59+
when: never
60+
- if: '$CI_COMMIT_BRANCH && $CI_COMMIT_BRANCH !~ /^(master|release\/)/'
61+
when: on_success
562
trigger:
663
project: DataDog/apm-reliability/test-environment
764
branch: main

.gitlab/ci_visibility_utils.sh

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/usr/bin/env bash
2+
3+
function get_pr_number() {
4+
local branch=$1
5+
6+
if [ -z "$branch" ]; then
7+
echo "Error: Branch name is required" >&2
8+
return 1
9+
fi
10+
11+
local pr_number
12+
pr_number=$(gh pr list --repo DataDog/dd-trace-java --head "$branch" --state open --json number --jq '.[0].number')
13+
14+
if [ -z "$pr_number" ]; then
15+
echo "Error: No open PR found for branch $branch" >&2
16+
return 1
17+
fi
18+
19+
echo "$pr_number"
20+
return 0
21+
}
22+
23+
function get_pr_labels() {
24+
local pr_number=$1
25+
26+
if [ -z "$pr_number" ]; then
27+
echo "Error: PR number is required" >&2
28+
return 1
29+
fi
30+
31+
local labels
32+
labels=$(gh pr view "$pr_number" --repo DataDog/dd-trace-java --json labels --jq '.labels[].name')
33+
34+
if [ -z "$labels" ]; then
35+
echo "Warning: No labels found for PR #$pr_number" >&2
36+
return 1
37+
fi
38+
39+
echo "$labels"
40+
return 0
41+
}
42+
43+
function pr_has_label() {
44+
local pr_number=$1
45+
local target_label=$2
46+
47+
if [ -z "$pr_number" ] || [ -z "$target_label" ]; then
48+
echo "Error: PR number and label are required" >&2
49+
return 1
50+
fi
51+
52+
local labels
53+
if ! labels=$(get_pr_labels "$pr_number"); then
54+
return 1
55+
fi
56+
57+
if echo "$labels" | grep -q "$target_label"; then
58+
return 0
59+
else
60+
return 1
61+
fi
62+
}
63+
64+
function write_pr_comment() {
65+
local pr_number=$1
66+
local header=$2
67+
local message=$3
68+
69+
if [ -z "$pr_number" ]; then
70+
echo "Error: PR number is required" >&2
71+
return 1
72+
fi
73+
74+
if [ -z "$message" ]; then
75+
echo "Error: Message is required" >&2
76+
return 1
77+
fi
78+
79+
if [ -z "$header" ]; then
80+
header="CI Notification"
81+
fi
82+
83+
# Create JSON payload
84+
local json_payload
85+
json_payload=$(jq -n \
86+
--argjson pr_num "$pr_number" \
87+
--arg message "$message" \
88+
--arg header "$header" \
89+
--arg org "DataDog" \
90+
--arg repo "dd-trace-java" \
91+
'{pr_num: $pr_num, message: $message, header: $header, org: $org, repo: $repo}')
92+
93+
# Ensure authanywhere is available
94+
if [ ! -x "./authanywhere-linux-amd64" ]; then
95+
echo "Error: authanywhere-linux-amd64 not found or not executable" >&2
96+
return 1
97+
fi
98+
99+
# Post comment to PR
100+
echo "Posting comment to PR #${pr_number}"
101+
curl -s 'https://pr-commenter.us1.ddbuild.io/internal/cit/pr-comment' \
102+
-H "$(./authanywhere-linux-amd64)" \
103+
-H "Content-Type: application/json" \
104+
-X PATCH \
105+
-d "$json_payload"
106+
107+
return $?
108+
}

0 commit comments

Comments
 (0)