Skip to content

Commit

Permalink
feat(community) Add 'createdAfter' filter on jira document loader
Browse files Browse the repository at this point in the history
  • Loading branch information
mgiorgino-iobeya committed Dec 2, 2024
1 parent e98b141 commit cc18630
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
4 changes: 4 additions & 0 deletions examples/src/document_loaders/jira.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ const accessToken = process.env.JIRA_ACCESS_TOKEN;
const projectKey = process.env.JIRA_PROJECT_KEY;

if (username && accessToken) {
// Created within last 30 days
const createdAfter = new Date();
createdAfter.setDate(createdAfter.getDate() - 30);
const loader = new JiraProjectLoader({
host,
projectKey,
username,
accessToken,
createdAfter
});

const documents = await loader.load();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* NOTE: Env var should be set, and configured project should exist
*/
import { Document } from "@langchain/core/documents";
import { expect, test } from "@jest/globals";
import { JiraProjectLoader } from "../web/jira.js";

Expand All @@ -19,16 +20,34 @@ describe("JiraProjectLoader Integration Tests", () => {
return value;
}

test("should load Jira project issues successfully", async () => {
async function loadJiraDocs({createdAfter = undefined}: {createdAfter?: Date} = {}): Promise<Document[]> {
const loader = new JiraProjectLoader({
host: JIRA_HOST,
projectKey: JIRA_PROJECT_KEY,
username: JIRA_USERNAME,
accessToken: JIRA_ACCESS_TOKEN,
limitPerRequest: 20,
createdAfter
});

const docs = await loader.load();
return loader.load();
}

test("should load Jira project issues successfully", async () => {
const now = new Date();
let months = 1;

let docs: Document[] = [];
while (docs.length === 0 && months < 120) {
const createdAfter = new Date(now);
createdAfter.setDate(now.getDate() - months * 30);
docs = await loadJiraDocs({createdAfter});
months *= 1.2;
}

if (months >= 10) {
docs = await loadJiraDocs({});
}

expect(docs).toBeDefined();
expect(Array.isArray(docs)).toBe(true);
Expand Down
14 changes: 13 additions & 1 deletion libs/langchain-community/src/document_loaders/web/jira.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ export interface JiraProjectLoaderParams {
username: string;
accessToken: string;
limitPerRequest?: number;
createdAfter?: Date;
}

const API_ENDPOINTS = {
Expand All @@ -339,6 +340,8 @@ export class JiraProjectLoader extends BaseDocumentLoader {

public readonly limitPerRequest: number;

private readonly createdAfter?: Date;

private readonly documentConverter: JiraDocumentConverter;

constructor({
Expand All @@ -347,13 +350,15 @@ export class JiraProjectLoader extends BaseDocumentLoader {
username,
accessToken,
limitPerRequest = 100,
createdAfter,
}: JiraProjectLoaderParams) {
super();
this.host = host;
this.projectKey = projectKey;
this.username = username;
this.accessToken = accessToken;
this.limitPerRequest = limitPerRequest;
this.createdAfter = createdAfter;
this.documentConverter = new JiraDocumentConverter({ host, projectKey });
}

Expand Down Expand Up @@ -385,7 +390,14 @@ export class JiraProjectLoader extends BaseDocumentLoader {

while (true) {
try {
const pageUrl = `${url}?jql=project=${this.projectKey}&startAt=${startAt}&maxResults=${this.limitPerRequest}`;
const jqlProps = [
`project=${this.projectKey}`,
`startAt=${startAt}`,
`maxResults=${this.limitPerRequest}`,
...(this.createdAfter ? [`created>${this.createdAfter.toISOString()}`] : [])
];
const pageUrl = `${url}?jql=${jqlProps.join('&')}`;

const options = {
method: "GET",
headers: {
Expand Down

0 comments on commit cc18630

Please sign in to comment.