Skip to content

Commit

Permalink
[FIX] Manifest URL (#14)
Browse files Browse the repository at this point in the history
* patch manifest url

* fix: 🚑 fix fetchReleases()

* refactor: 🚧 implement 4Source's requested change

see: https://github.com/4Source/vare-obsidian-plugin/pull/14/files#r1652895731

* fix: 🚨 fix linter warnings

* fix: 🚑 fix linter warnings

* fix: 🚑 fix missing partial release property

* style: ✏️ implement 4Source's requested change

* Fixes additional / if copy path in username/repository

* More consistant look of Troubleshooting state

* Fix missing Type for releases

---------

Co-authored-by: 4Source <[email protected]>
  • Loading branch information
anonhostpi and 4Source authored Jun 25, 2024
1 parent 307ba0e commit eb0e64c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 30 deletions.
15 changes: 9 additions & 6 deletions src/modals/PluginDataModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,21 @@ export class PluginDataModal extends Modal {
new Notice('Github <username>/<repository> do not match the pattern!');
return;
}
// Check a manifest could be fetched
const manifest = await fetchManifest(repo);
if (!manifest) {
new Notice('Github repository could not be found!');
return;
}
// check there are releases for the repo
const releases = await fetchReleases(repo);
if (!releases || releases.length <= 0) {
new Notice('No releases found for this plugin. May it do not have any.');
return;
}
// Check a manifest could be fetched
const manifest =
await fetchManifest(undefined,undefined,releases[0]) ||
await fetchManifest(repo, releases[0].tag_name) ||
await fetchManifest(repo);
if (!manifest) {
new Notice('Plugin manifest could not be found in releases, tag ref, or default branch!');
return;
}
// Combine data
const pluginInfo = Object.assign({}, manifest, { repo, releases }) as PluginInfo;
pluginInfo.targetVersion = pluginInfo.version;
Expand Down
55 changes: 36 additions & 19 deletions src/modals/PluginTroubleshootingModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export class PluginTroubleshootingModal extends Modal {
let repository = this.pluginInfo.repo.split('/').at(1) || '';
let manifest: PluginManifest | undefined;
let hasManifest = false;
let releases: Partial<Release>[] | undefined;
let hasReleases = false;

// Debonce text input
Expand All @@ -42,7 +41,14 @@ export class PluginTroubleshootingModal extends Modal {
.setPlaceholder('Username')
.setValue(username)
.onChange(value => {
username = value;
if (value.contains('/')) {
const repoSections = value.split('/');
username = repoSections[0];
repository = repoSections[1];
}
else {
username = value;
}
updateRepo();
}));

Expand All @@ -53,7 +59,14 @@ export class PluginTroubleshootingModal extends Modal {
.setPlaceholder('Repository')
.setValue(repository)
.onChange(value => {
repository = value;
if (value.contains('/')) {
const repoSections = value.split('/');
username = repoSections[0];
repository = repoSections[1];
}
else {
repository = value;
}
updateRepo();
}));

Expand All @@ -67,23 +80,9 @@ export class PluginTroubleshootingModal extends Modal {
.onClick(() => {
this.update();
}));


let releases: Partial<Release>[] | undefined = undefined;
if (repositoryRegEx.test(this.pluginInfo.repo)) {
manifest = await fetchManifest(this.pluginInfo.repo);
hasManifest = manifest !== undefined;
new Setting(contentEl)
.setName('Test connection')
.setDesc(hasManifest ? '' : 'Repo could not be found on GitHub. Is everything typed correctly?')
.addExtraButton(button => button
.setIcon(hasManifest ? ICON_ACCEPT : ICON_DENY)
.setTooltip(hasManifest ? '' : 'Try again?')
.setDisabled(hasManifest)
.onClick(() => {
this.update();
}));
}

if (hasManifest) {
releases = await fetchReleases(this.pluginInfo.repo);
hasReleases = releases !== undefined && (releases.length > 0);
new Setting(contentEl)
Expand All @@ -98,6 +97,24 @@ export class PluginTroubleshootingModal extends Modal {
}));
}

if (repositoryRegEx.test(this.pluginInfo.repo) && hasReleases) {
const last_release = releases ? releases[0] : undefined;
manifest =
await fetchManifest(undefined, undefined, last_release) ||
await fetchManifest(this.pluginInfo.repo);
hasManifest = manifest !== undefined;
new Setting(contentEl)
.setName('Test manifest')
.setDesc(hasManifest ? '' : 'Manifest could not be found on GitHub. Is everything including the repo typed correctly?')
.addExtraButton(button => button
.setIcon(hasManifest ? ICON_ACCEPT : ICON_DENY)
.setTooltip(hasManifest ? '' : 'Try again?')
.setDisabled(hasManifest)
.onClick(() => {
this.update();
}));
}

new Setting(contentEl)
.addButton(button => button
.setButtonText('Save')
Expand Down
6 changes: 5 additions & 1 deletion src/settings/SettingsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,11 @@ export class VareSettingTab extends PluginSettingTab {
.onClick(async () => {
try {
// Fetch the manifest from GitHub
const manifest = await fetchManifest(plugin.repo, plugin.targetVersion);
const release = plugin.releases.find(release => release.tag_name === plugin.targetVersion);
const manifest =
await fetchManifest(undefined,undefined,release) ||
await fetchManifest(plugin.repo, plugin.targetVersion) ||
await fetchManifest(plugin.repo);
if (!manifest) {
throw Error('No manifest found for this plugin!');
}
Expand Down
11 changes: 7 additions & 4 deletions src/util/GitHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export async function fetchReleases(repository: string): Promise<Partial<Release
return {
tag_name: value.tag_name,
prerelease: value.prerelease,
assets: value.assets,
};
});
return releases;
Expand All @@ -99,16 +100,18 @@ export async function fetchReleases(repository: string): Promise<Partial<Release
* Fetch the manifest for a plugin
* @param repository The <user>/<repo> of the plugin
* @param tag_name The name of the tag associated with a release. Required if a specific manifest version is needed.
* @param release The release object of the plugin. Used to replicate Obsidian's behavior of fetching the manifest for the plugin.
* @returns The plugin manifest object
*/
export async function fetchManifest(repository: string, tag_name?: string): Promise<PluginManifest | undefined> {
const URL = `https://raw.githubusercontent.com/${repository}/${tag_name ? tag_name : 'HEAD'}/manifest.json`;
export async function fetchManifest(repository?: string, tag_name?: string, release?: Partial<Release>): Promise<PluginManifest | undefined> {
const download_url: string | undefined = release?.assets?.find((asset: Asset) => asset.name === 'manifest.json')?.browser_download_url;
const url: string = download_url ? download_url : `https://raw.githubusercontent.com/${repository}/${tag_name ? tag_name : 'HEAD'}/manifest.json`;
try {
if (!repositoryRegEx.test(repository)) {
if (repository && !repositoryRegEx.test(repository)) {
throw Error('Repository string do not match the pattern!');
}
// Do a request to the url
const response = await request({ url: URL });
const response = await request({ url });

// Process the response
return (await JSON.parse(response)) as PluginManifest;
Expand Down

0 comments on commit eb0e64c

Please sign in to comment.