Skip to content

Commit

Permalink
feat(1769): Change data structure for queue-service cooperation (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
yk634 authored Jan 3, 2022
1 parent e22370a commit 05c4498
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 57 deletions.
25 changes: 14 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand All @@ -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])];
Expand Down
40 changes: 0 additions & 40 deletions test/data/gitlab.push.bad.json

This file was deleted.

15 changes: 15 additions & 0 deletions test/data/webhookConfig.merge_request.opened.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "pr",
"action": "opened",
"username": "bdangit",
"checkoutUrl": "[email protected]: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"
}
16 changes: 16 additions & 0 deletions test/data/webhookConfig.push.bad.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"type": "repo",
"action": "push",
"username": "jsmith",
"checkoutUrl": "[email protected]: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"
}
23 changes: 23 additions & 0 deletions test/data/webhookConfig.push.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"type": "repo",
"action": "push",
"username": "jsmith",
"checkoutUrl": "[email protected]: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": []
}
17 changes: 11 additions & 6 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -1460,7 +1465,7 @@ describe('index', function() {
.getChangedFiles({
type,
token,
payload: testPayloadPush
webhookConfig: testWebhookConfigPush
})
.then(result => {
assert.deepEqual(result, ['CHANGELOG', 'app/controller/application.rb']);
Expand All @@ -1472,7 +1477,7 @@ describe('index', function() {
.getChangedFiles({
type: 'pr',
token,
payload: null,
webhookConfig: null,
scmUri: 'github.com:28476:master',
prNum: 1
})
Expand All @@ -1488,7 +1493,7 @@ describe('index', function() {
.getChangedFiles({
type,
token,
payload: testPayloadOpen
webhookConfig: testWebhookConfigOpen
})
.then(result => {
assert.deepEqual(result, []);
Expand All @@ -1502,7 +1507,7 @@ describe('index', function() {
.getChangedFiles({
type,
token,
payload: testPayloadPushBadHead
webhookConfig: testWebhookConfigPushBadHead
})
.then(result => {
assert.deepEqual(result, []);
Expand Down

0 comments on commit 05c4498

Please sign in to comment.