Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove plugin.overwriteIssueSummary option #123

Merged
merged 20 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/authentication/credentials.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AxiosResponse } from "axios";
import { Requests } from "../https/requests";
import { logInfo, logSuccess } from "../logging/logging";
import { StringMap } from "../types/util";
import { encode } from "../util/base64";

/**
Expand All @@ -9,9 +10,7 @@ import { encode } from "../util/base64";
* { "Authorization": "Bearer xyz" }
* { "Content-Type": "application/json" }
*/
export interface HTTPHeader {
[key: string]: string;
}
export type HTTPHeader = StringMap<string>;

export abstract class APICredentials<O> {
public abstract getAuthenticationHeader(options?: O): Promise<HTTPHeader>;
Expand Down
69 changes: 65 additions & 4 deletions src/client/jira/jiraClient.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AxiosError, AxiosHeaders, HttpStatusCode } from "axios";
import { expect } from "chai";
import dedent from "dedent";
import fs from "fs";
import { expectToExist, resolveTestDirPath, stubLogging, stubRequests } from "../../../test/util";
import { BasicAuthCredentials } from "../../authentication/credentials";
Expand Down Expand Up @@ -73,8 +74,10 @@ describe("the jira clients", () => {
"./test/resources/turtle.png"
);
expect(stubbedSuccess).to.have.been.calledOnceWith(
"Successfully attached files to issue CYP-123:",
"turtle.png"
dedent(`
Successfully attached files to issue: CYP-123
turtle.png
`)
);
});
it("returns the correct values", async () => {
Expand Down Expand Up @@ -128,8 +131,11 @@ describe("the jira clients", () => {
"./test/resources/greetings.txt"
);
expect(stubbedSuccess).to.have.been.calledOnceWith(
"Successfully attached files to issue CYP-123:",
"turtle.png, greetings.txt"
dedent(`
Successfully attached files to issue: CYP-123
turtle.png
greetings.txt
`)
);
});
it("returns the correct values", async () => {
Expand Down Expand Up @@ -487,6 +493,61 @@ describe("the jira clients", () => {
);
});
});

describe("editIssue", () => {
it("logs correct messages", async () => {
const { stubbedInfo, stubbedSuccess } = stubLogging();
const { stubbedPut } = stubRequests();
stubbedPut.onFirstCall().resolves({
status: HttpStatusCode.NoContent,
data: null,
headers: null,
statusText: HttpStatusCode[HttpStatusCode.NoContent],
config: null,
});
await client.editIssue("CYP-123", {
fields: { summary: "Hello" },
});
expect(stubbedInfo).to.have.been.calledOnceWithExactly("Editing issue...");
expect(stubbedSuccess).to.have.been.calledOnceWithExactly(
"Successfully edited issue: CYP-123"
);
});

it("should handle bad responses", async () => {
const { stubbedError } = stubLogging();
const { stubbedPut } = stubRequests();
stubbedPut.onFirstCall().rejects(
new AxiosError(
"Request failed with status code 400",
HttpStatusCode.BadRequest.toString(),
undefined,
null,
{
status: HttpStatusCode.BadRequest,
statusText: HttpStatusCode[HttpStatusCode.BadRequest],
config: { headers: new AxiosHeaders() },
headers: {},
data: {
errorMessages: ["issue CYP-XYZ does not exist"],
},
}
)
);
const response = await client.editIssue("CYP-XYZ", {
fields: { summary: "Hi" },
});
expect(response).to.be.undefined;
expect(stubbedError).to.have.been.calledTwice;
expect(stubbedError).to.have.been.calledWithExactly(
"Failed to edit issue: AxiosError: Request failed with status code 400"
);
const expectedPath = resolveTestDirPath("editIssue.json");
expect(stubbedError).to.have.been.calledWithExactly(
`Complete error logs have been written to: ${expectedPath}`
);
});
});
});
});
});
91 changes: 74 additions & 17 deletions src/client/jira/jiraClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AxiosResponse } from "axios";
import dedent from "dedent";
import FormData from "form-data";
import fs from "fs";
import { BasicAuthCredentials, HTTPHeader, PATCredentials } from "../../authentication/credentials";
Expand All @@ -19,9 +20,9 @@ import {
IssueTypeDetailsCloud,
IssueTypeDetailsServer,
} from "../../types/jira/responses/issueTypeDetails";
import { IssueUpdateCloud, IssueUpdateServer } from "../../types/jira/responses/issueUpdate";
import { JsonTypeCloud, JsonTypeServer } from "../../types/jira/responses/jsonType";
import { SearchResults } from "../../types/jira/responses/searchResults";
import { OneOf } from "../../types/util";
import { Client } from "../client";

