-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
177 lines (167 loc) · 5.55 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import cloneDeep from 'lodash/cloneDeep'
import { create, query, fetch, deleteEntity } from '../../entity'
import { Compare } from './compare'
import { MergeQueue } from './mergeQueue'
import error from '../../core/contentstackError'
/**
*
* @namespace Branch
*/
export function Branch (http, data = {}) {
this.stackHeaders = data.stackHeaders
this.urlPath = `/stacks/branches`
data.branch = data.branch || data.branch_alias
delete data.branch_alias
if (data.branch) {
Object.assign(this, cloneDeep(data.branch))
this.urlPath = `/stacks/branches/${this.uid}`
/**
* @description The Delete Branch call is used to delete an existing Branch permanently from your Stack.
* @memberof Branch
* @func delete
* @returns {Object} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).branch('branch_uid').delete()
* .then((response) => console.log(response.notice))
*/
this.delete = deleteEntity(http, true)
/**
* @description The fetch Branch call fetches Branch details.
* @memberof Branch
* @func fetch
* @returns {Promise<Branch.Branch>} Promise for Branch instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).branch('branch_uid').fetch()
* .then((branch) => console.log(branch))
*
*/
this.fetch = fetch(http, 'branch')
/**
* @description Compare allows you compare any or specific ContentType or GlobalFields.
* @memberof Branch
* @func compare
* @returns {Compare} Instance of Compare.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).branch('branch_uid').compare('compare_uid')
*
*/
this.compare = (compareBranchUid) => {
const compareData = { stackHeaders: this.stackHeaders }
if (compareBranchUid) {
compareData.branches = {
base_branch: this.uid,
compare_branch: compareBranchUid
}
}
return new Compare(http, compareData)
}
} else {
/**
* @description The Create a Branch call creates a new branch in a particular stack of your Contentstack account.
* @memberof Branch
* @func create
* @returns {Promise<Branch.Branch>} Promise for Branch instance
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* const branch = {
* name: 'branch_name',
* source: 'master'
* }
* client.stack({ api_key: 'api_key'}).branch().create({ branch })
* .then((branch) => { console.log(branch) })
*/
this.create = create({ http: http })
/**
* @description The 'Get all Branch' request returns comprehensive information about branch created in a Stack.
* @memberof Branch
* @func query
* @returns {Promise<ContentstackCollection.ContentstackCollection>} Promise for ContentstackCollection instance
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).branch().query().find()
* .then((collection) => { console.log(collection) })
*/
this.query = query({ http, wrapperCollection: BranchCollection })
/**
* @description Merge allows user to merge branches in a Stack.
* @memberof Branch
* @func merge
* @returns {Object} Response Object
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* const params = {
* base_branch: "main",
* compare_branch: "dev",
* default_merge_strategy: "merge_prefer_base",
* merge_comment: "Merging dev into main",
* no_revert: true,
* }
* const mergeObj = {
* item_merge_strategies: [
* {
* uid: "global_field_uid",
* type: "global_field",
* merge_strategy: "merge_prefer_base"
* }
* ],
* }
*
* client.stack({ api_key: 'api_key'}).branch().merge(mergeObj, params)
*/
this.merge = async (mergeObj, params) => {
const url = '/stacks/branches_merge'
const header = {
headers: { ...cloneDeep(this.stackHeaders) },
params: params
}
try {
const response = await http.post(url, mergeObj, header)
if (response.data) return response.data
else throw error(response)
} catch (e) {
throw error(e)
}
}
/**
* @description Merge Queue provides list of all recent merge jobs in a Stack.
* @memberof Branch
* @func mergeQueue
* @returns {MergeQueue} Instance of MergeQueue
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).branch().mergeQueue()
*
*/
this.mergeQueue = (uid) => {
const mergeData = { stackHeaders: this.stackHeaders, uid }
return new MergeQueue(http, mergeData)
}
}
return this
}
export function BranchCollection (http, data) {
const obj = cloneDeep(data.branches) || data.branch_aliases || []
return obj.map((branchData) => {
return new Branch(http, { branch: branchData, stackHeaders: data.stackHeaders })
})
}