Skip to content

Commit

Permalink
Merge pull request #14 from gentlementlegen/feat/testing
Browse files Browse the repository at this point in the history
feat: tests for modules
  • Loading branch information
gentlementlegen authored May 5, 2024
2 parents 3525f4f + 989e56f commit a43b17d
Show file tree
Hide file tree
Showing 27 changed files with 2,592 additions and 139 deletions.
4 changes: 2 additions & 2 deletions .cspell.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"$schema": "https://raw.githubusercontent.com/streetsidesoftware/cspell/main/cspell.schema.json",
"version": "0.2",
"ignorePaths": ["**/*.json", "**/*.css", "node_modules", "**/*.log"],
"ignorePaths": ["**/*.json", "**/*.css", "node_modules", "**/*.log", "**/tests/__mocks__"],
"useGitignore": true,
"language": "en",
"words": ["dataurl", "devpool", "outdir", "servedir", "ubiquibot", "tiktoken", "typebox", "supabase", "wxdai", "noopener"],
"words": ["dataurl", "devpool", "outdir", "servedir", "ubiquibot", "tiktoken", "typebox", "supabase", "wxdai", "noopener", "knip", "hellip"],
"dictionaries": ["typescript", "node", "software-terms"],
"import": ["@cspell/dict-typescript/cspell-ext.json", "@cspell/dict-node/cspell-ext.json", "@cspell/dict-software-terms"],
"ignoreRegExpList": ["[0-9a-fA-F]{6}"]
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/jest-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ jobs:
SUPABASE_URL: ${{ secrets.SUPABASE_URL }}
SUPABASE_KEY: ${{ secrets.SUPABASE_KEY }}
X25519_PRIVATE_KEY: ${{ secrets.X25519_PRIVATE_KEY }}
NFT_MINTER_PRIVATE_KEY: ${{ secrets.NFT_MINTER_PRIVATE_KEY }}
NFT_CONTRACT_ADDRESS: ${{ secrets.NFT_CONTRACT_ADDRESS }}
EVM_PRIVATE_ENCRYPTED: ${{ secrets.EVM_PRIVATE_ENCRYPTED }}

