project clean up and update

This commit is contained in:
Michael Shick 2022-11-04 16:50:39 -04:00
parent 07f690343c
commit 94fd7ef52f
No known key found for this signature in database
GPG key ID: ADF5BC9704BB4A61
14 changed files with 12140 additions and 6559 deletions

View file

@ -1,14 +0,0 @@
# editorconfig.org
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
trim_trailing_whitespace = false

View file

@ -1,3 +0,0 @@
*.js
*.txt
!/.github

View file

@ -1,45 +0,0 @@
module.exports = {
root: true,
extends: ['eslint:recommended', 'plugin:prettier/recommended', 'plugin:import/errors', 'plugin:import/warnings'],
plugins: ['prettier'],
env: {
node: true,
es6: true,
},
parserOptions: {
ecmaVersion: 2018,
},
settings: {
'import/parsers': {
'@typescript-eslint/parser': ['.ts', '.tsx'],
},
'import/resolver': {
typescript: {
alwaysTryTypes: true,
},
},
},
overrides: [
{
files: ['**/*.ts'],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
extends: [
'plugin:import/typescript',
'plugin:@typescript-eslint/recommended',
'prettier/@typescript-eslint',
'plugin:prettier/recommended',
],
parserOptions: {
ecmaVersion: 2018,
project: ['tsconfig.json'],
sourceType: 'module',
tsconfigRootDir: __dirname,
},
env: {
node: true,
es6: true,
},
},
],
}

View file

@ -9,12 +9,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Setup node.js
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: "12.x"
node-version: 16
- name: Install dependencies
run: |
@ -37,12 +37,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Setup node.js
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: "12.x"
node-version: 16
- name: Install dependencies
run: |

1
.node-version Normal file
View file

@ -0,0 +1 @@
v16

View file

@ -1,9 +0,0 @@
module.exports = {
arrowParens: 'avoid',
bracketSpacing: false,
printWidth: 100,
semi: false,
singleQuote: true,
tabWidth: 2,
trailingComma: 'es5',
}

View file

@ -1,9 +1,9 @@
import * as fs from 'fs'
import * as path from 'path'
import * as core from '@actions/core'
import * as github from '@actions/github'
import {WebhookPayload} from '@actions/github/lib/interfaces'
import { WebhookPayload } from '@actions/github/lib/interfaces'
import * as fs from 'fs'
import nock from 'nock'
import * as path from 'path'
import run from '../src/main'
import apiResponse from './sample-pulls-api-response.json'
@ -12,8 +12,12 @@ const repoToken = '12345'
const userLogin = 'github-actions[bot]'
const commitSha = 'abc123'
const simpleMessage = 'hello world'
const multilineMessage = fs.readFileSync(path.resolve(__dirname, './message-windows.txt')).toString()
const multilineMessageWindows = fs.readFileSync(path.resolve(__dirname, './message-windows.txt')).toString()
const multilineMessage = fs
.readFileSync(path.resolve(__dirname, './message-windows.txt'))
.toString()
const multilineMessageWindows = fs
.readFileSync(path.resolve(__dirname, './message-windows.txt'))
.toString()
let issueNumber = 1
@ -81,7 +85,10 @@ describe('add-pr-comment action', () => {
})
nock('https://api.github.com')
.post(`/repos/${repoFullName}/issues/${issueNumber}/comments`, ({body}) => body === simpleMessage)
.post(
`/repos/${repoFullName}/issues/${issueNumber}/comments`,
({ body }) => body === simpleMessage,
)
.reply(200, {
url: 'https://github.com/#example',
})
@ -119,7 +126,10 @@ describe('add-pr-comment action', () => {
.reply(200, apiResponse.result)
nock('https://api.github.com')
.post(`/repos/${repoFullName}/issues/${issueNumber}/comments`, ({body}) => body === simpleMessage)
.post(
`/repos/${repoFullName}/issues/${issueNumber}/comments`,
({ body }) => body === simpleMessage,
)
.reply(200, {
url: 'https://github.com/#example',
})
@ -150,7 +160,9 @@ describe('add-pr-comment action', () => {
return originalSetOutput(key, value)
})
nock('https://api.github.com').get(`/repos/${repoFullName}/commits/${commitSha}/pulls`).reply(200, [])
nock('https://api.github.com')
.get(`/repos/${repoFullName}/commits/${commitSha}/pulls`)
.reply(200, [])
await run()
})
@ -180,7 +192,9 @@ describe('add-pr-comment action', () => {
},
]
nock('https://api.github.com').get(`/repos/${repoFullName}/issues/1/comments`).reply(200, replyBody)
nock('https://api.github.com')
.get(`/repos/${repoFullName}/issues/1/comments`)
.reply(200, replyBody)
await run()
})
@ -209,7 +223,9 @@ describe('add-pr-comment action', () => {
},
]
nock('https://api.github.com').get(`/repos/${repoFullName}/issues/1/comments`).reply(200, replyBody)
nock('https://api.github.com')
.get(`/repos/${repoFullName}/issues/1/comments`)
.reply(200, replyBody)
await run()
})

