Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring composer.json with private Bitbucket Mercurial repository

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"
    }
}
}
like image 791
Roman Avatar asked Feb 20 '15 09:02

Roman


People also ask

How do I use composer with Bitbucket?

The way Composer will access Bitbucket is through oAuth. So first of all, we’ll need to grab a key and a secret. When you’re logged into Bitbucket, click on your avatar bottom left, and select “All Workspaces” This will list all the workspaces you have access to. You may only have one, or you could have quite a few, depending on the way you work.

Why Bitbucket-OAuth method is buggy in composer?

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.

Why is composer asking for credentials for a protected repository?

Whenever Composer encounters a protected Composer repository it will try to authenticate using already defined credentials first. When none of those credentials apply it will prompt for credentials and save them (or a token if Composer is able to retrieve one).

How do I authenticate a composer repository?

Authentication principles# Whenever Composer encounters a protected Composer repository it will try to authenticate using already defined credentials first. When none of those credentials apply it will prompt for credentials and save them (or a token if Composer is able to retrieve one).


2 Answers

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"
},
like image 112
Petter Kjelkenes Avatar answered Sep 19 '22 13:09

Petter Kjelkenes


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"
        }
    ]
like image 44
Phil Cook Avatar answered Sep 20 '22 13:09

Phil Cook