- fix a ton of formatting issues automatically

- fix many javascript issues
This commit is contained in:
Mike Penz
2020-10-16 16:29:13 +02:00
parent 8d59911332
commit db70432deb
10 changed files with 636 additions and 557 deletions
+123 -106
View File
@@ -1,126 +1,143 @@
import { Octokit, RestEndpointMethodTypes } from "@octokit/rest"
import moment from 'moment';
import {Octokit, RestEndpointMethodTypes} from '@octokit/rest'
import moment from 'moment'
import { CommitInfo } from "./commits"
import * as core from '@actions/core';
import {CommitInfo} from './commits'
import * as core from '@actions/core'
export interface PullRequestInfo {
number: number
title: string
htmlURL: string
mergedAt: moment.Moment
author: string
repoName: string
labels: Array<string>
body: string
number: number
title: string
htmlURL: string
mergedAt: moment.Moment
author: string
repoName: string
labels: string[]
body: string
}
export class PullRequests {
constructor(private octokit: Octokit) {}
constructor(private octokit: Octokit) {}
async getSingle(owner: string, repo: string, prNumber: number): Promise<PullRequestInfo | null> {
try {
const pr = await this.octokit.pulls.get({ owner, repo, pull_number: prNumber })
async getSingle(
owner: string,
repo: string,
prNumber: number
): Promise<PullRequestInfo | null> {
try {
const pr = await this.octokit.pulls.get({
owner,
repo,
pull_number: prNumber
})
return {
number: pr.data.number,
title: pr.data.title,
htmlURL: pr.data.html_url,
mergedAt: moment(pr.data.merged_at),
author: pr.data.user.login,
repoName: pr.data.base.repo.full_name,
labels: pr.data.labels.map(function (label) { return label.name }),
body: pr.data.body
}
} catch (e) {
core.warning(`Cannot find PR ${owner}/${repo}#${prNumber} - ${e.message}`)
return null
}
return {
number: pr.data.number,
title: pr.data.title,
htmlURL: pr.data.html_url,
mergedAt: moment(pr.data.merged_at),
author: pr.data.user.login,
repoName: pr.data.base.repo.full_name,
labels: pr.data.labels.map(function (label) {
return label.name
}),
body: pr.data.body
}
} catch (e) {
core.warning(`Cannot find PR ${owner}/${repo}#${prNumber} - ${e.message}`)
return null
}
}
async getBetweenDates(
owner: string,
repo: string,
fromDate: moment.Moment,
toDate: moment.Moment
): Promise<PullRequestInfo[]> {
const mergedPRs: PullRequestInfo[] = []
const options = this.octokit.pulls.list.endpoint.merge({
owner,
repo,
state: "closed",
sort: "updated",
direction: "desc"
})
async getBetweenDates(
owner: string,
repo: string,
fromDate: moment.Moment,
toDate: moment.Moment
): Promise<PullRequestInfo[]> {
const mergedPRs: PullRequestInfo[] = []
const options = this.octokit.pulls.list.endpoint.merge({
owner,
repo,
state: 'closed',
sort: 'updated',
direction: 'desc'
})
for await (const response of this.octokit.paginate.iterator(options)) {
type PullsListData = RestEndpointMethodTypes["pulls"]["list"]["response"]["data"]
const prs: PullsListData = response.data as PullsListData
const firstPR = prs[0]
if(firstPR.merged_at && fromDate.isAfter(moment(firstPR.merged_at))) {
// bail out early to not keep iterating on PRs super old
return sortPullRequests(mergedPRs, true)
}
prs.filter(
pr =>
!!pr.merged_at &&
fromDate.isBefore(moment(pr.merged_at)) &&
toDate.isSameOrAfter(moment(pr.merged_at))
).forEach(pr => {
mergedPRs.push({
number: pr.number,
title: pr.title,
htmlURL: pr.html_url,
mergedAt: moment(pr.merged_at),
author: pr.user.login,
repoName: pr.base.repo.full_name,
labels: pr.labels.map(function (label) { return label.name }),
body: pr.body
})
})
}
for await (const response of this.octokit.paginate.iterator(options)) {
type PullsListData = RestEndpointMethodTypes['pulls']['list']['response']['data']
const prs: PullsListData = response.data as PullsListData
const firstPR = prs[0]
if (firstPR.merged_at && fromDate.isAfter(moment(firstPR.merged_at))) {
// bail out early to not keep iterating on PRs super old
return sortPullRequests(mergedPRs, true)
}
prs
.filter(
pr =>
!!pr.merged_at &&
fromDate.isBefore(moment(pr.merged_at)) &&
toDate.isSameOrAfter(moment(pr.merged_at))
)
.forEach(pr => {
mergedPRs.push({
number: pr.number,
title: pr.title,
htmlURL: pr.html_url,
mergedAt: moment(pr.merged_at),
author: pr.user.login,
repoName: pr.base.repo.full_name,
labels: pr.labels.map(function (label) {
return label.name
}),
body: pr.body
})
})
}
filterCommits(commits: CommitInfo[]): CommitInfo[] {
const prRegex = /Merge pull request #(\d+)/
const filteredCommits = []
return sortPullRequests(mergedPRs, true)
}
for (const commit of commits) {
const match = commit.summary.match(prRegex)
if (!match) {
continue
}
commit.prNumber = Number.parseInt(match[1], 10)
filteredCommits.push(commit)
}
filterCommits(commits: CommitInfo[]): CommitInfo[] {
const prRegex = /Merge pull request #(\d+)/
const filteredCommits = []
return filteredCommits
for (const commit of commits) {
const match = commit.summary.match(prRegex)
if (!match) {
continue
}
commit.prNumber = Number.parseInt(match[1], 10)
filteredCommits.push(commit)
}
return filteredCommits
}
}
export function sortPullRequests(pullRequests: PullRequestInfo[], ascending: Boolean): PullRequestInfo[] {
if(ascending) {
pullRequests.sort((a, b) => {
if (a.mergedAt.isBefore(b.mergedAt)) {
return -1
} else if (b.mergedAt.isBefore(a.mergedAt)) {
return 1
}
return 0
})
} else {
pullRequests.sort((b, a) => {
if (a.mergedAt.isBefore(b.mergedAt)) {
return -1
} else if (b.mergedAt.isBefore(a.mergedAt)) {
return 1
}
return 0
})
}
return pullRequests
}
export function sortPullRequests(
pullRequests: PullRequestInfo[],
ascending: Boolean
): PullRequestInfo[] {
if (ascending) {
pullRequests.sort((a, b) => {
if (a.mergedAt.isBefore(b.mergedAt)) {
return -1
} else if (b.mergedAt.isBefore(a.mergedAt)) {
return 1
}
return 0
})
} else {
pullRequests.sort((b, a) => {
if (a.mergedAt.isBefore(b.mergedAt)) {
return -1
} else if (b.mergedAt.isBefore(a.mergedAt)) {
return 1
}
return 0
})
}
return pullRequests
}