Skip to content

Commit 006a582

Browse files
feat: Up and Down Script for BitBucket Plugin v1.0.0 (#4949)
* added the sql of bitbucket runner plugin * Update 239_bitbucket_runner_plugin.down.sql * Update 239_bitbucket_runner_plugin.down.sql * bitbucket_runner_plugin.up.sql * Update 239_bitbucket_runner_plugin.up.sql * bitbucket_runner_plugin.up.sql * bitbucket_runner_plugin.up.sql * bitbucket_runner_plugin.up.sql * update the sql script * bitbucket_runner_plugin.down.sql * added the logo of the bitbucket plugin * rename the logo name * Delete assets/bitbucket.jpeg * update the migration number * update the migration number * change the variable name workspaceName, branchName and repoName * change the migration number * change the migration number * Rename bitbucket-logo-plugin.jpeg to bitbucket-logo-plugin.jpg * Delete assets/bitbucket-logo-plugin.jpg * added bitbucket png image * rename the image name * Rename bitbucket-logo-plugin.png to bitbucket-logo-plugin.jpeg * Delete assets/bitbucket-logo-plugin.jpeg * Add files via upload --------- Co-authored-by: Prakarsh <71125043+prakarsh-dt@users.noreply.github.com>
1 parent 1c7b816 commit 006a582

File tree

3 files changed

+210
-0
lines changed

3 files changed

+210
-0
lines changed

assets/bitbucket-logo-plugin.jpeg

7.19 KB
Loading
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
DELETE FROM plugin_step_variable WHERE plugin_step_id in (SELECT ps.id FROM plugin_metadata p inner JOIN plugin_step ps on ps.plugin_id=p.id WHERE p.name='BitBucket Runner Trigger v1.0.0' and ps."index"=1 and ps.deleted=false);
2+
DELETE FROM plugin_stage_mapping where plugin_id=(SELECT id from plugin_metadata where name='BitBucket Runner Trigger v1.0.0');
3+
DELETE FROM plugin_step where plugin_id=(SELECT id FROM plugin_metadata WHERE name='BitBucket Runner Trigger v1.0.0');
4+
DELETE FROM plugin_tag_relation WHERE plugin_id=(SELECT id FROM plugin_metadata WHERE name='BitBucket Runner Trigger v1.0.0');
5+
DELETE FROM pipeline_stage_step_variable where pipeline_stage_step_id in (select id from pipeline_stage_step where name ='BitBucket Runner Trigger v1.0.0');
6+
DELETE FROM pipeline_stage_step WHERE name ='BitBucket Runner Trigger v1.0.0';
7+
DELETE FROM plugin_metadata where name='BitBucket Runner Trigger v1.0.0';
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
INSERT INTO plugin_metadata (id,name,description,type,icon,deleted,created_on,created_by,updated_on,updated_by)
2+
VALUES (nextval('id_seq_plugin_metadata'),'BitBucket Runner Trigger v1.0.0' , 'The plugin enables users to trigger the runner in the BitBucket .','PRESET','https://raw.githubusercontent.com/devtron-labs/devtron/main/assets/bitbucket-logo-plugin.jpeg',false,'now()',1,'now()',1);
3+
4+
INSERT INTO plugin_stage_mapping (id,plugin_id,stage_type,created_on,created_by,updated_on,updated_by)VALUES (nextval('id_seq_plugin_stage_mapping'),
5+
(SELECT id from plugin_metadata where name='BitBucket Runner Trigger v1.0.0'), 0,'now()',1,'now()',1);
6+
7+
INSERT INTO "plugin_pipeline_script" ("id", "script","type","deleted","created_on", "created_by", "updated_on", "updated_by")
8+
VALUES ( nextval('id_seq_plugin_pipeline_script'),
9+
E'#!/bin/bash
10+
11+
# Extract bitbucket username from CI_CD_EVENT variable
12+
if [[ -z "$BitBucketUsername" ]]; then
13+
BitBucketUsername=$(echo "$CI_CD_EVENT" | jq -r \'.commonWorkflowRequest.ciProjectDetails[0].gitOptions.userName\')
14+
fi
15+
16+
# Extract token from CI_CD_EVENT variable
17+
if [[ -z "$BitBucketToken" ]]; then
18+
BitBucketToken=$(echo "$CI_CD_EVENT" | jq -r \'.commonWorkflowRequest.ciProjectDetails[0].gitOptions.password\')
19+
fi
20+
21+
git_repository_url=$(echo "$CI_CD_EVENT" | jq -r \'.commonWorkflowRequest.ciProjectDetails[0].gitRepository\')
22+
23+
# Extract the workspace name and repository name from the gitRepository URL
24+
if [[ -z "$BitBucketWorkspaceName" ]]; then
25+
BitBucketWorkspaceName=$(echo "$git_repository_url" | awk -F \'/\' \'{print $(NF-1)}\' | cut -d \'@\' -f2)
26+
if [[ "$BitBucketWorkspaceName" == *"bitbucket.org:"* ]]; then
27+
# Extract everything after bitbucket.org:
28+
BitBucketWorkspaceName="${BitBucketWorkspaceName#*bitbucket.org:}"
29+
fi
30+
fi
31+
32+
# If BitBucketRepoName not provided my the user then we will extract the name from the git_repository_url
33+
if [[ -z "$BitBucketRepoName" ]]; then
34+
BitBucketRepoName=$(echo "$git_repository_url" | awk -F \'/\' \'{print $NF}\' | sed \'s/.git//\' )
35+
fi
36+
37+
38+
if [[ -z "$BitBucketBranchName" ]]; then
39+
# Set a default value for sourceValue
40+
default_source_value="main"
41+
42+
# Extract sourceType and sourceValue.
43+
source_type=$(echo "$CI_CD_EVENT" | jq -r \'.commonWorkflowRequest.ciProjectDetails[0].sourceType\')
44+
source_value=$(echo "$CI_CD_EVENT" | jq -r \'.commonWorkflowRequest.ciProjectDetails[0].sourceValue\')
45+
46+
# Conditionally assign sourceValue based on sourceType
47+
if [ "$source_type" == "SOURCE_TYPE_BRANCH_FIXED" ]; then
48+
BitBucketBranchName="$source_value"
49+
else
50+
BitBucketBranchName="$default_source_value"
51+
fi
52+
fi
53+
54+
# if the username and token is empty because it doesnot able to extract from the CI_CD_EVENT variable because the pipeline is configured with SSH.
55+
if [[ -z "$BitBucketUsername" || -z "$BitBucketToken" ]]; then
56+
echo "BitBucket username or api/token mandatory for the BitBucket pipelines configured with SSH. Exiting..."
57+
exit 1
58+
fi
59+
60+
# Set default value for StatusTimeOutSeconds to 0 if not provided or not an integer
61+
if ! [[ "$StatusTimeOutSeconds" =~ ^[0-9]+$ ]]; then
62+
StatusTimeOutSeconds=0
63+
fi
64+
65+
# Determine sleep interval based on StatusTimeOutSeconds
66+
if [ "$StatusTimeOutSeconds" -lt "60" ]; then
67+
sleepInterval=$(($StatusTimeOutSeconds / 2))
68+
else
69+
sleepInterval=30
70+
fi
71+
72+
# Function for verify the BitBucketworkspaceName, BitBucketRepoName and bitbucket Access API token
73+
verify(){
74+
curl -s -u $BitBucketUsername:$BitBucketToken --request GET "https://api.bitbucket.org/2.0/repositories/$BitBucketWorkspaceName/$BitBucketRepoName/pipelines/?page=1&pagelen=1&sort=-created_on" --compressed
75+
}
76+
77+
verify_status_code()
78+
{
79+
curl -s -i -u $BitBucketUsername:$BitBucketToken --request GET "https://api.bitbucket.org/2.0/repositories/$BitBucketWorkspaceName/$BitBucketRepoName/pipelines/?page=1&pagelen=1&sort=-created_on" --compressed
80+
81+
}
82+
83+
verify_response=$(verify_status_code)
84+
status_code=$(echo "$verify_response" | sed -n \'s/HTTP\\/[0-9.]* \\([0-9]\\+\\) .*/\\1/p\')
85+
86+
if [[ "$status_code" -eq "401" ]]; then
87+
echo "Error: Unauthorized! Please check Username or api/token provided. Exiting..."
88+
exit 1
89+
elif [[ "$status_code" -ge 300 && "$status_code" -lt 400 ]]; then
90+
verify_response=$(verify)
91+
echo $verify_response, Exiting...
92+
exit 1
93+
fi
94+
95+
# For v1.0, we fixed the type name as branch
96+
type="branch"
97+
98+
# function for trigger a runner in bitbucket
99+
trigger_pipeline() {
100+
curl -s -X POST \\
101+
-u "$BitBucketUsername:$BitBucketToken" \\
102+
-H \'Content-Type: application/json\' \\
103+
"https://api.bitbucket.org/2.0/repositories/$BitBucketWorkspaceName/$BitBucketRepoName/pipelines/" \\
104+
-d \'{"target": {"ref_type": "\'$type\'", "type": "pipeline_ref_target", "ref_name": "\'$BitBucketBranchName\'"}}\' \\
105+
-w " Status_Code %{http_code}"
106+
107+
}
108+
109+
# call the trigger_pipeline function to get the error if we get error otherwise it will triggered successfully.
110+
response=$(trigger_pipeline)
111+
112+
113+
# Extracting status code
114+
status_code=$(echo "$response" | awk \'{print $NF}\')
115+
116+
117+
if [[ "$status_code" -eq "404" ]]; then
118+
echo $response, Exiting...
119+
exit 1
120+
elif [[ "$status_code" -ge 200 && "$status_code" -lt 300 ]]; then
121+
echo "Pipeline triggered successfully..."
122+
fi
123+
124+
125+
# check the status of the pipeline in the bitbucket
126+
check_healthy_status() {
127+
128+
if [ "$StatusTimeOutSeconds" -le "0" ]; then
129+
echo "Skipping application status check. Taking 0 as a default input"
130+
return
131+
fi
132+
133+
local max_wait=$StatusTimeOutSeconds
134+
135+
136+
local start_time=$(date +%s)
137+
138+
while :; do
139+
local current_time=$(date +%s)
140+
local elapsed_time=$((current_time - start_time))
141+
142+
if [ "$elapsed_time" -ge "$max_wait" ]; then
143+
echo "StatusTimeOutSeconds reached without success. Exiting..."
144+
exit 1
145+
fi
146+
147+
resources=$(verify)
148+
149+
stateName=$(echo $resources | jq -r \'.values[].state.name\' )
150+
151+
if [[ $stateName == "COMPLETED" ]]; then
152+
status=$(echo $resources | jq -r \'.values[].state.result.type\')
153+
elif [[ $stateName == "IN_PROGRESS" ]]; then
154+
status=$(echo $resources | jq -r \'.values[].state.stage.type\')
155+
elif [[ $stateName == "PENDING" ]]; then
156+
status="PENDING"
157+
fi
158+
159+
if [[ "$status" == "pipeline_state_in_progress_running" ]]; then
160+
echo "Triggered Pipeline status is progressing..."
161+
elif [[ "$status" == "PENDING" ]]; then
162+
echo "Triggered Pipeline status is pending..."
163+
elif [[ "$status" == "pipeline_state_completed_successful" ]]; then
164+
echo "Pipeline succeeded."
165+
break
166+
elif [[ "$status" == "pipeline_state_completed_failed" ]]; then
167+
echo "Pipeline Failed."
168+
exit 1
169+
elif [[ "$status" == "pipeline_state_in_progress_halted" ]]; then
170+
echo "Pipeline Paused."
171+
exit 1
172+
fi
173+
sleep $sleepInterval
174+
done
175+
}
176+
177+
# # Optionally check the healthy status of the pipeline in bitbucket
178+
sleep 2
179+
check_healthy_status',
180+
181+
'SHELL',
182+
'f',
183+
'now()',
184+
1,
185+
'now()',
186+
1
187+
);
188+
189+
190+
INSERT INTO "plugin_step" ("id", "plugin_id","name","description","index","step_type","script_id","deleted", "created_on", "created_by", "updated_on", "updated_by") VALUES (nextval('id_seq_plugin_step'), (SELECT id FROM plugin_metadata WHERE name='BitBucket Runner Trigger v1.0.0'),'Step 1','BitBucket Runner Trigger v1.0.0','1','INLINE',(SELECT last_value FROM id_seq_plugin_pipeline_script),'f','now()', 1, 'now()', 1);
191+
192+
INSERT INTO plugin_step_variable (id,plugin_step_id,name,format,description,is_exposed,allow_empty_value,default_value,value,variable_type,value_type,previous_step_index,variable_step_index,variable_step_index_in_plugin,reference_variable_name,deleted,created_on,created_by,updated_on,updated_by)
193+
VALUES
194+
(nextval('id_seq_plugin_step_variable'),(SELECT ps.id FROM plugin_metadata p inner JOIN plugin_step ps on ps.plugin_id=p.id WHERE p.name='BitBucket Runner Trigger v1.0.0' and ps."index"=1 and ps.deleted=false),'BitBucketUsername','STRING','Enter BitBucket Username (mandatory when pipeline configured with SSH)','t','t',null,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1),
195+
(nextval('id_seq_plugin_step_variable'),(SELECT ps.id FROM plugin_metadata p inner JOIN plugin_step ps on ps.plugin_id=p.id WHERE p.name='BitBucket Runner Trigger v1.0.0' and ps."index"=1 and ps.deleted=false),'BitBucketToken','STRING','Enter BitBucket api/token (mandatory when pipeline configured with SSH)','t','t',null,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1),
196+
(nextval('id_seq_plugin_step_variable'),(SELECT ps.id FROM plugin_metadata p inner JOIN plugin_step ps on ps.plugin_id=p.id WHERE p.name='BitBucket Runner Trigger v1.0.0' and ps."index"=1 and ps.deleted=false),'BitBucketWorkspaceName','STRING','Enter Workspace Name','t','t',null,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1),
197+
(nextval('id_seq_plugin_step_variable'),(SELECT ps.id FROM plugin_metadata p inner JOIN plugin_step ps on ps.plugin_id=p.id WHERE p.name='BitBucket Runner Trigger v1.0.0' and ps."index"=1 and ps.deleted=false),'BitBucketRepoName','STRING','Enter repository name in the bitbucket','t','t',null,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1),
198+
(nextval('id_seq_plugin_step_variable'),(SELECT ps.id FROM plugin_metadata p inner JOIN plugin_step ps on ps.plugin_id=p.id WHERE p.name='BitBucket Runner Trigger v1.0.0' and ps."index"=1 and ps.deleted=false),'BitBucketBranchName','STRING','Enter branch name.','t','t',null,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1),
199+
(nextval('id_seq_plugin_step_variable'),(SELECT ps.id FROM plugin_metadata p inner JOIN plugin_step ps on ps.plugin_id=p.id WHERE p.name='BitBucket Runner Trigger v1.0.0' and ps."index"=1 and ps.deleted=false),'StatusTimeOutSeconds','STRING','Enter the maximum time (in seconds) a user can wait for the application to deploy.Enter a positive integer value','t','t',0,null,'INPUT','NEW',null,1,null,null,'f','now()',1,'now()',1);
200+
201+
202+
203+

0 commit comments

Comments
 (0)