Last active
August 29, 2015 14:27
-
-
Save JogoShugh/ea69d69c51e78fa42eba to your computer and use it in GitHub Desktop.
SpaceMiner gitlab API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// npm install -g babel | |
// Then run with babel-node like this: | |
// babel-node --stage 2 index.js | |
import gitlab from 'gitlab'; | |
import request from 'request'; | |
import Promise from 'bluebird'; | |
let apiBaseUrl = 'http://v1cs-gitlab-dev.cloudapp.net'; | |
request.post = Promise.promisify(request.post, request); | |
let promisedClient = (token='secret') => { | |
let client = gitlab({ | |
url: apiBaseUrl, | |
token | |
}); | |
client.users.search = Promise.promisify(client.users.search, client.users); | |
client.users.create = Promise.promisify(client.users.create, client.users); | |
client.projects.search = Promise.promisify(client.projects.search, client.projects); | |
client.projects.create = Promise.promisify(client.projects.create, client.projects); | |
client.projects.repository.createFile = Promise.promisify(client.projects.repository.createFile, | |
client.projects.repository); | |
return client; | |
}; | |
(async () => { | |
try { | |
let client = promisedClient(); | |
let userInfo = { | |
username: 'jogoshugh', | |
password: 'shughjogo', | |
name: 'Josh', | |
email: 'zoozoo@zoozoo.com', | |
confirm: false | |
}; | |
let user = await client.users.search(userInfo.username); | |
if (user.length === 0) user = await client.users.create(userInfo); | |
else user = user[0]; | |
console.log("\nUser:"); | |
console.log(user); | |
let userData = await request.post(apiBaseUrl + '/api/v3/session', | |
{form:{login:userInfo.username, password:userInfo.password}}); | |
userData = JSON.parse(userData[0].body); | |
userInfo.token = userData.private_token; | |
client = promisedClient(userInfo.token); | |
let projectInfo = { | |
name: userInfo.username, | |
issues_enabled: true, | |
public: true | |
}; | |
let project = await client.projects.search(projectInfo.name); | |
if (project.length === 0) project = await client.projects.create(projectInfo); | |
else project = project[0]; | |
projectInfo.id = project.id; | |
console.log("\nProject:"); | |
console.log(project); | |
let commitInfo = { | |
projectId: project.id, | |
file_path: 'hello.js', | |
branch_name: 'master', | |
content: 'alert("Hello, world!");', | |
commit_message: 'First commit from SpaceMiner!' | |
}; | |
let commitResult = await client.projects.repository.createFile(commitInfo); | |
console.log("\nCommit result:"); | |
console.log(commitResult); | |
} catch (err) { | |
console.error('Error:'); | |
console.error(err); | |
} | |
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gitlab from 'gitlab'; | |
import request from 'request'; | |
let apiBaseUrl = 'http://v1cs-gitlab-dev.cloudapp.net'; | |
let client = gitlab({ | |
url: apiBaseUrl, | |
token: 'secret' | |
}); | |
let userInfo = { | |
username: 'jogoshugh', | |
password: 'shughjogo', | |
name: 'Josh', | |
email: 'zoozoo@zoozoo.com', | |
confirm: false, | |
token: null | |
}; | |
let projectInfo = { | |
name: userInfo.username, | |
issues_enabled: true, | |
public: true, | |
id: null | |
}; | |
let makeCommit = projectInfo => { | |
client.projects.repository.createFile({ | |
projectId: projectInfo.id, | |
file_path: 'hello.js', | |
branch_name: 'master', | |
content: 'alert("Hello, world!");', | |
commit_message: 'First commit from SpaceMiner!' | |
}, result => { | |
console.log("File create result:"); | |
console.log(result); | |
}); | |
}; | |
let getProject = projectInfo => { | |
client.projects.search(projectInfo.name, projects => { | |
if (projects && projects.length === 0) { | |
client.projects.create({ | |
name: projectInfo.name, | |
issues_enabled: true, | |
public: true | |
}, result => { | |
console.log("New project result:"); | |
console.log(result); | |
projectInfo.id = result.id; | |
makeCommit(projectInfo); | |
}) | |
} else { | |
console.log("Projects:"); | |
console.log(projects); | |
projectInfo.id = projects[0].id; | |
makeCommit(projectInfo); | |
} | |
}); | |
}; | |
let getUserSecretToken = userInfo => { | |
request.post(apiBaseUrl + '/api/v3/session', | |
{form:{login:userInfo.username, password:userInfo.password}}, (err, response, body) => { | |
let data = JSON.parse(body); | |
userInfo.token = data.private_token; | |
client = gitlab({ | |
url: apiBaseUrl, | |
token: userInfo.token | |
}); | |
getProject(projectInfo); | |
}); | |
}; | |
let getUser = userInfo => { | |
client.users.search(userInfo.username, users => { | |
if (users && users.length === 0) { | |
client.users.create({ | |
username:'jogoshugh', | |
password: 'shughjogo', | |
name:'Josh', | |
email:'zoozoo@zoozoo.com', | |
confirm: false | |
}, result => { | |
console.log("New user result:"); | |
console.log(result); | |
getUserSecretToken(userInfo); | |
}); | |
} else { | |
console.log('Users:'); | |
console.log(users); | |
getUserSecretToken(userInfo); | |
} | |
}); | |
}; | |
getUser(userInfo); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment