Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const request = require('request-promise-native');
  2.  
  3. function JenkinsClient(username, password, jenkins_url) {
  4.     this.username = null;
  5.     this.password = null;
  6.     this.jenkins_url = null;
  7.  
  8.     if (!username) {
  9.         throw ('[ERROR] Username must be defined.');
  10.     } else {
  11.         this.username = username
  12.     }
  13.  
  14.     if (!password) {
  15.         throw ('[ERROR] Password must be defined.');
  16.     } else {
  17.         this.password = password;
  18.     }
  19.  
  20.     if (!jenkins_url) {
  21.         throw ('[ERROR] Jenkins URL must be defined.');
  22.     } else {
  23.         const regex = /(http:\/\/|https:\/\/)(www)?/g;
  24.         if (jenkins_url.match(regex)) {
  25.             this.jenkins_url = jenkins_url.replace(regex, `$1${this.username}:${this.password}@`)
  26.         } else {
  27.             this.jenkins_url = `http://${this.username}:${this.password}@${jenkins_url}`;
  28.         }
  29.     }
  30.  
  31.     this.list_jobs = (params) => {
  32.         params = params || ['name', 'color', 'url', 'weather'];
  33.         return request({ method: 'get', uri: `${this.jenkins_url}/api/json?tree=jobs[${params.join(',')}]`, json: true })
  34.             .then(data => { return data; })
  35.             .catch(err => err);
  36.     }
  37.  
  38.     this.builds = (job_name, params) => {
  39.         if (!job_name) {
  40.             throw ('[ERROR] The name of the job must be specified.');
  41.         }
  42.         params = params || ['number', 'status', 'timestamp', 'id', 'result'];
  43.         return request({ method: 'get', uri: `${this.jenkins_url}/job/${job_name}/api/json?tree=builds[${params.join(',')}]`, json: true })
  44.             .then(data => { return data; })
  45.             .catch(err => err);
  46.     }
  47.  
  48.     this.last_build = (job_name, params) => {
  49.         params = params || ['timestamp', 'estimatedDuration', 'result'];
  50.         return request({ method: 'get', uri: `${this.jenkins_url}/job/${job_name}/lastBuild/api/json?tree=${params.join(',')}`, json: true})
  51.             .then(data => { return data; })
  52.             .catch(err => err);
  53.     }
  54.  
  55.     this.progress = (job_name) => {
  56.         const params = ['result', 'timestamp', 'estimatedDuration'];
  57.         return request({method: 'get', uri: `${this.jenkins_url}/job/${job_name}/lastBuild/api/json?tree=${params.join(',')}`, json: true})
  58.             .then(data => { return data; })
  59.             .catch(err => err);
  60.     }
  61.  
  62.     // S2;
  63. }
  64.  
  65. module.exports = JenkinsClient;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement