Skip to content
This repository has been archived by the owner on Nov 19, 2018. It is now read-only.

Commit

Permalink
feat($setup-columns): update columns from setup modal in github #7
Browse files Browse the repository at this point in the history
  • Loading branch information
Ray Nicholus committed May 6, 2014
1 parent 3dc8edf commit d8accc6
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 17 deletions.
54 changes: 43 additions & 11 deletions app/src/github.js
Expand Up @@ -6,20 +6,28 @@ nimbleModule.factory("github", ["token", "$http", "$q",
var apiRoot = "https://api.github.com",
apiVersion = "application/vnd.github.v3+json",

callApi = function(path, _verb, _params) {
callApi = function(path, _verb, _data) {
var deferred = $q.defer(),
verb = _verb || "get",
params = _params || {};

params.access_token = token.get();
params.per_page = 100;
verb = _verb || "GET",
data = _data || {},
params = {
access_token: token.get()
};

if (verb === "GET") {
params.per_page = 100;
}

$http[verb](apiRoot + "/" + path, {
$http({
method: verb,
url: apiRoot + "/" + path,
cache: true,
headers: {
Accept: apiVersion
Accept: apiVersion,
"Content-Type": "application/json;charset=utf-8"
},
params: params
params: params,
data: data
})
.success(function(data, status, headers) {
// grab all pages if more than one page of results exists
Expand Down Expand Up @@ -82,6 +90,24 @@ nimbleModule.factory("github", ["token", "$http", "$q",


return {
createLabel: function(fullRepoName, labelToCreate) {
return callApi("repos/" + fullRepoName + "/labels",
"POST",
{
name: labelToCreate,
color: "FFFFFF"
}
);
},

deleteLabel: function(fullRepoName, labelToDelete) {
return callApi("repos/" + fullRepoName + "/labels/" + labelToDelete, "DELETE");
},

getAuthenticatedUser: function() {
return callApi("user");
},

listAllLabels: function(fullRepoName) {
return callApi("repos/" + fullRepoName + "/labels");
},
Expand All @@ -98,8 +124,14 @@ nimbleModule.factory("github", ["token", "$http", "$q",
return callApi("user/repos");
},

getAuthenticatedUser: function() {
return callApi("user");
updateLabel: function(fullRepoName, labelDiff) {
return callApi("repos/" + fullRepoName + "/labels/" + labelDiff.oldLabel,
"PATCH",
{
name: labelDiff.newLabel,
color: "FFFFFF"
}
);
}
};
}]);
78 changes: 74 additions & 4 deletions app/src/issue/columns.js
Expand Up @@ -7,10 +7,14 @@ nimbleModule.controller("columnsController", ["$scope", "columns",
}
);
}])
.factory("columns", ["$rootScope", "user", "github", function($rootScope, user, github) {
.factory("columns", ["$rootScope", "user", "github", "$q",
function($rootScope, user, github, $q) {

var self = this,

formattedColumnLabels = function(unformattedLabels) {
columnPattern = /^(\d+)(\s*-\s*)(.+)/,

formattedLabels = function(unformattedLabels) {
var columns = [];

unformattedLabels.forEach(function(unformattedLabel) {
Expand All @@ -24,17 +28,83 @@ nimbleModule.controller("columnsController", ["$scope", "columns",
});

return columns;
},

updateLabelsFromNameAndPosition = function(dirtyColumns) {
var dirtyColumnsCopy = angular.copy(dirtyColumns);

dirtyColumnsCopy.forEach(function(column, index) {
var newLabel = index + " - " + column.name;

if (column.label) {
newLabel = column.label.replace(columnPattern, index + "$2" + column.name);
}

column.label = newLabel;
});

return dirtyColumnsCopy;
};

angular.extend(this, {
current: null, // referenced for readability

update: function(newColumns) {
var proposedLabels = updateLabelsFromNameAndPosition(newColumns),
promises = [];

if (!angular.equals(newColumns, this.current)) {
if (self.current.length > 0) {
(function() {
for (var idx = 0; idx < Math.min(self.current.length, proposedLabels.length); idx++) {
var existingLabel = self.current[idx].label,
newLabel = proposedLabels[idx].label;

if (existingLabel !== newLabel) {
promises.push(github.updateLabel(user.selectedRepoName, {
oldLabel: existingLabel,
newLabel: newLabel
}));
}
}
}());
}

(function() {
var idx;

if (self.current.length > proposedLabels.length) {
for (idx = proposedLabels.length; idx < self.current.length; idx++) {
promises.push(github.deleteLabel(user.selectedRepoName, self.current[idx].label));
}
}
else if (proposedLabels.length > self.current.length) {
for (idx = self.current.length; idx < proposedLabels.length; idx++) {
promises.push(
github.createLabel(user.selectedRepoName,
proposedLabels[idx].label
)
);
}
}
}());

this.current = proposedLabels;
}

return $q.all(promises);
}
});

$rootScope.$watch(function() {return user.selectedRepoName;},
function(selectedRepoName) {
if (selectedRepoName) {
github.listAllLabels(selectedRepoName).then(function(allLabels) {
var columnLabels = allLabels.filter(function(label) {
return (/^\d+\s*-\s*.+/).test(label.name);
return (columnPattern).test(label.name);
});

self.current = formattedColumnLabels(columnLabels);
self.current = formattedLabels(columnLabels);
});
}
}
Expand Down
8 changes: 6 additions & 2 deletions app/src/issue/setup-columns.js
Expand Up @@ -12,10 +12,14 @@ var setupColumnsInstanceController = ["$scope", "$modalInstance", "columns",
// in the UI (before save), we must use a deep copy instead.
columns: angular.copy(columns.current),

ok: $modalInstance.close,

remove: function(columnIndex) {
$scope.columns.splice(columnIndex, 1);
},

// TODO show alert on failure
save: function() {
columns.update($scope.columns);
$modalInstance.close();
}
});
}];
Expand Down
42 changes: 42 additions & 0 deletions test/unit/columns-service-spec.js
Expand Up @@ -43,4 +43,46 @@ describe("Columns service", function() {
}
]);
});

// TODO test removed columns
describe("update", function() {
it("does nothing if no changes exist", function() {
this.columns.current = [
{name: "1", label: "0 - 1"},
{name: "2", label: "0 - 2"}
];

this.columns.update(angular.copy(this.columns.current)).then(function(results) {
expect(results.length).toBe(0);
});

this.$rootScope.$digest();
});

// TODO test that proper github svc methods are called
it("handles a new & changed columns correctly", function() {
var proposedColumns = [
{name: "2", label: "0 - 1"},
{name: "5", label: "0 - 2"},
{name: "3", label: "0 - 3"},
{name: "foo"}
],
expectedColumns = [
{name: "2", label: "0 - 2"},
{name: "5", label: "1 - 5"},
{name: "3", label: "2 - 3"},
{name: "foo", label: "3 - foo"}
];

this.columns.current = [
{name: "1", label: "0 - 1"},
{name: "2", label: "0 - 2"},
{name: "3", label: "0 - 3"}
];

this.columns.update(proposedColumns);

expect(this.columns.current).toEqual(expectedColumns);
});
});
});
34 changes: 34 additions & 0 deletions test/unit/github-spec.js
Expand Up @@ -104,4 +104,38 @@ describe("Github API service", function() {

httpBackend.flush();
});

it("creates a label correctly", function() {
httpBackend.expectPOST(githubApiUrl + "/repos/garstasio/foobar/labels?access_token=test", {
name: "0 - testcolumn",
color: "FFFFFF"
}).respond(200);

githubService.createLabel("garstasio/foobar", "0 - testcolumn");

httpBackend.flush();
});

it("deletes a label correctly", function() {
httpBackend.expectDELETE(githubApiUrl + "/repos/garstasio/foobar/labels/0-testcolumn?access_token=test")
.respond(200);

githubService.deleteLabel("garstasio/foobar", "0-testcolumn");

httpBackend.flush();
});

it("updates a label correctly", function() {
httpBackend.expect("PATCH", githubApiUrl + "/repos/garstasio/foobar/labels/0-testcolumn?access_token=test", {
name: "1 - testcolumn",
color: "FFFFFF"
}).respond(200);

githubService.updateLabel("garstasio/foobar", {
oldLabel: "0-testcolumn",
newLabel: "1 - testcolumn"
});

httpBackend.flush();
});
});

0 comments on commit d8accc6

Please sign in to comment.