Skip to content

Commit

Permalink
feat: testrail v7 support (#15)
Browse files Browse the repository at this point in the history
* fix: update packages

* feat: update api to support testrail v7
  • Loading branch information
DamianOsipiuk committed Sep 25, 2021
1 parent 7072dea commit 7cce427
Show file tree
Hide file tree
Showing 14 changed files with 6,490 additions and 1,758 deletions.
File renamed without changes.
7,953 changes: 6,261 additions & 1,692 deletions package-lock.json

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,18 @@
"node-fetch": "^2.6.1"
},
"devDependencies": {
"@types/jest": "^26.0.23",
"@types/node": "^15.12.2",
"@types/jest": "^27.0.2",
"@types/node": "^16.9.6",
"@types/node-fetch": "^2.5.10",
"@typescript-eslint/eslint-plugin": "^4.26.1",
"@typescript-eslint/parser": "^4.26.1",
"eslint": "^7.28.0",
"@typescript-eslint/eslint-plugin": "^4.31.2",
"@typescript-eslint/parser": "^4.31.2",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"jest": "^27.0.4",
"prettier": "^2.3.1",
"ts-jest": "^27.0.3",
"typescript": "^4.3.2"
"eslint-plugin-prettier": "^4.0.0",
"jest": "^27.2.1",
"prettier": "^2.4.1",
"ts-jest": "^27.0.5",
"ts-node": "^10.2.1",
"typescript": "^4.4.3"
}
}
8 changes: 8 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import qs from "querystring";
import fetch, { Response } from "node-fetch";

