Aaron
Aaron

Reputation: 3419

BitBucket Server API Call For Deleting and/or Renaming Content

Looking here for an API endpoint to delete and/or rename a piece of content in a repository. I don't see anything relevant. How does one do this? https://developer.atlassian.com/bitbucket/api/2/reference/

Upvotes: 1

Views: 2271

Answers (3)

Euclécio
Euclécio

Reputation: 174

A way I found to delete files using the API was with a shell file delete-file.sh:

#!/usr/bin/env bash
set -e

host=https://api.bitbucket.org/2.0
workspace=my-workspace
author="MY COMPANY Automation Bot <[email protected]>"

TOKEN=${1:?'TOKEN: The first param is missing'}
REPO_SLUG=${2:?'REPO_SLUG: The second param is missing'}
BRANCH=${3:?'BRANCH: The third param is missing'}
FILE_PATH=${4:?'FILE_PATH: The forth param is missing'}

curl "$host/repositories/$workspace/$REPO_SLUG/src" \
  -H "Authorization: Bearer $TOKEN" \
  -F "files=/$FILE_PATH" \
  -F "message=Delete $FILE_PATH" \
  -F "branch=$BRANCH" \
  -F "author=$author"

Then just run the command to create a commit with the deleted file in the branch desired:

./delete-file.sh AAAAyyyy0000_my_token my-repo-slug my-branch src/file.txt

You can generate the token with client credentials:

curl -sX POST -u "$BITBUCKET_CLIENT_ID:$BITBUCKET_SECRET" \
      "https://bitbucket.org/site/oauth2/access_token" \
      -d grant_type=client_credentials -d scopes=repository | jq -r .access_token

Or, as my case, using python:

import requests
from requests.auth import HTTPBasicAuth
import subprocess
import os

BITBUCKET_CLIENT_ID = os.getenv('BITBUCKET_CLIENT_ID')
BITBUCKET_SECRET = os.getenv('BITBUCKET_SECRET')

def get_token():
    url = "https://bitbucket.org/site/oauth2/access_token"
    payload = {
        "grant_type": "client_credentials",
        "scopes": "repository"
    }
    response = requests.post(url, data=payload, auth = HTTPBasicAuth(BITBUCKET_CLIENT_ID, BITBUCKET_SECRET))
    return response.json()['access_token']

token = get_token()

subprocess.call([
    "sh",
    "scripts/delete-file.sh",
    token,
    "my-repo-slug",
    "my-branch",
    "src/file.txt"
])

Upvotes: 0

Vikas
Vikas

Reputation: 7185

/2.0/repositories/{username}/{repo_slug}/src This API can be used to update or delete files.

From docs

To create a commit that deletes files, use the files parameter:

$ curl https://api.bitbucket.org/2.0/repositories/username/slug/src \ -F files=/file/to/delete/1.txt \ -F files=/file/to/delete/2.txt

You can add/modify/delete multiple files in a request. Rename/move a file by deleting the old path and adding the content at the new path.

Upvotes: 0

user4410678
user4410678

Reputation:

Unfortunately, in May 2017 Atlassian stated that it is not supported.

When looking at the Bitbucket REST API of version 5.10.1 in June 2018, it is still not supported.

There is a files endpoint at /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/files, but it only has a GET endpoint to list the files in a specific directory of the repository.

There also is a /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/browse/{path:.*} endpoint. It supports GET to list the files in a directory of a repository and supports PUT to commit one file per call. However, DELETE is not supported on that endpoint.

The same goes for renames. The documentation does not mention the ability to do so with a REST API call.

Upvotes: 2

Related Questions