Skip to content

Commit

Permalink
fix: forgot to push token if not exists in env file
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Feb 24, 2024
1 parent db2f258 commit bf7b50a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
35 changes: 24 additions & 11 deletions src/settings/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ async function migrateWorFlow(plugin: GithubPublisher) {
export async function migrateToken(plugin: GithubPublisher, token?: string, repo?: string) {
logs({ settings: plugin.settings }, "migrating token");
const tokenPath = createTokenPath(plugin, plugin.settings.github.tokenPath);
console.log("TOKEN", token);
//@ts-ignore
if (plugin.settings.github.token && !token) {
logs({ settings: plugin.settings }, `Moving the GitHub Token in the file : ${tokenPath}`);
Expand All @@ -145,6 +146,10 @@ export async function migrateToken(plugin: GithubPublisher, token?: string, repo
return;
}
logs({ settings: plugin.settings }, `Moving the GitHub Token in the file : ${tokenPath} for ${repo ?? "default"} repository`);
const exists = await plugin.app.vault.adapter.exists(tokenPath);
if (!exists) {
await plugin.app.vault.adapter.mkdir(normalizePath(tokenPath).split("/").slice(0, -1).join("/"));
}
if (tokenPath.endsWith(".json")) {
const envToken = repo ? {
"GITHUB_PUBLISHER_REPOS" : {
Expand All @@ -153,28 +158,36 @@ export async function migrateToken(plugin: GithubPublisher, token?: string, repo
} : {
GITHUB_PUBLISHER_TOKEN: token
};
if (!(await plugin.app.vault.adapter.exists(tokenPath))) {
await plugin.app.vault.adapter.mkdir(normalizePath(tokenPath).split("/").slice(0, -1).join("/"));
if (!exists) {
await plugin.app.vault.adapter.write(tokenPath, JSON.stringify(envToken, null, 2));
return;
}
const oldToken = JSON.parse(await plugin.app.vault.adapter.read(tokenPath));
const newToken = { ...oldToken, ...envToken };
await plugin.app.vault.adapter.write(tokenPath, JSON.stringify(newToken, null, 2));

} else {
const envToken = repo ? `${repo}_TOKEN=${token}` : `GITHUB_TOKEN=${token}`;
console.log("ENV TOKEN", envToken, tokenPath, exists);
if (!exists) {
await plugin.app.vault.adapter.write(tokenPath, envToken);
return;
}
const oldToken = (await plugin.app.vault.adapter.read(tokenPath)).split("\n");
//search if old token is already in the file, if yes, replace it
for (const key in oldToken) {
if (key.startsWith("GITHUB_TOKEN") && !repo) {
oldToken[key] = envToken;
await plugin.app.vault.adapter.write(tokenPath, oldToken.join("\n"));
return;
} else if (key.startsWith(`${repo}_TOKEN`) && repo) {
oldToken[key] = envToken;
await plugin.app.vault.adapter.write(tokenPath, oldToken.join("\n"));
return;
for (let i = 0; i < oldToken.length; i++) {
if (oldToken[i].startsWith("GITHUB_TOKEN") && !repo) {
oldToken[i] = envToken;
break;
} else if (oldToken[i].startsWith(`${repo}_TOKEN`) && repo) {
oldToken[i] = envToken;
break;
} else if (i === oldToken.length - 1) {
oldToken.push(envToken);
}
}
await plugin.app.vault.adapter.write(tokenPath, oldToken.join("\n"));

}
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/settings/modals/manage_repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ class ModalEditingRepository extends Modal {
const decryptedToken: string = await this.plugin.loadToken(this.repository.smartKey);
text
.setPlaceholder("ghp_1234567890")
.setValue(this.repository.token ?? decryptedToken)
.setValue(decryptedToken)
.onChange(async (value) => {
await migrateToken(this.plugin, value.trim(), this.repository.smartKey);
await this.plugin.saveSettings();
Expand Down

0 comments on commit bf7b50a

Please sign in to comment.