/**
Expand All @@ -33,8 +34,9 @@ export abstract class JiraClient<
FieldDetailType extends FieldDetailServer | FieldDetailCloud,
JsonType extends JsonTypeServer | JsonTypeCloud,
IssueType extends IssueServer | IssueCloud,
IssueTypeDetailsResponse extends OneOf<[IssueTypeDetailsServer, IssueTypeDetailsCloud]>,
SearchRequestType extends SearchRequestServer | SearchRequestCloud
IssueTypeDetailsResponse extends IssueTypeDetailsServer | IssueTypeDetailsCloud,
SearchRequestType extends SearchRequestServer | SearchRequestCloud,
IssueUpdateType extends IssueUpdateServer | IssueUpdateCloud
> extends Client<CredentialsType> {
/**
* Construct a new Jira client using the provided credentials.
Expand Down Expand Up @@ -99,10 +101,12 @@ export abstract class JiraClient<
}
);
logSuccess(
`Successfully attached files to issue ${issueIdOrKey}:`,
response.data
.map((attachment: AttachmentType) => attachment.filename)
.join(", ")
dedent(`
Successfully attached files to issue: ${issueIdOrKey}
${response.data
.map((attachment: AttachmentType) => attachment.filename)
.join("\n")}
`)
);
return response.data;
} finally {
Expand Down Expand Up @@ -146,11 +150,15 @@ export abstract class JiraClient<
);
logSuccess(`Successfully retrieved data for ${response.data.length} issue types.`);
logDebug(
"Received data for issue types:",
...response.data.map(
(issueType: IssueTypeDetailsResponse) =>
`${issueType.name} (id: ${issueType.id})`
)
dedent(`
Received data for issue types:
${response.data
.map(
(issueType: IssueTypeDetailsResponse) =>
`${issueType.name} (id: ${issueType.id})`
)
.join("\n")}
`)
);
return response.data;
} finally {
Expand Down Expand Up @@ -199,10 +207,12 @@ export abstract class JiraClient<
);
logSuccess(`Successfully retrieved data for ${response.data.length} fields`);
logDebug(
"Received data for fields:",
...response.data.map(
(field: FieldDetailType) => `\n${field.name} (id: ${field.id})`
)
dedent(`
Received data for fields:
${response.data
.map((field: FieldDetailType) => `${field.name} (id: ${field.id})`)
.join("\n")}
`)
);
return response.data;
} finally {
Expand Down Expand Up @@ -234,7 +244,7 @@ export abstract class JiraClient<
return await this.credentials
.getAuthenticationHeader()
.then(async (header: HTTPHeader) => {
logInfo(`Searching issues...`);
logInfo("Searching issues...");
const progressInterval = this.startResponseInterval(this.apiBaseURL);
try {
let total = 0;
Expand Down Expand Up @@ -275,4 +285,51 @@ export abstract class JiraClient<
* @returns the endpoint
*/
public abstract getUrlPostSearch(): string;

