Advertisement
Guest User

Untitled

a guest
Jul 12th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. #!/usr/bin/env node
  2. 'use strict';
  3. /**
  4. * This script creates labels at github.
  5. * It depends on github-api and yargs packages, so make sure you have them installed
  6. * on the environment you are going to run this.
  7. * It accepts a github token or a combination of username and password for auth.
  8. * The token can also be provided as an environment variable.
  9. * To display help just run it with the -h or --help flag.
  10. *
  11. * It is used as part of the monorepo to create all the required labels, which means one label per package.
  12. */
  13. const Issue = require( 'github-api/dist/components/Issue' );
  14. const argv = require( 'yargs' )
  15. .usage( 'Usage: \n $0 --password [string] --label [string]' )
  16. .help('h')
  17. .alias('h', 'help')
  18. .alias( 'u', 'username' )
  19. .describe('username', 'Github username. Not required if you provide a token')
  20. .alias( 't', 'token' )
  21. .describe('token', 'Github access token. You can also provide it as a environment variable named GITHUB_TOKEN or GITHUB_AUTH')
  22. .alias( 'p', 'password' )
  23. .describe('password', 'Github password. Not required if you provide a token')
  24. .alias( 'l', 'label' )
  25. .require('label', 'Please, specify a label name')
  26. .describe('label', 'The label you want to create. Should not exist')
  27. .alias( 'r', 'repository' )
  28. .require('repository', 'Please, specify the target repository')
  29. .describe('repository', 'The repository where you want to create the label')
  30. .argv;
  31.  
  32. const password = argv.password;
  33. const labelName = argv.label;
  34. const username = argv.username;
  35. const repository = argv.repository;
  36. const token = argv.token || process.env.GITHUB_TOKEN || process.env.GITHUB_AUTH;
  37.  
  38. if ( (typeof password !== 'string' && typeof username !== 'string') && typeof token !== 'string' ) {
  39. console.error( 'Missing required arguments' );
  40. process.exit();
  41. }
  42.  
  43. const auth = {
  44. username,
  45. password,
  46. token
  47. };
  48. const issue = new Issue(repository,auth);
  49.  
  50. issue.createLabel( { name: labelName, color: Math.floor( Math.random() * 16777215 ).toString( 16 ) } )
  51. .then(( { data } ) => {
  52.  
  53. console.log( `Created label: ${data.name}` );
  54. } ).catch(( { response } ) => {
  55.  
  56. console.error( 'Failed creating label' );
  57. if ( response.status === 422 ) {
  58. console.log( '\t Probably because the label exist already' );
  59. }
  60. } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement