From 05c4498104e33525a1b1dc4f63d199778f383a5a Mon Sep 17 00:00:00 2001 From: yk634 <59165943+yk634@users.noreply.github.com> Date: Tue, 4 Jan 2022 05:01:06 +0900 Subject: [PATCH] feat(1769): Change data structure for queue-service cooperation (#49) --- index.js | 25 +++++++----- test/data/gitlab.push.bad.json | 40 ------------------- .../webhookConfig.merge_request.opened.json | 15 +++++++ test/data/webhookConfig.push.bad.json | 16 ++++++++ test/data/webhookConfig.push.json | 23 +++++++++++ test/index.test.js | 17 +++++--- 6 files changed, 79 insertions(+), 57 deletions(-) delete mode 100644 test/data/gitlab.push.bad.json create mode 100644 test/data/webhookConfig.merge_request.opened.json create mode 100644 test/data/webhookConfig.push.bad.json create mode 100644 test/data/webhookConfig.push.json diff --git a/index.js b/index.js index ffb7387..6ab382b 100644 --- a/index.js +++ b/index.js @@ -429,7 +429,10 @@ class GitlabScm extends Scm { lastCommitMessage: Hoek.reach(webhookPayload, 'commits.-1.message', { default: '' }) || '', hookId, scmContext, - ref: Hoek.reach(webhookPayload, 'ref') + ref: Hoek.reach(webhookPayload, 'ref'), + addedFiles: Hoek.reach(webhookPayload, ['commits', 0, 'added'], { default: [] }), + modifiedFiles: Hoek.reach(webhookPayload, ['commits', 0, 'modified'], { default: [] }), + removedFiles: Hoek.reach(webhookPayload, ['commits', 0, 'removed'], { default: [] }) }; } default: @@ -1011,14 +1014,14 @@ class GitlabScm extends Scm { * Get the changed files from a GitLab event * @async _getChangedFiles * @param {Object} config - * @param {String} config.type Can be 'pr' or 'repo' - * @param {Object} [config.payload] The webhook payload received from the SCM service. - * @param {String} config.token Service token to authenticate with GitLab - * @param {String} [config.scmUri] The scmUri to get PR info of - * @param {Integer} [config.prNum] The PR number - * @return {Promise} Resolves to an array of filenames of the changed files + * @param {String} config.type Can be 'pr' or 'repo' + * @param {Object} [config.webhookConfig] The webhook payload received from the SCM service. + * @param {String} config.token Service token to authenticate with GitLab + * @param {String} [config.scmUri] The scmUri to get PR info of + * @param {Integer} [config.prNum] The PR number + * @return {Promise} Resolves to an array of filenames of the changed files */ - async _getChangedFiles({ type, payload, token, scmUri, prNum }) { + async _getChangedFiles({ type, webhookConfig, token, scmUri, prNum }) { if (type === 'pr') { try { const { repoId } = getScmUriParts(scmUri); @@ -1039,9 +1042,9 @@ class GitlabScm extends Scm { if (type === 'repo') { const options = { default: [] }; - const added = Hoek.reach(payload, ['commits', 0, 'added'], options); - const modified = Hoek.reach(payload, ['commits', 0, 'modified'], options); - const removed = Hoek.reach(payload, ['commits', 0, 'removed'], options); + const added = Hoek.reach(webhookConfig, 'addedFiles', options); + const modified = Hoek.reach(webhookConfig, 'modifiedFiles', options); + const removed = Hoek.reach(webhookConfig, 'removedFiles', options); // Adding the arrays together and pruning duplicates return [...new Set([...added, ...modified, ...removed])]; diff --git a/test/data/gitlab.push.bad.json b/test/data/gitlab.push.bad.json deleted file mode 100644 index 4271346..0000000 --- a/test/data/gitlab.push.bad.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "object_kind": "push", - "before": "95790bf891e76fee5e1747ab589903a6a1f80f22", - "after": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", - "ref": "refs/heads/master", - "checkout_sha": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", - "user_id": 4, - "user_name": "John Smith", - "user_username": "jsmith", - "user_email": "john@example.com", - "user_avatar": "https://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=8://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=80", - "project_id": 15, - "project":{ - "id": 15, - "name":"Diaspora", - "description":"", - "web_url":"http://example.com/mike/diaspora", - "avatar_url":null, - "git_ssh_url":"git@example.com:mike/diaspora.git", - "git_http_url":"http://example.com/mike/diaspora.git", - "namespace":"Mike", - "visibility_level":0, - "path_with_namespace":"mike/diaspora", - "default_branch":"master", - "homepage":"http://example.com/mike/diaspora", - "url":"git@example.com:mike/diaspora.git", - "ssh_url":"git@example.com:mike/diaspora.git", - "http_url":"http://example.com/mike/diaspora.git" - }, - "repository":{ - "name": "Diaspora", - "url": "git@example.com:mike/diaspora.git", - "description": "", - "homepage": "http://example.com/mike/diaspora", - "git_http_url":"http://example.com/mike/diaspora.git", - "git_ssh_url":"git@example.com:mike/diaspora.git", - "visibility_level":0 -}, - "total_commits_count": 4 -} diff --git a/test/data/webhookConfig.merge_request.opened.json b/test/data/webhookConfig.merge_request.opened.json new file mode 100644 index 0000000..31bab94 --- /dev/null +++ b/test/data/webhookConfig.merge_request.opened.json @@ -0,0 +1,15 @@ +{ + "type": "pr", + "action": "opened", + "username": "bdangit", + "checkoutUrl": "git@example.com:bdangit/quickstart-generic.git", + "branch": "master", + "sha": "249b26f2278c39f9efc55986f845dd98ae011763", + "prNum": 6, + "prRef": "merge_requests/6", + "prSource": "branch", + "prTitle": "fix tabby cat", + "ref": "pull/6/merge", + "hookId": "", + "scmContext": "gitlab:gitlab.com" +} diff --git a/test/data/webhookConfig.push.bad.json b/test/data/webhookConfig.push.bad.json new file mode 100644 index 0000000..d12f263 --- /dev/null +++ b/test/data/webhookConfig.push.bad.json @@ -0,0 +1,16 @@ +{ + "type": "repo", + "action": "push", + "username": "jsmith", + "checkoutUrl": "git@example.com:mike/diaspora.git", + "commitAuthors": [ + "Jordi Mallach", + "GitLab dev user" + ], + "branch": "master", + "lastCommitMessage": "fixed readme", + "ref": "refs/heads/master", + "sha": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", + "hookId": "", + "scmContext": "gitlab:gitlab.com" +} diff --git a/test/data/webhookConfig.push.json b/test/data/webhookConfig.push.json new file mode 100644 index 0000000..6306452 --- /dev/null +++ b/test/data/webhookConfig.push.json @@ -0,0 +1,23 @@ +{ + "type": "repo", + "action": "push", + "username": "jsmith", + "checkoutUrl": "git@example.com:mike/diaspora.git", + "commitAuthors": [ + "Jordi Mallach", + "GitLab dev user" + ], + "branch": "master", + "lastCommitMessage": "fixed readme", + "ref": "refs/heads/master", + "sha": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", + "hookId": "", + "scmContext": "gitlab:gitlab.com", + "addedFiles": [ + "CHANGELOG" + ], + "modifiedFiles": [ + "app/controller/application.rb" + ], + "removedFiles": [] +} diff --git a/test/index.test.js b/test/index.test.js index 982c662..487c1a6 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -17,8 +17,10 @@ const testPayloadClose = require('./data/gitlab.merge_request.closed.json'); const testPayloadPush = require('./data/gitlab.push.json'); const testCommit = require('./data/gitlab.commit.json'); const testChangedFiles = require('./data/gitlab.merge_request.changedFiles.json'); -const testPayloadPushBadHead = require('./data/gitlab.push.bad.json'); const testMergeRequest = require('./data/gitlab.merge_request.json'); +const testWebhookConfigOpen = require('./data/webhookConfig.merge_request.opened.json'); +const testWebhookConfigPushBadHead = require('./data/webhookConfig.push.bad.json'); +const testWebhookConfigPush = require('./data/webhookConfig.push.json'); const token = 'myAccessToken'; const commentUserToken = 'commentUserToken'; const prefixUrl = 'https://gitlab.com/api/v4'; @@ -349,7 +351,10 @@ describe('index', function() { ref: 'refs/heads/master', sha: 'da1560886d4f094c3e6c9ef40349f7d38b5d27d7', hookId: '', - scmContext + scmContext, + addedFiles: ['CHANGELOG'], + modifiedFiles: ['app/controller/application.rb'], + removedFiles: [] }; const headers = { 'content-type': 'application/json', @@ -1460,7 +1465,7 @@ describe('index', function() { .getChangedFiles({ type, token, - payload: testPayloadPush + webhookConfig: testWebhookConfigPush }) .then(result => { assert.deepEqual(result, ['CHANGELOG', 'app/controller/application.rb']); @@ -1472,7 +1477,7 @@ describe('index', function() { .getChangedFiles({ type: 'pr', token, - payload: null, + webhookConfig: null, scmUri: 'github.com:28476:master', prNum: 1 }) @@ -1488,7 +1493,7 @@ describe('index', function() { .getChangedFiles({ type, token, - payload: testPayloadOpen + webhookConfig: testWebhookConfigOpen }) .then(result => { assert.deepEqual(result, []); @@ -1502,7 +1507,7 @@ describe('index', function() { .getChangedFiles({ type, token, - payload: testPayloadPushBadHead + webhookConfig: testWebhookConfigPushBadHead }) .then(result => { assert.deepEqual(result, []);