Skip to content

Commit e230e84

Browse files
authored
Merge pull request #25 from nexoscreator/dev
fix
2 parents 9aafa0e + ee01144 commit e230e84

7 files changed

Lines changed: 98 additions & 100 deletions

File tree

index.cjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// index.js
2+
require('./lib/index');

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,7 @@
3232
"engines": {
3333
"node": ">= 18"
3434
},
35+
3536
"type": "module"
36-
}
37+
}
38+

src/Release/version.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,28 @@ import path from "path";
55

66
const version = async (context: any) => {
77
// Bump version in package.json
8-
98
if (context.payload.pull_request.merged) {
10-
const packageJsonPath = path.join(__dirname, "package.json");
9+
const packageJsonPath = path.resolve(process.cwd(), "package.json");
1110
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
1211

1312
const newVersion = semver.inc(packageJson.version, "patch");
1413

1514
packageJson.version = newVersion;
1615
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
1716

17+
const { data: currentCommit } = await context.octokit.repos.getCommit({
18+
owner: context.payload.repository.owner.login,
19+
repo: context.payload.repository.name,
20+
ref: context.payload.pull_request.head.sha,
21+
});
22+
1823
await context.octokit.repos.createOrUpdateFileContents({
1924
owner: context.payload.repository.owner.login,
2025
repo: context.payload.repository.name,
2126
path: "package.json",
2227
message: `chore: bump version to ${newVersion}`,
2328
content: Buffer.from(JSON.stringify(packageJson, null, 2)).toString("base64"),
24-
sha: context.payload.pull_request.head.sha,
29+
sha: currentCommit.sha,
2530
});
2631
}
2732
};

test/fixtures/issues.opened.json

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
{
22
"action": "opened",
33
"issue": {
4-
"number": 1,
5-
"user": {
6-
"login": "hiimbex"
7-
}
4+
"title": "Test issue",
5+
"body": "This is a test issue",
6+
"html_url": "https://github.com/test/repo/issues/1"
87
},
98
"repository": {
10-
"name": "testing-things",
9+
"name": "repo",
1110
"owner": {
12-
"login": "hiimbex"
11+
"login": "test"
1312
}
14-
},
15-
"installation": {
16-
"id": 2
1713
}
1814
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"action": "opened",
3+
"pull_request": {
4+
"title": "Test PR",
5+
"body": "This is a test pull request",
6+
"html_url": "https://github.com/test/repo/pull/1"
7+
},
8+
"repository": {
9+
"name": "repo",
10+
"owner": {
11+
"login": "test"
12+
}
13+
}
14+
}
15+

test/index.test.ts

Lines changed: 56 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,72 @@
1-
// You can import your modules
2-
// import index from '../src/index'
1+
// tests/handlers.test.ts
2+
import { test, expect, beforeEach, afterEach } from 'vitest';
3+
import nock from 'nock';
4+
import { Probot, ProbotOctokit } from 'probot';
5+
import myProbotApp from '../src';
36

4-
import nock from "nock";
5-
// Requiring our app implementation
6-
import myProbotApp from "../src/index.js";
7-
import { Probot, ProbotOctokit } from "probot";
8-
// Requiring our fixtures
9-
//import payload from "./fixtures/issues.opened.json" with { "type": "json"};
10-
import fs from "fs";
11-
import path from "path";
12-
import { fileURLToPath } from "url";
13-
import { describe, beforeEach, afterEach, test, expect } from "vitest";
7+
const issuePayload = require('./fixtures/issues.opened.json');
8+
const pullRequestPayload = require('./fixtures/pull_request.opened.json');
149

15-
const issueCreatedBody = { body: "Thanks for opening this issue!" };
10+
let probot: Probot;
1611

17-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
12+
beforeEach(() => {
13+
probot = new Probot({
14+
appId: 123,
15+
privateKey: 'test',
16+
octokit: ProbotOctokit.defaults({
17+
throttle: { enabled: false },
18+
retry: { enabled: false },
19+
}),
20+
});
21+
probot.load(myProbotApp);
22+
});
1823

19-
const privateKey = fs.readFileSync(
20-
path.join(__dirname, "fixtures/mock-cert.pem"),
21-
"utf-8",
22-
);
24+
afterEach(() => {
25+
nock.cleanAll();
26+
});
2327

