Skip to content

Commit ba71166

Browse files
committed
Add detailed debug logging for pull request rebasing process
1 parent dca7e91 commit ba71166

File tree

4 files changed

+251
-8
lines changed

4 files changed

+251
-8
lines changed

src/Rebaser/githubRebase.ts

Lines changed: 97 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {rebasePullRequest} from 'github-rebase/lib';
22
import {Octokit} from '@octokit/rest';
3+
import {debug} from '@actions/core';
34

45
export interface GithubRebase {
56
rebasePullRequest(owner: string, pullRequestNumber: number, repo: string): Promise<string>;
@@ -13,11 +14,101 @@ export class RealGithubRebase implements GithubRebase {
1314
}
1415

1516
public async rebasePullRequest(owner: string, pullRequestNumber: number, repo: string): Promise<string> {
16-
return rebasePullRequest({
17-
octokit: this.octokit,
18-
owner: owner,
19-
pullRequestNumber: pullRequestNumber,
20-
repo: repo,
21-
});
17+
debug(`[GithubRebase] Starting rebase for PR #${pullRequestNumber} in ${owner}/${repo}`);
18+
19+
try {
20+
// Log information about the PR before rebasing
21+
debug(`[GithubRebase] Fetching PR details before rebase`);
22+
try {
23+
const prDetails = await this.octokit.pulls.get({
24+
owner,
25+
repo,
26+
pull_number: pullRequestNumber,
27+
});
28+
29+
debug(`[GithubRebase] PR #${pullRequestNumber} details:`);
30+
debug(` Base branch: ${prDetails.data.base.ref}`);
31+
debug(` Head branch: ${prDetails.data.head.ref}`);
32+
debug(` Head SHA: ${prDetails.data.head.sha}`);
33+
debug(` Mergeable state: ${prDetails.data.mergeable_state}`);
34+
debug(` Rebaseable: ${String(prDetails.data.rebaseable)}`);
35+
} catch (prError) {
36+
debug(`[GithubRebase] Error fetching PR details: ${String(prError)}`);
37+
}
38+
39+
// Perform the actual rebase
40+
debug(`[GithubRebase] Calling github-rebase library`);
41+
const result = await rebasePullRequest({
42+
octokit: this.octokit,
43+
owner: owner,
44+
pullRequestNumber: pullRequestNumber,
45+
repo: repo,
46+
});
47+
48+
debug(`[GithubRebase] Rebase completed successfully with result: ${result}`);
49+
return result;
50+
} catch (error) {
51+
debug(`[GithubRebase] Error during rebase: ${String(error)}`);
52+
53+
// Try to extract more information about the error
54+
if (String(error).includes('Reference does not exist')) {
55+
debug(`[GithubRebase] This appears to be a missing reference error`);
56+
57+
// Try to get information about the branches
58+
try {
59+
debug(`[GithubRebase] Attempting to get more information about the branches`);
60+
const pr = await this.octokit.pulls.get({
61+
owner,
62+
repo,
63+
pull_number: pullRequestNumber,
64+
});
65+
66+
const baseRef = pr.data.base.ref;
67+
const headRef = pr.data.head.ref;
68+
69+
debug(`[GithubRebase] Base branch: ${baseRef}, Head branch: ${headRef}`);
70+
71+
// Check if base branch exists
72+
try {
73+
await this.octokit.git.getRef({
74+
owner,
75+
repo,
76+
ref: `heads/${baseRef}`,
77+
});
78+
debug(`[GithubRebase] Base branch '${baseRef}' exists`);
79+
} catch (baseError) {
80+
debug(
81+
`[GithubRebase] Base branch '${baseRef}' does not exist or is not accessible: ${String(
82+
baseError,
83+
)}`,
84+
);
85+
}
86+
87+
// Check if head branch exists
88+
try {
89+
const headOwner = pr.data.head.repo ? pr.data.head.repo.owner.login : owner;
90+
const headRepo = pr.data.head.repo ? pr.data.head.repo.name : repo;
91+
92+
await this.octokit.git.getRef({
93+
owner: headOwner,
94+
repo: headRepo,
95+
ref: `heads/${headRef}`,
96+
});
97+
debug(`[GithubRebase] Head branch '${headRef}' exists in ${headOwner}/${headRepo}`);
98+
} catch (headError) {
99+
debug(
100+
`[GithubRebase] Head branch '${headRef}' does not exist or is not accessible: ${String(
101+
headError,
102+
)}`,
103+
);
104+
}
105+
} catch (prError) {
106+
debug(`[GithubRebase] Could not get PR details to check branches: ${String(prError)}`);
107+
}
108+
}
109+
110+
// Re-throw the error to be handled by the caller
111+
throw error;
112+
}
22113
}
23114
}

src/Rebaser/rebaser.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {PullRequestInfo} from '../pullrequestinfo';
2-
import {info, warning} from '@actions/core';
2+
import {info, warning, debug} from '@actions/core';
33
import {GithubRebase} from './githubRebase';
44

