From 59449c7668051f02054d96a58c0e5660fd97967f Mon Sep 17 00:00:00 2001 From: Paulina Nguyen Date: Wed, 21 Jun 2023 02:56:02 -0700 Subject: [PATCH 1/7] add fetch acl sample code for content warehouse --- .github/workflows/document-warehouse.yaml | 62 +++++++++++++++++++ .github/workflows/utils/workflows.json | 1 + document-warehouse/.eslintrc.yml | 4 ++ document-warehouse/fetch-acl.js | 72 +++++++++++++++++++++++ document-warehouse/package.json | 13 ++++ document-warehouse/test/.eslintrc.yml | 5 ++ document-warehouse/test/fetch-acl.test.js | 49 +++++++++++++++ package.json | 7 ++- 8 files changed, 210 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/document-warehouse.yaml create mode 100644 document-warehouse/.eslintrc.yml create mode 100644 document-warehouse/fetch-acl.js create mode 100644 document-warehouse/package.json create mode 100644 document-warehouse/test/.eslintrc.yml create mode 100644 document-warehouse/test/fetch-acl.test.js diff --git a/.github/workflows/document-warehouse.yaml b/.github/workflows/document-warehouse.yaml new file mode 100644 index 0000000000..dfcb9eb340 --- /dev/null +++ b/.github/workflows/document-warehouse.yaml @@ -0,0 +1,62 @@ +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +name: document-warehouse +on: + push: + branches: + - main + paths: + - 'document-warehouse/**' + - '.github/workflows/document-warehouse.yaml' + pull_request: + paths: + - 'document-warehouse/**' + - '.github/workflows/document-warehouse.yaml' + pull_request_target: + types: [labeled] + paths: + - 'document-warehouse/**' + - '.github/workflows/document-warehouse.yaml' + schedule: + - cron: '0 0 * * 0' +jobs: + test: + # Ref: https://github.com/google-github-actions/auth#usage + permissions: + contents: 'read' + id-token: 'write' + if: github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' + uses: ./.github/workflows/test.yaml + with: + name: 'document-warehouse' + path: 'document-warehouse' + remove_label: + # Ref: https://github.com/google-github-actions/auth#usage + permissions: + contents: 'read' + id-token: 'write' + if: | + github.event.action == 'labeled' && + github.event.label.name == 'actions:force-run' && + always() + uses: ./.github/workflows/remove-label.yaml + flakybot: + # Ref: https://github.com/google-github-actions/auth#usage + permissions: + contents: 'read' + id-token: 'write' + if: github.event_name == 'schedule' && always() # always() submits logs even if tests fail + uses: ./.github/workflows/flakybot.yaml + needs: [test] diff --git a/.github/workflows/utils/workflows.json b/.github/workflows/utils/workflows.json index ba34c76fd6..d995889f7b 100644 --- a/.github/workflows/utils/workflows.json +++ b/.github/workflows/utils/workflows.json @@ -39,6 +39,7 @@ "discoveryengine", "dlp", "document-ai", + "document-warehouse", "endpoints/getting-started", "endpoints/getting-started-grpc", "error-reporting", diff --git a/document-warehouse/.eslintrc.yml b/document-warehouse/.eslintrc.yml new file mode 100644 index 0000000000..98634adbef --- /dev/null +++ b/document-warehouse/.eslintrc.yml @@ -0,0 +1,4 @@ +--- +rules: + no-console: off + node/no-unsupported-features/node-builtins: off diff --git a/document-warehouse/fetch-acl.js b/document-warehouse/fetch-acl.js new file mode 100644 index 0000000000..fa8e77f3e4 --- /dev/null +++ b/document-warehouse/fetch-acl.js @@ -0,0 +1,72 @@ +/** Copyright 2023 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +async function main( + projectId = 'YOUR_PROJECT_ID', + location = 'YOUR_PROJECT_LOCATION', + userId = 'user:xxxx@example.com', + documentId = 'YOUR_DOCUMENT_ID' +) { + // [START contentwarehouse_fetch_acl] + + /** + * TODO(developer): Uncomment these variables before running the sample. + * const projectId = 'YOUR_PROJECT_ID'; + * const location = 'YOUR_PROJECT_LOCATION'; // Format is 'us' or 'eu' + * const documentId = 'YOUR_DOCUMENT_ID', + * const userId = "user:xxxx@example.com" // Format is "user:xxxx@example.com" + */ + + // Import from google cloud + const {DocumentServiceClient} = require('@google-cloud/contentwarehouse').v1; + + // Create service client + const serviceClient = new DocumentServiceClient(); + + // Fetches access control policies on project or document level. + async function fetchACL() { + // Initialize request argument(s) + const request = {}; + if (documentId !== 'YOUR_DOCUMENT_ID') { + // Full document resource name, e.g.: + // projects/{project_id}/locations/{location}/documents/{document_id} + request.resource = `projects/${projectId}/locations/${location}/documents/${documentId}`; + request.requestMetadata = {userInfo: {id: userId}}; + } else { + // Full document resource name, e.g.: projects/{project_id} + request.resource = `projects/${projectId}`; + request.projectOwner = true; + } + + // Make Request + const response = serviceClient.fetchAcl(request); + + // Print out response + response.then( + result => console.log(`Success! Response: \n${JSON.stringify(result)}`), + error => console.log(`Failed! Response: \n${error}`) + ); + } + + // [END contentwarehouse_fetch_acl] + await fetchACL(); +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err); + process.exitCode = 1; +}); diff --git a/document-warehouse/package.json b/document-warehouse/package.json new file mode 100644 index 0000000000..3f22485e62 --- /dev/null +++ b/document-warehouse/package.json @@ -0,0 +1,13 @@ +{ + "name": "nodejs-document-warehouse-samples", + "private": true, + "license": "Apache-2.0", + "author": "Google LLC", + "scripts": { + "test": "c8 mocha test/*.js --timeout 600000" + }, + "dependencies": { + "@google-cloud/contentwarehouse": "^0.5.1", + "assert": "^2.0.0" + } +} diff --git a/document-warehouse/test/.eslintrc.yml b/document-warehouse/test/.eslintrc.yml new file mode 100644 index 0000000000..29af919cd8 --- /dev/null +++ b/document-warehouse/test/.eslintrc.yml @@ -0,0 +1,5 @@ +--- +env: + mocha: true +rules: + node/no-extraneous-require: off diff --git a/document-warehouse/test/fetch-acl.test.js b/document-warehouse/test/fetch-acl.test.js new file mode 100644 index 0000000000..0bd120d420 --- /dev/null +++ b/document-warehouse/test/fetch-acl.test.js @@ -0,0 +1,49 @@ +/** + * Copyright 2021, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const cp = require('child_process'); +const path = require('path'); +const assert = require('assert'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); +const PROJECT_ID_PASS = '510080582367'; +const PROJECT_ID_FAILED = '821411934445'; +const LOCATION = 'us'; +const USER_ID = 'user:paulinanguyen@google.com'; +const DOCUMENT_ID = '3qof76fb3avl0'; + +//TODO: Create a PR for this! :) +describe('Fetch document acl', () => { + it('should get acl given only a projectId', async () => { + const stdout = execSync(`node ./fetch-acl.js ${PROJECT_ID_PASS} `, {cwd}); + assert(stdout.startsWith('Success!')); + }); + + it('should get acl given a documentId', async () => { + const stdout = execSync( + `node ./fetch-acl.js ${PROJECT_ID_PASS} ${LOCATION} ${USER_ID} ${DOCUMENT_ID}`, + {cwd} + ); + assert(stdout.startsWith('Success!')); + }); + + it('should fail given an incorrent projectId', async () => { + const stdout = execSync(`node ./fetch-acl.js ${PROJECT_ID_FAILED}`, {cwd}); + assert(stdout.startsWith('Failed!')); + }); +}); diff --git a/package.json b/package.json index 068f1e8d9b..caecbe496a 100644 --- a/package.json +++ b/package.json @@ -17,14 +17,15 @@ "test": "echo 'Please run tests in each sample directory.' && exit 1", "generate-ci": "node .github/workflows/utils/generate.js" }, - "devDependencies": { - "c8": "^7.13.0", + "devDependencies": { "@typescript-eslint/eslint-plugin": "^5.59.0", "@typescript-eslint/parser": "^5.59.0", + "c8": "^7.13.0", + "eslint-config-prettier": "^8.8.0", "eslint-plugin-node": "^11.1.0", "eslint-plugin-prettier": "^4.2.1", - "eslint-config-prettier": "^8.8.0", "gts": "^3.1.1", + "mocha": "^10.2.0", "nunjucks": "^3.2.4", "prettier": "^2.8.7", "typescript": "^5.0.4" From eca619f1b64eed34d93ff7c54f988a027b835f44 Mon Sep 17 00:00:00 2001 From: Paulina Nguyen Date: Wed, 21 Jun 2023 03:54:46 -0700 Subject: [PATCH 2/7] remove google related values from test --- document-warehouse/test/fetch-acl.test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/document-warehouse/test/fetch-acl.test.js b/document-warehouse/test/fetch-acl.test.js index 0bd120d420..1c696575ed 100644 --- a/document-warehouse/test/fetch-acl.test.js +++ b/document-warehouse/test/fetch-acl.test.js @@ -21,11 +21,11 @@ const assert = require('assert'); const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); const cwd = path.join(__dirname, '..'); -const PROJECT_ID_PASS = '510080582367'; -const PROJECT_ID_FAILED = '821411934445'; +const PROJECT_ID_PASS = 'YOUR_PROJECT_ID'; +const PROJECT_ID_FAILED = 'PROJECT_ID_WITHOUT_ACL'; const LOCATION = 'us'; -const USER_ID = 'user:paulinanguyen@google.com'; -const DOCUMENT_ID = '3qof76fb3avl0'; +const USER_ID = 'user:xxxx@example.com'; +const DOCUMENT_ID = 'YOUR_DOCUMENT_ID'; //TODO: Create a PR for this! :) describe('Fetch document acl', () => { From 6858d565c1aba6e130cfdcdcba4ed3b0f0b064ad Mon Sep 17 00:00:00 2001 From: Paulina Nguyen Date: Wed, 21 Jun 2023 04:10:27 -0700 Subject: [PATCH 3/7] add document-warehouse directory codeowner --- CODEOWNERS | 1 + 1 file changed, 1 insertion(+) diff --git a/CODEOWNERS b/CODEOWNERS index 0f2a8fe69c..b82ca15060 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -58,6 +58,7 @@ dialogflow @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples- dialogflow-cx @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers discoveryengine @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers document-ai @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers +document-warehouse @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers mediatranslation @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers notebooks @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers speech @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers From 6164bfa91384c0d4f6fcf4e472cee2f2672d03a9 Mon Sep 17 00:00:00 2001 From: Paulina Nguyen Date: Wed, 21 Jun 2023 04:18:21 -0700 Subject: [PATCH 4/7] add document-warehouse blunderbuss issue and pr auto-assigner --- .github/blunderbuss.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.github/blunderbuss.yml b/.github/blunderbuss.yml index 5d5fbe59a6..a481f7fc38 100644 --- a/.github/blunderbuss.yml +++ b/.github/blunderbuss.yml @@ -37,6 +37,11 @@ assign_issues_by: - 'api: dlp' to: - GoogleCloudPlatform/googleapis-dlp +- labels: + - 'api: contentwarehouse' + - 'api: documentai' + to: + - GoogleCloudPlatform/document-ai-samples-contributors assign_prs_by: - labels: @@ -58,3 +63,8 @@ assign_prs_by: - 'api: dlp' to: - GoogleCloudPlatform/googleapis-dlp +- labels: + - 'api: contentwarehouse' + - 'api: documentai' + to: + - GoogleCloudPlatform/document-ai-samples-contributors \ No newline at end of file From 94a61ec95b3063b4d713a3bbf9f5817b321fd6a1 Mon Sep 17 00:00:00 2001 From: Paulina Nguyen Date: Wed, 21 Jun 2023 11:00:30 -0700 Subject: [PATCH 5/7] fix some requested changes and lint issues --- .github/blunderbuss.yml | 2 +- document-warehouse/test/fetch-acl.test.js | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/.github/blunderbuss.yml b/.github/blunderbuss.yml index a481f7fc38..9481d26855 100644 --- a/.github/blunderbuss.yml +++ b/.github/blunderbuss.yml @@ -67,4 +67,4 @@ assign_prs_by: - 'api: contentwarehouse' - 'api: documentai' to: - - GoogleCloudPlatform/document-ai-samples-contributors \ No newline at end of file + - GoogleCloudPlatform/document-ai-samples-contributors diff --git a/document-warehouse/test/fetch-acl.test.js b/document-warehouse/test/fetch-acl.test.js index 1c696575ed..6db2aa9b56 100644 --- a/document-warehouse/test/fetch-acl.test.js +++ b/document-warehouse/test/fetch-acl.test.js @@ -1,5 +1,5 @@ -/** - * Copyright 2021, Google, Inc. +/** Copyright 2023 Google LLC + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -27,7 +27,6 @@ const LOCATION = 'us'; const USER_ID = 'user:xxxx@example.com'; const DOCUMENT_ID = 'YOUR_DOCUMENT_ID'; -//TODO: Create a PR for this! :) describe('Fetch document acl', () => { it('should get acl given only a projectId', async () => { const stdout = execSync(`node ./fetch-acl.js ${PROJECT_ID_PASS} `, {cwd}); @@ -42,7 +41,7 @@ describe('Fetch document acl', () => { assert(stdout.startsWith('Success!')); }); - it('should fail given an incorrent projectId', async () => { + it('should fail given an incorrect projectId', async () => { const stdout = execSync(`node ./fetch-acl.js ${PROJECT_ID_FAILED}`, {cwd}); assert(stdout.startsWith('Failed!')); }); From 4d3c449745edb5c5b6f21793ba65104298b3abb9 Mon Sep 17 00:00:00 2001 From: Paulina Nguyen Date: Wed, 21 Jun 2023 12:03:06 -0700 Subject: [PATCH 6/7] add document-ai-samples-contributors to directory --- CODEOWNERS | 4 ++-- document-warehouse/test/.eslintrc.yml | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/CODEOWNERS b/CODEOWNERS index b82ca15060..99c71af94e 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -57,8 +57,8 @@ datalabeling @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-sample dialogflow @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers dialogflow-cx @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers discoveryengine @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers -document-ai @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers -document-warehouse @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers +document-ai @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/document-ai-samples-contributors +document-warehouse @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/document-ai-samples-contributors mediatranslation @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers notebooks @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers speech @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers diff --git a/document-warehouse/test/.eslintrc.yml b/document-warehouse/test/.eslintrc.yml index 29af919cd8..1858dee890 100644 --- a/document-warehouse/test/.eslintrc.yml +++ b/document-warehouse/test/.eslintrc.yml @@ -1,4 +1,3 @@ ---- env: mocha: true rules: From c899bcb2f4ac93a77bb622b3576dcd5ce0ea9608 Mon Sep 17 00:00:00 2001 From: Paulina Nguyen Date: Thu, 22 Jun 2023 22:11:27 -0700 Subject: [PATCH 7/7] add license to header --- document-warehouse/.eslintrc.yml | 13 +++++++++++++ document-warehouse/test/.eslintrc.yml | 14 ++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/document-warehouse/.eslintrc.yml b/document-warehouse/.eslintrc.yml index 98634adbef..874e3161ce 100644 --- a/document-warehouse/.eslintrc.yml +++ b/document-warehouse/.eslintrc.yml @@ -1,3 +1,16 @@ +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. --- rules: no-console: off diff --git a/document-warehouse/test/.eslintrc.yml b/document-warehouse/test/.eslintrc.yml index 1858dee890..734e9736fe 100644 --- a/document-warehouse/test/.eslintrc.yml +++ b/document-warehouse/test/.eslintrc.yml @@ -1,3 +1,17 @@ +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +--- env: mocha: true rules: