-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Augment watermark with label (#1173)
* Augment watermark with label. The label may contain special strings `{workflow}` or `{run}` as an easy way to make updatable comments specific to the workflow or execution. * Check for presence of a node title. * Specify conflict between commentLabel and rmWatermark flags. * Update bin/cml/comment/create.js Co-authored-by: Casper da Costa-Luis <[email protected]> * Update cli flag for comment label. * Rename --label to --id * Rename parameter to commentTag. * Update src/cml.js Co-authored-by: Helio Machado <[email protected]> * Rename commentTag flag to watermarkTitle. * Update bin/cml/comment/create.js Co-authored-by: Casper da Costa-Luis <[email protected]> * Update snapshots. * Refactor watermark function. * Fix test. * Use markdown-escape to escape markdown control characters in comment watermark titles. * Revert "Use markdown-escape to escape markdown control characters in comment watermark titles." This reverts commit 1400685. * Simplify escaping of markdown control characters. It appears that github only escapes asterisks and underscores. * Add more patterns to escape. * Run replacement on title, not full watermark. Co-authored-by: Casper da Costa-Luis <[email protected]> Co-authored-by: Helio Machado <[email protected]> Co-authored-by: Daniel Barnes <[email protected]>
- Loading branch information
1 parent
329ed42
commit 17a045e
Showing
7 changed files
with
92 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ const { | |
|
||
const { GITHUB_REPOSITORY, CI_PROJECT_URL, BITBUCKET_REPO_UUID } = process.env; | ||
|
||
const WATERMARK_IMAGE = 'https://cml.dev/watermark.png'; | ||
const GIT_USER_NAME = 'Olivaw[bot]'; | ||
const GIT_USER_EMAIL = '[email protected]'; | ||
const GIT_REMOTE = 'origin'; | ||
|
@@ -164,26 +165,55 @@ class CML { | |
const triggerSha = await this.triggerSha(); | ||
const { | ||
commitSha: inCommitSha = triggerSha, | ||
rmWatermark, | ||
update, | ||
markdownFile, | ||
pr, | ||
publish, | ||
publishUrl, | ||
markdownFile, | ||
report: testReport, | ||
rmWatermark, | ||
triggerFile, | ||
update, | ||
watch, | ||
triggerFile | ||
watermarkTitle | ||
} = opts; | ||
|
||
const drv = this.getDriver(); | ||
|
||
const commitSha = | ||
(await this.revParse({ ref: inCommitSha })) || inCommitSha; | ||
|
||
if (rmWatermark && update) | ||
throw new Error('watermarks are mandatory for updateable comments'); | ||
|
||
// Create the watermark. | ||
const genWatermark = (opts = {}) => { | ||
const { label = '', workflow, run } = opts; | ||
// Replace {workflow} and {run} placeholders in label with actual values. | ||
const lbl = label.replace('{workflow}', workflow).replace('{run}', run); | ||
|
||
let title = `CML watermark ${lbl}`.trim(); | ||
// Github appears to escape underscores and asterisks in markdown content. | ||
// Without escaping them, the watermark content in comments retrieved | ||
// from github will not match the input. | ||
const patterns = [ | ||
[/_/g, '\\_'], // underscore | ||
[/\*/g, '\\*'], // asterisk | ||
[/\[/g, '\\['], // opening square bracket | ||
[/</g, '\\<'] // opening angle bracket | ||
]; | ||
title = patterns.reduce( | ||
(label, pattern) => label.replace(pattern[0], pattern[1]), | ||
title | ||
); | ||
return `![](${WATERMARK_IMAGE} "${title}")`; | ||
}; | ||
const watermark = rmWatermark | ||
? '' | ||
: '![CML watermark](https://raw.githubusercontent.com/iterative/cml/master/assets/watermark.svg)'; | ||
: genWatermark({ | ||
label: watermarkTitle, | ||
workflow: drv.workflowId, | ||
run: drv.runId | ||
}); | ||
|
||
let userReport = testReport; | ||
try { | ||
|
@@ -195,15 +225,17 @@ class CML { | |
} | ||
|
||
let report = `${userReport}\n\n${watermark}`; | ||
const drv = this.getDriver(); | ||
|
||
const publishLocalFiles = async (tree) => { | ||
const nodes = []; | ||
|
||
visit(tree, ['definition', 'image', 'link'], (node) => nodes.push(node)); | ||
|
||
const isWatermark = (node) => { | ||
return node.title && node.title.startsWith('CML watermark'); | ||
}; | ||
const visitor = async (node) => { | ||
if (node.url && node.alt !== 'CML watermark') { | ||
if (node.url && !isWatermark(node)) { | ||
const absolutePath = path.resolve( | ||
path.dirname(markdownFile), | ||
node.url | ||
|
@@ -264,7 +296,7 @@ class CML { | |
let comment; | ||
const updatableComment = (comments) => { | ||
return comments.reverse().find(({ body }) => { | ||
return body.includes('watermark.svg'); | ||
return body.includes(watermark); | ||
}); | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17a045e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test Comment
17a045e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test Comment