Skip to content

Instantly share code, notes, and snippets.

@michaelschofield
Last active January 18, 2017 19:25
Show Gist options
  • Save michaelschofield/4e108aa4637744c033c598f1ba1ed321 to your computer and use it in GitHub Desktop.
Save michaelschofield/4e108aa4637744c033c598f1ba1ed321 to your computer and use it in GitHub Desktop.
/**
* Hi. Michael here. This code is just abstracted from a bigger prototypal thing
* but you might find it informative for interacting with the Bitbucket API.
* We're still using Angular 1.5.x, and this def needs some refactoring.
* Thanks for being polite :). - @schoeyfield
*/
/**
* Create a function for connecting with the Bitbucket API.
* @returns an object
*/
dataService.prototype.getBitbucketIssues = function( repository, state, kind, assignee ) {
var _this = this, query, kind, assignee;
switch ( state ) {
case 'open' :
query = '?q=state="open"';
break;
case 'new' :
query = '?q=state="new"';
break;
case 'resolved' :
query = '?q=state="resolved"';
break;
default :
query = '?q=state!="resolved"';
break;
}
return _this.$http.get('https://api.bitbucket.org/2.0/repositories/YOUR+REPO/' + repository + '/issues' + query + ( kind ? ' AND kind="' + kind + '"': '') + ( assignee ? ' AND assignee.username="' + assignee + '"': '') )
.then(function(response){
if (typeof response.data === 'object') {
return response.data;
} else {
return response.data;
}
}, function(response) {
return _this.$q.reject(response.data);
})
};
/**
* Module for our IssuesController. Gets compiled during build process.
*/
'use strict';
// Pass dataService and angular $routeParams. The latter lets us use the
// url (e.g., /tickets/wp-theme) to inform the API.
var IssuesController = function( dataService, $routeParams ) {
var vm = this;
vm.repositories = [
'a-list-of-repositories',
'from-somewhere',
'or-entered-manually-like-this'
];
function getBitbucketIssues( repository, state, kind, assignee ){
dataService.getBitbucketIssues( repository, state, kind, assignee ).then(function( data ){
if( data ) {
vm.issues = data.values;
}
});
}
if ( $routeParams.repository ) { // looking for a url like /tickets/repositoryHere/
var repository = $routeParams.repository;
var state = ( $routeParams.state ? $routeParams.state : 'any' ); // open, closed, on-hold, ...
var kind = ( $routeParams.kind ? $routeParams.kind : '' ); // bug, proposal, task, ...
var assignee = ( $routeParams.assignee ? $routeParams.assignee : '' );
vm.repository = repository;
// If instead of a specific repo, "any" is passed, retrieve everything
if ( repository === 'any' ) {
vm.repositories.forEach( function( repository ) {
getBitbucketIssues( repository, state, kind, assignee );
});
}
else {
getBitbucketIssues( repository, state, kind, assignee );
}
}
return false;
} // closing bracket
IssuesController.$inject = [ 'dataService', '$routeParams' ];
module.exports = IssuesController;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment