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 352b5dd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 49 deletions.
39 changes: 18 additions & 21 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,9 @@ async function draftMessage(newDependencies) {
${info[dep].description
? `<tr><td>Description</td><td>${info[dep].description}</td></tr>`
: ``}
${info[dep].author
? `<tr><td>Author</td><td>${info[dep].author}</td></tr>`
: ``}
${info[dep].license
? `<tr><td>License</td><td>${info[dep].license}</td></tr>`
: ``}
${info[dep].contributors
? `<tr><td>Contributors</td><td>${info[dep].contributors
?.map(contributor => contributor)
.join(', ')}</td></tr>`
: ``}
${info[dep].time?.created
? `<tr><td>Created on</td><td>${info[dep].time.created}</td></tr>`
: ``}
Expand Down Expand Up @@ -420,13 +412,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 +437,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 +450,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 +461,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 +479,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 +510,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 +524,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 +556,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 +569,6 @@ class GitHubClient {
static getClient() {
if (!this.hydratedInstance)
this.hydratedInstance = new GitHubClient();
console.log(this.hydratedInstance);
return this.hydratedInstance;
}
}
Expand Down
13 changes: 0 additions & 13 deletions src/modules/message/draftMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ async function draftMessage(
debug(`Package not found: ${dependency}`)
}
}

const messageInfo = (dep: string): string =>
`
### ${
Expand All @@ -48,23 +47,11 @@ async function draftMessage(
? `<tr><td>Description</td><td>${info[dep].description}</td></tr>`
: ``
}
${
info[dep].author
? `<tr><td>Author</td><td>${info[dep].author}</td></tr>`
: ``
}
${
info[dep].license
? `<tr><td>License</td><td>${info[dep].license}</td></tr>`
: ``
}
${
info[dep].contributors
? `<tr><td>Contributors</td><td>${info[dep].contributors
?.map(contributor => contributor)
.join(', ')}</td></tr>`
: ``
}
${
info[dep].time?.created
? `<tr><td>Created on</td><td>${info[dep].time.created}</td></tr>`
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 352b5dd

Please sign in to comment.