github-changelog
Version:
Generate a changelog from GitHub pull requests
65 lines (64 loc) • 2.83 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const path = require("path");
const configuration_error_1 = require("./configuration-error");
const fetch_1 = require("./fetch");
class GithubAPI {
constructor(config) {
this.cacheDir = config.cacheDir && path.join(config.rootPath, config.cacheDir, "github");
this.github = config.github || process.env.GITHUB_DOMAIN || "github.com";
this.auth = this.getAuthToken();
if (!this.auth) {
throw new configuration_error_1.default("Must provide GITHUB_AUTH");
}
}
getBaseIssueUrl(repo) {
return `https://${this.github}/${repo}/issues/`;
}
getIssueData(repo, issue) {
return __awaiter(this, void 0, void 0, function* () {
const prefix = process.env.GITHUB_API_URL || `https://api.${this.github}`;
return this._fetch(`${prefix}/repos/${repo}/issues/${issue}`);
});
}
getUserData(userInfo) {
return __awaiter(this, void 0, void 0, function* () {
let login = userInfo.login;
let path = "users";
if (userInfo.html_url && userInfo.html_url.includes("/apps/")) {
path = "apps";
login = userInfo.html_url.split("/").pop();
}
const prefix = process.env.GITHUB_API_URL || `https://api.${this.github}`;
return yield this._fetch(`${prefix}/${path}/${login}`);
});
}
_fetch(url) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield (0, fetch_1.default)(url, {
cachePath: this.cacheDir,
headers: {
Authorization: `token ${this.auth}`,
},
});
const parsedResponse = yield res.json();
if (res.ok) {
return parsedResponse;
}
throw new configuration_error_1.default(`Fetch error: ${res.statusText}.\n${JSON.stringify(parsedResponse)}`);
});
}
getAuthToken() {
return process.env.GITHUB_AUTH || "";
}
}
exports.default = GithubAPI;