Skip to content

Commit 6063bc9

Browse files
authored
Merge pull request #3 from lucassabreu/gitlab
Gitlab
2 parents b64b19a + bd05196 commit 6063bc9

File tree

6 files changed

+189
-5
lines changed

6 files changed

+189
-5
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
## [0.1.2] - 2018-02-25
10+
11+
### Added
12+
- Added suport to setup GitLab projects
13+
14+
### Changed
15+
- Changed README.md to reflect the right settings
16+
917
## [0.1.1] - 2018-02-25
1018

1119
### Fixed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ More about this at: https://blog.coderockr.com/simplificando-o-setup-de-projetos
99
Development Start
1010
-----------------
1111

12-
Add `cwps.lucassabreu.test` to your `/etc/hosts` and run `yarn start`, all dependencs will be installed and a watch-server using webpack will be started at http://cwps.lucassabreu.test:3000.
12+
Just run the command `yarn start`, the service must be served through `http://localhost:3000` to [Netlify Auth Providers](https://www.netlify.com/docs/authentication-providers/) to work.
13+
14+
The command will watch for changes on the source and update it automatically.
1315

1416
Deploy
1517
------

src/App.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import CoderockrLogo from './assets/images/hand-yellow.svg'
66
import Home from './Home'
77
import Footer from './Footer';
88
import GitHub from './Origins/GitHub';
9+
import GitLab from './Origins/GitLab';
910

1011
const App = () => (
1112
<div className="CWSPApp">
@@ -18,7 +19,8 @@ const App = () => (
1819
<div className=" container">
1920
<Switch>
2021
<Route exact path="/" component={Home} />
21-
<Route path="/github/" component={GitHub} />
22+
<Route exact path="/github" component={GitHub} />
23+
<Route exact path="/gitlab" component={GitLab} />
2224
</Switch>
2325
</div>
2426
<Footer />

src/Home.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,19 @@ const Home = ({ history }) => {
3131
{
3232
name: "GitLab",
3333
logo: gitlabLogo,
34-
enabled: false,
35-
callback: nullCallback,
34+
enabled: true,
35+
callback: (event) => {
36+
auth.authenticate({ provider: "gitlab", scope: ["user", 'repo'] }, (err, data) => {
37+
if (err) {
38+
console.error(err);
39+
return
40+
}
41+
42+
sessionStorage.setItem('gitlab-token', data.token);
43+
sessionStorage.setItem('gitlab-refresh-token', data.refresh_token);
44+
history.push('/gitlab');
45+
})
46+
}
3647
},
3748
{
3849
name: "Trello",

src/Origins/GitLab.js

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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

src/assets/images/gitlab.svg

Lines changed: 1 addition & 1 deletion
Loading

0 commit comments

Comments
 (0)