55
/**
@@ -21,11 +21,30 @@ export class Rebaser {
2121

2222
private async rebase(pullRequest: PullRequestInfo) {
2323
info(`Rebasing pull request ${JSON.stringify(pullRequest)}`);
24+
debug(
25+
`Starting rebase process for PR #${pullRequest.number} in ${pullRequest.ownerName}/${pullRequest.repoName}`,
26+
);
27+
debug(
28+
`PR details: mergeableState=${pullRequest.mergeableState}, rebaseable=${String(
29+
pullRequest.rebaseable,
30+
)}, draft=${String(pullRequest.draft)}`,
31+
);
32+
debug(`PR labels: ${JSON.stringify(pullRequest.labels)}`);
33+
2434
try {
25-
await this.githubRebase.rebasePullRequest(pullRequest.ownerName, pullRequest.number, pullRequest.repoName);
35+
debug(`Calling github-rebase library for PR #${pullRequest.number}`);
36+
const result = await this.githubRebase.rebasePullRequest(
37+
pullRequest.ownerName,
38+
pullRequest.number,
39+
pullRequest.repoName,
40+
);
41+
debug(`Rebase result: ${result}`);
2642

2743
info(`${JSON.stringify(pullRequest)} was successfully rebased.`);
2844
} catch (e) {
45+
debug(`Error during rebase of PR #${pullRequest.number}: ${String(e)}`);
46+
// Log the full error object for debugging
47+
debug(`Full error details: ${JSON.stringify(e, Object.getOwnPropertyNames(e))}`);
2948
if (String(e).includes('Rebase aborted because the head branch changed')) {
3049
warning(`Rebase aborted because the head branch changed for ${JSON.stringify(pullRequest)}`);
3150
return;

src/Util/logging.ts

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import {debug, info, warning, error} from '@actions/core';
2+
3+
/**
4+
* Utility functions for standardized logging across the application.
5+
* These functions help provide consistent, detailed diagnostic information.
6+
*/
7+
8+
/**
9+
* Logs detailed information about an error.
10+
*
11+
* @param message A descriptive message about where/when the error occurred
12+
* @param err The error object
13+
* @param context Optional additional context information
14+
*/
15+
export function logError(message: string, err: unknown, context?: Record<string, unknown>): void {
16+
error(`ERROR: ${message}`);
17+
18+
// Log the error message
19+
error(`Error message: ${String(err)}`);
20+
21+
// Log the stack trace if available
22+
if (err instanceof Error && err.stack) {
23+
error(`Stack trace: ${err.stack}`);
24+
}
25+
26+
// Log all properties of the error object
27+
try {
28+
error(`Error details: ${JSON.stringify(err, Object.getOwnPropertyNames(err))}`);
29+
} catch (jsonError) {
30+
error(`Could not stringify error: ${String(jsonError)}`);
31+
}
32+
33+
// Log additional context if provided
34+
if (context) {
35+
try {
36+
error(`Error context: ${JSON.stringify(context)}`);
37+
} catch (jsonError) {
38+
error(`Could not stringify context: ${String(jsonError)}`);
39+
}
40+
}
41+
}
42+
43+
/**
44+
* Logs detailed debug information.
45+
*
46+
* @param component The component/module name for context
47+
* @param message The debug message
48+
* @param data Optional data to include in the log
49+
*/
50+
export function logDebug(component: string, message: string, data?: unknown): void {
51+
const prefix = component ? `[${component}] ` : '';
52+
53+
debug(`${prefix}${message}`);
54+
55+
if (data !== undefined) {
56+
try {
57+
if (typeof data === 'string') {
58+
debug(`${prefix}Data: ${data}`);
59+
} else {
60+
debug(`${prefix}Data: ${JSON.stringify(data)}`);
61+
}
62+
} catch (jsonError) {
63+
debug(`${prefix}Could not stringify data: ${String(jsonError)}`);
64+
}
65+
}
66+
}
67+
68+
/**
69+
* Logs information about a pull request.
70+
*
71+
* @param component The component/module name for context
72+
* @param message A descriptive message
73+
* @param pullRequest The pull request information
74+
*/
75+
export function logPullRequestInfo(
76+
component: string,
77+
message: string,
78+
pullRequest: {
79+
number: number;
80+
ownerName: string;
81+
repoName: string;
82+
[key: string]: unknown;
83+
},
84+
): void {
85+
const prefix = component ? `[${component}] ` : '';
86+
const prId = `PR #${pullRequest.number} in ${pullRequest.ownerName}/${pullRequest.repoName}`;
87+
88+
info(`${prefix}${message} - ${prId}`);
89+
90+
// Log detailed PR information at debug level
91+
try {
92+
debug(`${prefix}Pull request details: ${JSON.stringify(pullRequest)}`);
93+
} catch (jsonError) {
94+
debug(`${prefix}Could not stringify pull request: ${String(jsonError)}`);
95+
}
96+
}
97+
98+
/**
99+
* Logs a warning with context information.
100+
*
101+
* @param component The component/module name for context
102+
* @param message The warning message
103+
* @param data Optional data to include in the log
104+
*/
105+
export function logWarning(component: string, message: string, data?: unknown): void {
106+
const prefix = component ? `[${component}] ` : '';
107+
108+
warning(`${prefix}${message}`);
109+
110+
if (data !== undefined) {
111+
try {
112+
if (typeof data === 'string') {
113+
debug(`${prefix}Warning data: ${data}`);
114+
} else {
115+
debug(`${prefix}Warning data: ${JSON.stringify(data)}`);
116+
}
117+
} catch (jsonError) {
118+
debug(`${prefix}Could not stringify warning data: ${String(jsonError)}`);
119+
}
120+
}
121+
}

src/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ async function run(): Promise<void> {
6060
`Rate limit at end: ${JSON.stringify(endLimit)} (~${startLimit.remaining - endLimit.remaining} requests*)`,
6161
);
6262
} catch (e) {
63+
// Log detailed error information for debugging
64+
debug(`Error in AutoRebase action: ${String(e)}`);
65+
debug(`Full error details: ${JSON.stringify(e, Object.getOwnPropertyNames(e))}`);
66+
67+
// Log the stack trace if available
68+
if (e instanceof Error && e.stack) {
69+
debug(`Stack trace: ${e.stack}`);
70+
}
71+
72+
// Log additional context about what was happening when the error occurred
73+
debug(`Error occurred during AutoRebase action execution. Check logs above for context.`);
74+
6375
setFailed(e);
6476
}
6577
}

0 commit comments

Comments
 (0)