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

Fix incorrect Github REST API response types #1321

Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 8 additions & 0 deletions src/app/core/models/github/github-issue.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IssueState } from '../../../../../graphql/graphql-types';
import { GithubComment } from './github-comment.model';
import { GithubLabel } from './github-label.model';
import { GithubRestIssue } from './github-rest-issue';

export class GithubIssue {
id: string; // Github's backend's id
Expand All @@ -25,6 +26,13 @@ export class GithubIssue {
};
comments: Array<GithubComment>;

static fromRestGithubIssue(restGithubIssue: GithubRestIssue): GithubIssue {
return new GithubIssue({
...restGithubIssue,
state: restGithubIssue.state.toUpperCase()
});
}

constructor(githubIssue: {}) {
Object.assign(this, githubIssue);
this.labels = [];
Expand Down
26 changes: 26 additions & 0 deletions src/app/core/models/github/github-rest-issue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { GithubComment } from './github-comment.model';
import { GithubLabel } from './github-label.model';

export type GithubRestIssue = {
id: string; // Github's backend's id
number: number; // Issue's display id
assignees: Array<{
id: number;
login: string;
url: string;
}>;
body: string;
created_at: string;
labels: Array<GithubLabel>;
state: 'open' | 'closed';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some code tracing, there is a RestGithubIssueState type in github-issue-filter.model.ts:
Screenshot 2025-02-10 at 1 50 07 PM

I think it would be better to reuse this type instead of hard coding the state values here

title: string;
updated_at: string;
url: string;
user: {
// Author of the issue
login: string;
avatar_url: string;
url: string;
};
comments: Array<GithubComment>;
};
15 changes: 8 additions & 7 deletions src/app/core/services/github.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { GithubRelease } from '../models/github/github.release';
import { SessionData } from '../models/session.model';
import { ERRORCODE_NOT_FOUND, ErrorHandlingService } from './error-handling.service';
import { LoggingService } from './logging.service';
import { GithubRestIssue } from '../models/github/github-rest-issue';

const { Octokit } = require('@octokit/rest');
const CATCHER_ORG = 'CATcher-org';
Expand Down Expand Up @@ -362,25 +363,25 @@ export class GithubService {

closeIssue(id: number): Observable<GithubIssue> {
return from(octokit.issues.update({ owner: ORG_NAME, repo: REPO, issue_number: id, state: 'closed' })).pipe(
map((response: GithubResponse<GithubIssue>) => {
map((response: GithubResponse<GithubRestIssue>) => {
this.issuesLastModifiedManager.set(id, response.headers['last-modified']);
return new GithubIssue(response.data);
return GithubIssue.fromRestGithubIssue(response.data);
})
);
}

reopenIssue(id: number): Observable<GithubIssue> {
return from(octokit.issues.update({ owner: ORG_NAME, repo: REPO, issue_number: id, state: 'open' })).pipe(
map((response: GithubResponse<GithubIssue>) => {
map((response: GithubResponse<GithubRestIssue>) => {
this.issuesLastModifiedManager.set(id, response.headers['last-modified']);
return new GithubIssue(response.data);
return GithubIssue.fromRestGithubIssue(response.data);
})
);
}

createIssue(title: string, description: string, labels: string[]): Observable<GithubIssue> {
return from(octokit.issues.create({ owner: ORG_NAME, repo: REPO, title: title, body: description, labels: labels })).pipe(
map((response: GithubResponse<GithubIssue>) => new GithubIssue(response.data))
map((response: GithubResponse<GithubRestIssue>) => GithubIssue.fromRestGithubIssue(response.data))
);
}

Expand All @@ -402,9 +403,9 @@ export class GithubService {
assignees: assignees
})
).pipe(
map((response: GithubResponse<GithubIssue>) => {
map((response: GithubResponse<GithubRestIssue>) => {
this.issuesLastModifiedManager.set(id, response.headers['last-modified']);
return new GithubIssue(response.data);
return GithubIssue.fromRestGithubIssue(response.data);
}),
catchError((err) => throwError(err))
);
Expand Down
Loading