I need to get all the Bitbucket repository variable uuids. For this requirement, I am making use of Bitbucket API. But Bitbucket is not printing all the variables at once. Instead it is giving only a set of values for a request and it is giving a next token.
The requirement to list the Bitbucket repository variable uuids is to update them using an automated pipeline.
I tried to make use of next token with a loop to list the other variables uuids but not able to. I tried making use of python script in Lambda as well as bash script in my local. Nothing is working.
Both the scripts are giving json errors. I tried many ways to resolve them but not able to.
In both the scripts, only first iteration is working. Next ones aren’t.
Attaching the python script of Lambda as well as bash script
**The following is my python script of Lambda:
import json
import boto3
import requests
def lambda_handler(event, context):
access_token = "<token>"
base_url = "https://api.bitbucket.org/2.0/repositories/<workspace>/<repo-slug>/pipelines_config/variables"
headers = {
"Accept": "application/json",
"Authorization" : "Bearer %s" %access_token
}
variables = []
url = base_url
while url:
response_obj = requests.request(
"GET",
url,
headers=headers,
)
data = json.loads(response_obj.text)
variables.extend(data['values'])
url = data['next']
print(variables)
**The following is my bash script:
#!/bin/bash
url="https://api.bitbucket.org/2.0/repositories/<workspace>/<repo-slug>/pipelines_config/variables/"
header="Authorization: Bearer <access-token>"
get_variables() {
next_url=$1
response=$(curl --header $header --header 'Accept: application/json' --url $next_url)
if [[ $? -ne 0 ]]; then
echo "Error: Failed to retrieve repository variables."
exit 1
fi
echo "$response"
}
print_variables() {
response="$1"
variable_names=$(echo "$response" | jq -r '.values[].key')
variable_values=$(echo "$response" | jq -r '.values[].value')
for i in $(seq 0 $(expr $(echo "$response" | jq -r '.values | length') - 1)); do
echo "${variable_names[$i]}=${variable_values[$i]}"
done
}
response=$(get_variables $url)
echo "Repository variables (Page 1):"
print_variables "$response"
# Check if there are more pages
next_url=$(echo "$response" | jq -r '.next')
while [[ "$next_url" != "null" ]]; do
echo "Loading next page..."
response=$(get_variables "$next_url")
print_variables "$response"
next_url=$(echo "$response" | jq -r '.next')
done
Please let me know what am I missing in both the above scripts.
Thanks in advance!!
satya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.