import {
addAttachmentToCase,
addAttachmentToPlan,
addAttachmentToPlanEntry,
addAttachmentToResult,
addAttachmentToRun,
delete_attachment,
get_attachment,
get_attachments_for_case,
Expand All @@ -15,11 +17,13 @@ import {
} from "./attachments";
import {
addCase,
copyCasesToSection,
deleteCase,
deleteCases,
getCase,
getCases,
getHistoryForCase,
moveCasesToSection,
updateCase,
updateCases,
} from "./cases";
Expand Down Expand Up @@ -213,9 +217,11 @@ export class TestRail {
//#endregion

//#region Attachments
addAttachmentToCase = addAttachmentToCase;
addAttachmentToPlan = addAttachmentToPlan;
addAttachmentToPlanEntry = addAttachmentToPlanEntry;
addAttachmentToResult = addAttachmentToResult;
addAttachmentToRun = addAttachmentToRun;
get_attachments_for_case = get_attachments_for_case;
get_attachments_for_plan = get_attachments_for_plan;
get_attachments_for_plan_entry = get_attachments_for_plan_entry;
Expand All @@ -230,8 +236,10 @@ export class TestRail {
getCases = getCases;
getHistoryForCase = getHistoryForCase;
addCase = addCase;
copyCasesToSection = copyCasesToSection;
updateCase = updateCase;
updateCases = updateCases;
moveCasesToSection = moveCasesToSection;
deleteCase = deleteCase;
deleteCases = deleteCases;
//#endregion
Expand Down
95 changes: 83 additions & 12 deletions src/attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,30 @@ import FormData from "form-data";
import { RequestType } from "./interfaces";

import type { TestRail } from "./api";
import type { Attachment, AddAttachmentResult } from "./interfaces";
import type {
AddAttachmentResult,
Attachment,
BulkFilters,
BulkResult,
} from "./interfaces";

export function addAttachmentToCase(
this: TestRail,
case_id: number,
filePath: string
) {
const formData = new FormData();
formData.append("attachment", fs.createReadStream(filePath));

return this.apiPost<AddAttachmentResult>(
"add_attachment_to_case/" + case_id,
formData,
{
headers: {},
requestType: RequestType.Blob,
}
);
}

export function addAttachmentToPlan(
this: TestRail,
Expand Down Expand Up @@ -63,30 +86,78 @@ export function addAttachmentToResult(
);
}

export function get_attachments_for_case(this: TestRail, case_id: number) {
return this.apiGet<Attachment[]>("get_attachments_for_case/" + case_id);
export function addAttachmentToRun(
this: TestRail,
run_id: number,
filePath: string
) {
const formData = new FormData();
formData.append("attachment", fs.createReadStream(filePath));

return this.apiPost<AddAttachmentResult>(
"add_attachment_to_run/" + run_id,
formData,
{
headers: {},
requestType: RequestType.Blob,
}
);
}

export function get_attachments_for_case(
this: TestRail,
case_id: number,
queryVariables?: BulkFilters
) {
return this.apiGet<BulkResult<Attachment, "attachments">>(
"get_attachments_for_case/" + case_id,
{ queryVariables }
);
}

export function get_attachments_for_plan(this: TestRail, plan_id: number) {
return this.apiGet<Attachment[]>("get_attachments_for_plan/" + plan_id);
export function get_attachments_for_plan(
this: TestRail,
plan_id: number,
queryVariables?: BulkFilters
) {
return this.apiGet<BulkResult<Attachment, "attachments">>(
"get_attachments_for_plan/" + plan_id,
{ queryVariables }
);
}

export function get_attachments_for_plan_entry(
this: TestRail,
plan_id: number,
entry_id: number
entry_id: number,
queryVariables?: BulkFilters
) {
return this.apiGet<Attachment[]>(
"get_attachments_for_plan_entry/" + plan_id + "/" + entry_id
return this.apiGet<BulkResult<Attachment, "attachments">>(
"get_attachments_for_plan_entry/" + plan_id + "/" + entry_id,
{ queryVariables }
);
}

export function get_attachments_for_run(this: TestRail, run_id: number) {
return this.apiGet<Attachment[]>("get_attachments_for_run/" + run_id);
export function get_attachments_for_run(
this: TestRail,
run_id: number,
queryVariables?: BulkFilters
) {
return this.apiGet<BulkResult<Attachment, "attachments">>(
"get_attachments_for_run/" + run_id,
{ queryVariables }
);
}

export function get_attachments_for_test(this: TestRail, test_id: number) {
return this.apiGet<Attachment[]>("get_attachments_for_test/" + test_id);
export function get_attachments_for_test(
this: TestRail,
test_id: number,
queryVariables?: BulkFilters
) {
return this.apiGet<BulkResult<Attachment, "attachments">>(
"get_attachments_for_test/" + test_id,
{ queryVariables }
);
}

export function get_attachment(this: TestRail, attachment_id: number) {
Expand Down
34 changes: 31 additions & 3 deletions src/cases.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BulkFilters, BulkResult } from ".";
import type { TestRail } from "./api";
import type { Case, AddCase, CaseHistory } from "./interfaces";

Expand Down Expand Up @@ -27,13 +28,20 @@ export function getCases(
updated_by?: number[];
}
) {
return this.apiGet<Case[]>("get_cases/" + project_id, {
return this.apiGet<BulkResult<Case, "cases">>("get_cases/" + project_id, {
queryVariables,
});
}

export function getHistoryForCase(this: TestRail, case_id: number) {
return this.apiGet<CaseHistory>("get_history_for_case/" + case_id);
export function getHistoryForCase(
this: TestRail,
case_id: number,
queryVariables?: BulkFilters
) {
return this.apiGet<BulkResult<CaseHistory, "history">>(
"get_history_for_case/" + case_id,
{ queryVariables }
);
}

export function addCase(
Expand All @@ -44,6 +52,16 @@ export function addCase(
return this.apiPost<Case>("add_case/" + section_id, case_data);
}

export function copyCasesToSection(
this: TestRail,
section_id: number,
case_ids: number[]
) {
return this.apiPost("copy_cases_to_section/" + section_id, {
case_ids,
});
}

export function updateCase(
this: TestRail,
case_id: number,
Expand All @@ -65,6 +83,16 @@ export function updateCases(
});
}

export function moveCasesToSection(
this: TestRail,
section_id: number,
case_ids: number[]
) {
return this.apiPost("move_cases_to_section/" + section_id, {
case_ids,
});
}

/**
* Please Note: Deleting a test case cannot be undone and also permanently deletes all test results in active test runs (i.e. test runs that haven’t been closed (archived) yet).
*/
Expand Down
18 changes: 16 additions & 2 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,31 @@ export interface Context {
project_ids: null;
}

export type BulkResult<Type, PropertyName extends string> = {
offset: number;
limit: number;
size: number;
_link: {
next: string | null;
prev: string | null;
};
} & { [P in PropertyName]: Type[] };

export type BulkFilters = {
limit?: number;
offset?: number;
};

//#region Attachments
export interface Attachment {
id: number;
name: string;
filename: string;
size: number;
created_on: number;
project_id: number;
case_id: number;
test_change_id?: number;
user_id: number;
result_id: number;
}

export interface AddAttachmentResult {
Expand Down
19 changes: 14 additions & 5 deletions src/milestones.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import type { TestRail } from "./api";
import type { Milestone, AddMilestone, UpdateMilestone } from "./interfaces";
import type {
BulkFilters,
BulkResult,
Milestone,
AddMilestone,
UpdateMilestone,
} from "./interfaces";

export function getMilestone(this: TestRail, milestone_id: number) {
return this.apiGet<Milestone>("get_milestone/" + milestone_id);
Expand All @@ -8,14 +14,17 @@ export function getMilestone(this: TestRail, milestone_id: number) {
export function getMilestones(
this: TestRail,
project_id: number,
filters?: {
filters?: BulkFilters & {
is_completed?: 0 | 1;
is_started?: 0 | 1;
}
) {
return this.apiGet<Milestone[]>("get_milestones/" + project_id, {
queryVariables: filters,
});
return this.apiGet<BulkResult<Milestone, "milestones">>(
"get_milestones/" + project_id,
{
queryVariables: filters,
}
);
}

export function addMilestone(
Expand Down
8 changes: 4 additions & 4 deletions src/plans.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { TestRail } from "./api";
import type {
BulkFilters,
BulkResult,
Plan,
AddPlan,
PlanEntry,
Expand All @@ -15,17 +17,15 @@ export function getPlan(this: TestRail, plan_id: number) {
export function getPlans(
this: TestRail,
project_id: number,
filters?: {
filters?: BulkFilters & {
created_after?: number;
created_before?: number;
created_by?: number[];
is_completed?: 0 | 1;
limit?: number;
offset?: number;
milestone_id?: number[];
}
) {
return this.apiGet<Plan[]>("get_plans/" + project_id, {
return this.apiGet<BulkResult<Plan, "plans">>("get_plans/" + project_id, {
queryVariables: filters,
});
}
Expand Down
14 changes: 11 additions & 3 deletions src/projects.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import type { TestRail } from "./api";
import type { Project, AddProject, UpdateProject } from "./interfaces";
import type {
BulkFilters,
BulkResult,
Project,
AddProject,
UpdateProject,
} from "./interfaces";

export function getProject(this: TestRail, project_id: number) {
return this.apiGet<Project>("get_project/" + project_id);
}

export function getProjects(
this: TestRail,
filters?: { is_completed?: 0 | 1 }
filters?: BulkFilters & { is_completed?: 0 | 1 }
) {
return this.apiGet<Project[]>("get_projects", { queryVariables: filters });
return this.apiGet<BulkResult<Project, "projects">>("get_projects", {
queryVariables: filters,
});
}

export function addProject(this: TestRail, data: AddProject) {
Expand Down
Loading

0 comments on commit 7cce427

Please sign in to comment.