Skip to main content

Enhanced Python GitLab client.

Project description

PyPI version Python Development Status Maintenance PyPI License


Python GitLab Plus

An enhanced Python client for GitLab that extends the functionality of the official python-gitlab package, providing better error handling, merge request management, branch operations, and more.


Features

  • ✅ Simplified connection to GitLab Cloud and Self-hosted instances
  • ✅ Robust error handling with comprehensive logging
  • Service-based architecture for organized functionality
  • Project management (members, info)
  • Branch operations (create, delete, protect, list)
  • Merge Request management (create, merge, approve, assign, comment)
  • Pipeline operations (trigger, monitor, wait for completion)
  • Tag management (create, delete, list)
  • File operations (read, create, update, delete)
  • Advanced MR features (approval, assignment, state management)

Installation

pip install python-gitlab-plus

Configuration

The package uses environment variables for authentication and configuration:

# Required environment variables
GITLAB_ACCESS_TOKEN=your_gitlab_access_token
GITLAB_URL=https://gitlab.com  # Your GitLab instance URL (default: gitlab.com)

Examples

Basic Setup and Connection

from python_gitlab_plus import GitLabClient

# Initialize GitLab client with service-based architecture
gitlab_client = GitLabClient(
    gitlab_url="https://gitlab.com",
    access_token="your_access_token",  # Note: parameter name changed
    project_id="your-project-id"
)

# Access different services
project_service = gitlab_client.project
branch_service = gitlab_client.branch
mr_service = gitlab_client.merge_request
pipeline_service = gitlab_client.pipeline
tag_service = gitlab_client.tag
file_service = gitlab_client.file

Branch Management

from python_gitlab_plus import GitLabClient

gitlab_client = GitLabClient(
    gitlab_url="https://gitlab.com",
    access_token="your_access_token",
    project_id="your-project-id"
)

# Create a new branch
branch = gitlab_client.branch.create(
    branch_name="feature/new-feature",
    from_branch="main"
)
print(f"Created branch: {branch.name}")

# List branches
branches = gitlab_client.branch.list()
for branch in branches:
    print(f"Branch: {branch.name}")

# Protect a branch
gitlab_client.branch.protect("main")

# Delete a branch
gitlab_client.branch.delete("feature/old-feature")

Merge Request Management

from python_gitlab_plus import GitLabClient

gitlab_client = GitLabClient(
    gitlab_url="https://gitlab.com",
    access_token="your_access_token",
    project_id="your-project-id"
)

# Create a merge request
mr = gitlab_client.merge_request.create(
    title="Add new feature",
    from_branch="feature/new-feature",
    target="main"
)
print(f"Created MR: !{mr.iid}")

# Assign MR to a user
gitlab_client.merge_request.assign(mr.iid, "username")

# Add a comment
gitlab_client.merge_request.add_comment(mr.iid, "Great work!")

# Approve the MR
gitlab_client.merge_request.approve(mr.iid)

# Merge the MR
gitlab_client.merge_request.merge(mr.iid)

Pipeline Operations

from python_gitlab_plus import GitLabClient

gitlab_client = GitLabClient(
    gitlab_url="https://gitlab.com",
    access_token="your_access_token",
    project_id="your-project-id"
)

# Trigger a pipeline
pipeline = gitlab_client.pipeline.trigger(
    branch_name="main",
    variables={"ENVIRONMENT": "production"}
)
print(f"Pipeline triggered: {pipeline.id}")

# Check pipeline status
status = gitlab_client.pipeline.status(pipeline.id)
print(f"Pipeline status: {status}")

# Wait for pipeline completion
final_status = gitlab_client.pipeline.wait_until_finished(
    pipeline.id,
    check_interval=30,
    timeout=3600
)
print(f"Pipeline completed with status: {final_status}")

File Operations

from python_gitlab_plus import GitLabClient

gitlab_client = GitLabClient(
    gitlab_url="https://gitlab.com",
    access_token="your_access_token",
    project_id="your-project-id"
)

# Read file content
file_content = gitlab_client.file.fetch_content(
    file_path="README.md",
    ref="main"
)
print(f"File content: {file_content[:100]}...")

# Create a new file
gitlab_client.file.create(
    file_path="new-feature.py",
    branch="feature/new-feature",
    content="# New feature implementation\nprint('Hello World')",
    commit_message="Add new feature implementation"
)

# Update an existing file
gitlab_client.file.update(
    file_path="README.md",
    branch="feature/update-readme",
    content="# Updated README\nThis is the new content",
    commit_message="Update README with new information"
)

# Delete a file
gitlab_client.file.delete(
    file_path="old-file.txt",
    branch="feature/cleanup",
    commit_message="Remove old file"
)

Tag Management

from python_gitlab_plus import GitLabClient

gitlab_client = GitLabClient(
    gitlab_url="https://gitlab.com",
    access_token="your_access_token",
    project_id="your-project-id"
)

# Create a tag
tag = gitlab_client.tag.create(
    tag_name="v1.0.0",
    from_branch="main",
    message="Release version 1.0.0"
)
print(f"Created tag: {tag.name}")

# List tags
tags = gitlab_client.tag.list()
for tag in tags:
    print(f"Tag: {tag.name}")

# Delete a tag
gitlab_client.tag.delete("v0.9.0")

Project Management

from python_gitlab_plus import GitLabClient

gitlab_client = GitLabClient(
    gitlab_url="https://gitlab.com",
    access_token="your_access_token",
    project_id="your-project-id"
)

# Get project information
project_info = gitlab_client.project.get_info()
print(f"Project: {project_info.name}")
print(f"Description: {project_info.description}")

# List project members
members = gitlab_client.project.list_members()
for member in members:
    print(f"Member: {member.username}")

# Add a member
gitlab_client.project.add_member("newuser", 30)  # 30 = Developer access level

# Remove a member
gitlab_client.project.remove_member("olduser")

🤝 Contributing

If you have a helpful tool, pattern, or improvement to suggest: Fork the repo
Create a new branch
Submit a pull request
I welcome additions that promote clean, productive, and maintainable development.


🙏 Thanks

Thanks for exploring this repository!
Happy coding!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_gitlab_plus-1.1.0.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_gitlab_plus-1.1.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file python_gitlab_plus-1.1.0.tar.gz.

File metadata

  • Download URL: python_gitlab_plus-1.1.0.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for python_gitlab_plus-1.1.0.tar.gz
Algorithm Hash digest
SHA256 172b62d883ab897f41e0417f6e95d14a2aa30be282726fe24158977371b139bc
MD5 8f113b27887febb29c406ce9fba8c49e
BLAKE2b-256 e3c0816797fb9a167b55c7f7fe1ebba0b8ea58f07caa34d373a6438c5183c3ca

See more details on using hashes here.

File details

Details for the file python_gitlab_plus-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_gitlab_plus-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c5b0c3848624ba7102c258e8699222d795666a3a71ab9f687901f027ba2df98f
MD5 d7f3d2c933d51ca66843c77000ad5582
BLAKE2b-256 16d7ab1116f510de4a0102745b882cd9b4f9ea4e7dde1717dc8abef84b2100b3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page