24-
const payload = JSON.parse(
25-
fs.readFileSync(path.join(__dirname, "fixtures/issues.opened.json"), "utf-8"),
26-
);
28+
test('sends a notification when an issue is opened', async () => {
29+
nock('https://hooks.slack.com')
30+
.post('/services/your/slack/webhook')
31+
.reply(200);
2732

28-
describe("My Probot app", () => {
29-
let probot: any;
33+
nock('https://discord.com')
34+
.post('/api/webhooks/your/discord/webhook')
35+
.reply(200);
3036

31-
beforeEach(() => {
32-
nock.disableNetConnect();
33-
probot = new Probot({
34-
appId: 937788,
35-
privateKey,
36-
// disable request throttling and retries for testing
37-
Octokit: ProbotOctokit.defaults({
38-
retry: { enabled: false },
39-
throttle: { enabled: false },
40-
}),
41-
});
42-
// Load our app into probot
43-
probot.load(myProbotApp);
37+
const transporter = {
38+
sendMail: vi.fn().mockResolvedValueOnce({}),
39+
};
40+
vi.mock('nodemailer', () => {
41+
return {
42+
createTransport: () => transporter,
43+
};
4444
});
4545

46-
test("creates a comment when an issue is opened", async () => {
47-
const mock = nock("https://api.github.com")
48-
// Test that we correctly return a test token
49-
.post("/app/installations/2/access_tokens")
50-
.reply(200, {
51-
token: "test",
52-
permissions: {
53-
issues: "write",
54-
},
55-
})
46+
await probot.receive({ name: 'issues', payload: issuePayload });
5647

57-
// Test that a comment is posted
58-
.post("/repos/hiimbex/testing-things/issues/1/comments", (body: any) => {
59-
expect(body).toMatchObject(issueCreatedBody);
60-
return true;
61-
})
62-
.reply(200);
48+
expect(transporter.sendMail).toHaveBeenCalled();
49+
});
6350

64-
// Receive a webhook event
65-
await probot.receive({ name: "issues", payload });
51+
test('sends a notification when a pull request is opened', async () => {
52+
nock('https://hooks.slack.com')
53+
.post('/services/your/slack/webhook')
54+
.reply(200);
6655

67-
expect(mock.pendingMocks()).toStrictEqual([]);
68-
});
56+
nock('https://discord.com')
57+
.post('/api/webhooks/your/discord/webhook')
58+
.reply(200);
6959

70-
afterEach(() => {
71-
nock.cleanAll();
72-
nock.enableNetConnect();
60+
const transporter = {
61+
sendMail: vi.fn().mockResolvedValueOnce({}),
62+
};
63+
vi.mock('nodemailer', () => {
64+
return {
65+
createTransport: () => transporter,
66+
};
7367
});
74-
});
7568

76-
// For more information about testing with Jest see:
77-
// https://facebook.github.io/jest/
69+
await probot.receive({ name: 'pull_request', payload: pullRequestPayload });
7870

79-
// For more information about using TypeScript in your tests, Jest recommends:
80-
// https://github.com/kulshekhar/ts-jest
81-
82-
// For more information about testing with Nock see:
83-
// https://github.com/nock/nock
71+
expect(transporter.sendMail).toHaveBeenCalled();
72+
});

tsconfig.json

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
11
{
22
"compilerOptions": {
3-
"incremental": true,
4-
"target": "es2022",
5-
"module": "Node16",
6-
"declaration": true,
7-
"sourceMap": true,
3+
"module": "commonjs",
4+
"target": "es2018",
85
"outDir": "./lib",
9-
10-
/* Strict Type-Checking Options */
11-
"strict": true,
12-
13-
/* Additional Checks */
14-
"noUnusedLocals": true,
15-
"noUnusedParameters": true,
16-
"noImplicitReturns": true,
17-
"noFallthroughCasesInSwitch": true,
18-
19-
"moduleResolution": "Node16",
6+
"rootDir": "src",
207
"esModuleInterop": true,
21-
"forceConsistentCasingInFileNames": true
8+
"resolveJsonModule": true,
9+
"skipLibCheck": true,
10+
"strict": true
2211
},
23-
"include": ["src/"],
24-
"compileOnSave": false
25-
}
12+
"include": ["src/**/*.ts"],
13+
"exclude": ["node_modules"]
14+
}

0 commit comments

Comments
 (0)