import gitlab from "../../gitlab.app.mjs";
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
export default {
key: "gitlab-new-project",
name: "New Project",
description: "Emit new event when a project (i.e. repository) is created",
version: "0.1.3",
dedupe: "greatest",
type: "source",
props: {
gitlab,
db: "$.service.db",
timer: {
type: "$.interface.timer",
default: {
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
},
},
},
hooks: {
async activate() {
const projects = await this.gitlab.listProjects({
params: {
owned: true,
per_page: 1,
},
});
if (projects.length > 0) {
const lastProcessedProjectId = projects[0].id;
this._setLastProcessedProjectId(lastProcessedProjectId);
console.log(`Polling GitLab projects created after ID ${lastProcessedProjectId}`);
}
},
},
methods: {
_getLastProcessedProjectId() {
return this.db.get("lastProcessedProjectId");
},
_setLastProcessedProjectId(lastProcessedProjectId) {
this.db.set("lastProcessedProjectId", lastProcessedProjectId);
},
generateMeta(data) {
const {
id,
created_at: createdAt,
name,
} = data;
return {
id,
summary: `New project: ${name}`,
ts: +new Date(createdAt),
};
},
},
async run() {
let lastProcessedProjectId = this._getLastProcessedProjectId();
const projects = await this.gitlab.listProjects({
params: {
id_after: lastProcessedProjectId,
owned: true,
},
});
if (projects.length === 0) {
console.log("No new GitLab projects detected");
return;
}
console.log(`Detected ${projects.length} new projects`);
lastProcessedProjectId = projects[0].id;
this._setLastProcessedProjectId(lastProcessedProjectId);
projects.reverse().forEach((project) => {
const meta = this.generateMeta(project);
this.$emit(project, meta);
});
},
};