Create a Streamlit app to interact with Bitbucket using `atlassian-python-api`. Design a UI with fields for: `base_url`, `token`, `project_key`, `repo_name`, `repo_description`, `branches`, `repo_url`, `local_repo_path`, `default_branch_name`. Authentication should use token, not username/password. The app must fulfill these requirements: 1. **Create Repository**: Check if the repository exists, if not, create it with `atlassian-python-api` using `project_key` and `repo_name`. Stop if unsuccessful. 2. **Set Default Branch**: Set the default branch (`default_branch_name`) for the repository. Stop if unsuccessful. 3. **Verify Repository State**: Confirm repository existence and if it has no commits. If empty, proceed to Git initialization. 4. **Initialize Git Repository**: Using `gitpython`, clone the repository to `local_repo_path`, create the default branch, make an initial commit, and push. Stop if unsuccessful. 5. **Create Additional Branches**: Create additional branches (`branches`) starting from `default_branch_name`. If any fail, log but continue. **Non-functional Requirements**: - **Error Handling**: Use try-except blocks. Log success, failure, and skipped actions using `logger`. - **Atomicity**: Each step (create repo, set default branch, Git init, create branches) must succeed independently. - **Idempotency**: Ensure operations can be safely re-run. If repo or branches already exist, skip them and log. Implement the Git operations with `gitpython` to temporarily clone, create a branch, add a file, commit, and push. Assume you need to design a suitable UI and workflow to manage these requirements in Streamlit.
To upload files, please first save the app
import streamlit as st
from atlassian import Bitbucket
import os
import git
import logging
# Set up logger
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Function to check repository existence
def repo_exists(bitbucket, project_key, repo_name):
try:
repos = bitbucket.get_repositories(project_key)
return any(repo['name'] == repo_name for repo in repos)
except Exception as e:
logger.error(f'Error checking repository existence: {e}')
return False
# Function to create repository
def create_repository(bitbucket, project_key, repo_name, repo_description):
try:
bitbucket.create_repository(
project_key,
repo_name,
description=repo_description,
is_private=True
)
logger.info(f'Repository {repo_name} created successfully.')
except Exception as e:
logger.error(f'Failed to create repository {repo_name}: {e}')
return False
return True
# Function to set default branch
def set_default_branch(bitbucket, project_key, repo_name, default_branch_name):
try:
bitbucket.set_default_branch(project_key, repo_name, default_branch_name)
logger.info(f'Default branch set to {default_branch_name}.')
except Exception as e:
logger.error(f'Failed to set default branch: {e}')
return False
return True
# Function to initialize Git repository
def initialize_git_repo(repo_url, local_repo_path, default_branch_name):
try:
if os.path.exists(local_repo_path):
logger.info(f'Repository already exists at {local_repo_path}. Skipping clone.')
else:
logger.info(f'Cloning repository from {repo_url}.')
git.Repo.clone_from(repo_url, local_repo_path)
repo = git.Repo(local_repo_path)
# Create and checkout the default branch
repo.git.checkout('-b', default_branch_name)
# Create an initial file and commit
with open(os.path.join(local_repo_path, 'README.md'), 'w') as f:
f.write('# Initial Commit\n')
repo.index.add(['README.md'])
repo.index.commit('Initial commit')
repo.git.push('--set-upstream', 'origin', default_branch_name)
logger.info(f'Initialized Git repository and made first commit on branch {default_branch_name}.')
except Exception as e:
logger.error(f'Error initializing git repository: {e}')
return False
return True
# Function to create branches
def create_branches(repo, branches, default_branch_name):
for branch in branches:
try:
repo.git.checkout('-b', branch, default_branch_name)
repo.index.commit(f'Created branch {branch}')
repo.git.push('--set-upstream', 'origin', branch)
logger.info(f'Branch {branch} created successfully.')
except Exception as e:
logger.warning(f'Failed to create branch {branch}: {e}')
# Streamlit UI
st.title('Bitbucket Repository Manager')
base_url = st.text_input('Base URL')
token = st.text_input('Token', type='password')
project_key = st.text_input('Project Key')
repo_name = st.text_input('Repository Name')
repo_description = st.text_input('Repository Description')
branches = st.text_area('Branches (comma separated)').split(',')
local_repo_path = st.text_input('Local Repository Path')
default_branch_name = st.text_input('Default Branch Name')
if st.button('Create/Manage Repository'):
bitbucket = Bitbucket(
url=base_url,
token=token
)
if not repo_exists(bitbucket, project_key, repo_name):
if create_repository(bitbucket, project_key, repo_name, repo_description):
st.success('Repository created successfully.')
else:
st.error('Failed to create repository.')
else:
st.info('Repository already exists. Skipping creation.')
if set_default_branch(bitbucket, project_key, repo_name, default_branch_name):
st.success('Default branch set successfully.')
else:
st.error('Failed to set default branch.')
repo_url = f'{base_url}/{project_key}/{repo_name}'
if initialize_git_repo(repo_url, local_repo_path, default_branch_name):
st.success('Git repository initialized successfully.')
repo = git.Repo(local_repo_path)
create_branches(repo, branches, default_branch_name)
else:
st.error('Failed to initialize Git repository.')
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?