Configuring composer.json with private Bitbucket Mercurial repository
Asked Answered
J

7

8

My project uses my own library which is in the private Mercurial repository placed on bitbucket.org. That library has no composer.json configured.

I try to make that library as a dependency to my project.

Firstly I wrote to composer.json the following strings:

{
"require": {
    "php": ">=5.4",
    "myname/mylibname": "dev"
},

"repositories":[
    {
        "type": "hg",
        "url" : "https://bitbucket.org/myname/mylibname"
    }
]
}

And running composer install I've got an error:

[RuntimeException]
Failed to clone https://bitbucket.org/myname/mylibname, could not read packages from it
abort: http authorization required

Than I changed "type": "hg" to "type": "vcs" and got another error:

[Composer\Repository\InvalidRepositoryException]
No valid composer.json was found in any branch or tag of https:/***/mylibname, could not load a package from it.

After additional reading of documentation I added description of my library to the composer.json of my project, and it began to look so:

{
"require": {
    "php": ">=5.4",
    "myname/mylibname": "dev"
},

"repositories":[

    {
        "type": "vcs",
        "url" : "https://bitbucket.org/myname/mylibname"
    },
    {
        "type":"package",
        "package":{
            "name":"myname/mylibname",
            "version": "dev",
            "source":{
                "type":"vcs",
                "url":"https://bitbucket.org/myname/mylibname",
                "reference":"dev"
            }
        }
    }
]}

The same error occured:

[Composer\Repository\InvalidRepositoryException]
No valid composer.json was found in any branch or tag of https:/***/mylibname, could not load a package from it.

I removed the part:

        {
        "type": "vcs",
        "url" : "https://bitbucket.org/myname/mylibname"
    },

and got an error:

[InvalidArgumentException]
Unknown downloader type: vcs. Available types: git, svn, hg, perforce, zip, rar, tar, gzip, phar, file.

I changed "type": "vcs" back to "type": "hg", composer.json looks like:

{
"require": {
    "php": ">=5.4",
    "myname/mylibname": "dev"
},

"repositories":[
    {
        "type":"package",
        "package":{
            "name":"myname/mylibname",
            "version": "dev",
            "source":{
                "type":"hg",
                "url":"https://bitbucket.org/myname/mylibname",
                "reference":"dev"
            }
        }
    }
]}

and an error:

[RuntimeException]
Failed to execute hg clone 'https:/***/mylibname' '/path/to/myproject' abort: http authorization required

The structure of my auth.json, which lies besides of composer.json is:

{
"http-basic": {
    "bitbucket.org": {
        "username": "myusername",
        "password": "mypassword"
    }
}
}
Jespersen answered 20/2, 2015 at 9:16 Comment(0)
I
8

Seems like bitbucket-oauth method is buggy in the current state as of 1.1 of composer. This means that either you must have setup the SSH key on the client or if you are like me and cant setup keys because of deployment server, you will have to use basic auth.

The only way I got this working was:

~/.composer/auth.json

{
    "http-basic": {
        "bitbucket.org": {
            "username": "bitbucketUsername",
            "password": "PasswordToBitbucket"
        }
    }
}

composer.json

"repositories": [
        {
            "url": "https://[email protected]/username/my-package.git",
            "type": "git"
        }

],
"require": {
        "username/my-package": "dev-master"
},
Interoffice answered 12/5, 2016 at 7:37 Comment(0)
K
6

Composer as of version 1.2.0 have sorted this with bitbucket oauth, this is a much better method than ssh-keys if multiple developers are working on a project as the auth.json can stay within the project repository (if it's private) and only has to be setup once.

auth.json

{
    "bitbucket-oauth": {
        "bitbucket.org": {
            "consumer-key": "key",
            "consumer-secret": "secret"
        }
    }
}

composer.json

"repositories":[
        {
            "type": "vcs",
            "url":  "[email protected]:path/to.git"
        }
    ]
Koenig answered 26/3, 2019 at 14:47 Comment(3)
From the docs: getcomposer.org/doc/… : Note that the repository endpoint needs to be https rather than git.Landside
@Landside the git version works perfectly fine for me therefore I guess you can use either. Just my preference to use the git endpoint.Koenig
I agree with @aaronbauman. Had to use "type": "git", "url": "https://bitbucket.org/path/to.git" - Also make sure you make your changes in composer.lock as well. :)Folkways
Z
2

