@dev-angsu/cli 1.0.3 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/utils/engine.js +52 -24
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dev-angsu/cli",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -30,25 +30,42 @@ async function getPRDiff() {
30
30
  const octokit = github.getOctokit(token);
31
31
 
32
32
  const context = github.context;
33
- const prNumber = context.payload.pull_request?.number;
34
33
 
35
- if (!prNumber) {
36
- throw new Error(
37
- "❌ No Pull Request found in context. Are you running this on push?"
38
- );
39
- }
34
+ if (context.payload.pull_request) {
35
+ const prNumber = context.payload.pull_request.number;
36
+ // Fetch the diff specifically for PR
37
+ const { data: diff } = await octokit.rest.pulls.get({
38
+ owner: context.repo.owner,
39
+ repo: context.repo.repo,
40
+ pull_number: prNumber,
41
+ mediaType: {
42
+ format: "diff", // Ask GitHub to return the raw diff text
43
+ },
44
+ });
45
+ return diff;
46
+ } else if (context.eventName === "push") {
47
+ const { before, after, repository } = context.payload;
48
+
49
+ let base = before;
50
+ // Handle new branch creation (before commit is zeroed out)
51
+ if (before === "0000000000000000000000000000000000000000") {
52
+ base = repository.default_branch;
53
+ }
40
54
 
41
- // Fetch the diff specifically
42
- const { data: diff } = await octokit.rest.pulls.get({
43
- owner: context.repo.owner,
44
- repo: context.repo.repo,
45
- pull_number: prNumber,
46
- mediaType: {
47
- format: "diff", // Ask GitHub to return the raw diff text
48
- },
49
- });
55
+ // Compare head commit against previous commit
56
+ const { data: diff } = await octokit.rest.repos.compareCommits({
57
+ owner: context.repo.owner,
58
+ repo: context.repo.repo,
59
+ base,
60
+ head: after,
61
+ mediaType: { format: "diff" },
62
+ });
63
+ return diff;
64
+ }
50
65
 
51
- return diff;
66
+ throw new Error(
67
+ "❌ Unsupported event type. Only PRs and Pushes are supported."
68
+ );
52
69
  }
53
70
 
54
71
  async function generateReview(diff) {
@@ -120,13 +137,23 @@ export async function run() {
120
137
  const octokit = github.getOctokit(token);
121
138
  const context = github.context;
122
139
 
123
- await octokit.rest.issues.createComment({
124
- owner: context.repo.owner,
125
- repo: context.repo.repo,
126
- issue_number: context.payload.pull_request.number,
127
- body: review,
128
- });
129
- console.log("✅ Review posted to GitHub PR!");
140
+ if (context.payload.pull_request) {
141
+ await octokit.rest.issues.createComment({
142
+ owner: context.repo.owner,
143
+ repo: context.repo.repo,
144
+ issue_number: context.payload.pull_request.number,
145
+ body: review,
146
+ });
147
+ console.log("✅ Review posted to GitHub PR!");
148
+ } else if (context.eventName === "push") {
149
+ await octokit.rest.repos.createCommitComment({
150
+ owner: context.repo.owner,
151
+ repo: context.repo.repo,
152
+ commit_sha: context.payload.after,
153
+ body: review,
154
+ });
155
+ console.log("✅ Review posted to Commit!");
156
+ }
130
157
  } else {
131
158
  // Locally, we just print it
132
159
  console.log("\n================ 🤖 AI CODE REVIEW ================ \n");
@@ -143,7 +170,8 @@ const currentFile = fileURLToPath(import.meta.url);
143
170
  // Check if run directly (robust against symlinks/path variations in CI)
144
171
  if (
145
172
  process.argv[1] === currentFile ||
146
- process.argv[1].endsWith("src/utils/engine.js")
173
+ process.argv[1].endsWith("src/utils/engine.js") ||
174
+ process.argv[1].endsWith("review-code")
147
175
  ) {
148
176
  run();
149
177
  }