steps:
- name: Checkout code
Expand Down
2 changes: 1 addition & 1 deletion knip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const config: KnipConfig = {
project: ["src/**/*.ts"],
ignore: ["src/data-collection/examples/*.ts", "src/configuration/common-config-type.ts"],
ignoreExportsUsedInFile: true,
ignoreDependencies: ["ts-node", "msw"],
ignoreDependencies: ["ts-node", "msw", "@mswjs/data"],
jest: {
config: ["jest.config.ts"],
entry: ["src/**/*.test.ts"],
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@octokit/rest": "20.1.0",
"@sinclair/typebox": "0.32.20",
"@supabase/supabase-js": "2.42.0",
"@ubiquibot/permit-generation": "1.2.1",
"@ubiquibot/permit-generation": "1.2.2",
"commander": "12.0.0",
"decimal.js": "10.4.3",
"dotenv": "16.4.5",
Expand All @@ -50,6 +50,7 @@
"@cspell/dict-node": "4.0.3",
"@cspell/dict-software-terms": "3.3.18",
"@cspell/dict-typescript": "3.1.2",
"@mswjs/data": "0.16.1",
"@types/jest": "29.5.12",
"@types/jsdom": "21.1.6",
"@types/markdown-it": "13.0.7",
Expand Down
22 changes: 11 additions & 11 deletions rewards-configuration.default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,39 +27,39 @@ formattingEvaluator:
td: 1
hr: 0
multipliers:
- type: [ISSUE, ISSUER, TASK]:
- type: [ISSUE, ISSUER, SPECIFICATION]
formattingMultiplier: 1
wordValue: 0.1
- type: [ISSUE, ISSUER, COMMENTED]:
- type: [ISSUE, ISSUER, COMMENTED]
formattingMultiplier: 1
wordValue: 0.2
- type: [ISSUE, ASSIGNEE, COMMENTED]:
- type: [ISSUE, ASSIGNEE, COMMENTED]
formattingMultiplier: 0
wordValue: 0
- type: [ISSUE, COLLABORATOR, COMMENTED]:
- type: [ISSUE, COLLABORATOR, COMMENTED]
formattingMultiplier: 1
wordValue: 0.1
- type: [ISSUE, CONTRIBUTOR, COMMENTED]:
- type: [ISSUE, CONTRIBUTOR, COMMENTED]
formattingMultiplier: 0.25
wordValue: 0.1
- type: [REVIEW, ISSUER, SPECIFICATION]:
- type: [REVIEW, ISSUER, TASK]
formattingMultiplier: 0
wordValue: 0
- type: [REVIEW, ISSUER, COMMENTED]:
- type: [REVIEW, ISSUER, COMMENTED]
formattingMultiplier: 2
wordValue: 0.2
- type: [REVIEW, ASSIGNEE, COMMENTED]:
- type: [REVIEW, ASSIGNEE, COMMENTED]
formattingMultiplier: 1
wordValue: 0.1
- type: [REVIEW, COLLABORATOR, COMMENTED]:
- type: [REVIEW, COLLABORATOR, COMMENTED]
formattingMultiplier: 1
wordValue: 0.1
- type: [REVIEW, CONTRIBUTOR, COMMENTED]:
- type: [REVIEW, CONTRIBUTOR, COMMENTED]
formattingMultiplier: 0.25
wordValue: 0.1
permitGeneration:
enabled: true
githubComment:
enabled: true
post: false
post: true
debug: true
35 changes: 33 additions & 2 deletions src/data-collection/collect-linked-pulls.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
import { GitHubLinkEvent, isGitHubLinkEvent } from "../github-types";
import { IssueParams, getAllTimelineEvents } from "../start";
import { IssueParams, getAllTimelineEvents, parseGitHubUrl } from "../start";

export async function collectLinkedMergedPulls(issue: IssueParams) {
// normally we should only use this one to calculate incentives, because this specifies that the pull requests are merged (accepted)
// and that are also related to the current issue, no just mentioned by
const onlyPullRequests = await collectLinkedPulls(issue);
return onlyPullRequests.filter((event) => isGitHubLinkEvent(event) && event.source.issue.pull_request?.merged_at);
return onlyPullRequests.filter((event) => {
if (!event.source.issue.body) {
return false;
}
// Matches all keywords according to the docs:
// https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword
// Works on multiple linked issues, and matches #<number> or URL patterns
const linkedIssueRegex =
/\b(?:Close(?:s|d)?|Fix(?:es|ed)?|Resolve(?:s|d)?):?\s+(?:#(\d+)|https?:\/\/(?:www\.)?github\.com\/(?:[^/\s]+\/[^/\s]+\/(?:issues|pull)\/(\d+)))\b/gi;
const linkedPrUrls = event.source.issue.body.match(linkedIssueRegex);
if (!linkedPrUrls) {
return false;
}
let isClosingPr = false;
for (const linkedPrUrl of linkedPrUrls) {
const idx = linkedPrUrl.indexOf("#");
if (idx !== -1) {
isClosingPr = Number(linkedPrUrl.slice(idx + 1)) === issue.issue_number;
} else {
const url = linkedPrUrl.match(/https.+/)?.[0];
if (url) {
const linkedRepo = parseGitHubUrl(url);
isClosingPr =
linkedRepo.issue_number === issue.issue_number &&
linkedRepo.repo === issue.repo &&
linkedRepo.owner === issue.owner;
}
}
}
return isGitHubLinkEvent(event) && event.source.issue.pull_request?.merged_at && isClosingPr;
});
}
export async function collectLinkedPulls(issue: IssueParams) {
// this one was created to help with tests, but probably should not be used in the main code
Expand Down
64 changes: 42 additions & 22 deletions src/issue-activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,19 @@ export class IssueActivity {
}

_getTypeFromComment(
comment: GitHubIssueComment | GitHubPullRequestReviewComment | GitHubIssue | GitHubPullRequest,
issueType: CommentType.ISSUE | CommentType.REVIEW,
comment:
| GitHubIssueComment
| GitHubPullRequestReviewComment
| GitHubPullRequestReviewState
| GitHubIssue
| GitHubPullRequest,
self: GitHubPullRequest | GitHubIssue | null
) {
let ret = 0;
ret |= "pull_request_review_id" in comment || "draft" in comment ? CommentType.REVIEW : CommentType.ISSUE;
ret |= issueType;
if (comment.id === self?.id) {
ret |= ret & CommentType.ISSUE ? CommentType.TASK : CommentType.SPECIFICATION;
ret |= ret & CommentType.ISSUE ? CommentType.SPECIFICATION : CommentType.TASK;
} else {
ret |= CommentType.COMMENTED;
}
Expand All @@ -121,36 +127,43 @@ export class IssueActivity {
return ret;
}

_getLinkedReviewComments() {
const comments = [];
for (const linkedReview of this.linkedReviews) {
for (const value of Object.values(linkedReview)) {
if (Array.isArray(value)) {
for (const review of value) {
comments.push({
...review,
type: this._getTypeFromComment(CommentType.REVIEW, review, linkedReview.self),
});
}
} else if (value) {
comments.push({
...value,
type: this._getTypeFromComment(CommentType.REVIEW, value, value),
});
}
}
}
return comments;
}

get allComments() {
const comments: Array<
(GitHubIssueComment | GitHubPullRequestReviewComment | GitHubIssue | GitHubPullRequest) & { type: CommentType }
> = this.comments.map((comment) => ({
...comment,
type: this._getTypeFromComment(comment, this.self),
type: this._getTypeFromComment(CommentType.ISSUE, comment, this.self),
}));
if (this.self) {
comments.push({
...this.self,
type: this._getTypeFromComment(this.self, this.self),
type: this._getTypeFromComment(CommentType.ISSUE, this.self, this.self),
});
}
if (this.linkedReviews) {
for (const linkedReview of this.linkedReviews) {
if (linkedReview.self) {
comments.push({
...linkedReview.self,
type: this._getTypeFromComment(linkedReview.self, linkedReview.self),
});
}
if (linkedReview.reviewComments) {
for (const reviewComment of linkedReview.reviewComments) {
comments.push({
...reviewComment,
type: this._getTypeFromComment(reviewComment, linkedReview.self),
});
}
}
}
comments.push(...this._getLinkedReviewComments());
}
return comments;
}
Expand All @@ -160,14 +173,21 @@ export class Review {
self: GitHubPullRequest | null = null;
reviews: GitHubPullRequestReviewState[] | null = null; // this includes every comment on the files view.
reviewComments: GitHubPullRequestReviewComment[] | null = null;
comments: GitHubIssueComment[] | null = null;

constructor(private _pullParams: PullParams) {}

async init() {
[this.self, this.reviews, this.reviewComments] = await Promise.all([
[this.self, this.reviews, this.reviewComments, this.comments] = await Promise.all([
getPullRequest(this._pullParams),
getPullRequestReviews(this._pullParams),
getPullRequestReviewComments(this._pullParams),
// This fetches all the comments inside the Pull Request that were not created in a reviewing context
getIssueComments({
owner: this._pullParams.owner,
repo: this._pullParams.repo,
issue_number: this._pullParams.pull_number,
}),
]);
}
}
2 changes: 1 addition & 1 deletion src/parser/command-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if (process.env.NODE_ENV === "test") {
process.argv.push("-n");
process.argv.push("100");
process.argv.push("-e");
process.argv.push("privateKey");
process.argv.push(`${process.env.EVM_PRIVATE_ENCRYPTED}`);
}

const program = new Command()
Expand Down
6 changes: 4 additions & 2 deletions src/parser/data-purge-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ export class DataPurgeModule implements Module {
for (const comment of data.allComments) {
if (comment.body && comment.user?.login && result[comment.user.login]) {
const newContent = comment.body
// Remove quoted text
.replace(/^>.*$/gm, "")
// Remove commands such as /start
.replace(/^\/.+/g, "")
// makes the content single lined
.replace(/[\r\n]+/g, " ")
.replace(/\[.*?\]\(.*?\)/g, "")
.replace(/^\/\S+/g, "")
.trim();
if (newContent.length) {
result[comment.user.login].comments = [
Expand Down
Loading

0 comments on commit a43b17d

Please sign in to comment.