forked from raycast/extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Github Gists] OAuth 2.0 support (raycast#13403)
* feat(github-gists): Adds OAuth2.0 support and deprecates tokens closes raycast#13402 * refactor(github-gists): fixed lint issues closes raycast#13402 * refactor(github-gists): changed the readme screencast closes raycast#13402 * Update CHANGELOG.md and optimise images --------- Co-authored-by: raycastbot <[email protected]>
- Loading branch information
1 parent
91141e9
commit 997d7e7
Showing
15 changed files
with
514 additions
and
306 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import { Octokit } from "@octokit/core"; | ||
import { formatBytes, isEmpty } from "../util/utils"; | ||
import { Clipboard, open, showToast, Toast } from "@raycast/api"; | ||
import { Gist, GistFile, GithubGistTag } from "../util/gist-utils"; | ||
|
||
export class GithubClient { | ||
constructor(public readonly octokit: Octokit) {} | ||
|
||
public async requestGist(tag: string, page: number, perPage: number) { | ||
const { octokit } = this; | ||
const response = await (async () => { | ||
switch (tag) { | ||
case GithubGistTag.MY_GISTS: { | ||
return await octokit.request(`GET /gists`, { page: page, per_page: perPage }); | ||
} | ||
case GithubGistTag.ALL_GISTS: { | ||
return await octokit.request(`GET /gists/public`, { page: page, per_page: perPage }); | ||
} | ||
case GithubGistTag.STARRED: { | ||
return await octokit.request(`GET /gists/starred`, { page: page, per_page: perPage }); | ||
} | ||
default: { | ||
return await octokit.request(`GET /gists`, { page: page, per_page: perPage }); | ||
} | ||
} | ||
})(); | ||
const _gists: Gist[] = []; | ||
response.data.forEach((_data) => { | ||
const _gist: Gist = { | ||
gist_id: _data.id, | ||
description: isEmpty(String(_data.description)) ? "[ No description ]" : String(_data.description), | ||
html_url: _data.html_url, | ||
file: [], | ||
}; | ||
for (const value of Object.values(_data.files)) { | ||
if (value !== undefined) { | ||
_gist.file.push({ | ||
filename: String(value.filename), | ||
language: String(value.language), | ||
raw_url: String(value.raw_url), | ||
size: formatBytes(value.size), | ||
type: value.type, | ||
}); | ||
} | ||
} | ||
_gists.push(_gist); | ||
}); | ||
return _gists; | ||
} | ||
|
||
public async starGist(gist_id: string) { | ||
return await this.octokit.request(`PUT /gists/${gist_id}/star`, { | ||
gist_id: gist_id, | ||
}); | ||
} | ||
|
||
public async unStarGist(gist_id: string) { | ||
return await this.octokit.request(`DELETE /gists/${gist_id}/star`, { | ||
gist_id: gist_id, | ||
}); | ||
} | ||
|
||
public async deleteGist(gist_id: string) { | ||
return await this.octokit.request(`DELETE /gists/${gist_id}`, { | ||
gist_id: gist_id, | ||
}); | ||
} | ||
|
||
public async createGist(description: string, isPublic = false, gistFiles: GistFile[]) { | ||
const files: { [p: string]: { content: string } } = {}; | ||
gistFiles.forEach((value) => { | ||
files[value.filename] = { content: value.content }; | ||
}); | ||
return await this.octokit.request("POST /gists", { | ||
description: description, | ||
public: isPublic, | ||
files: files, | ||
}); | ||
} | ||
|
||
public async updateGist(gistId: string, description: string, oldFileNames: string[], newFiles: GistFile[]) { | ||
const files: { [p: string]: { content: string } } = {}; | ||
const newFileName = newFiles.map((value) => value.filename); | ||
const deleteFiles = oldFileNames.filter((value) => !newFileName.includes(value)); | ||
newFiles.forEach((value) => { | ||
files[value.filename] = { content: value.content }; | ||
}); | ||
deleteFiles.forEach((value) => { | ||
files[value] = { content: "" }; | ||
}); | ||
return await this.octokit.request("PATCH /gists/" + gistId, { | ||
description: description, | ||
files: files, | ||
}); | ||
} | ||
|
||
public async updateOrCreateGists( | ||
isEdit: boolean, | ||
gist: Gist, | ||
description: string, | ||
isPublic: string, | ||
gistFiles: GistFile[], | ||
oldGistFiles: string[], | ||
gistMutate: () => void, | ||
) { | ||
const toast = await showToast(Toast.Style.Animated, isEdit ? "Updating" : "Creating"); | ||
try { | ||
let response; | ||
if (isEdit) { | ||
response = await this.updateGist(gist.gist_id, description, oldGistFiles, gistFiles); | ||
} else { | ||
response = await this.createGist(description, isPublic === "true", gistFiles); | ||
} | ||
if (response.status === 201 || response.status === 200) { | ||
const options: Toast.Options = { | ||
title: "Gist " + (isEdit ? "Updated" : "Created"), | ||
primaryAction: { | ||
title: "Copy Gist Link", | ||
shortcut: { modifiers: ["shift", "cmd"], key: "l" }, | ||
onAction: (toast) => { | ||
Clipboard.copy(String(response.data.html_url)); | ||
toast.title = "Link Copied to Clipboard"; | ||
}, | ||
}, | ||
secondaryAction: { | ||
title: "Open in Browser", | ||
shortcut: { modifiers: ["shift", "cmd"], key: "o" }, | ||
onAction: (toast) => { | ||
open(String(response.data.html_url)); | ||
toast.hide(); | ||
}, | ||
}, | ||
}; | ||
toast.style = Toast.Style.Success; | ||
toast.title = options.title; | ||
toast.primaryAction = options.primaryAction; | ||
toast.secondaryAction = options.secondaryAction; | ||
gistMutate(); | ||
} else { | ||
toast.style = Toast.Style.Failure; | ||
toast.title = "Failed to " + (isEdit ? "Update" : "Create"); | ||
} | ||
} catch (error) { | ||
toast.style = Toast.Style.Failure; | ||
toast.title = "Failed to " + (isEdit ? "Update" : "Create"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { OAuthService } from "@raycast/utils"; | ||
import fetch from "node-fetch"; | ||
import { Octokit } from "@octokit/core"; | ||
import { GithubClient } from "./github-client"; | ||
|
||
let client: GithubClient | undefined = undefined; | ||
|
||
export const githubOAuthService = OAuthService.github({ | ||
scope: "repo gist read:user", | ||
onAuthorize: ({ token }) => { | ||
const octokit = new Octokit({ auth: token, request: { fetch } }); | ||
|
||
client = new GithubClient(octokit); | ||
}, | ||
}); | ||
|
||
export function getGitHubClient() { | ||
if (!client) { | ||
throw new Error("GitHub client not initialized"); | ||
} | ||
|
||
return client; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { withAccessToken } from "@raycast/utils"; | ||
import React from "react"; | ||
import { githubOAuthService } from "../api/oauth"; | ||
|
||
export function withGitHubClient(Component: React.ComponentType) { | ||
return withAccessToken(githubOAuthService)(Component); | ||
} |
Oops, something went wrong.