Skip to content

Commit

Permalink
use octokit method (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshick committed Nov 5, 2022
1 parent bd2c7fd commit 3bebc58
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 51 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ jobs:
name: Dogfood
runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: read
pull-requests: write
steps:
- name: Checkout repo
uses: actions/checkout@v3
Expand Down
33 changes: 16 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
"@types/node": "^18.11.9",
"@typescript-eslint/eslint-plugin": "^5.42.0",
"@typescript-eslint/parser": "^5.42.0",
"@zeit/ncc": "^0.22.3",
"@vercel/ncc": "^0.34.0",
"eslint": "^8.26.0",
"eslint-config-prettier": "^8.5.0",
"eslint-import-resolver-typescript": "^3.5.2",
Expand Down
39 changes: 8 additions & 31 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import { HttpClient } from '@actions/http-client'
import { Endpoints, RequestHeaders } from '@octokit/types'
import { Endpoints } from '@octokit/types'
import fs from 'node:fs/promises'

type ListCommitPullsResponseData =
Expand All @@ -10,32 +10,6 @@ type CreateIssueCommentResponseData =
Endpoints['POST /repos/{owner}/{repo}/issues/{issue_number}/comments']['response']['data']
type IssuesListCommentsResponseData =
Endpoints['GET /repos/{owner}/{repo}/issues/comments']['response']['data']
interface ListCommitPullsParams {
repoToken: string
owner: string
repo: string
commitSha: string
}

const listCommitPulls = async (
params: ListCommitPullsParams,
): Promise<ListCommitPullsResponseData | null> => {
const { repoToken, owner, repo, commitSha } = params

const http = new HttpClient('http-client-add-pr-comment')

const additionalHeaders: RequestHeaders = {
accept: 'application/vnd.github.groot-preview+json',
authorization: `token ${repoToken}`,
}

const body = await http.getJson<ListCommitPullsResponseData>(
`https://api.github.com/repos/${owner}/${repo}/commits/${commitSha}/pulls`,
additionalHeaders,
)

return body.result
}

const getIssueNumberFromCommitPullsList = (
commitPullsList: ListCommitPullsResponseData,
Expand Down Expand Up @@ -151,6 +125,7 @@ const run = async (): Promise<void> => {
}

const [owner, repo] = repoFullName.split('/')
const octokit = github.getOctokit(repoToken)

let issueNumber

Expand All @@ -160,8 +135,12 @@ const run = async (): Promise<void> => {
issueNumber = pullRequest.number
} else {
// If this is not a pull request, attempt to find a PR matching the sha
const commitPullsList = await listCommitPulls({ repoToken, owner, repo, commitSha })
issueNumber = commitPullsList && getIssueNumberFromCommitPullsList(commitPullsList)
const commitPullsList = await octokit.rest.repos.listPullRequestsAssociatedWithCommit({
owner,
repo,
commit_sha: commitSha,
})
issueNumber = commitPullsList.data && getIssueNumberFromCommitPullsList(commitPullsList.data)
}

if (!issueNumber) {
Expand All @@ -172,8 +151,6 @@ const run = async (): Promise<void> => {
return
}

const octokit = github.getOctokit(repoToken)

let shouldCreateComment = true

if (!allowRepeats) {
Expand Down

0 comments on commit 3bebc58

Please sign in to comment.