|
| 1 | +const childProcess = require('child_process') |
| 2 | +import { flags, SfdxCommand } from '@salesforce/command'; |
| 3 | +import { Messages, SfdxError } from '@salesforce/core'; |
| 4 | +import { AnyJson } from '@salesforce/ts-types'; |
| 5 | +import { HttpClient } from '../../utils/HttpClient'; |
| 6 | + |
| 7 | +// Initialize Messages with the current plugin directory |
| 8 | +Messages.importMessagesDirectory(__dirname); |
| 9 | + |
| 10 | +// Load the specific messages for this file. Messages from @salesforce/command, @salesforce/core, |
| 11 | +// or any library that is using the messages framework can also be loaded this way. |
| 12 | +const messages = Messages.loadMessages('sfdx-notify', 'teams'); |
| 13 | + |
| 14 | +export default class Teams extends SfdxCommand { |
| 15 | + |
| 16 | + public static description = messages.getMessage('commandDescription'); |
| 17 | + |
| 18 | + public static examples = [ |
| 19 | + `$ sfdx notify:teams -u https://outlook.office.com/webhook/WEBHOOK_URL -e UAT -b $BITBUCKET_BRANCH |
| 20 | + Notify deployment status on Microsoft Teams... Done! |
| 21 | + ` |
| 22 | + ]; |
| 23 | + |
| 24 | + public static args = [{}]; |
| 25 | + |
| 26 | + protected static flagsConfig = { |
| 27 | + url: flags.string({char: 'u', description: messages.getMessage('urlFlagDescription')}), |
| 28 | + env: flags.string({char: 'e', description: messages.getMessage('envFlagDescription')}), |
| 29 | + branch: flags.string({char: 'b', description: messages.getMessage('branchFlagDescription')}) |
| 30 | + }; |
| 31 | + |
| 32 | + // Comment this out if your command does not require an org username |
| 33 | + protected static requiresUsername = false; |
| 34 | + |
| 35 | + // Comment this out if your command does not support a hub org username |
| 36 | + protected static supportsDevhubUsername = false; |
| 37 | + |
| 38 | + // Set this to true if your command requires a project workspace; 'requiresProject' is false by default |
| 39 | + protected static requiresProject = false; |
| 40 | + |
| 41 | + public async run(): Promise<AnyJson> { |
| 42 | + const { stdout: log } = childProcess.spawnSync( |
| 43 | + 'git', |
| 44 | + ['log', '5.0..HEAD', '--oneline'], |
| 45 | + { cwd: '/Users/gavignon/dev/CMA CGM/Git', encoding: 'utf8' } |
| 46 | + ); |
| 47 | + |
| 48 | + let pattern = /[0-9]{5,} \/ (Feature|Fix).*/g; |
| 49 | + let matches = log.match(pattern); |
| 50 | + |
| 51 | + // Construct Microsoft Teams Card Data |
| 52 | + let features = new Array(); |
| 53 | + let fixes = new Array(); |
| 54 | + for(let match of matches){ |
| 55 | + // Remove technical tags |
| 56 | + match = match.replace('[ci skip]',''); |
| 57 | + let matchParts = match.split('/'); |
| 58 | + |
| 59 | + let item = {}; |
| 60 | + |
| 61 | + item.number = matchParts[0] !== undefined ? matchParts[0].trim() : ''; |
| 62 | + item.name = matchParts[2] !== undefined ? matchParts[2].trim() : ''; |
| 63 | + item.type = 'fix'; // Default value |
| 64 | + |
| 65 | + if(matchParts[1] !== undefined){ |
| 66 | + if(matchParts[1].includes('Feature')){ |
| 67 | + item.type = 'feature'; |
| 68 | + features.push(item); |
| 69 | + }else{ |
| 70 | + fixes.push(item); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + let facts = new Array(); |
| 76 | + let firstFeature = true; |
| 77 | + let firstDefect = true; |
| 78 | + |
| 79 | + for(let feature of features){ |
| 80 | + let fact = {}; |
| 81 | + fact.name = ''; |
| 82 | + if(firstFeature){ |
| 83 | + fact.name = 'User Stories:'; |
| 84 | + } |
| 85 | + fact.value = '**' + feature.number + '** - ' + feature.name; |
| 86 | + facts.push(fact); |
| 87 | + |
| 88 | + firstFeature = false; |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + for(let fix of fixes){ |
| 93 | + let fact = {}; |
| 94 | + fact.name = ''; |
| 95 | + if(firstDefect){ |
| 96 | + fact.name = 'Defects:'; |
| 97 | + } |
| 98 | + fact.value = '**' + fix.number + '** - ' + fix.name; |
| 99 | + facts.push(fact); |
| 100 | + |
| 101 | + firstDefect = false; |
| 102 | + } |
| 103 | + |
| 104 | + let data = |
| 105 | + { |
| 106 | + "@type": "MessageCard", |
| 107 | + "@context": "http://schema.org/extensions", |
| 108 | + "themeColor": "0076D7", |
| 109 | + "summary": this.flags.branch + " deployed", |
| 110 | + "sections": [{ |
| 111 | + "activityTitle": this.flags.branch + " deployed", |
| 112 | + "activitySubtitle": "on " + this.flags.env, |
| 113 | + "facts": facts, |
| 114 | + "markdown": true |
| 115 | + }] |
| 116 | + }; |
| 117 | + |
| 118 | + this.ux.startSpinner('Notify deployment status on Microsoft Teams'); |
| 119 | + await HttpClient.sendRequest(this.flags.url, data); |
| 120 | + this.ux.stopSpinner('Done!'); |
| 121 | + |
| 122 | + |
| 123 | + // Return an object to be displayed with --json |
| 124 | + return data; |
| 125 | + } |
| 126 | +} |
0 commit comments