1+ import React from 'react'
2+ import LABELS_TO_ADD from '../Labels' ;
3+ import ProjectListApplyer from '../Components/ProjectListApplyer' ;
4+ import GitLabLogo from '../assets/images/gitlab.svg'
5+ import Loading from '../Components/Loading'
6+ import './helper.css'
7+
8+ const LABELS_TO_REMOVE = [
9+ { name : "bug" , color : "d9534f" } ,
10+ { name : "confirmed" , color : "d9534f" } ,
11+ { name : "critical" , color : "d9534f" } ,
12+ { name : "discussion" , color : "428bca" } ,
13+ { name : "documentation" , color : "f0ad4e" } ,
14+ { name : "enhancement" , color : "5cb85c" } ,
15+ { name : "suggestion" , color : "428bca" } ,
16+ { name : "support" , color : "f0ad4e" }
17+ ] ;
18+
19+ class GitLab extends React . Component {
20+
21+ constructor ( props ) {
22+ super ( props )
23+ this . state = {
24+ loading : true ,
25+ projects : [ ] ,
26+ selectedOption : null ,
27+ applying : false ,
28+ applyedLabels : [ ] ,
29+ applyingStatus : null ,
30+ alert : null ,
31+ }
32+ }
33+
34+ fetch ( url , method , body ) {
35+ return fetch ( url , {
36+ method : method || 'GET' ,
37+ body : body ,
38+ headers : new Headers ( {
39+ Authorization : `Bearer ${ sessionStorage . getItem ( 'gitlab-token' ) } ` ,
40+ Accept : 'application/json' ,
41+ } )
42+ } )
43+ }
44+
45+ async componentDidMount ( ) {
46+ window . coisa = this . fetch
47+ const resp = await this . fetch ( `https://gitlab.com/api/v3/projects` ) ;
48+ const projects = await resp . json ( ) ;
49+
50+ this . setState ( {
51+ loading : false ,
52+ projects : projects ,
53+ } )
54+ }
55+
56+ handleApply ( selectedOption ) {
57+ this . setState ( { selectedOption, applying : true } )
58+ this . applyChangesToProject ( selectedOption . value )
59+ }
60+
61+ async applyChangesToProject ( projectId ) {
62+ this . setState ( {
63+ applyedLabels : [ ] ,
64+ alert : null
65+ } )
66+
67+ try {
68+ const createLabelsPromices = LABELS_TO_ADD . map ( l => this . createLabel ( projectId , l ) )
69+ const removeLabelPromices = LABELS_TO_REMOVE . map ( l => this . removeLabel ( projectId , l ) )
70+
71+ await Promise . all ( [ ...createLabelsPromices , ...removeLabelPromices ] )
72+ this . setState ( {
73+ applying : false ,
74+ alert : { type : 'success' , message : 'Setup completed !' }
75+ } ) ;
76+ } catch ( error ) {
77+ this . setState ( {
78+ applying : false ,
79+ alert : { type : 'danger' , message : error . message }
80+ } ) ;
81+ }
82+ }
83+
84+ async removeLabel ( projectId , { name } ) {
85+ await this . fetch (
86+ `https://gitLab.com/api/v4/projects/${ projectId } /labels?name=${ name } ` ,
87+ 'DELETE'
88+ ) ;
89+
90+ this . setState ( ( { applyedLabels } ) => ( {
91+ applyedLabels : [ ...applyedLabels , name ] ,
92+ applyingStatus : `${ name } removed` ,
93+ } ) )
94+ }
95+
96+ async createLabel ( projectId , { name, color, priority } ) {
97+ let formData = new FormData ( )
98+ formData . append ( 'name' , name ) ;
99+ formData . append ( 'color' , `#${ color } ` ) ;
100+ if ( priority ) {
101+ formData . append ( 'priority' , priority ) ;
102+ }
103+
104+ const resp = await this . fetch (
105+ `https://gitLab.com/api/v4/projects/${ projectId } /labels` ,
106+ 'POST' ,
107+ formData
108+ ) ;
109+
110+ if ( resp . status !== 201 && resp . status !== 409 ) {
111+ const content = await resp . json ( ) ;
112+ if ( ! content . message . toLowerCase ( ) !== 'label already exists' ) {
113+ throw new Error ( content . message ) ;
114+ }
115+ }
116+
117+ this . setState ( ( { applyedLabels } ) => ( {
118+ applyedLabels : [ ...applyedLabels , name ] ,
119+ applyingStatus : `${ name } created` ,
120+ } ) )
121+ }
122+
123+ render ( ) {
124+ const { loading, projects, selectedOption, applyedLabels, applying, applyingStatus, alert } = this . state ;
125+
126+ if ( loading ) {
127+ return < section >
128+ < h2 > Loading GitLab...</ h2 >
129+ < Loading />
130+ </ section >
131+ }
132+ return (
133+ < div className = "GitLab" >
134+ < section className = "origin-header" >
135+ < h1 >
136+ < span className = "origin-logo" > < GitLabLogo /> </ span >
137+ < span className = "origin-name" > GitLab</ span >
138+ </ h1 >
139+ </ section >
140+ < ProjectListApplyer
141+ projects = { projects . map ( r => Object . assign ( r , {
142+ value : r . id ,
143+ label : r . path_with_namespace ,
144+ } ) ) }
145+ selectedPreject = { selectedOption }
146+
147+ labelsToRemove = { LABELS_TO_REMOVE }
148+
149+ onApply = { ( selected ) => this . handleApply ( selected ) }
150+
151+ applyedLabels = { applyedLabels }
152+ applying = { applying }
153+ applyingStatus = { applyingStatus }
154+ alert = { alert }
155+ />
156+ </ div >
157+ )
158+ }
159+ }
160+
161+ export default GitLab
0 commit comments