View file

@ -45,7 +45,10 @@
"tree_id": "055d2e982d249db10b8dd73fc1745ff97576ecc9",
"url": "https://github.com/mshick/add-pr-comment/commit/566e30ed2c7a061fa9e14117c14eb385a197e750"
},
"pusher": { "email": "m@shick.us", "name": "mshick" },
"pusher": {
"email": "m@shick.us",
"name": "mshick"
},
"ref": "refs/heads/master",
"repository": {
"archive_url": "https://api.github.com/repos/mshick/add-pr-comment/{archive_format}{/ref}",

View file

@ -305,7 +305,9 @@
"self": {
"href": "https://api.github.com/repos/mshick/add-pr-comment/pulls/6"
},
"html": { "href": "https://github.com/mshick/add-pr-comment/pull/6" },
"html": {
"href": "https://github.com/mshick/add-pr-comment/pull/6"
},
"issue": {
"href": "https://api.github.com/repos/mshick/add-pr-comment/issues/6"
},

View file

@ -21,5 +21,5 @@ branding:
icon: message-circle
color: purple
runs:
using: "node12"
using: "node16"
main: "dist/index.js"

View file

@ -1,7 +1,11 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
@ -14,7 +18,7 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
@ -46,10 +50,10 @@ const isMessagePresent = (message, comments, login) => {
const messageClean = message.replace(cleanRe, '');
return comments.some(({ user, body }) => {
// If a username is provided we can save on a bit of processing
if (login && user.login !== login) {
if (login && (user === null || user === void 0 ? void 0 : user.login) !== login) {
return false;
}
return body.replace(cleanRe, '') === messageClean;
return (body === null || body === void 0 ? void 0 : body.replace(cleanRe, '')) === messageClean;
});
};
const getInputs = () => {
@ -74,6 +78,11 @@ const run = async () => {
return;
}
const { full_name: repoFullName } = repository;
if (!repoFullName) {
core.info('repository is missing a full_name property... weird');
core.setOutput('comment-created', 'false');
return;
}
const [owner, repo] = repoFullName.split('/');
let issueNumber;
if (issue && issue.number) {
@ -96,7 +105,7 @@ const run = async () => {
let shouldCreateComment = true;
if (!allowRepeats) {
core.debug('repeat comments are disallowed, checking for existing');
const { data: comments } = await octokit.issues.listComments({
const { data: comments } = await octokit.rest.issues.listComments({
owner,
repo,
issue_number: issueNumber,
@ -118,7 +127,7 @@ const run = async () => {
});
}
else {
await octokit.issues.createComment({
await octokit.rest.issues.createComment({
owner,
repo,
issue_number: issueNumber,
@ -131,8 +140,10 @@ const run = async () => {
core.setOutput('comment-created', 'false');
}
}
catch (error) {
core.setFailed(error.message);
catch (err) {
if (err instanceof Error) {
core.setFailed(err.message);
}
}
};
// Don't auto-execute in the test environment

18332
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -2,17 +2,6 @@
"name": "@mshick/add-pr-comment",
"version": "1.0.0",
"description": "A GitHub Action which adds a comment to a Pull Request Issue.",
"main": "lib/main.js",
"scripts": {
"build": "tsc && ncc build lib/main.js",
"lint": "eslint . --ext .ts",
"test": "jest",
"clean": "rm -rf node_modules dist package-lock.json __tests__/runner/**/*"
},
"repository": {
"type": "git",
"url": "git+https://github.com/mshick/add-pr-comment.git"
},
"keywords": [
"GitHub",
"Actions",
@ -21,32 +10,123 @@
"comments",
"pr"
],
"author": "Michael Shick <m@shick.us>",
"license": "MIT",
"homepage": "https://github.com/mshick/add-pr-comment#readme",
"bugs": {
"url": "https://github.com/mshick/add-pr-comment/issues"
},
"homepage": "https://github.com/mshick/add-pr-comment#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/mshick/add-pr-comment.git"
},
"license": "MIT",
"author": "Michael Shick <m@shick.us>",
"main": "lib/main.js",
"scripts": {
"build": "tsc",
"clean": "rm -rf node_modules dist package-lock.json __tests__/runner/**/*",
"lint": "eslint src/**/*.ts",
"package": "ncc build --source-map --license licenses.txt",
"test": "jest"
},
"prettier": {
"bracketSpacing": true,
"jsxSingleQuote": true,
"printWidth": 100,
"semi": false,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "all"
},
"eslintConfig": {
"env": {
"node": true
},
"settings": {
"import/resolver": {
"typescript": {}
}
},
"extends": [
"eslint:recommended",
"plugin:prettier/recommended"
],
"rules": {
"capitalized-comments": "off",
"no-console": "error",
"no-unreachable": "error"
},
"overrides": [
{
"files": [
"**/*.ts"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"extends": [
"plugin:@typescript-eslint/recommended"
],
"rules": {
"@typescript-eslint/no-explicit-any": "off"
}
},
{
"files": [
"*.json"
],
"plugins": [
"json-format"
]
},
{
"files": [
"*.mdx",
"*.md"
],
"settings": {
"mdx/code-blocks": false,
"mdx/language-mapper": {}
},
"extends": "plugin:mdx/recommended",
"rules": {
"indent": "off"
}
}
]
},
"eslintIgnore": [
"tsconfig.json",
"dist"
],
"dependencies": {
"@actions/core": "^1.2.4",
"@actions/github": "^4.0.0",
"@actions/http-client": "^1.0.8"
"@actions/core": "^1.10.0",
"@actions/github": "^5.1.1",
"@actions/http-client": "^2.0.1"
},
"devDependencies": {
"@octokit/types": "^5.4.0",
"@types/jest": "^26.0.9",
"@types/node": "^12.12.42",
"@typescript-eslint/eslint-plugin": "^3.9.0",
"@typescript-eslint/parser": "^3.9.0",
"@octokit/types": "^8.0.0",
"@types/jest": "^29.2.2",
"@types/node": "^18.11.9",
"@typescript-eslint/eslint-plugin": "^5.42.0",
"@typescript-eslint/parser": "^5.42.0",
"@zeit/ncc": "^0.22.3",
"eslint": "^7.6.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-prettier": "^3.1.4",
"jest": "^26.3.0",
"jest-circus": "^26.3.0",
"nock": "^13.0.4",
"prettier": "^2.0.5",
"ts-jest": "^26.2.0",
"typescript": "^3.9.7"
"eslint": "^8.26.0",
"eslint-config-prettier": "^8.5.0",
"eslint-import-resolver-typescript": "^3.5.2",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-json-format": "^2.0.1",
"eslint-plugin-mdx": "^2.0.5",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^29.2.2",
"jest-circus": "^29.2.2",
"nock": "^13.2.9",
"prettier": "^2.7.1",
"ts-jest": "^29.0.3",
"typescript": "^4.8.4"
},
"engines": {
"node": "^14.15.0 || ^16.13.0 || ^18.0.0"
}
}

View file

@ -1,10 +1,14 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import {HttpClient} from '@actions/http-client'
import {Endpoints, RequestHeaders, IssuesListCommentsResponseData} from '@octokit/types'
import { HttpClient } from '@actions/http-client'
import { Endpoints, RequestHeaders } from '@octokit/types'
type ListCommitPullsResponseData = Endpoints['GET /repos/:owner/:repo/commits/:commit_sha/pulls']['response']['data']
type CreateIssueCommentResponseData = Endpoints['POST /repos/:owner/:repo/issues/:issue_number/comments']['response']['data']
type ListCommitPullsResponseData =
Endpoints['GET /repos/{owner}/{repo}/commits/{commit_sha}/pulls']['response']['data']
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
@ -14,9 +18,9 @@ interface ListCommitPullsParams {
}
const listCommitPulls = async (
params: ListCommitPullsParams
params: ListCommitPullsParams,
): Promise<ListCommitPullsResponseData | null> => {
const {repoToken, owner, repo, commitSha} = params
const { repoToken, owner, repo, commitSha } = params
const http = new HttpClient('http-client-add-pr-comment')
@ -27,14 +31,14 @@ const listCommitPulls = async (
const body = await http.getJson<ListCommitPullsResponseData>(
`https://api.github.com/repos/${owner}/${repo}/commits/${commitSha}/pulls`,
additionalHeaders
additionalHeaders,
)
return body.result
}
const getIssueNumberFromCommitPullsList = (
commitPullsList: ListCommitPullsResponseData
commitPullsList: ListCommitPullsResponseData,
): number | null => (commitPullsList.length ? commitPullsList[0].number : null)
interface CreateCommentProxyParams {
@ -47,18 +51,18 @@ interface CreateCommentProxyParams {
}
const createCommentProxy = async (
params: CreateCommentProxyParams
params: CreateCommentProxyParams,
): Promise<CreateIssueCommentResponseData | null> => {
const {repoToken, owner, repo, issueNumber, body, proxyUrl} = params
const { repoToken, owner, repo, issueNumber, body, proxyUrl } = params
const http = new HttpClient('http-client-add-pr-comment')
const response = await http.postJson<CreateIssueCommentResponseData>(
`${proxyUrl}/repos/${owner}/${repo}/issues/${issueNumber}/comments`,
{body},
{ body },
{
['temporary-github-token']: repoToken,
}
},
)
return response.result
@ -67,18 +71,18 @@ const createCommentProxy = async (
const isMessagePresent = (
message: AddPrCommentInputs['message'],
comments: IssuesListCommentsResponseData,
login?: string
login?: string,
): boolean => {
const cleanRe = new RegExp('\\R|\\s', 'g')
const messageClean = message.replace(cleanRe, '')
return comments.some(({user, body}) => {
return comments.some(({ user, body }) => {
// If a username is provided we can save on a bit of processing
if (login && user.login !== login) {
if (login && user?.login !== login) {
return false
}
return body.replace(cleanRe, '') === messageClean
return body?.replace(cleanRe, '') === messageClean
})
}
@ -102,16 +106,16 @@ const getInputs = (): AddPrCommentInputs => {
const run = async (): Promise<void> => {
try {
const {allowRepeats, message, repoToken, repoTokenUserLogin, proxyUrl} = getInputs()
const { allowRepeats, message, repoToken, repoTokenUserLogin, proxyUrl } = getInputs()
if (!repoToken) {
throw new Error(
'no github token provided, set one with the repo-token input or GITHUB_TOKEN env variable'
'no github token provided, set one with the repo-token input or GITHUB_TOKEN env variable',
)
}
const {
payload: {pull_request: pullRequest, issue, repository},
payload: { pull_request: pullRequest, issue, repository },
sha: commitSha,
} = github.context
@ -121,8 +125,15 @@ const run = async (): Promise<void> => {
return
}
const {full_name: repoFullName} = repository
const [owner, repo] = repoFullName!.split('/')
const { full_name: repoFullName } = repository
if (!repoFullName) {
core.info('repository is missing a full_name property... weird')
core.setOutput('comment-created', 'false')
return
}
const [owner, repo] = repoFullName.split('/')
let issueNumber
@ -132,13 +143,13 @@ 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})
const commitPullsList = await listCommitPulls({ repoToken, owner, repo, commitSha })
issueNumber = commitPullsList && getIssueNumberFromCommitPullsList(commitPullsList)
}
if (!issueNumber) {
core.info(
'this action only works on issues and pull_request events or other commits associated with a pull'
'this action only works on issues and pull_request events or other commits associated with a pull',
)
core.setOutput('comment-created', 'false')
return
@ -151,7 +162,7 @@ const run = async (): Promise<void> => {
if (!allowRepeats) {
core.debug('repeat comments are disallowed, checking for existing')
const {data: comments} = await octokit.issues.listComments({
const { data: comments } = await octokit.rest.issues.listComments({
owner,
repo,
issue_number: issueNumber,
@ -174,7 +185,7 @@ const run = async (): Promise<void> => {
proxyUrl,
})
} else {
await octokit.issues.createComment({
await octokit.rest.issues.createComment({
owner,
repo,
issue_number: issueNumber,
@ -186,8 +197,10 @@ const run = async (): Promise<void> => {
} else {
core.setOutput('comment-created', 'false')
}
} catch (error) {
core.setFailed(error.message)
} catch (err) {
if (err instanceof Error) {
core.setFailed(err.message)
}
}
}