Skip to content

Commit

Permalink
chore: update version to 1.1.4 and enhance GitHub error handling and …
Browse files Browse the repository at this point in the history
…repository initialization

- Bumped the version number from 1.1.3 to 1.1.4 in manifest.json to reflect the latest release.
- Improved error handling in GitHubService to provide more informative error messages for GitHub API errors.
- Added methods to check if a repository is empty and to initialize it with an auto-generated README if necessary.
- Updated ZipHandler to check for empty repositories and initialize them during the upload process, enhancing user experience and reliability.
  • Loading branch information
mamertofabian committed Dec 28, 2024
1 parent 43ea8fb commit 49f485c
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 19 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "Bolt to GitHub",
"version": "1.1.3",
"version": "1.1.4",
"description": "Automatically process your Bolt project zip files and upload them to your GitHub repository.",
"icons": {
"16": "assets/icons/icon16.png",
Expand Down
7 changes: 5 additions & 2 deletions src/background/BackgroundService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,12 @@ export class BackgroundService {
const isGitHubError = errorMessage.includes('GitHub API Error');

if (isGitHubError) {
// Extract the original GitHub error message if available
const originalMessage = (decodeError as any).originalMessage ||
'GitHub authentication or API error occurred';

throw new Error(
`GitHub authentication failed. Please check your GitHub token in the extension settings and try again. \n\n` +
`If the issue persists, please open a GitHub issue.`
`GitHub Error: ${originalMessage}`
);
} else {
throw new Error(
Expand Down
96 changes: 80 additions & 16 deletions src/services/GitHubService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,49 @@ export class GitHubService {
async request(method: string, endpoint: string, body?: any, options: RequestInit = {}) {
const url = `${this.baseUrl}${endpoint}`;

const response = await fetch(url, {
method,
...options,
headers: {
'Accept': 'application/vnd.github.v3+json',
'Authorization': `Bearer ${this.token}`,
'Content-Type': 'application/json',
...options.headers,
},
body: body ? JSON.stringify(body) : undefined,
});
try {
const response = await fetch(url, {
method,
...options,
headers: {
'Accept': 'application/vnd.github.v3+json',
'Authorization': `Bearer ${this.token}`,
'Content-Type': 'application/json',
...options.headers,
},
body: body ? JSON.stringify(body) : undefined,
});

if (!response.ok) {
const error = await response.json();
throw new Error(`GitHub API Error: ${response.status} ${error.message || JSON.stringify(error)}`);
}
if (!response.ok) {
let errorDetails;
try {
errorDetails = await response.json();
} catch {
// If parsing JSON fails, use the status text
errorDetails = { message: response.statusText };
}

return response.json();
// Construct a more informative error message
const errorMessage = errorDetails.message ||
errorDetails.error ||
'Unknown GitHub API error';

const fullErrorMessage = `GitHub API Error (${response.status}): ${errorMessage}`;

// Create a custom error with additional properties
const apiError = new Error(fullErrorMessage) as any;
apiError.status = response.status;
apiError.originalMessage = errorMessage;
apiError.githubErrorResponse = errorDetails;

throw apiError;
}

return await response.json();
} catch (error) {
// Re-throw the error to maintain the original error details
throw error;
}
}

async repoExists(owner: string, repo: string): Promise<boolean> {
Expand Down Expand Up @@ -144,6 +169,45 @@ export class GitHubService {
}
}

async isRepoEmpty(owner: string, repo: string): Promise<boolean> {
try {
const commits = await this.request('GET', `/repos/${owner}/${repo}/commits`);
return commits.length === 0;
} catch (error) {
console.log('error', error);
if (error instanceof Error && error.message.includes('409')) {
// 409 is returned for empty repositories
return true;
}
throw error;
}
}

async initializeEmptyRepo(owner: string, repo: string, branch: string): Promise<void> {
// Create a more informative README.md to initialize the repository
const readmeContent = `# ${repo}
## Repository Initialization Notice
This repository was automatically initialized by the Bolt to GitHub extension.
**Auto-Generated Repository**
- Created to ensure a valid Git repository structure
- Serves as an initial commit point for your project
If you did not intend to create this repository or have any questions,
please check your Bolt to GitHub extension settings.`;

await this.pushFile({
owner,
repo,
path: 'README.md',
content: btoa(readmeContent),
branch,
message: 'Initialize repository with auto-generated README'
});
}

async pushFile(params: {
owner: string;
repo: string;
Expand Down
7 changes: 7 additions & 0 deletions src/services/zipHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ export class ZipHandler {
await this.updateStatus('uploading', 15, 'Checking repository...');
await this.githubService.ensureRepoExists(repoOwner, repoName);

// Check if repo is empty and needs initialization
const isEmpty = await this.githubService.isRepoEmpty(repoOwner, repoName);
if (isEmpty) {
await this.updateStatus('uploading', 18, 'Initializing empty repository...');
await this.githubService.initializeEmptyRepo(repoOwner, repoName, targetBranch);
}

await this.ensureBranchExists(repoOwner, repoName, targetBranch);

const processedFiles = await this.processFilesWithGitignore(files);
Expand Down

0 comments on commit 49f485c

Please sign in to comment.