use octokit method (#59)

This commit is contained in:
Michael Shick 2022-11-05 12:44:20 -04:00 committed by GitHub
parent bd2c7fd13a
commit 3bebc5877c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 51 deletions

View file

@ -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

33
package-lock.json generated
View file

@ -18,7 +18,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",
@ -971,6 +971,15 @@
"url": "https://opencollective.com/typescript-eslint"
}
},
"node_modules/@vercel/ncc": {
"version": "0.34.0",
"resolved": "https://registry.npmjs.org/@vercel/ncc/-/ncc-0.34.0.tgz",
"integrity": "sha512-G9h5ZLBJ/V57Ou9vz5hI8pda/YQX5HQszCs3AmIus3XzsmRn/0Ptic5otD3xVST8QLKk7AMk7AqpsyQGN7MZ9A==",
"dev": true,
"bin": {
"ncc": "dist/ncc/cli.js"
}
},
"node_modules/@xmldom/xmldom": {
"version": "0.8.5",
"resolved": "https://registry.npmjs.org/@xmldom/xmldom/-/xmldom-0.8.5.tgz",
@ -980,16 +989,6 @@
"node": ">=10.0.0"
}
},
"node_modules/@zeit/ncc": {
"version": "0.22.3",
"resolved": "https://registry.npmjs.org/@zeit/ncc/-/ncc-0.22.3.tgz",
"integrity": "sha512-jnCLpLXWuw/PAiJiVbLjA8WBC0IJQbFeUwF4I9M+23MvIxTxk5pD4Q8byQBSPmHQjz5aBoA7AKAElQxMpjrCLQ==",
"deprecated": "@zeit/ncc is no longer maintained. Please use @vercel/ncc instead.",
"dev": true,
"bin": {
"ncc": "dist/ncc/cli.js"
}
},
"node_modules/@zxing/text-encoding": {
"version": "0.9.0",
"resolved": "https://registry.npmjs.org/@zxing/text-encoding/-/text-encoding-0.9.0.tgz",
@ -7741,18 +7740,18 @@
"eslint-visitor-keys": "^3.3.0"
}
},
"@vercel/ncc": {
"version": "0.34.0",
"resolved": "https://registry.npmjs.org/@vercel/ncc/-/ncc-0.34.0.tgz",
"integrity": "sha512-G9h5ZLBJ/V57Ou9vz5hI8pda/YQX5HQszCs3AmIus3XzsmRn/0Ptic5otD3xVST8QLKk7AMk7AqpsyQGN7MZ9A==",
"dev": true
},
"@xmldom/xmldom": {
"version": "0.8.5",
"resolved": "https://registry.npmjs.org/@xmldom/xmldom/-/xmldom-0.8.5.tgz",
"integrity": "sha512-0dpjDLeCXYThL2YhqZcd/spuwoH+dmnFoND9ZxZkAYxp1IJUB2GP16ow2MJRsjVxy8j1Qv8BJRmN5GKnbDKCmQ==",
"dev": true
},
"@zeit/ncc": {
"version": "0.22.3",
"resolved": "https://registry.npmjs.org/@zeit/ncc/-/ncc-0.22.3.tgz",
"integrity": "sha512-jnCLpLXWuw/PAiJiVbLjA8WBC0IJQbFeUwF4I9M+23MvIxTxk5pD4Q8byQBSPmHQjz5aBoA7AKAElQxMpjrCLQ==",
"dev": true
},
"@zxing/text-encoding": {
"version": "0.9.0",
"resolved": "https://registry.npmjs.org/@zxing/text-encoding/-/text-encoding-0.9.0.tgz",

View file

@ -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",

View file

@ -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 =
@ -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,
@ -151,6 +125,7 @@ const run = async (): Promise<void> => {
}
const [owner, repo] = repoFullName.split('/')
const octokit = github.getOctokit(repoToken)
let issueNumber
@ -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) {
@ -172,8 +151,6 @@ const run = async (): Promise<void> => {
return
}
const octokit = github.getOctokit(repoToken)
let shouldCreateComment = true
if (!allowRepeats) {