(opts = {})
| 3 | const SEPARATOR = '/'; |
| 4 | |
| 5 | async function parseCommentTarget(opts = {}) { |
| 6 | const { commitSha: commit, pr, target, drv } = opts; |
| 7 | |
| 8 | let commentTarget = target; |
| 9 | // Handle legacy comment target flags. |
| 10 | if (commit) { |
| 11 | drv.warn( |
| 12 | `Deprecation warning: use --target="commit${SEPARATOR}<sha>" instead of --commit-sha=<sha>` |
| 13 | ); |
| 14 | commentTarget = `commit${SEPARATOR}${commit}`; |
| 15 | } |
| 16 | if (pr) { |
| 17 | drv.warn('Deprecation warning: use --target=pr instead of --pr'); |
| 18 | commentTarget = 'pr'; |
| 19 | } |
| 20 | // Handle comment targets that are incomplete, e.g. 'pr' or 'commit'. |
| 21 | let prNumber; |
| 22 | let commitPr; |
| 23 | switch (commentTarget.toLowerCase()) { |
| 24 | case 'commit': |
| 25 | logger.debug(`Comment target "commit" mapped to "commit/${drv.sha}"`); |
| 26 | return { target: 'commit', commitSha: drv.sha }; |
| 27 | case 'pr': |
| 28 | case 'auto': |
| 29 | // Determine PR id from forge env vars (if we're in a PR context). |
| 30 | prNumber = drv.pr; |
| 31 | if (prNumber) { |
| 32 | logger.debug( |
| 33 | `Comment target "${commentTarget}" mapped to "pr/${prNumber}"` |
| 34 | ); |
| 35 | return { target: 'pr', prNumber: prNumber }; |
| 36 | } |
| 37 | // Or fallback to determining PR by HEAD commit. |
| 38 | // TODO: handle issue with PR HEAD commit not matching source branch in github. |
| 39 | [commitPr = {}] = await drv.commitPrs({ commitSha: drv.sha }); |
| 40 | if (commitPr.url) { |
| 41 | [prNumber] = commitPr.url.split('/').slice(-1); |
| 42 | logger.debug( |
| 43 | `Comment target "${commentTarget}" mapped to "pr/${prNumber}" based on commit "${drv.sha}"` |
| 44 | ); |
| 45 | return { target: 'pr', prNumber }; |
| 46 | } |
| 47 | // If target is 'auto', fallback to issuing commit comments. |
| 48 | if (commentTarget === 'auto') { |
| 49 | logger.debug( |
| 50 | `Comment target "${commentTarget}" mapped to "commit/${drv.sha}"` |
| 51 | ); |
| 52 | return { target: 'commit', commitSha: drv.sha }; |
| 53 | } |
| 54 | throw new Error(`PR for commit sha "${drv.sha}" not found`); |
| 55 | } |
| 56 | // Handle qualified comment targets, e.g. 'issue/id'. |
| 57 | const separatorPos = commentTarget.indexOf(SEPARATOR); |
| 58 | if (separatorPos === -1) { |
| 59 | throw new Error(`Failed to parse comment --target="${commentTarget}"`); |
| 60 | } |
| 61 | const targetType = commentTarget.slice(0, separatorPos); |
| 62 | const id = commentTarget.slice(separatorPos + 1); |
no test coverage detected