Skip to content

Commit

Permalink
Merge pull request #71 from hateofhades/privateGitlab
Browse files Browse the repository at this point in the history
Private Repos and Self-Hosted Gitlab
  • Loading branch information
alexandruradovici committed Jun 29, 2021
2 parents 32c7888 + a48f3b1 commit 2cf2be2
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
6 changes: 6 additions & 0 deletions source/plugins/libs/github/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ let github = {
async downloadFile (filePath, owner, repo, ref, responseType = 'json') {
let response = await Axios.get(`https://raw.githubusercontent.com/${owner}/${repo}/${ref}/${filePath}`, {responseType: responseType,});
return response.data;
},
authenticate(token) {
return token;
},
changeURL(newURL) {
return newURL;
}
};

Expand Down
44 changes: 37 additions & 7 deletions source/plugins/libs/gitlab/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import Axios from 'axios';
import axios from 'axios';

let gitlab = {
token: null,
urlAPI: 'https://gitlab.com',
async getDirListOfFiles (path, fileHierarchy, owner, repo, ref) {
let gitURL = `https://gitlab.com/api/v4/projects/${owner}%2F${repo}/repository/tree?path=${path}`;
let gitURL = `${this.urlAPI}/api/v4/projects/${owner}%2F${repo}/repository/tree?path=${path}`;
if (ref) {
gitURL += `&ref=${ref}`;
}
let response = await Axios.get(gitURL);
if(this.token) {
gitURL += `&private_token=${this.token}`;
}

let response = await axios.get(gitURL);

for(let item of response.data) {
if (item.type === 'blob') {
Expand All @@ -21,11 +27,15 @@ let gitlab = {
}
},
async getContentOfDir(path, owner, repo, ref) {
let gitURL = `https://gitlab.com/api/v4/projects/${owner}%2F${repo}/repository/tree?path=${path}`;
let gitURL = `${this.urlAPI}/api/v4/projects/${owner}%2F${repo}/repository/tree?path=${path}`;
if (ref) {
gitURL += `&ref=${ref}`;
}
let response = await Axios.get(gitURL);
if(this.token) {
gitURL += `&private_token=${this.token}`;
}

let response = await axios.get(gitURL);

let contents = {dirs: [], files: []};

Expand All @@ -46,8 +56,28 @@ let gitlab = {
return fileHierarchy;
},
async downloadFile (filePath, owner, repo, ref, responseType = 'json') {
let response = await Axios.get(`https://gitlab.com/${owner}/${repo}/-/raw/${ref}/${filePath}`, {responseType: responseType,});
return response.data;
filePath = filePath.replace(/\//g, '%2F');

let gitURL = `${this.urlAPI}/api/v4/projects/${owner}%2F${repo}/repository/files/${filePath}?ref=${ref}`;
if(this.token) {
gitURL += `&private_token=${this.token}`;
}
let response = await axios.get(gitURL);

response = Buffer.from(response.data.content, response.data.encoding);

if(responseType == 'json')
return JSON.parse(response.toString());
else return response.toString();
},
authenticate(token) {
this.token = token;
},
changeURL(newURL) {
if(newURL.endsWith('/'))
newURL = newURL.slice(0, -1);

this.urlAPI = newURL;
}
};

Expand Down
9 changes: 7 additions & 2 deletions source/plugins/tutorials/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ let studio = null;
export function setup(options, imports, register)
{
studio = imports;
let platformData = 'github';
let platformData = {
platform: 'github',
branch: 'main',
token: null,
gitlabURL: null
};

let tutorials = {
/**
* Show a list for tutorials from a repository
*
* @param {String} repository - username/repository
*/
showTutorials (repository) {
showTutorials (repository) {
let owner = repository.split('/')[0];
repository = repository.split('/')[1];
studio.workspace.showDialog (Tutorials, {
Expand Down
17 changes: 11 additions & 6 deletions source/plugins/tutorials/views/Tutorials.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<script>
export default {
name: 'Tutorials',
props: ['repository', 'owner', 'platformData'],
props: ['owner', 'repository', 'platformData'],
data ()
{
return {
Expand All @@ -67,14 +67,19 @@ export default {
};
},
async created () {
if(this.platformData == 'github') this.platform = this.studio.github;
if(this.platformData.platform == 'github') this.platform = this.studio.github;
else this.platform = this.studio.gitlab;
let response = await this.platform.getContentOfDir('', this.owner, this.repository, 'main');
if(this.platformData.token)
this.platform.authenticate(this.platformData.token);
if(this.platformData.gitlabURL)
this.platform.changeURL(this.platformData.gitlabURL);
let response = await this.platform.getContentOfDir('', this.owner, this.repository, this.platformData.branch);
let tutorials = [];
for (let dir of response.dirs) {
let tutorial = await this.platform.downloadFile(`${dir}/.project/tutorial.json`, this.owner, this.repository, 'main');
let tutorial = await this.platform.downloadFile(`${dir}/.project/tutorial.json`, this.owner, this.repository, this.platformData.branch);
tutorials.push(tutorial);
tutorial['path'] = dir;
Expand Down Expand Up @@ -114,7 +119,7 @@ export default {
let createProject = await this.studio.projects.createEmptyProject(nameProject, tutorial.language);
if (createProject) {
let dirInfos = {};
await this.platform.getDirListOfFiles(tutorial.path, dirInfos, this.owner, this.repository, 'main');
await this.platform.getDirListOfFiles(tutorial.path, dirInfos, this.owner, this.repository, this.platformData.branch);
let numberOfFiles = 0;
for (let key in dirInfos) {
numberOfFiles += dirInfos[key].length;
Expand All @@ -130,7 +135,7 @@ export default {
for (let file of dirInfos[key]) {
let filePath = file.replace(tutorial.path, '');
let fileData = await this.platform.downloadFile(file, this.owner, this.repository, 'main', 'arraybuffer');
let fileData = await this.platform.downloadFile(file, this.owner, this.repository, this.platformData.branch, 'arraybuffer');
await this.studio.projects.newFile(createProject, filePath, Buffer.from (fileData));
downloadedFiles++;
Expand Down

0 comments on commit 2cf2be2

Please sign in to comment.