Skip to content

Commit c4f6717

Browse files
author
naman-contentstack
committed
feat: add support for branch settings
1 parent 36d5350 commit c4f6717

File tree

4 files changed

+117
-41
lines changed

4 files changed

+117
-41
lines changed

.talismanrc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ fileignoreconfig:
3636
checksum: 2597efae3c1ab8cc173d5bf205f1c76932211f8e0eb2a16444e055d83481976c
3737
- filename: test/unit/concurrency-Queue-test.js
3838
checksum: 186438f9eb9ba4e7fd7f335dbea2afbae9ae969b7ae3ab1b517ec7a1633d255e
39+
- filename: lib/stack/branch/index.js
40+
checksum: f09b33c0a144139b7ffddcd7dc39fa6cee3bc42eadc06f11c8495c48d740341c
3941
version: "1.0"
40-
41-
42-
43-

lib/stack/branch/index.js

Lines changed: 64 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
import cloneDeep from 'lodash/cloneDeep'
2-
import { create, query, fetch, deleteEntity } from '../../entity'
3-
import { Compare } from './compare'
4-
import { MergeQueue } from './mergeQueue'
5-
import error from '../../core/contentstackError'
1+
import cloneDeep from "lodash/cloneDeep";
2+
import { create, query, fetch, deleteEntity } from "../../entity";
3+
import { Compare } from "./compare";
4+
import { MergeQueue } from "./mergeQueue";
5+
import error from "../../core/contentstackError";
66

77
/**
88
*
99
* @namespace Branch
1010
*/
11-
export function Branch (http, data = {}) {
12-
this.stackHeaders = data.stackHeaders
13-
this.urlPath = `/stacks/branches`
11+
export function Branch(http, data = {}) {
12+
this.stackHeaders = data.stackHeaders;
13+
this.urlPath = `/stacks/branches`;
1414

15-
data.branch = data.branch || data.branch_alias
16-
delete data.branch_alias
15+
data.branch = data.branch || data.branch_alias;
16+
delete data.branch_alias;
1717

1818
if (data.branch) {
19-
Object.assign(this, cloneDeep(data.branch))
20-
this.urlPath = `/stacks/branches/${this.uid}`
19+
Object.assign(this, cloneDeep(data.branch));
20+
this.urlPath = `/stacks/branches/${this.uid}`;
2121

2222
/**
2323
* @description The Delete Branch call is used to delete an existing Branch permanently from your Stack.
@@ -31,7 +31,7 @@ export function Branch (http, data = {}) {
3131
* client.stack({ api_key: 'api_key'}).branch('branch_uid').delete()
3232
* .then((response) => console.log(response.notice))
3333
*/
34-
this.delete = deleteEntity(http, true)
34+
this.delete = deleteEntity(http, true);
3535

3636
/**
3737
* @description The fetch Branch call fetches Branch details.
@@ -46,7 +46,7 @@ export function Branch (http, data = {}) {
4646
* .then((branch) => console.log(branch))
4747
*
4848
*/
49-
this.fetch = fetch(http, 'branch')
49+
this.fetch = fetch(http, "branch");
5050

5151
/**
5252
* @description Compare allows you to compare any or specific ContentType or GlobalFields.
@@ -61,15 +61,39 @@ export function Branch (http, data = {}) {
6161
*
6262
*/
6363
this.compare = (compareBranchUid) => {
64-
const compareData = { stackHeaders: this.stackHeaders }
64+
const compareData = { stackHeaders: this.stackHeaders };
6565
if (compareBranchUid) {
6666
compareData.branches = {
6767
base_branch: this.uid,
68-
compare_branch: compareBranchUid
69-
}
68+
compare_branch: compareBranchUid,
69+
};
7070
}
71-
return new Compare(http, compareData)
72-
}
71+
return new Compare(http, compareData);
72+
};
73+
74+
/**
75+
* @description The Update Branch Settings call updates the settings of an existing Branch.
76+
* @memberof Branch
77+
* @func updateSettings
78+
* @param {IBranchSettings} payload - The settings payload to update.
79+
* @returns {Promise<Object>} Response Object.
80+
* @example
81+
* import * as contentstack from '@contentstack/management'
82+
* const client = contentstack.client()
83+
* client.stack({ api_key: 'api_key' }).branch('branch_uid').updateSettings(payload)
84+
* .then((response) => console.log(response.notice))
85+
*/
86+
this.updateSettings = async (payload) => {
87+
try {
88+
const response = await http.post(`${this.urlPath}/settings`, payload, {
89+
headers: { ...cloneDeep(this.stackHeaders) },
90+
});
91+
if (response.data) return response.data;
92+
else throw error(response);
93+
} catch (e) {
94+
throw error(e);
95+
}
96+
};
7397
} else {
7498
/**
7599
* @description The Create a Branch call creates a new branch in a particular stack of your Contentstack account.
@@ -87,7 +111,7 @@ export function Branch (http, data = {}) {
87111
* client.stack({ api_key: 'api_key'}).branch().create({ branch })
88112
* .then((branch) => { console.log(branch) })
89113
*/
90-
this.create = create({ http: http })
114+
this.create = create({ http: http });
91115

92116
/**
93117
* @description The 'Get all Branch' request returns comprehensive information about branches created in a Stack.
@@ -102,7 +126,7 @@ export function Branch (http, data = {}) {
102126
* client.stack({ api_key: 'api_key'}).branch().query().find()
103127
* .then((collection) => { console.log(collection) })
104128
*/
105-
this.query = query({ http, wrapperCollection: BranchCollection })
129+
this.query = query({ http, wrapperCollection: BranchCollection });
106130

107131
/**
108132
* @description Merge allows user to merge branches in a Stack.
@@ -135,19 +159,19 @@ export function Branch (http, data = {}) {
135159
* client.stack({ api_key: 'api_key'}).branch().merge(mergeObj, params)
136160
*/
137161
this.merge = async (mergeObj, params) => {
138-
const url = '/stacks/branches_merge'
162+
const url = "/stacks/branches_merge";
139163
const header = {
140164
headers: { ...cloneDeep(this.stackHeaders) },
141-
params: params
142-
}
165+
params: params,
166+
};
143167
try {
144-
const response = await http.post(url, mergeObj, header)
145-
if (response.data) return response.data
146-
else throw error(response)
168+
const response = await http.post(url, mergeObj, header);
169+
if (response.data) return response.data;
170+
else throw error(response);
147171
} catch (e) {
148-
throw error(e)
172+
throw error(e);
149173
}
150-
}
174+
};
151175

