/ src / helpers.js
helpers.js
 1  import Handlebars from 'handlebars'
 2  
 3  /* loop helper compares commits of build and previous build */
 4  const commitChanged = (data, index, options) => {
 5    if (index == 0) { return options.inverse(this); }
 6    if (data[index].commit !== data[index-1].commit) { return options.fn(this); }
 7    return options.inverse(this);
 8  }
 9  
10  /* turns epoch time to human readable format */
11  const formatDate = (data) => new Handlebars.SafeString(
12    (new Date(data)).toISOString('utc').slice(0, 19).replace('T', ' ')
13  )
14  
15  /* extracts file extension from url */
16  const fileExt = (data) => {
17    let ext = 'pkg' /* generic option for unexpected situations */
18    if (data.includes('diawi')) {
19      ext = 'ipa' /* diawi urls don't contain file extension */
20    } else if (data.includes('allure')) {
21      ext = 'rpt' /* three-letter extensions just look nicer */
22    } else if (data.endsWith('tar.gz')) {
23      ext = 'tgz' /* three-letter extensions just look nicer */
24    } else if (data.endsWith('consoleText') || data == 'log') {
25      ext = 'log' /* log link is often a fallback */
26    } else if (data.match(/^https?:\/\/.+\/[^.]+\.(\w{3,8})$/)) {
27      ext = data.split('.').pop()
28    }
29    return Handlebars.Utils.escapeExpression(ext.slice(0, 3))
30  }
31  
32  /* pick different icons for different urls */
33  const fileIcon = (data) => {
34    switch (fileExt(data)) {
35      case 'pkg': return ':package:';
36      case 'apk': return ':robot:';
37      case 'ipa': return ':iphone:';
38      case 'exe': return ':cd:';
39      case 'dmg': return ':apple:';
40      case 'rpt': return ':bar_chart:';
41      case 'log': return ':page_facing_up:';
42      default:    return ':package:';
43    }
44  }
45  
46  /* remove seconds from duration to make columns equal width */
47  const shortenDuration = (data) => (data.replace(/ [0-9]+ sec$/, ''))
48  
49  /* generate URL for a QR code of given text */
50  const genQRCodeUrl = (data) => {
51    /* just for mobile packages, useless for others */
52    if (!data.endsWith('apk') && !data.includes('i.diawi.com')) {
53      return ''
54    }
55  
56    const baseUrl = 'https://quickchart.io/qr'
57    const queryParams = new URLSearchParams({
58      text: data,
59      size: '400x400',
60      errorCorrectionLevel: 'L',
61    })
62  
63    return new Handlebars.SafeString(
64      `[:calling:](https://quickchart.io/qr?${queryParams.toString()})`
65    )
66  }
67  
68  
69  export default {
70    commitChanged,
71    formatDate,
72    fileExt,
73    fileIcon,
74    shortenDuration,
75    genQRCodeUrl,
76  }