/**
* Edits an issue. A transition may be applied and issue properties updated as part of the edit.
* The edits to the issue's fields are defined using `update` and `fields`.
*
* The parent field may be set by key or ID. For standard issue types, the parent may be removed
* by setting `update.parent.set.none` to `true`.
*
* @param issueIdOrKey the ID or key of the issue
* @param issueUpdateData the edit data
* @returns the ID or key of the edited issue or `undefined` in case of errors
* @see https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issues/#api-rest-api-3-issue-issueidorkey-put
* @see https://docs.atlassian.com/software/jira/docs/api/REST/9.10.0/#api/2/issue-editIssue
*/
public async editIssue(
issueIdOrKey: string,
issueUpdateData: IssueUpdateType
): Promise<string | undefined> {
try {
await this.credentials.getAuthenticationHeader().then(async (header: HTTPHeader) => {
logInfo("Editing issue...");
const progressInterval = this.startResponseInterval(this.apiBaseURL);
try {
await Requests.put(this.getUrlEditIssue(issueIdOrKey), issueUpdateData, {
headers: {
...header,
},
});
logSuccess(`Successfully edited issue: ${issueIdOrKey}`);
} finally {
clearInterval(progressInterval);
}
});
return issueIdOrKey;
} catch (error: unknown) {
logError(`Failed to edit issue: ${error}`);
writeErrorFile(error, "editIssue");
}
}
/**
*
* Returns the endpoint to use for editing issues.
*
* @param issueIdOrKey the ID or key of the issue
* @returns the endpoint
*/
public abstract getUrlEditIssue(issueIdOrKey: string): string;
}
5 changes: 5 additions & 0 deletions src/client/jira/jiraClientCloud.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@ describe("the jira cloud client", () => {
it("issue types", () => {
expect(client.getUrlGetIssueTypes()).to.eq("https://example.org/rest/api/3/issuetype");
});
it("edit issue", () => {
expect(client.getUrlEditIssue("CYP-123")).to.eq(
"https://example.org/rest/api/3/issue/CYP-123"
);
});
});
});
8 changes: 7 additions & 1 deletion src/client/jira/jiraClientCloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AttachmentCloud } from "../../types/jira/responses/attachment";
import { FieldDetailCloud } from "../../types/jira/responses/fieldDetail";
import { IssueCloud } from "../../types/jira/responses/issue";
import { IssueTypeDetailsCloud } from "../../types/jira/responses/issueTypeDetails";
import { IssueUpdateCloud } from "../../types/jira/responses/issueUpdate";
import { JsonTypeCloud } from "../../types/jira/responses/jsonType";
import { JiraClient } from "./jiraClient";

Expand All @@ -17,7 +18,8 @@ export class JiraClientCloud extends JiraClient<
JsonTypeCloud,
IssueCloud,
IssueTypeDetailsCloud,
SearchRequestCloud
SearchRequestCloud,
IssueUpdateCloud
> {
public getUrlAddAttachment(issueIdOrKey: string): string {
return `${this.apiBaseURL}/rest/api/3/issue/${issueIdOrKey}/attachments`;
Expand All @@ -34,4 +36,8 @@ export class JiraClientCloud extends JiraClient<
public getUrlPostSearch(): string {
return `${this.apiBaseURL}/rest/api/3/search`;
}

public getUrlEditIssue(issueIdOrKey: string): string {
return `${this.apiBaseURL}/rest/api/3/issue/${issueIdOrKey}`;
}
}
5 changes: 5 additions & 0 deletions src/client/jira/jiraClientServer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@ describe("the jira server client", () => {
it("issue types", () => {
expect(client.getUrlGetIssueTypes()).to.eq("https://example.org/rest/api/2/issuetype");
});
it("edit issue", () => {
expect(client.getUrlEditIssue("CYP-123")).to.eq(
"https://example.org/rest/api/2/issue/CYP-123"
);
});
});
});
8 changes: 7 additions & 1 deletion src/client/jira/jiraClientServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AttachmentServer } from "../../types/jira/responses/attachment";
import { FieldDetailServer } from "../../types/jira/responses/fieldDetail";
import { IssueServer } from "../../types/jira/responses/issue";
import { IssueTypeDetailsServer } from "../../types/jira/responses/issueTypeDetails";
import { IssueUpdateServer } from "../../types/jira/responses/issueUpdate";
import { JsonTypeServer } from "../../types/jira/responses/jsonType";
import { JiraClient } from "./jiraClient";

Expand All @@ -17,7 +18,8 @@ export class JiraClientServer extends JiraClient<
JsonTypeServer,
IssueServer,
IssueTypeDetailsServer,
SearchRequestServer
SearchRequestServer,
IssueUpdateServer
> {
public getUrlAddAttachment(issueIdOrKey: string): string {
return `${this.apiBaseURL}/rest/api/2/issue/${issueIdOrKey}/attachments`;
Expand All @@ -34,4 +36,8 @@ export class JiraClientServer extends JiraClient<
public getUrlPostSearch(): string {
return `${this.apiBaseURL}/rest/api/2/search`;
}

public getUrlEditIssue(issueIdOrKey: string): string {
return `${this.apiBaseURL}/rest/api/2/issue/${issueIdOrKey}`;
}
}
2 changes: 1 addition & 1 deletion src/client/xray/xrayClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe("the xray clients", () => {
new BasicAuthCredentials("user", "token"),
new DummyJiraClient()
)
: new XrayClientCloud(RESOLVED_JWT_CREDENTIALS, new DummyJiraClient());
: new XrayClientCloud(RESOLVED_JWT_CREDENTIALS);
});

describe("import execution", () => {
Expand Down
Loading