renovate
Version:
Automated dependency updates. Flexible so you don't need to be.
115 lines • 4.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GitlabPrCache = void 0;
const tslib_1 = require("tslib");
const logger_1 = require("../../../logger");
const memCache = tslib_1.__importStar(require("../../../util/cache/memory"));
const repository_1 = require("../../../util/cache/repository");
const repository_http_cache_provider_1 = require("../../../util/http/cache/repository-http-cache-provider");
const regex_1 = require("../../../util/regex");
const url_1 = require("../../../util/url");
const utils_1 = require("./utils");
class GitlabPrCache {
repo;
ignorePrAuthor;
items = [];
cache;
constructor(repo, author, ignorePrAuthor) {
this.repo = repo;
this.ignorePrAuthor = ignorePrAuthor;
const repoCache = (0, repository_1.getCache)();
repoCache.platform ??= {};
repoCache.platform.gitlab ??= {};
let pullRequestCache = repoCache.platform.gitlab
.pullRequestsCache;
if (!pullRequestCache) {
logger_1.logger.debug('Initializing new PR cache at repository cache');
pullRequestCache = {
items: {},
updated_at: null,
author,
};
}
else if (pullRequestCache.author !== author) {
logger_1.logger.debug('Resetting PR cache because authors do not match');
pullRequestCache = {
items: {},
updated_at: null,
author,
};
}
else if (pullRequestCache.updated_at?.match((0, regex_1.regEx)(/\.\d\d\dZ$/))) {
logger_1.logger.debug('Resetting PR cache of older format');
pullRequestCache = {
items: {},
updated_at: null,
author,
};
}
repoCache.platform.gitlab.pullRequestsCache = pullRequestCache;
this.cache = pullRequestCache;
this.updateItems();
}
static async init(http, repo, author, ignorePrAuthor) {
const res = new GitlabPrCache(repo, author, ignorePrAuthor);
const isSynced = memCache.get('gitlab-pr-cache-synced');
if (!isSynced) {
await res.sync(http);
memCache.set('gitlab-pr-cache-synced', true);
}
return res;
}
getPrs() {
return this.items;
}
static async getPrs(http, repo, author, ignorePrAuthor) {
const prCache = await GitlabPrCache.init(http, repo, author, ignorePrAuthor);
return prCache.getPrs();
}
setPr(pr) {
logger_1.logger.debug(`Adding PR #${pr.number} to the PR cache`);
this.cache.items[pr.number] = pr;
this.updateItems();
}
static async setPr(http, repo, author, item, ignorePrAuthor) {
const prCache = await GitlabPrCache.init(http, repo, author, ignorePrAuthor);
prCache.setPr(item);
}
async sync(http) {
logger_1.logger.debug('Syncing PR list');
const searchParams = {
per_page: '100',
order_by: 'updated_at',
sort: 'desc',
};
const opts = { paginate: true };
const updated_after = this.cache.updated_at;
if (updated_after) {
opts.cacheProvider = repository_http_cache_provider_1.repoCacheProvider;
searchParams.updated_after = updated_after;
}
if (!this.ignorePrAuthor) {
searchParams.scope = 'created_by_me';
}
const query = (0, url_1.getQueryString)(searchParams);
const { body: items } = await http.getJsonUnchecked(`/projects/${this.repo}/merge_requests?${query}`, opts);
if (items.length) {
for (const item of items) {
const id = item.iid;
this.cache.items[id] = (0, utils_1.prInfo)(item);
}
const [{ updated_at }] = items;
this.cache.updated_at = updated_at.replace((0, regex_1.regEx)(/\.\d\d\dZ$/), 'Z');
}
this.updateItems();
return this;
}
/**
* Ensure the pr cache starts with the most recent PRs.
*/
updateItems() {
this.items = Object.values(this.cache.items).sort((a, b) => b.number - a.number);
}
}
exports.GitlabPrCache = GitlabPrCache;
//# sourceMappingURL=pr-cache.js.map