Skip to content

Commit

Permalink
chore: add UserDescriptor to branches.list (#293)
Browse files Browse the repository at this point in the history
* chore: add UserDescriptor to branches.list

* fix: test

* fix: cleanup

* fix: feedback + docs

* Revert "fix: feedback + docs"

This reverts commit 02afb20.

* progress: allow project & user filter

* progress: update docs + type definitions

* Update docs/abstract-api.md

Co-authored-by: Tom Moor <[email protected]>

* fix: stringify once

Co-authored-by: Tom Moor <[email protected]>
  • Loading branch information
Tim and Tom Moor authored Jul 16, 2020
1 parent 1737299 commit 0849b63
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 11 deletions.
2 changes: 1 addition & 1 deletion abstract-sdk.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ interface Assets extends Endpoint {
interface Branches extends Endpoint {
info(descriptor: BranchDescriptor, requestOptions?: RequestOptions): Promise<Branch>;
list(
descriptor?: ProjectDescriptor,
descriptor?: ProjectDescriptor | UserDescriptor | ProjectMembershipDescriptor,
options?: RequestOptions & { filter?: "active" | "archived" | "mine", search?: string }
): Promise<Branch[]>;
mergeState(descriptor: BranchDescriptor, options?: { parent?: string }): Promise<BranchMergeState>;
Expand Down
23 changes: 21 additions & 2 deletions docs/abstract-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,25 @@ abstract.branches.list({
});
```

List all active branches for a user in a project

```js
abstract.branches.list({
userId: "48b5d670-2002-45ea-929d-4b00863778e4",
projectId: "616daa90-1736-11e8-b8b0-8d1fec7aef78"
}, {
filter: "active"
});
```

List all branches for a user across all organizations

```js
abstract.branches.list({
userId: "48b5d670-2002-45ea-929d-4b00863778e4"
});
```

Search for a branch by name across all projects

```js
Expand Down Expand Up @@ -2390,7 +2409,7 @@ Reference for the parameters required to load resources with the Abstract SDK.
}
```

### OrganizationProjectDescriptor
### ProjectMembershipDescriptor

```js
{
Expand All @@ -2399,7 +2418,7 @@ Reference for the parameters required to load resources with the Abstract SDK.
}
```

### ProjectMembershipDescriptor
### OrganizationMembershipDescriptor

```js
{
Expand Down
34 changes: 26 additions & 8 deletions src/endpoints/Branches.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import type {
BranchMergeState,
ListOptions,
ProjectDescriptor,
RequestOptions
RequestOptions,
UserDescriptor
} from "../types";
import Endpoint from "../endpoints/Endpoint";
import { wrap } from "../util/helpers";
Expand Down Expand Up @@ -47,7 +48,7 @@ export default class Branches extends Endpoint {
}

list(
descriptor?: ProjectDescriptor,
descriptor?: ProjectDescriptor | UserDescriptor,
options: {
...ListOptions,
filter?: "active" | "archived" | "mine",
Expand All @@ -58,17 +59,34 @@ export default class Branches extends Endpoint {

return this.configureRequest<Promise<Branch[]>>("list", {
api: async () => {
const query = querystring.stringify({ limit, offset, filter, search });
const requestUrl = descriptor
? `projects/${descriptor.projectId}/branches/?${query}`
: `branches/?${query}`;
let queryOptions = { limit, offset, filter, search };
let response = null;

if (descriptor && descriptor.userId) {
queryOptions = {
...queryOptions,
userId: descriptor.userId
};
}

const query = querystring.stringify(queryOptions);

if (descriptor && descriptor.projectId) {
response = await this.apiRequest(
`projects/${descriptor.projectId}/branches/?${query}`,
{ headers }
);
} else {
response = await this.apiRequest(`branches/?${query}`, {
headers
});
}

const response = await this.apiRequest(requestUrl, { headers });
return wrap(response.data.branches, response);
},

cli: async () => {
if (!descriptor) {
if (!descriptor || (descriptor && !descriptor.projectId)) {
throw new BranchSearchCLIError();
}

Expand Down
20 changes: 20 additions & 0 deletions tests/endpoints/Branches.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,26 @@ describe("branches", () => {
]);
});

test("api - all branches for a user", async () => {
mockAPI("/branches/?userId=1234", {
data: {
branches: [
{
id: "branch-id"
}
]
}
});

const response = await API_CLIENT.branches.list({ userId: "1234" });

expect(response).toEqual([
{
id: "branch-id"
}
]);
});

test("cli", async () => {
mockCLI(["branches", "list", "--project-id=project-id"], {
branches: [
Expand Down

0 comments on commit 0849b63

Please sign in to comment.