Just remove https://. Works for me :)

{
"require": {
    "php": ">=5.4",
    "myname/mylibname": "dev"
},

"repositories":[
    {
        "type":"package",
        "package":{
            "name":"myname/mylibname",
            "version": "dev",
            "source":{
                "type":"hg",
                "url":"bitbucket.org/myname/mylibname",
                "reference":"dev"
            }
        }
    }
]}
Zilpah answered 2/4, 2015 at 15:42 Comment(0)
T
2

That didn't quite work for me, but it got me pointed into the right direction. Make sure you get your SSH key installed to access it via git@.

{
"repositories": [
{
  "type": "package",
  "package": {
    "name": "myname/mylibname",
    "version": "master",
    "source": {
      "type": "git",
      "url": "[email protected]:myname/mylibname.git",
      "reference": "master"
    }
  }
}
]
}
Tungstite answered 26/3, 2016 at 0:51 Comment(1)
Just a little addition to your configuration: "require": { "username/my-package": "dev-master" }Ashil
T
1

One comment on my end. I have tested above scenarios I encountered on composer suggestion that repository needs to have at least one stable version.

https://getcomposer.org/doc/04-schema.md#minimum-stability

Due to this, I used "dev" TAG along with SSH connection and it works.

{
    "require": {
        "php": ">=5.4",
        "myname/mylibname": "dev"
    },

    "repositories":[
    {
        "type":"package",
        "package":{
            "name":"myname/mylibname",
            "version": "dev",
            "source":{
                "type":"git",
                "url":"[email protected]:myname/mylibname.git",
                "reference":"dev"
            }
        }
    }
]}
Troll answered 22/2, 2017 at 21:14 Comment(0)
A
1

I thought I had best contribute to the confusion and share what configuration worked for me. Firstly, I absolutely could not get the recommended setup from composer to work. However, the following did:

1.Edit ~.composer/auth.json and configure the http-basic key.

{
    "bitbucket-oauth": {},
    "github-oauth": {},
    "gitlab-oauth": {},
    "gitlab-token": {},
    "http-basic": {
        "bitbucket.org": {
            "username": "USERNAME",
            "password": "PASSWORD"
        }
    }
}

2.Use the following to define the package in your composer.json (i.e. private repository). Also bare in mind this is not a personal BitBucket account, I am a part of a team so the [email protected] is MY username and second instance is the company (https://{USERNAME}@bitbucket.org/{USERNAME||VENDOR}/{REPO}.git).

"require": {
    "{USERNAME||VENDOR}/{REPO}": "dev-{BRANCH}
}
"repositories:" [
    {
        "type": "package",
           "package": {
            "name": "{USERNAME/VENDOR/REPO}",
            "version": "master",
            "source": {
                "url": 
                "https://{URL}",
                "type": "git",
                "reference": "master"
            }
        }
    }
]

Miscellaneous & noteworthy things to consider:

  • I did not use access keys
  • I do have my SSH key on BitBucket
  • I am an administrator on BitBucket
  • I am target the master branch
  • There are NO tags on the repository
  • Minimum stability is set to dev
  • You will need to add the autoload options to the package (see this)

I hope this helps anyone frustrated by this, it defintely seems as though everybody has issues. Thanks to @Tomasz Czechowski for providing the answer that eventually got this working for me!

Allwein answered 26/9, 2019 at 18:57 Comment(0)
P
1

Another example with bitbucket git repo using OAuth consumer authorization, which is actually working. Note that repo is starting with https, otherwise composer will fail with an error 'undefined offset: 1'.

auth.json

{
    "bitbucket-oauth": {
        "bitbucket.org": {
            "consumer-key": "...",
            "consumer-secret": "..."
        }
    }
}

composer.json

"repositories": [
    {
        "type": "git-bitbucket",
        "url": "https://bitbucket.org/workspace/repo.git"
    }
]
Partlow answered 11/7, 2020 at 10:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.