From de1cbd9a084140f8d5d8937747256ebba66cd4bf Mon Sep 17 00:00:00 2001 From: DavidGOrtega Date: Tue, 9 Nov 2021 22:42:32 +0100 Subject: [PATCH 01/14] send-comment rev-parse --- src/cml.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/cml.js b/src/cml.js index 10576bef0..1ccd18f5d 100755 --- a/src/cml.js +++ b/src/cml.js @@ -82,9 +82,13 @@ class CML { this.driver = driver || inferDriver({ repo: this.repo }); } + async revParse({ ref = 'HEAD' }) { + return await exec(`git rev-parse ${ref}`); + } + async triggerSha() { const { sha } = getDriver(this); - return sha || (await exec(`git rev-parse HEAD`)); + return sha || (await this.revParse()); } async branch() { @@ -93,14 +97,17 @@ class CML { } async commentCreate(opts = {}) { + const triggerSha = await this.triggerSha(); const { report: userReport, - commitSha = await this.triggerSha(), + commitSha = triggerSha, rmWatermark, update, pr } = opts; + const sha = await this.revParse({ ref: commitSha }); + if (rmWatermark && update) throw new Error('watermarks are mandatory for updateable comments'); @@ -121,15 +128,13 @@ class CML { if (pr || this.driver === 'bitbucket') { let commentUrl; - if ( - (await exec(`git rev-parse ${commitSha}`)) !== - (await exec('git rev-parse HEAD')) - ) + if (sha !== triggerSha) winston.info( `Looking for PR associated with --commit-sha="${commitSha}".\nSee https://cml.dev/doc/ref/send-comment.` ); + const longReport = `${commitSha.substr(0, 7)}\n\n${report}`; - const [commitPr = {}] = await drv.commitPrs({ commitSha }); + const [commitPr = {}] = await drv.commitPrs({ commitSha: sha }); const { url } = commitPr; if (!url) throw new Error(`PR for commit sha "${commitSha}" not found`); @@ -161,13 +166,13 @@ class CML { return await drv.commentUpdate({ report, id: comment.id, - commitSha + commitSha: sha }); } return await drv.commentCreate({ report, - commitSha + commitSha: sha }); } From b8ba4fe28152965ea14b10d5fb9ff874210e890f Mon Sep 17 00:00:00 2001 From: DavidGOrtega Date: Tue, 9 Nov 2021 23:30:25 +0100 Subject: [PATCH 02/14] long report sha --- src/cml.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cml.js b/src/cml.js index 1ccd18f5d..c1076b6b1 100755 --- a/src/cml.js +++ b/src/cml.js @@ -133,7 +133,7 @@ class CML { `Looking for PR associated with --commit-sha="${commitSha}".\nSee https://cml.dev/doc/ref/send-comment.` ); - const longReport = `${commitSha.substr(0, 7)}\n\n${report}`; + const longReport = `${sha.substr(0, 7)}\n\n${report}`; const [commitPr = {}] = await drv.commitPrs({ commitSha: sha }); const { url } = commitPr; From 56809a51bc7303332e79c6f32f93a983831c6294 Mon Sep 17 00:00:00 2001 From: DavidGOrtega Date: Wed, 10 Nov 2021 00:08:23 +0100 Subject: [PATCH 03/14] bb no PRs --- src/cml.js | 38 +++++++++++++++++++--------------- src/drivers/bitbucket_cloud.js | 5 +---- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/cml.js b/src/cml.js index c1076b6b1..db907c814 100755 --- a/src/cml.js +++ b/src/cml.js @@ -125,7 +125,8 @@ class CML { }); }; - if (pr || this.driver === 'bitbucket') { + const isBB = this.driver === 'bitbucket'; + if (pr || isBB) { let commentUrl; if (sha !== triggerSha) @@ -137,26 +138,29 @@ class CML { const [commitPr = {}] = await drv.commitPrs({ commitSha: sha }); const { url } = commitPr; - if (!url) throw new Error(`PR for commit sha "${commitSha}" not found`); + if (!url && !isBB) + throw new Error(`PR for commit sha "${commitSha}" not found`); - const [prNumber] = url.split('/').slice(-1); + if (url) { + const [prNumber] = url.split('/').slice(-1); - if (update) - comment = updatableComment(await drv.prComments({ prNumber })); + if (update) + comment = updatableComment(await drv.prComments({ prNumber })); - if (update && comment) { - commentUrl = await drv.prCommentUpdate({ - report: longReport, - id: comment.id, - prNumber - }); - } else - commentUrl = await drv.prCommentCreate({ - report: longReport, - prNumber - }); + if (update && comment) { + commentUrl = await drv.prCommentUpdate({ + report: longReport, + id: comment.id, + prNumber + }); + } else + commentUrl = await drv.prCommentCreate({ + report: longReport, + prNumber + }); - if (this.driver !== 'bitbucket') return commentUrl; + if (this.driver !== 'bitbucket') return commentUrl; + } } if (update) diff --git a/src/drivers/bitbucket_cloud.js b/src/drivers/bitbucket_cloud.js index c89998f55..78bb27469 100644 --- a/src/drivers/bitbucket_cloud.js +++ b/src/drivers/bitbucket_cloud.js @@ -81,10 +81,7 @@ class BitbucketCloud { }; }); } catch (err) { - if (err.message === 'Not Found Resource not found') - err.message = - "Click 'Go to pull request' on any commit details page to enable this API"; - throw err; + return []; } } From 180c4583d2125fbbbfed907ea5ffe03ad3d01f1e Mon Sep 17 00:00:00 2001 From: DavidGOrtega Date: Wed, 10 Nov 2021 00:10:08 +0100 Subject: [PATCH 04/14] default opts --- src/cml.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cml.js b/src/cml.js index db907c814..34b67d761 100755 --- a/src/cml.js +++ b/src/cml.js @@ -82,7 +82,7 @@ class CML { this.driver = driver || inferDriver({ repo: this.repo }); } - async revParse({ ref = 'HEAD' }) { + async revParse({ ref = 'HEAD' } = {}) { return await exec(`git rev-parse ${ref}`); } From 413c79ca8fa01586d26161a751519bbcccd0130d Mon Sep 17 00:00:00 2001 From: DavidGOrtega Date: Wed, 10 Nov 2021 00:29:36 +0100 Subject: [PATCH 05/14] tests --- src/cml.test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/cml.test.js b/src/cml.test.js index 8d30b9323..8be0ef541 100644 --- a/src/cml.test.js +++ b/src/cml.test.js @@ -73,7 +73,9 @@ describe('Github tests', () => { caughtErr = err.message; } - expect(caughtErr).toBe('No commit found for SHA: invalid_sha'); + expect( + caughtErr.includes('unknown revision or path not in the working tree') + ).toBe(true); }); }); From 29006779fae6972864fa72a44e2b28b2c270520b Mon Sep 17 00:00:00 2001 From: DavidGOrtega Date: Wed, 10 Nov 2021 00:36:20 +0100 Subject: [PATCH 06/14] tests --- src/cml.test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/cml.test.js b/src/cml.test.js index 8be0ef541..e0354ac2c 100644 --- a/src/cml.test.js +++ b/src/cml.test.js @@ -196,6 +196,8 @@ describe('Gitlab tests', () => { caughtErr = err.message; } - expect(caughtErr).toBe('Not Found'); + expect( + caughtErr.includes('unknown revision or path not in the working tree') + ).toBe(true); }); }); From b4f0b92a2cefda07668afb2f11cb165abf6e96d8 Mon Sep 17 00:00:00 2001 From: DavidGOrtega Date: Wed, 10 Nov 2021 11:28:53 +0100 Subject: [PATCH 07/14] back tests and soft revParse --- report.md | 1 + src/cml.js | 10 ++++++++-- src/cml.test.js | 8 ++------ 3 files changed, 11 insertions(+), 8 deletions(-) create mode 100644 report.md diff --git a/report.md b/report.md new file mode 100644 index 000000000..37d4e6c5c --- /dev/null +++ b/report.md @@ -0,0 +1 @@ +hi there diff --git a/src/cml.js b/src/cml.js index 34b67d761..4b4c2961a 100755 --- a/src/cml.js +++ b/src/cml.js @@ -83,7 +83,13 @@ class CML { } async revParse({ ref = 'HEAD' } = {}) { - return await exec(`git rev-parse ${ref}`); + try { + return await exec(`git rev-parse ${ref}`); + } catch (err) { + winston.warn( + 'Failed calculating the SHA. This might be that we are operating in a non git folder or current git folder is not the desired repo' + ); + } } async triggerSha() { @@ -106,7 +112,7 @@ class CML { pr } = opts; - const sha = await this.revParse({ ref: commitSha }); + const sha = (await this.revParse({ ref: commitSha })) || commitSha; if (rmWatermark && update) throw new Error('watermarks are mandatory for updateable comments'); diff --git a/src/cml.test.js b/src/cml.test.js index e0354ac2c..8d30b9323 100644 --- a/src/cml.test.js +++ b/src/cml.test.js @@ -73,9 +73,7 @@ describe('Github tests', () => { caughtErr = err.message; } - expect( - caughtErr.includes('unknown revision or path not in the working tree') - ).toBe(true); + expect(caughtErr).toBe('No commit found for SHA: invalid_sha'); }); }); @@ -196,8 +194,6 @@ describe('Gitlab tests', () => { caughtErr = err.message; } - expect( - caughtErr.includes('unknown revision or path not in the working tree') - ).toBe(true); + expect(caughtErr).toBe('Not Found'); }); }); From bdbe0de52b50e8680980ee86644b92080929dd61 Mon Sep 17 00:00:00 2001 From: DavidGOrtega Date: Wed, 10 Nov 2021 15:24:17 +0100 Subject: [PATCH 08/14] log --- report.md | 2 +- src/cml.js | 3 ++- src/drivers/bitbucket_cloud.js | 45 ++++++++++++++++++---------------- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/report.md b/report.md index 37d4e6c5c..66f1aa72e 100644 --- a/report.md +++ b/report.md @@ -1 +1 @@ -hi there +hi there3 diff --git a/src/cml.js b/src/cml.js index 4b4c2961a..438daf2de 100755 --- a/src/cml.js +++ b/src/cml.js @@ -131,7 +131,7 @@ class CML { }); }; - const isBB = this.driver === 'bitbucket'; + const isBB = this.driver === BB; if (pr || isBB) { let commentUrl; @@ -142,6 +142,7 @@ class CML { const longReport = `${sha.substr(0, 7)}\n\n${report}`; const [commitPr = {}] = await drv.commitPrs({ commitSha: sha }); + console.log(commitPr); const { url } = commitPr; if (!url && !isBB) diff --git a/src/drivers/bitbucket_cloud.js b/src/drivers/bitbucket_cloud.js index 78bb27469..f34995b7f 100644 --- a/src/drivers/bitbucket_cloud.js +++ b/src/drivers/bitbucket_cloud.js @@ -66,10 +66,12 @@ class BitbucketCloud { const { projectPath } = this; const { commitSha, state = 'OPEN' } = opts; + console.log('here'); try { const endpoint = `/repositories/${projectPath}/commit/${commitSha}/pullrequests?state=${state}`; + console.log(endpoint); const prs = await this.paginatedRequest({ endpoint }); - + console.log('here'); return prs.map((pr) => { const { links: { @@ -219,9 +221,30 @@ class BitbucketCloud { throw new Error('BitBucket Cloud does not support workflowRestart!'); } + async pipelineJobs(opts = {}) { + throw new Error('Not implemented'); + } + + async paginatedRequest(opts = {}) { + const { method = 'GET', body } = opts; + const { next, values } = await this.request(opts); + + if (next) { + const nextValues = await this.paginatedRequest({ + url: next, + method, + body + }); + values.push(...nextValues); + } + + return values; + } + async request(opts = {}) { const { token, api } = this; const { url, endpoint, method = 'GET', body } = opts; + console.log(token); if (!(url || endpoint)) throw new Error('Bitbucket Cloud API endpoint not found'); const headers = { @@ -246,26 +269,6 @@ class BitbucketCloud { return await response.json(); } - async pipelineJobs(opts = {}) { - throw new Error('Not implemented'); - } - - async paginatedRequest(opts = {}) { - const { method = 'GET', body } = opts; - const { next, values } = await this.request(opts); - - if (next) { - const nextValues = await this.paginatedRequest({ - url: next, - method, - body - }); - values.push(...nextValues); - } - - return values; - } - get sha() { return BITBUCKET_COMMIT; } From 56d89c30053a946f54f5ca04c762b2e4c8c6c99b Mon Sep 17 00:00:00 2001 From: DavidGOrtega Date: Wed, 10 Nov 2021 15:37:50 +0100 Subject: [PATCH 09/14] log --- src/drivers/bitbucket_cloud.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/drivers/bitbucket_cloud.js b/src/drivers/bitbucket_cloud.js index f34995b7f..b80d3716f 100644 --- a/src/drivers/bitbucket_cloud.js +++ b/src/drivers/bitbucket_cloud.js @@ -244,7 +244,7 @@ class BitbucketCloud { async request(opts = {}) { const { token, api } = this; const { url, endpoint, method = 'GET', body } = opts; - console.log(token); + console.log({ url, endpoint, method, body }); if (!(url || endpoint)) throw new Error('Bitbucket Cloud API endpoint not found'); const headers = { From 423e9fad450af6e36a67d388d1bcd472c5dc66ac Mon Sep 17 00:00:00 2001 From: DavidGOrtega Date: Wed, 10 Nov 2021 16:05:11 +0100 Subject: [PATCH 10/14] commitComments sha --- src/cml.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cml.js b/src/cml.js index 438daf2de..f4b1b4b23 100755 --- a/src/cml.js +++ b/src/cml.js @@ -171,7 +171,7 @@ class CML { } if (update) - comment = updatableComment(await drv.commitComments({ commitSha })); + comment = updatableComment(await drv.commitComments({ commitSha: sha })); if (update && comment) { return await drv.commentUpdate({ From 91822710d4e74df58d12d5d43f62c0251a987cfa Mon Sep 17 00:00:00 2001 From: DavidGOrtega Date: Wed, 10 Nov 2021 16:26:51 +0100 Subject: [PATCH 11/14] prs no array --- src/drivers/bitbucket_cloud.js | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/src/drivers/bitbucket_cloud.js b/src/drivers/bitbucket_cloud.js index b80d3716f..77956d99a 100644 --- a/src/drivers/bitbucket_cloud.js +++ b/src/drivers/bitbucket_cloud.js @@ -66,25 +66,18 @@ class BitbucketCloud { const { projectPath } = this; const { commitSha, state = 'OPEN' } = opts; - console.log('here'); - try { - const endpoint = `/repositories/${projectPath}/commit/${commitSha}/pullrequests?state=${state}`; - console.log(endpoint); - const prs = await this.paginatedRequest({ endpoint }); - console.log('here'); - return prs.map((pr) => { - const { - links: { - html: { href: url } - } - } = pr; - return { - url - }; - }); - } catch (err) { - return []; - } + const endpoint = `/repositories/${projectPath}/commit/${commitSha}/pullrequests?state=${state}`; + const prs = await this.paginatedRequest({ endpoint }); + return prs.map((pr) => { + const { + links: { + html: { href: url } + } + } = pr; + return { + url + }; + }); } async checkCreate() { From 8a70962d97657c38d1a7a85abc84f8d0c4104041 Mon Sep 17 00:00:00 2001 From: DavidGOrtega Date: Wed, 10 Nov 2021 16:54:16 +0100 Subject: [PATCH 12/14] remove unused --- report.md | 1 - src/cml.js | 1 - src/drivers/bitbucket_cloud.js | 2 +- 3 files changed, 1 insertion(+), 3 deletions(-) delete mode 100644 report.md diff --git a/report.md b/report.md deleted file mode 100644 index 66f1aa72e..000000000 --- a/report.md +++ /dev/null @@ -1 +0,0 @@ -hi there3 diff --git a/src/cml.js b/src/cml.js index f4b1b4b23..b1211d185 100755 --- a/src/cml.js +++ b/src/cml.js @@ -142,7 +142,6 @@ class CML { const longReport = `${sha.substr(0, 7)}\n\n${report}`; const [commitPr = {}] = await drv.commitPrs({ commitSha: sha }); - console.log(commitPr); const { url } = commitPr; if (!url && !isBB) diff --git a/src/drivers/bitbucket_cloud.js b/src/drivers/bitbucket_cloud.js index 77956d99a..a057f1957 100644 --- a/src/drivers/bitbucket_cloud.js +++ b/src/drivers/bitbucket_cloud.js @@ -237,7 +237,7 @@ class BitbucketCloud { async request(opts = {}) { const { token, api } = this; const { url, endpoint, method = 'GET', body } = opts; - console.log({ url, endpoint, method, body }); + if (!(url || endpoint)) throw new Error('Bitbucket Cloud API endpoint not found'); const headers = { From d3f0c38bc77be946eed510ffe3d9f57fc50a0577 Mon Sep 17 00:00:00 2001 From: DavidGOrtega Date: Thu, 11 Nov 2021 13:13:52 +0100 Subject: [PATCH 13/14] inCommitSha --- src/cml.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/cml.js b/src/cml.js index b1211d185..2d18e29c5 100755 --- a/src/cml.js +++ b/src/cml.js @@ -106,13 +106,14 @@ class CML { const triggerSha = await this.triggerSha(); const { report: userReport, - commitSha = triggerSha, + commitSha: inCommitSha = triggerSha, rmWatermark, update, pr } = opts; - const sha = (await this.revParse({ ref: commitSha })) || commitSha; + const commitSha = + (await this.revParse({ ref: inCommitSha })) || inCommitSha; if (rmWatermark && update) throw new Error('watermarks are mandatory for updateable comments'); @@ -135,17 +136,17 @@ class CML { if (pr || isBB) { let commentUrl; - if (sha !== triggerSha) + if (commitSha !== triggerSha) winston.info( - `Looking for PR associated with --commit-sha="${commitSha}".\nSee https://cml.dev/doc/ref/send-comment.` + `Looking for PR associated with --commit-sha="${inCommitSha}".\nSee https://cml.dev/doc/ref/send-comment.` ); - const longReport = `${sha.substr(0, 7)}\n\n${report}`; - const [commitPr = {}] = await drv.commitPrs({ commitSha: sha }); + const longReport = `${commitSha.substr(0, 7)}\n\n${report}`; + const [commitPr = {}] = await drv.commitPrs({ commitSha }); const { url } = commitPr; if (!url && !isBB) - throw new Error(`PR for commit sha "${commitSha}" not found`); + throw new Error(`PR for commit sha "${inCommitSha}" not found`); if (url) { const [prNumber] = url.split('/').slice(-1); @@ -170,19 +171,19 @@ class CML { } if (update) - comment = updatableComment(await drv.commitComments({ commitSha: sha })); + comment = updatableComment(await drv.commitComments({ commitSha })); if (update && comment) { return await drv.commentUpdate({ report, id: comment.id, - commitSha: sha + commitSha }); } return await drv.commentCreate({ report, - commitSha: sha + commitSha }); } From 7ac7f4d3c251004451866ad0fb9b0d006687fd45 Mon Sep 17 00:00:00 2001 From: DavidGOrtega Date: Thu, 11 Nov 2021 20:32:09 +0100 Subject: [PATCH 14/14] Update message Co-authored-by: Casper da Costa-Luis --- src/cml.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cml.js b/src/cml.js index 2d18e29c5..e25def7a6 100755 --- a/src/cml.js +++ b/src/cml.js @@ -87,7 +87,7 @@ class CML { return await exec(`git rev-parse ${ref}`); } catch (err) { winston.warn( - 'Failed calculating the SHA. This might be that we are operating in a non git folder or current git folder is not the desired repo' + 'Failed to obtain SHA. Perhaps not in the correct git folder' ); } }