evergreen.js
Version:
JavaScript/TypeScript library for the Evergreen REST API
245 lines • 8.22 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = require("axios");
class client {
constructor(serverURL, webURL, username, key) {
this.apiURL = serverURL;
this.uiURL = webURL;
this.username = username;
this.key = key;
}
/**
* General function to send a HTTP GET to the Evergreen API
*
* @param resource - resource to GET, can be a path
* @param params - query params to append to the request URL, in the format {"param": "value"}
* @returns a promise for the caller to handle responses
*/
getAPIResource(resource, params) {
const url = this.apiURL + "/" + resource + queryString(params);
return axios_1.default(this.formRequest("GET", url, true));
}
/**
* General function to send a HTTP GET to the Evergreen UI
*
* @param resource - resource to GET, can be a path
* @param params - query params to append to the request URL, in the format {"param": "value"}
* @returns a promise for the caller to handle responses
*/
getUIResource(resource, params) {
const url = this.uiURL + "/" + resource + queryString(params);
return axios_1.default(this.formRequest("GET", url, false));
}
/**
* General function to send a HTTP POST to Evergreen
*
* @param resource - resource to POST to, can be a path
* @param body - body of the request, usually as an object
* @returns a promise for the caller to handle responses
*/
postAPIResource(resource, body) {
const url = this.apiURL + "/" + resource;
return axios_1.default(this.formRequest("POST", url, true, body));
}
/**
* General function to send a HTTP POST to the Evergreen UI
*
* @param resource - resource to POST to, can be a path
* @param body - body of the request, usually as an object
* @returns a promise for the caller to handle responses
*/
postUIResource(resource, body) {
const url = this.uiURL + "/" + resource;
return axios_1.default(this.formRequest("POST", url, false, body));
}
// routes are below
/**
* Gets all distros
*
* @returns a promise for the caller to handle responses
*/
getDistros() {
// TODO: add type of resp
return this.getAPIResource(apiV2Resource("distros"));
}
/**
* Gets aggregated or detailed stats for tasks that have finished recently
*
* @param verbose - returns task details rather than aggregated stats
* @param lookbackMins - look for tasks that ended at most this many minutes before now
* @param status - task statuses (can be comma-separated list) to filter on
* @returns a promise for the caller to handle responses
*/
getRecentTasks(verbose, lookbackMins, status) {
// TODO: add type of resp
const params = {
verbose: verbose,
minutes: lookbackMins,
status: status,
};
return this.getAPIResource(apiV2Resource("status/recent_tasks"), params);
}
/**
* Updates header cookies when given login credentials
*
* @param username - Evergreen user's username
* @param password - Evergreen user's password
* @returns a promise for the caller to handle responses
*/
getToken(username, password) {
// TODO: add type of resp
const params = {
username: username,
password: password,
};
return this.postUIResource("login", params);
}
/**
* Gets patches for a particular user
* @param username - Evergreen user's username
* @returns a promise for the caller to handle responses
*/
getPatches(username, page) {
const resource = "json/patches/user/" + username;
const params = {
page: page,
};
return this.getUIResource(resource, params);
}
/**
* Gets patches for a particular project
* @param projectName - Evergreen project name
* @returns a promise for the caller to handle responses
*/
getProjectPatches(projectName, page) {
const resource = `json/patches/project/${projectName}`;
const params = {
page,
};
return this.getUIResource(resource, params);
}
/**
* Gets logs of a particular type for a particular task/
* @param taskId - identifier for the task whose logs we want
* @param type - type of log to return
* @returns a promise for the caller to handle responses
*/
getLogs(taskId, type, executionNumber) {
const params = {
type: type,
text: true,
};
const resource = "task_log_raw/" + taskId + "/" + executionNumber + queryString(params);
return this.getUIResource(resource);
}
/**
* Gets build for a given build ID
* @param id - build ID whose tasks we want
* @returns a promise for the caller to handle responses
*/
getBuild(id) {
const resource = "builds/" + id;
return this.getAPIResource(apiV2Resource(resource));
}
/**
* Gets tasks for a given build ID
* @param buildId - build ID whose tasks we want
* @returns a promise for the caller to handle responses
*/
getTasksForBuild(buildId) {
const resource = "builds/" + buildId + "/tasks";
return this.getAPIResource(apiV2Resource(resource));
}
/**
* Gets tests for a given task ID
* @param taskId - task ID whose tests we want
* @returns a promise for the caller to handle responses
*/
getTestsForTask(taskId) {
const resource = "tasks/" + taskId + "/tests";
return this.getAPIResource(apiV2Resource(resource));
}
/**
* Gets the admin settings
*
* @param callback - function to process the response
* @returns a promise for the caller to handle responses
*/
getAdminConfig() {
return this.getAPIResource(apiV2Resource("admin/settings"));
}
/**
* Sets the admin settings
*
* @param callback - function to process the response
* @param settings - settings object to set in the db
* @returns a promise for the caller to handle responses
*/
setAdminConfig(settings) {
return this.postAPIResource(apiV2Resource("admin/settings"), settings);
}
/**
* Gets the Evergreen banner message
*
* @param callback - function to process the response
* @returns a promise for the caller to handle responses
*/
getBanner() {
// TODO: add type of resp
return this.getAPIResource(apiV2Resource("admin/banner"));
}
/**
* Sets the Evergreen banner message
*
* @param message - text to set in the banner
* @param theme - color theme to use
* @returns a promise for the caller to handle responses
*/
setBanner(message, theme) {
// TODO: add type of resp
const body = {
banner: message,
theme: theme,
};
return this.postAPIResource(apiV2Resource("admin/settings"), body);
}
// end routes
formRequest(method, url, requireHeaders, body) {
let headers = {};
if (requireHeaders) {
headers = {
"Api-User": this.username,
"Api-Key": this.key,
};
}
const opts = {
headers: headers,
url: url,
withCredentials: true,
method: method,
};
if (body) {
opts.data = body;
}
return opts;
}
}
exports.client = client;
function apiV2Resource(resource) {
return "rest/v2/" + resource;
}
exports.apiV2Resource = apiV2Resource;
function queryString(params) {
if (!params || params === {}) {
return "";
}
let queryStr = "?";
for (const key in params) {
if (params[key] !== undefined && params[key] !== null) {
queryStr += key + "=" + params[key] + "&";
}
}
return queryStr.slice(0, -1);
}
exports.queryString = queryString;
//# sourceMappingURL=evergreen.js.map