1
1
const core = require ( "@actions/core" ) ;
2
2
const github = require ( "@actions/github" ) ;
3
3
const parse = require ( "parse-diff" ) ;
4
+ const Hjson = require ( "hjson" ) ;
5
+ const snakeCase = require ( "lodash.snakecase" ) ;
6
+ const ColorContrastChecker = require ( "color-contrast-checker" ) ;
7
+
4
8
require ( "dotenv" ) . config ( ) ;
5
9
6
10
function getPrNumber ( ) {
@@ -21,6 +25,8 @@ const themeContribGuidelines = `
21
25
22
26
async function run ( ) {
23
27
try {
28
+ const ccc = new ColorContrastChecker ( ) ;
29
+ const warnings = [ ] ;
24
30
const token = core . getInput ( "token" ) ;
25
31
const octokit = github . getOctokit ( token || process . env . PERSONAL_TOKEN ) ;
26
32
const pullRequestId = getPrNumber ( ) ;
@@ -30,7 +36,7 @@ async function run() {
30
36
return ;
31
37
}
32
38
33
- let res = await octokit . pulls . get ( {
39
+ const res = await octokit . pulls . get ( {
34
40
owner : "anuraghazra" ,
35
41
repo : "github-readme-stats" ,
36
42
pull_number : pullRequestId ,
@@ -39,15 +45,20 @@ async function run() {
39
45
} ,
40
46
} ) ;
41
47
42
- let diff = parse ( res . data ) ;
43
- let colorStrings = diff
48
+ const diff = parse ( res . data ) ;
49
+ const content = diff
44
50
. find ( ( file ) => file . to === "themes/index.js" )
45
51
. chunks [ 0 ] . changes . filter ( ( c ) => c . type === "add" )
46
52
. map ( ( c ) => c . content . replace ( "+" , "" ) )
47
53
. join ( "" ) ;
48
54
49
- let matches = colorStrings . match ( / ( t i t l e _ c o l o r : .* b g _ c o l o r .* \" ) / ) ;
50
- let colors = matches && matches [ 0 ] . split ( "," ) ;
55
+ const themeObject = Hjson . parse ( content ) ;
56
+ const themeName = Object . keys ( themeObject ) [ 0 ] ;
57
+ const colors = themeObject [ themeName ] ;
58
+
59
+ if ( themeName !== snakeCase ( themeName ) ) {
60
+ warnings . push ( "Theme name isn't in snake_case" ) ;
61
+ }
51
62
52
63
if ( ! colors ) {
53
64
await octokit . issues . createComment ( {
@@ -64,22 +75,39 @@ async function run() {
64
75
} ) ;
65
76
return ;
66
77
}
67
- colors = colors . map ( ( color ) =>
68
- color . replace ( / .* \: \s / , "" ) . replace ( / \" / g, "" ) ,
69
- ) ;
70
-
71
- const titleColor = colors [ 0 ] ;
72
- const iconColor = colors [ 1 ] ;
73
- const textColor = colors [ 2 ] ;
74
- const bgColor = colors [ 3 ] ;
78
+
79
+ const titleColor = colors . title_color ;
80
+ const iconColor = colors . icon_color ;
81
+ const textColor = colors . text_color ;
82
+ const bgColor = colors . bg_color ;
75
83
const url = `https://github-readme-stats.vercel.app/api?username=anuraghazra&title_color=${ titleColor } &icon_color=${ iconColor } &text_color=${ textColor } &bg_color=${ bgColor } &show_icons=true` ;
76
84
85
+ const colorPairs = {
86
+ title_color : [ titleColor , bgColor ] ,
87
+ icon_color : [ iconColor , bgColor ] ,
88
+ text_color : [ textColor , bgColor ] ,
89
+ } ;
90
+
91
+ // check color contrast
92
+ Object . keys ( colorPairs ) . forEach ( ( key ) => {
93
+ const color1 = colorPairs [ key ] [ 0 ] ;
94
+ const color2 = colorPairs [ key ] [ 1 ] ;
95
+ if ( ! ccc . isLevelAA ( `#${ color1 } ` , `#${ color2 } ` ) ) {
96
+ const permalink = `https://webaim.org/resources/contrastchecker/?fcolor=${ color1 } &bcolor=${ color2 } ` ;
97
+ warnings . push (
98
+ `\`${ key } \` does not passes [AA contrast ratio](${ permalink } )` ,
99
+ ) ;
100
+ }
101
+ } ) ;
102
+
77
103
await octokit . issues . createComment ( {
78
104
owner : "anuraghazra" ,
79
105
repo : "github-readme-stats" ,
80
106
body : `
81
107
\r**Automated Theme preview**
82
108
109
+ \r${ warnings . map ( ( warning ) => `- :warning: ${ warning } \n` ) . join ( "" ) }
110
+
83
111
\ntitle_color: <code>#${ titleColor } </code> | icon_color: <code>#${ iconColor } </code> | text_color: <code>#${ textColor } </code> | bg_color: <code>#${ bgColor } </code>
84
112
85
113
\r[Preview Link](${ url } )
0 commit comments