1
1
import React from 'react'
2
+ import { Alert } from 'reactstrap' ;
2
3
import Loading from '../Components/Loading' ;
3
4
import ProjectListSelector from '../Components/ProjectListSelector' ;
4
5
import { withGithubRepositories , fetchGitHub } from './withGitHubRepositories' ;
5
6
import LABELS_TO_ADD from '../Labels' ;
6
7
import invertColor from '../invertColor' ;
7
8
import './GitHub.css' ;
8
9
10
+ const LABELS_TO_REMOVE = [
11
+ { name : "bug" , color : "d73a4a" } ,
12
+ { name : "duplicate" , color : "cfd3d7" } ,
13
+ { name : "enhancement" , color : "a2eeef" } ,
14
+ { name : "good first issue" , color : "7057ff" } ,
15
+ { name : "help wanted" , color : "008672" } ,
16
+ { name : "invalid" , color : "e4e669" } ,
17
+ { name : "question" , color : "d876e3" } ,
18
+ { name : "wontfix" , color : "ffffff" }
19
+ ] ;
20
+
21
+ const LabelList = ( { header, labels, className, applyedLabels } ) => (
22
+ < div className = { `row labels ${ className } ` } >
23
+ < h2 className = "col-md-12" > { header } </ h2 >
24
+ { labels . map ( ( { name, color } ) => (
25
+ < div key = { name } className = "col-md-4" >
26
+ < label alt = { name } className = "label-item"
27
+ style = { { backgroundColor : '#' + color , color : invertColor ( '#' + color ) } }
28
+ >
29
+ < i className = "material-icons label-icon" >
30
+ { applyedLabels . indexOf ( name ) > - 1 ? 'check_box' : 'check_box_outline_blank' }
31
+ </ i >
32
+ < span className = "label-name" children = { name } />
33
+ </ label >
34
+ </ div >
35
+ ) ) }
36
+ </ div >
37
+ )
38
+
9
39
class GitHub extends React . Component {
10
40
11
41
constructor ( props ) {
12
42
super ( props )
13
43
this . state = {
14
44
selectedOption : null ,
15
45
applying : false ,
16
- applyedLabels : [ ]
46
+ applyedLabels : [ ] ,
47
+ applyingStatus : null ,
48
+ alert : null ,
17
49
}
18
50
}
19
51
@@ -23,20 +55,57 @@ class GitHub extends React.Component {
23
55
}
24
56
25
57
async applyChangesToRepository ( repoName ) {
58
+ this . setState ( {
59
+ applyedLabels : [ ] ,
60
+ alert : null
61
+ } )
26
62
27
63
const createLabelsPromices = LABELS_TO_ADD . map ( l => this . createLabel ( repoName , l ) )
64
+ const removeLabelPromices = LABELS_TO_REMOVE . map ( l => this . removeLabel ( repoName , l ) )
65
+ try {
28
66
67
+ await Promise . all ( [ ...createLabelsPromices , ...removeLabelPromices ] )
68
+ this . setState ( {
69
+ applying : false ,
70
+ alert : { type : 'success' , message : 'Setup completed !' }
71
+ } ) ;
72
+ } catch ( error ) {
73
+ this . setState ( {
74
+ applying : false ,
75
+ alert : { type : 'danger' , message : error . message }
76
+ } ) ;
77
+ }
78
+ }
79
+
80
+ async removeLabel ( repoName , { name } ) {
81
+ await fetchGitHub (
82
+ `https://api.github.com/repos/${ repoName } /labels/${ name } ` ,
83
+ 'DELETE'
84
+ ) ;
85
+
86
+ this . setState ( ( { applyedLabels } ) => ( {
87
+ applyedLabels : [ ...applyedLabels , name ] ,
88
+ applyingStatus : `${ name } removed` ,
89
+ } ) )
29
90
}
30
91
31
92
async createLabel ( repoName , { name, color } ) {
32
- fetchGitHub (
93
+ const resp = await fetchGitHub (
33
94
`https://api.github.com/repos/${ repoName } /labels` ,
34
95
'POST' ,
35
96
{ name, color }
36
- )
97
+ ) ;
98
+
99
+ if ( resp . status === 422 ) {
100
+ const message = await resp . json ( ) ;
101
+ if ( ! message . errors . find ( ( { code } ) => code === 'already_exists' ) ) {
102
+ throw new Error ( JSON . stringify ( message . errors ) ) ;
103
+ }
104
+ }
37
105
38
106
this . setState ( ( { applyedLabels } ) => ( {
39
- applyedLabels : [ ...applyedLabels , name ]
107
+ applyedLabels : [ ...applyedLabels , name ] ,
108
+ applyingStatus : `${ name } created` ,
40
109
} ) )
41
110
}
42
111
@@ -61,23 +130,29 @@ class GitHub extends React.Component {
61
130
} ) ) }
62
131
onApply = { ( selected ) => this . handleApply ( selected ) }
63
132
/>
133
+ { ! this . state . alert ? null :
134
+ < Alert
135
+ color = { this . state . alert . type }
136
+ children = { this . state . alert . message }
137
+ />
138
+ }
64
139
{ ! this . state . applying ? null :
65
140
< div className = "row applying-status" >
66
- < div className = "col-md-12" > < Loading /> </ div >
141
+ < div className = "col-md-12" > < Loading status = { this . state . applyingStatus } /> </ div >
67
142
</ div >
68
143
}
69
- < div className = "row labels" >
70
- { LABELS_TO_ADD . map ( ( { name , color } ) => (
71
- < div key = { name } className = "col-md-4" >
72
- < label alt = { name } className = "label-item"
73
- style = { { backgroundColor : '#' + color , color : invertColor ( '#' + color , true ) } }
74
- >
75
- < input disabled type = "checkbox" checked = { this . state . applyedLabels . indexOf ( name ) > - 1 } />
76
- { name }
77
- </ label >
78
- </ div >
79
- ) ) }
80
- </ div >
144
+ < LabelList
145
+ header = "Labels to Remove:"
146
+ className = "labels-to-remove"
147
+ labels = { LABELS_TO_REMOVE }
148
+ applyedLabels = { this . state . applyedLabels }
149
+ / >
150
+ < LabelList
151
+ header = "Labels to Add:"
152
+ className = "labels-to-add"
153
+ labels = { LABELS_TO_ADD }
154
+ applyedLabels = { this . state . applyedLabels }
155
+ / >
81
156
</ section >
82
157
}
83
158
</ div >
0 commit comments