Source: lib/git.js

  1. /**
  2. * <h3>Module contains various functions in order to edit solution before submitting </h3>
  3. * @module lib/git
  4. */
  5. var chalk = require('chalk');
  6. var inquirer = require('inquirer');
  7. var Preferences = require('preferences');
  8. var Spinner = require('cli-spinner').Spinner;
  9. var git = require('simple-git')();
  10. var fs = require('fs');
  11. var request = require('request');
  12. /**
  13. * @exports function as object
  14. */
  15. /**
  16. * Function to create a new repository
  17. * @function createRepo
  18. * @param {null}
  19. * @return {null}
  20. */
  21. module.exports.createRepo = function(callback) {
  22. var questions = [
  23. {
  24. name: 'lab',
  25. type: 'input',
  26. message: 'Enter the Lab Name to be created :',
  27. /**
  28. * Checks if the length of the entered string is greater than zero
  29. * @function validate
  30. * @param {value} string string entered by user on prompt
  31. * @return {bool} True if the length of string entered is greater than zero
  32. * @return {string} string prompting user to input Lab no. if length of username entered was null
  33. */
  34. validate: function(value) {
  35. if (value.length) {
  36. return true;
  37. }
  38. else {
  39. return 'Please enter the Lab Name';
  40. }
  41. }
  42. }];
  43. inquirer.prompt(questions).then(function (answers) {
  44. var prefs = new Preferences('in.ac.bits-goa.autolab');
  45. var labno = arguments['0'].lab;
  46. request.post(
  47. hostpref.host.host +'/api/v3/projects?private_token=' + prefs.gitlab.token,
  48. { json: {'name' : labno}},
  49. function (error, response, body) {
  50. if (error) {
  51. console.log(error);
  52. return;
  53. }
  54. if (response.statusCode == 201) {
  55. console.log(chalk.green('Successfully created online repo ' + labno));
  56. var options = {
  57. username: prefs.gitlab.username,
  58. lab: labno
  59. };
  60. fs.writeFile('./.config.json', JSON.stringify(options), function (err) {
  61. if (err) {
  62. console.log(err.message);
  63. return;
  64. }
  65. });
  66. }
  67. if (response.statusCode == 401 || response.statusCode == 403 ) {
  68. console.log(chalk.red("Authentication problem!. Use 'autolab init' to authenticate." ));
  69. }
  70. if (response.statusCode == 400) {
  71. console.log(chalk.yellow('Already created ' + labno));
  72. }
  73. console.log(response.statusCode);
  74. });
  75. });
  76. };
  77. /**
  78. * @exports function as object
  79. */
  80. /**
  81. * Function to delete Repository
  82. * @function deleteRepo
  83. * @param {null}
  84. * @return {null}
  85. */
  86. module.exports.deleteRepo = function(callback) {
  87. var questions = [
  88. {
  89. name: 'lab',
  90. type: 'input',
  91. message: 'Enter the Lab Name to be deleted :',
  92. validate: function(value) {
  93. if (value.length) {
  94. return true;
  95. }
  96. else {
  97. return 'Please enter the Lab Name';
  98. }
  99. }
  100. }];
  101. inquirer.prompt(questions).then(function (answers) {
  102. var prefs = new Preferences('in.ac.bits-goa.autolab');
  103. var labno = arguments['0'].lab;
  104. request.delete(
  105. hostpref.host.host +'/api/v3/projects/' + prefs.gitlab.username + '%2F' +labno +'?private_token=' + prefs.gitlab.token,
  106. function (error, response, body) {
  107. if (response.statusCode == 200) {
  108. console.log(chalk.green('Successfully deleted ' + labno));
  109. }
  110. if (response.statusCode == 401 || response.statusCode == 403 ) {
  111. console.log(chalk.red("Authentication problem!. Use 'autolab init' to authenticate." ));
  112. }
  113. if (response.statusCode == 400) {
  114. console.log(chalk.yellow('No online repo with the name ' + labno));
  115. }
  116. });
  117. });
  118. };
  119. /**
  120. * @exports function as object
  121. */
  122. /**
  123. * Function to push the commits made
  124. * @function push
  125. * @param {null}
  126. * @return {null}
  127. */
  128. module.exports.push = function() {
  129. var prefs = new Preferences('in.ac.bits-goa.autolab');
  130. var questions = [
  131. {
  132. name: 'message',
  133. type: 'input',
  134. message: 'Enter the commit message',
  135. validate: function(value) {
  136. if (value.length) {
  137. return true;
  138. }
  139. else {
  140. return 'Please enter commit message';
  141. }
  142. }
  143. }];
  144. inquirer.prompt(questions).then(function (answers) {
  145. var status = new Spinner('Pushing the code');
  146. status.setSpinnerString(0);
  147. status.start();
  148. git.add('./*').commit(answers.message);
  149. var labno = JSON.parse(fs.readFileSync('./.config.json')).lab;
  150. git.addRemote('autolab', (hostpref.host.host.search(/https:\/\//)? 'http://' : 'https://') + prefs.gitlab.username.replace(/@/g, '%40') + ':' + prefs.gitlab.password.replace(/@/g, '%40') + '@' + hostpref.host.host.replace(/^https?\:\/\//i, '') +'/' + prefs.gitlab.username + '/' + labno);
  151. git.push('autolab', 'master');
  152. git.removeRemote('autolab');
  153. status.stop();
  154. });
  155. };