Skip to content

Add support for Bitbucket API tokens #6209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,37 @@ final class BitbucketRepositoryProvider extends RepositoryProvider {
this.config = config ?: new ProviderConfig('bitbucket')
}

/** {@inheritDoc} */
@Override
protected String[] getAuth() {
return config.token
? new String[] { "Authorization", "Bearer ${config.token}" }
: super.getAuth()
}

@Override
boolean hasCredentials() {
return config.token
? true
: super.hasCredentials()
}

/** {@inheritDoc} */
@Override
String getName() { "BitBucket" }

@Override
String getEndpointUrl() {
"${config.endpoint}/api/2.0/repositories/${project}"
"${config.endpoint}/2.0/repositories/${project}"
}

@Override
String getContentUrl( String path ) {
final ref = revision ? getRefForRevision(revision) : getMainBranch()
return "${config.endpoint}/api/2.0/repositories/$project/src/$ref/$path"
return "${config.endpoint}/2.0/repositories/$project/src/$ref/$path"
}

private String getMainBranchUrl() {
"${config.endpoint}/api/2.0/repositories/$project"
"${config.endpoint}/2.0/repositories/$project"
}

String getMainBranch() {
Expand Down Expand Up @@ -87,7 +101,7 @@ final class BitbucketRepositoryProvider extends RepositoryProvider {
}

private String getRefForRevision0(String revision, String type){
final resp = invokeAndParseResponse("${config.endpoint}/api/2.0/repositories/$project/refs/$type/$revision")
final resp = invokeAndParseResponse("${config.endpoint}/2.0/repositories/$project/refs/$type/$revision")
return resp?.target?.hash
}

Expand Down Expand Up @@ -117,7 +131,7 @@ final class BitbucketRepositoryProvider extends RepositoryProvider {
@Override
List<TagInfo> getTags() {
final result = new ArrayList<TagInfo>()
final url = "$config.endpoint/api/2.0/repositories/$project/refs/tags"
final url = "$config.endpoint/2.0/repositories/$project/refs/tags"
final mapper = { Map entry -> result.add( new TagInfo(entry.name, entry.target?.hash) ) }
invokeAndResponseWithPaging(url, mapper)
return result
Expand All @@ -131,7 +145,7 @@ final class BitbucketRepositoryProvider extends RepositoryProvider {
@Override
List<BranchInfo> getBranches() {
final result = new ArrayList<BranchInfo>()
final url = "$config.endpoint/api/2.0/repositories/$project/refs/branches"
final url = "$config.endpoint/2.0/repositories/$project/refs/branches"
final mapper = { Map entry -> result.add( new BranchInfo(entry.name, entry.target?.hash) ) }
invokeAndResponseWithPaging(url, mapper)
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class ProviderConfig {
case 'bitbucket':
attr.platform = name
if( !attr.server ) attr.server = 'https://bitbucket.org'
if( !attr.endpoint ) attr.endpoint = 'https://api.bitbucket.org'
break

case 'azurerepos':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import spock.lang.IgnoreIf
import spock.lang.Requires
import spock.lang.Specification
import spock.lang.Timeout
import spock.lang.Unroll

@Timeout(30)
@IgnoreIf({System.getenv('NXF_SMOKE')})
Expand All @@ -35,7 +36,7 @@ class BitbucketRepositoryProviderTest extends Specification {
when:
def url = new BitbucketRepositoryProvider('pditommaso/tutorial',config).getCloneUrl()
then:
url == "https://${config.user}@bitbucket.org/pditommaso/tutorial.git".toString()
url ==~ /https:\/\/\w+@bitbucket.org\/pditommaso\/tutorial.git/
}

def testGetHomePage() {
Expand All @@ -50,11 +51,11 @@ class BitbucketRepositoryProviderTest extends Specification {
def config = new ProviderConfig('bitbucket').setAuth(token)

when:
def repo = new BitbucketRepositoryProvider('pditommaso/tutorial', config)
def repo = new BitbucketRepositoryProvider('pditommaso/secret', config)
def result = repo.readText('main.nf')

then:
result.trim().startsWith('#!/usr/bin/env nextflow')
result.trim() == "println 'Hello from Bitbucket'"
}

@Requires( { System.getenv('NXF_BITBUCKET_ACCESS_TOKEN') } )
Expand Down Expand Up @@ -110,17 +111,17 @@ class BitbucketRepositoryProviderTest extends Specification {
expect:
new BitbucketRepositoryProvider('pditommaso/tutorial', config)
.setRevision('test-branch')
.getContentUrl('main.nf') == 'https://bitbucket.org/api/2.0/repositories/pditommaso/tutorial/src/test-branch/main.nf'
.getContentUrl('main.nf') == 'https://api.bitbucket.org/2.0/repositories/pditommaso/tutorial/src/test-branch/main.nf'

and:
new BitbucketRepositoryProvider('pditommaso/tutorial', config)
.setRevision('feature/with-slash')
.getContentUrl('main.nf') == 'https://bitbucket.org/api/2.0/repositories/pditommaso/tutorial/src/a6b825b22d46758cdeb496ae6cf26aef839ace52/main.nf'
.getContentUrl('main.nf') == 'https://api.bitbucket.org/2.0/repositories/pditommaso/tutorial/src/a6b825b22d46758cdeb496ae6cf26aef839ace52/main.nf'

and:
new BitbucketRepositoryProvider('pditommaso/tutorial', config)
.setRevision('test/tag/v2')
.getContentUrl('main.nf') == 'https://bitbucket.org/api/2.0/repositories/pditommaso/tutorial/src/8f849beceb2ea479ef836809ca33d3daeeed25f9/main.nf'
.getContentUrl('main.nf') == 'https://api.bitbucket.org/2.0/repositories/pditommaso/tutorial/src/8f849beceb2ea479ef836809ca33d3daeeed25f9/main.nf'

}

Expand Down Expand Up @@ -171,4 +172,35 @@ class BitbucketRepositoryProviderTest extends Specification {
!data.contains('world')
data.contains('mundo')
}

@Unroll
def 'should validate hasCredentials' () {
given:
def provider = new BitbucketRepositoryProvider('pditommaso/tutorial', CONFIG)

expect:
provider.hasCredentials() == EXPECTED

where:
EXPECTED | CONFIG
false | new ProviderConfig('bitbucket')
false | new ProviderConfig('bitbucket').setUser('foo')
true | new ProviderConfig('bitbucket').setUser('foo').setPassword('bar')
true | new ProviderConfig('bitbucket').setToken('xyz')
}

@Unroll
def 'should validate getAuth' () {
given:
def provider = new BitbucketRepositoryProvider('pditommaso/tutorial', CONFIG)

expect:
provider.getAuth() == EXPECTED as String[]

where:
EXPECTED | CONFIG
null | new ProviderConfig('bitbucket')
["Authorization", "Bearer xyz"] | new ProviderConfig('bitbucket').setToken('xyz')
["Authorization", "Basic ${"foo:bar".bytes.encodeBase64()}"] | new ProviderConfig('bitbucket').setUser('foo').setPassword('bar')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class ProviderConfigTest extends Specification {
then:
config.name == 'bitbucket'
config.server == 'https://bitbucket.org'
config.endpoint == 'https://bitbucket.org'
config.endpoint == 'https://api.bitbucket.org'
config.platform == 'bitbucket'
config.domain == 'bitbucket.org'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class RepositoryProviderTest extends Specification {
provider = RepositoryFactory.newRepositoryProvider(new ProviderConfig('bitbucket'),'project/z')
then:
provider instanceof BitbucketRepositoryProvider
provider.endpointUrl == 'https://bitbucket.org/api/2.0/repositories/project/z'
provider.endpointUrl == 'https://api.bitbucket.org/2.0/repositories/project/z'

when:
provider = RepositoryFactory.newRepositoryProvider(new ProviderConfig('local', [path:'/user/data']),'local/w')
Expand Down