Skip to content

Commit c213ca5

Browse files
committed
Workflow Stage for entry
- Autorisation token - Update User roles from Stack - GitHub action for Package publish
1 parent 55ae2da commit c213ca5

File tree

9 files changed

+209
-1
lines changed

9 files changed

+209
-1
lines changed

.github/workflows/npm-publish.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# This workflow will publish a package to GitHub Packages when a release is created
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
3+
4+
name: Publish package to NPM repository
5+
on:
6+
release:
7+
types: [created]
8+
9+
jobs:
10+
publish-npm:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
- uses: actions/setup-node@v3
15+
with:
16+
node-version: '12.x'
17+
registry-url: 'https://registry.npmjs.org'
18+
- run: npm ci
19+
- run: npm publish
20+
env:
21+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
22+
publish-git:
23+
runs-on: ubuntu-latest
24+
steps:
25+
- uses: actions/checkout@v3
26+
- uses: actions/setup-node@v3
27+
with:
28+
node-version: '12.x'
29+
registry-url: 'https://npm.pkg.github.com'
30+
scope: '@contentstack'
31+
- run: npm ci
32+
- run: npm publish
33+
env:
34+
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

lib/contentstack.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ import httpClient from './core/contentstackHTTPClient.js'
3434
* import * as contentstack from '@contentstack/management'
3535
* const client = contentstack.client({ headers: { 'headerkey': 'value'} })
3636
*
37+
*
38+
* @prop {string=} params.authtoken - Optional Authtoken is a read-write token used to make authorized CMA requests, but it is a user-specific token.
39+
* @example //Set the `authtoken`
40+
* import * as contentstack from '@contentstack/management'
41+
* const client = contentstack.client({ authtoken: 'value' })
42+
*
43+
* @prop {string=} params.timeout - Optional authorization token is a read-write token used to make authorized CMA requests, but it is a user-specific token.
44+
* @example //Set the `authorization`
45+
* import * as contentstack from '@contentstack/management'
46+
* const client = contentstack.client({ authorization: 'Bearer <token_value>' })
47+
*
3748
* @prop {number=} params.timeout - Optional number of milliseconds before the request times out. Default is 30000ms
3849
* @example //Set the `timeout` to 50000ms
3950
* import * as contentstack from '@contentstack/management'
@@ -132,6 +143,9 @@ export function client (params = {}) {
132143
if (params.authtoken) {
133144
requiredHeaders.authtoken = params.authtoken
134145
}
146+
if (params.authorization) {
147+
requiredHeaders.authorization = params.authorization
148+
}
135149
params = {
136150
...defaultParameter,
137151
...clonedeep(params)

lib/stack/contentType/entry/index.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,58 @@ export function Entry (http, data) {
185185
throw error(err)
186186
}
187187
}
188+
189+
/**
190+
* @description The Set Entry Workflow Stage request allows you to either set a particular workflow stage of an entry or update the workflow stage details of an entry.
191+
* @memberof Entry
192+
* @func setWorkflowStage
193+
* @returns {Promise<Object>} Response Object.
194+
* @param {Object} publishing_rule Details for the publish request
195+
* @param {String} locale Enter the code of the locale that the entry belongs to.
196+
* @example
197+
* import * as contentstack from '@contentstack/management'
198+
* const client = contentstack.client()
199+
*
200+
* const workflow_stage = {
201+
* "comment": "Workflow Comment",
202+
* "due_date": "Thu Dec 01 2018",
203+
* "notify": false,
204+
* "uid": "workflow_stage_uid",
205+
* "assigned_to": [{
206+
* "uid": "user_uid",
207+
* "name": "Username",
208+
* "email": "user_email_id"
209+
* }],
210+
* "assigned_by_roles": [{
211+
* "uid": "role_uid",
212+
* "name": "Role name"
213+
* }]
214+
* }
215+
* client.stack({ api_key: 'api_key'}).contentType('content_type_uid').entry('uid').publishRequest({ publishing_rule, locale: 'en-us'})
216+
* .then((response) => console.log(response.notice))
217+
*/
218+
this.setWorkflowStage = async ({workflow_stage, locale}) => {
219+
const publishDetails = {
220+
workflow: { workflow_stage }
221+
}
222+
const headers = {}
223+
if (this.stackHeaders) {
224+
headers.headers = this.stackHeaders
225+
}
226+
headers.params = {
227+
locale
228+
}
229+
try {
230+
const response = await http.post(`${this.urlPath}/workflow`, publishDetails, headers)
231+
if (response.data) {
232+
return response.data
233+
} else {
234+
throw error(response)
235+
}
236+
} catch (err) {
237+
throw error(err)
238+
}
239+
}
188240
} else {
189241
/**
190242
* @description The Create an entry call creates a new entry for the selected content type.

lib/stack/index.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,43 @@ export function Stack (http, data) {
434434
}
435435
}
436436

437+
/**
438+
* @description The Update User Role API Request updates the roles of an existing user account.
439+
* This API Request will override the existing roles assigned to a user
440+
* @memberof Stack
441+
* @func updateUsersRoles
442+
* @param {*} users object containing userId and array of roles to assign user.
443+
* @returns {Object} Response Object.
444+
* @example
445+
* import * as contentstack from '@contentstack/management'
446+
* const client = contentstack.client()
447+
* const users = {
448+
* user_uid: ['role_uid_1', 'role_uid_2' ]
449+
* }
450+
*
451+
* client.stack({ api_key: 'api_key'}).updateUsersRoles(users)
452+
* .then((response) => console.log(response.notice))
453+
*
454+
*/
455+
this.updateUsersRoles = async (users) => {
456+
try {
457+
const response = await http.post(`${this.urlPath}/users/roles`,
458+
{ users },
459+
{
460+
headers: {
461+
...cloneDeep(this.stackHeaders)
462+
}
463+
})
464+
if (response.data) {
465+
return UserCollection(http, response.data.stack)
466+
} else {
467+
return error(response)
468+
}
469+
} catch (err) {
470+
return error(err)
471+
}
472+
}
473+
437474
/**
438475
* @description The Transfer stack ownership to other users call sends the specified user an email invitation for accepting the ownership of a particular stack.
439476
* @memberof Stack

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/management",
3-
"version": "1.3.1",
3+
"version": "1.4.0",
44
"description": "The Content Management API is used to manage the content of your Contentstack account",
55
"main": "./dist/node/contentstack-management.js",
66
"browser": "./dist/web/contentstack-management.js",

test/unit/entry-test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,38 @@ describe('Contentstack Entry test', () => {
290290
.catch(done)
291291
})
292292

293+
it('Entry set Workflow stage test', done => {
294+
var mock = new MockAdapter(Axios);
295+
296+
mock.post('/content_types/content_type_uid/entries/UID/workflow').reply(200, {
297+
...noticeMock
298+
})
299+
300+
const workflow_stage = {
301+
uid: 'uid',
302+
comment: 'Please review this.',
303+
due_date: 'Thu Dec 01 2018',
304+
notify: true,
305+
assigned_to: [{
306+
uid: "user_uid",
307+
name: "Username",
308+
email: "user_email_id"
309+
}],
310+
assigned_by_roles: [{
311+
uid: "role_uid",
312+
name: "Role name"
313+
}]
314+
}
315+
316+
makeEntry({entry: { ...systemUidMock }})
317+
.setWorkflowStage({workflow_stage, locale: 'en-us'})
318+
.then((response) => {
319+
expect(response.notice).to.be.equal(noticeMock.notice)
320+
done()
321+
})
322+
.catch(done)
323+
})
324+
293325
it('Entry publish request test', done => {
294326
var mock = new MockAdapter(Axios)
295327
mock.onPost('/content_types/content_type_uid/entries/UID/workflow').reply(200, {

test/unit/stack-test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ describe('Contentstack Stack test', () => {
3131
expect(stack.release).to.be.equal(undefined)
3232
expect(stack.bulkOperation).to.be.equal(undefined)
3333
expect(stack.users).to.be.equal(undefined)
34+
expect(stack.updateUsersRoles).to.be.equal(undefined)
3435
expect(stack.transferOwnership).to.be.equal(undefined)
3536
expect(stack.settings).to.be.equal(undefined)
3637
expect(stack.resetSettings).to.be.equal(undefined)
@@ -66,6 +67,7 @@ describe('Contentstack Stack test', () => {
6667
expect(stack.release).to.be.equal(undefined)
6768
expect(stack.bulkOperation).to.be.equal(undefined)
6869
expect(stack.users).to.be.equal(undefined)
70+
expect(stack.updateUsersRoles).to.be.equal(undefined)
6971
expect(stack.transferOwnership).to.be.equal(undefined)
7072
expect(stack.settings).to.be.equal(undefined)
7173
expect(stack.resetSettings).to.be.equal(undefined)
@@ -101,6 +103,7 @@ describe('Contentstack Stack test', () => {
101103
expect(stack.release).to.be.equal(undefined)
102104
expect(stack.bulkOperation).to.be.equal(undefined)
103105
expect(stack.users).to.be.equal(undefined)
106+
expect(stack.updateUsersRoles).to.be.equal(undefined)
104107
expect(stack.transferOwnership).to.be.equal(undefined)
105108
expect(stack.settings).to.be.equal(undefined)
106109
expect(stack.resetSettings).to.be.equal(undefined)
@@ -136,6 +139,7 @@ describe('Contentstack Stack test', () => {
136139
expect(stack.release).to.be.equal(undefined)
137140
expect(stack.bulkOperation).to.be.equal(undefined)
138141
expect(stack.users).to.be.equal(undefined)
142+
expect(stack.updateUsersRoles).to.be.equal(undefined)
139143
expect(stack.transferOwnership).to.be.equal(undefined)
140144
expect(stack.settings).to.be.equal(undefined)
141145
expect(stack.resetSettings).to.be.equal(undefined)
@@ -171,6 +175,7 @@ describe('Contentstack Stack test', () => {
171175
expect(stack.release).to.be.equal(undefined)
172176
expect(stack.bulkOperation).to.be.equal(undefined)
173177
expect(stack.users).to.be.equal(undefined)
178+
expect(stack.updateUsersRoles).to.be.equal(undefined)
174179
expect(stack.transferOwnership).to.be.equal(undefined)
175180
expect(stack.settings).to.be.equal(undefined)
176181
expect(stack.resetSettings).to.be.equal(undefined)
@@ -208,6 +213,7 @@ describe('Contentstack Stack test', () => {
208213
expect(stack.release).to.not.equal(undefined)
209214
expect(stack.bulkOperation).to.not.equal(undefined)
210215
expect(stack.users).to.not.equal(undefined)
216+
expect(stack.updateUsersRoles).to.not.equal(undefined)
211217
expect(stack.transferOwnership).to.not.equal(undefined)
212218
expect(stack.settings).to.not.equal(undefined)
213219
expect(stack.resetSettings).to.not.equal(undefined)
@@ -245,6 +251,7 @@ describe('Contentstack Stack test', () => {
245251
expect(stack.release).to.not.equal(undefined)
246252
expect(stack.bulkOperation).to.not.equal(undefined)
247253
expect(stack.users).to.not.equal(undefined)
254+
expect(stack.updateUsersRoles).to.not.equal(undefined)
248255
expect(stack.transferOwnership).to.not.equal(undefined)
249256
expect(stack.settings).to.not.equal(undefined)
250257
expect(stack.resetSettings).to.not.equal(undefined)
@@ -282,6 +289,7 @@ describe('Contentstack Stack test', () => {
282289
expect(stack.release).to.not.equal(undefined)
283290
expect(stack.bulkOperation).to.not.equal(undefined)
284291
expect(stack.users).to.not.equal(undefined)
292+
expect(stack.updateUsersRoles).to.not.equal(undefined)
285293
expect(stack.transferOwnership).to.not.equal(undefined)
286294
expect(stack.settings).to.not.equal(undefined)
287295
expect(stack.resetSettings).to.not.equal(undefined)
@@ -320,6 +328,7 @@ describe('Contentstack Stack test', () => {
320328
expect(stack.release).to.not.equal(undefined)
321329
expect(stack.bulkOperation).to.not.equal(undefined)
322330
expect(stack.users).to.not.equal(undefined)
331+
expect(stack.updateUsersRoles).to.not.equal(undefined)
323332
expect(stack.transferOwnership).to.not.equal(undefined)
324333
expect(stack.settings).to.not.equal(undefined)
325334
expect(stack.resetSettings).to.not.equal(undefined)
@@ -835,6 +844,24 @@ describe('Contentstack Stack test', () => {
835844
})
836845
.catch(done)
837846
})
847+
it('Update users roles in Stack test', done => {
848+
const mock = new MockAdapter(Axios)
849+
mock.onGet('/stacks').reply(200, {
850+
notice: "The roles were applied successfully.",
851+
})
852+
makeStack({
853+
stack: {
854+
api_key: 'stack_api_key'
855+
}
856+
})
857+
.updateUsersRoles({ user_id: ['role1', 'role2']})
858+
.then((response) => {
859+
expect(response.notice).to.be.equal(noticeMock.notice)
860+
done()
861+
})
862+
.catch(done)
863+
})
864+
838865

839866
it('Stack transfer ownership test', done => {
840867
const mock = new MockAdapter(Axios)

types/stack/contentType/entry.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import { Response } from "../../contentstackCollection";
12
import { AnyProperty, SystemFields } from "../../utility/fields";
23
import { Queryable, SystemFunction } from "../../utility/operations";
34
import { Publishable } from "../../utility/publish";
45
import { Unpublishable } from "../../utility/unpublish";
56

67

78
export interface Entry extends Publishable, Unpublishable, SystemFields, SystemFunction<Entry> {
9+
setWorkflowStage(data: { workflow_stage: WorkflowStage, locale?:string}): Promise<Response>
810
}
911

1012
export interface Entries extends Queryable<Entry, {entry: EntryData}> {
@@ -14,4 +16,13 @@ export interface Entries extends Queryable<Entry, {entry: EntryData}> {
1416
export interface EntryData extends AnyProperty {
1517
title: string
1618
url?: string
19+
}
20+
21+
export interface WorkflowStage extends AnyProperty {
22+
uid: string
23+
comment: string
24+
due_date?: string
25+
notify?: boolean
26+
assign_to?: Array<any>
27+
assigned_by_roles?: Array<any>
1728
}

types/stack/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export interface Stack extends SystemFields {
7979
bulkOperation(): BulkOperation
8080

8181
user(): Promise<Array<User>>
82+
updateUsersRoles(users: AnyProperty): Promise<any>
8283
transferOwnership(email): Promise<Response>
8384
settings(): Promise<any>
8485
resetSettings(): Promise<any>

0 commit comments

Comments
 (0)