152176
/**
153177
* @description Merge Queue provides list of all recent merge jobs in a Stack.
@@ -163,16 +187,19 @@ export function Branch (http, data = {}) {
163187
*
164188
*/
165189
this.mergeQueue = (uid) => {
166-
const mergeData = { stackHeaders: this.stackHeaders, uid }
167-
return new MergeQueue(http, mergeData)
168-
}
190+
const mergeData = { stackHeaders: this.stackHeaders, uid };
191+
return new MergeQueue(http, mergeData);
192+
};
169193
}
170-
return this
194+
return this;
171195
}
172196

173-
export function BranchCollection (http, data) {
174-
const obj = cloneDeep(data.branches) || data.branch_aliases || []
197+
export function BranchCollection(http, data) {
198+
const obj = cloneDeep(data.branches) || data.branch_aliases || [];
175199
return obj.map((branchData) => {
176-
return new Branch(http, { branch: branchData, stackHeaders: data.stackHeaders })
177-
})
200+
return new Branch(http, {
201+
branch: branchData,
202+
stackHeaders: data.stackHeaders,
203+
});
204+
});
178205
}

test/unit/branch-test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,33 @@ describe('Contentstack Branch test', () => {
166166
.catch(done)
167167
})
168168

169+
it('Branch updateSettings test', done => {
170+
var mock = new MockAdapter(axios)
171+
mock.onPost('/stacks/branches/UID/settings').reply(200, { ...noticeMock })
172+
makeBranch({
173+
branch: {
174+
...systemUidMock
175+
},
176+
stackHeaders: stackHeadersMock
177+
})
178+
.updateSettings({
179+
branch: {
180+
settings: {
181+
am_v2: {
182+
linked_workspaces: [
183+
{ uid: 'branch_uid', space_uid: 'space_uid', is_default: true }
184+
]
185+
}
186+
}
187+
}
188+
})
189+
.then((response) => {
190+
expect(response.notice).to.be.equal(noticeMock.notice)
191+
done()
192+
})
193+
.catch(done)
194+
})
195+
169196
it('Branch compare all test', done => {
170197
var mock = new MockAdapter(axios)
171198
mock.onGet('/stacks/branches_compare', { params: { base_branch: 'UID', compare_branch: 'dev', skip: 0, limit: 100 } }).reply(200, branchCompareAllMock)

types/stack/branch/index.d.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { MergeQueue, MergeQueues } from "./merge-queue";
55

66
export interface Branch extends SystemFields, SystemFunction<Branch> {
77
compare(compareBranchUid: string): Compare
8+
updateSettings(payload: IBranchSettings): Promise<IBranchSettingsResponse>
89
}
910

1011
export interface Branches extends Queryable<Branch, {branch: BranchData}> {
@@ -18,6 +19,29 @@ export interface BranchData extends AnyProperty {
1819
source: string
1920
}
2021

22+
export interface ILinkedWorkspace {
23+
uid: string
24+
space_uid: string
25+
is_default: boolean
26+
operation?: string
27+
}
28+
29+
export interface IBranchSettingsData {
30+
am_v2?: {
31+
linked_workspaces: ILinkedWorkspace[]
32+
}
33+
}
34+
35+
export interface IBranchSettings {
36+
branch: {
37+
settings: IBranchSettingsData
38+
}
39+
}
40+
41+
export interface IBranchSettingsResponse {
42+
notice: string
43+
}
44+
2145
export interface IMergeObj {
2246
item_merge_strategies?: IMergeStrategy[]
2347
}

0 commit comments

Comments
 (0)