Skip to content

Commit

Permalink
use rest
Browse files Browse the repository at this point in the history
  • Loading branch information
trumant committed Mar 30, 2024
1 parent 2b3fa0c commit b2c4dbe
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
32 changes: 19 additions & 13 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ async function draftMessage(newDependencies) {
try {
info[dependency] = await (0, package_json_1.default)(dependency, { fullMetadata: true, allVersions: true });
advisorScorePicture[dependency] = await (0, advisorPackageMetadata_1.getAdvisorPackageScore)(dependency);
(0, core_1.debug)(`Package found: ${info[dependency]}`);
}
catch (error) {
(0, core_1.debug)(`Package not found: ${dependency}`);
Expand Down Expand Up @@ -420,13 +421,16 @@ class GitHubClient {
repo;
/** Hydrated Octokit client */
octokit;
/** Hydrated base branch */
baseBranch;
/** Hydrated id of the message created by this action */
messageId;
/** Hydrated instance of this client */
static hydratedInstance = undefined;
constructor() {
/** Hydrates the Octokit client with the provided token */
this.octokit = (0, github_1.getOctokit)((0, core_1.getInput)('token'));
this.baseBranch = '';
/** Initializes the context information */
const { number } = github_1.context.issue;
const { owner, repo } = github_1.context.repo;
Expand All @@ -442,7 +446,7 @@ class GitHubClient {
async createMessage(content) {
if (this.messageId)
return this.updateMessage(content);
await this.octokit.issues.createComment({
await this.octokit.rest.issues.createComment({
owner: this.owner,
repo: this.repo,
issue_number: this.prNumber,
Expand All @@ -455,7 +459,7 @@ class GitHubClient {
async deleteMessage() {
if (!this.messageId)
return;
await this.octokit.issues.deleteComment({
await this.octokit.rest.issues.deleteComment({
owner: this.owner,
repo: this.repo,
comment_id: this.messageId
Expand All @@ -466,12 +470,15 @@ class GitHubClient {
* Returns the ref of the base branch for the current pull request
*/
async getBaseBranch() {
const { data } = await this.octokit.pulls.get({
pull_number: this.prNumber,
owner: this.owner,
repo: this.repo
});
return data.base.ref;
if (this.baseBranch === '') {
const { data } = await this.octokit.rest.pulls.get({
pull_number: this.prNumber,
owner: this.owner,
repo: this.repo
});
this.baseBranch = data.base.ref;
}
return this.baseBranch;
}
/**
* Returns the content of the requested file in the requested branch
Expand All @@ -481,7 +488,7 @@ class GitHubClient {
*/
async getPackage(file, baseBranch) {
try {
const { data: fileInfo } = await this.octokit.repos.getContents({
const { data: fileInfo } = await this.octokit.rest.repos.getContents({
owner: this.owner,
path: file,
ref: baseBranch,
Expand Down Expand Up @@ -512,7 +519,7 @@ class GitHubClient {
*/
async fetchMessage() {
if (this.messageId === undefined) {
const { data } = await this.octokit.issues.listComments({
const { data } = await this.octokit.rest.issues.listComments({
owner: this.owner,
repo: this.repo,
issue_number: this.prNumber
Expand All @@ -526,7 +533,7 @@ class GitHubClient {
* List files in the current pull request
*/
async listFiles() {
const { data } = await this.octokit.pulls.listFiles({
const { data } = await this.octokit.rest.pulls.listFiles({
owner: this.owner,
repo: this.repo,
pull_number: this.prNumber
Expand Down Expand Up @@ -558,7 +565,7 @@ class GitHubClient {
async updateMessage(content) {
if (!this.messageId)
return this.createMessage(content);
await this.octokit.issues.updateComment({
await this.octokit.rest.issues.updateComment({
owner: this.owner,
repo: this.repo,
comment_id: this.messageId,
Expand All @@ -571,7 +578,6 @@ class GitHubClient {
static getClient() {
if (!this.hydratedInstance)
this.hydratedInstance = new GitHubClient();
console.log(this.hydratedInstance);
return this.hydratedInstance;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/modules/message/draftMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ async function draftMessage(
try {
info[dependency] = await packageJson(dependency, {fullMetadata: true, allVersions: true})
advisorScorePicture[dependency] = await getAdvisorPackageScore(dependency)
debug(`Package found: ${info[dependency]}`)
} catch (error) {
debug(`Package not found: ${dependency}`)
}
Expand Down
35 changes: 20 additions & 15 deletions src/services/github-sdk/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ class GitHubClient {
public readonly repo: string
/** Hydrated Octokit client */
private octokit: any
/** Hydrated base branch */
private baseBranch: string
/** Hydrated id of the message created by this action */
private messageId?: number | false
/** Hydrated instance of this client */
private static hydratedInstance?: GitHubClient = undefined
constructor() {
/** Hydrates the Octokit client with the provided token */
this.octokit = getOctokit(getInput('token'))

this.baseBranch = ''
/** Initializes the context information */
const {number} = context.issue
const {owner, repo} = context.repo
Expand All @@ -39,7 +41,7 @@ class GitHubClient {
public async createMessage(content: string): Promise<void> {
if (this.messageId) return this.updateMessage(content)

await this.octokit.issues.createComment({
await this.octokit.rest.issues.createComment({
owner: this.owner,
repo: this.repo,
issue_number: this.prNumber,
Expand All @@ -53,7 +55,7 @@ class GitHubClient {
public async deleteMessage(): Promise<void> {
if (!this.messageId) return

await this.octokit.issues.deleteComment({
await this.octokit.rest.issues.deleteComment({
owner: this.owner,
repo: this.repo,
comment_id: this.messageId
Expand All @@ -66,12 +68,16 @@ class GitHubClient {
* Returns the ref of the base branch for the current pull request
*/
public async getBaseBranch(): Promise<string> {
const {data} = await this.octokit.pulls.get({
pull_number: this.prNumber,
owner: this.owner,
repo: this.repo
})
return data.base.ref
if (this.baseBranch === '') {
const {data} = await this.octokit.rest.pulls.get({
pull_number: this.prNumber,
owner: this.owner,
repo: this.repo
})
this.baseBranch = data.base.ref
}

return this.baseBranch
}

/**
Expand All @@ -82,7 +88,7 @@ class GitHubClient {
*/
public async getPackage(file: string, baseBranch: string): Promise<Package> {
try {
const {data: fileInfo} = await this.octokit.repos.getContents({
const {data: fileInfo} = await this.octokit.rest.repos.getContents({
owner: this.owner,
path: file,
ref: baseBranch,
Expand Down Expand Up @@ -118,7 +124,7 @@ class GitHubClient {
*/
public async fetchMessage(): Promise<number | undefined> {
if (this.messageId === undefined) {
const {data} = await this.octokit.issues.listComments({
const {data} = await this.octokit.rest.issues.listComments({
owner: this.owner,
repo: this.repo,
issue_number: this.prNumber
Expand All @@ -138,7 +144,7 @@ class GitHubClient {
* List files in the current pull request
*/
public async listFiles(): Promise<string[]> {
const {data} = await this.octokit.pulls.listFiles({
const {data} = await this.octokit.rest.pulls.listFiles({
owner: this.owner,
repo: this.repo,
pull_number: this.prNumber
Expand Down Expand Up @@ -173,7 +179,7 @@ class GitHubClient {
public async updateMessage(content: string): Promise<void> {
if (!this.messageId) return this.createMessage(content)

await this.octokit.issues.updateComment({
await this.octokit.rest.issues.updateComment({
owner: this.owner,
repo: this.repo,
comment_id: this.messageId,
Expand All @@ -186,8 +192,7 @@ class GitHubClient {
*/
public static getClient(): GitHubClient {
if (!this.hydratedInstance) this.hydratedInstance = new GitHubClient()
console.log(this.hydratedInstance);


return this.hydratedInstance
}
}
Expand Down

0 comments on commit b2c4dbe

Please sign in to comment.