Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
laughk committed Jan 2, 2021
0 parents commit 0806418
Show file tree
Hide file tree
Showing 14 changed files with 393 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/
.pyc
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Kei IWASAKI (a.k.a laughk)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Keypirinha Plugin: GitHub

![demo](demo.gif)

This is a plugin for the [Keypirinha](http://keypirinha.com) launcher.

It provide command to quick access to repository and your account page of github.com.

### Inspire of

- [gharlan/alfred-github-workflow: GitHub Workflow for Alfred 3/4](https://github.com/gharlan/alfred-github-workflow)
- [buty4649/ulauncher-github-extension: Github extension for Ulauncher](https://github.com/buty4649/ulauncher-github-extension)


## Download

https://github.com/laughk/keypirinha-GitHub/releases


## Install

Once the `github.keypirinha-package` file is installed,
move it to the `InstalledPackage` folder located at:

* `Keypirinha\portable\Profile\InstalledPackages` in **Portable mode**
* **Or** `%APPDATA%\Keypirinha\InstalledPackages` in **Installed mode** (the
final path would look like
`C:\Users\%USERNAME%\AppData\Roaming\Keypirinha\InstalledPackages`)

## Setup

1. Get your github personal access token with below scope from [this page]([Personal Access Tokens (github.com)](https://github.com/settings/tokens)).

- `repo` (if you want use with private repository)
- `user read:user`

2. Put github personal access token to config file. like below.

1. input "configure github" on Keyperinha and open config file.

![screenshot1](screenshot1.png)

2. set the token to `github_access_token` in `[var]` and save.

```dosini
[var]
github_access_token = ****************************
```

## Usage

#### 1. Select Repository or Your (My) Account

![sceenshot2](screenshot2.png)

#### 2. GitHub Repository: Select github repository and page.

Case of select "GitHub: Repository". this is provide quick access to repository.

suggestions will appear. these is result that is github repository search with graphql api by input text. select one.

![screenshot3](screenshot3.png)

select page of the repository from suggestions.

![screenshot4](screenshot4.png)

#### 3. GitHub My Account: Select your account page

Case of select "GitHub: My Account". this is provide quick access to your account page.

![screenshot5](screenshot5.png)

13 changes: 13 additions & 0 deletions build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
$PACKAGE_NAME = "github"
$BUILD_DIR = [string]::IsNullOrEmpty($BUILD_DIR) ? ($PSScriptRoot + "\build") : $BUILD_DIR

if (!(Test-Path $BUILD_DIR)) {
mkdir $BUILD_DIR
}

cd $PSScriptRoot

Compress-Archive `
-Update `
-Path src\*,LICENSE*,README* `
-DestinationPath ($BUILD_DIR + "\" + $PACKAGE_NAME + ".keypirinha-package")
Binary file added demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshot1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshot2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshot3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshot4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshot5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/github.ico
Binary file not shown.
12 changes: 12 additions & 0 deletions src/github.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[main]

[var]
#
# set your github access token from: https://github.com/settings/tokens
# require scope:
# - repo (if you want use with private repository)
# - user (read:user only)
#
#github_access_token = <your access token>

[env]
220 changes: 220 additions & 0 deletions src/github.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
# Keypirinha launcher (keypirinha.com)

import keypirinha as kp
import keypirinha_util as kpu
import keypirinha_net as kpnet

from .github_api_helper import fetch_repos_by_name, fetch_user_id


class github(kp.Plugin):
"""
GitHub Plugin
- repository
- my account access
"""

CMD_PREFIX = {
"root": "github:",
"repos": "github repos:",
"repos_choise": "github repos choice:",
"repos_action": "github repos action:",
"my_account": "github my:",
"my_account_action": "github my action:",
}

CMD_GH = CMD_PREFIX["root"]
CMD_GH_REPOS = CMD_PREFIX["repos"]
CMD_GH_REPO_CHOICE = CMD_PREFIX["repos_choise"] + "{reponame}"
CMD_GH_REPO_ACTION = CMD_PREFIX["repos_action"] + "{action}"
CMD_GITHUB_MY_ACCOUNT = CMD_PREFIX["my_account"]
CMD_GITHUB_MY_ACCOUNT_ACTION = CMD_PREFIX["my_account_action"] + "{action}"

LABEL_PREFIX = "GitHub: "

def __init__(self):
super().__init__()

def on_start(self):
self.token = self.load_settings().get(
"github_access_token", "var", unquote=True
)

def on_catalog(self):
catalog = []
catalog.append(
self.create_item(
category=kp.ItemCategory.KEYWORD,
label="GitHub",
short_desc="Github Plugin",
target=self.CMD_GH,
args_hint=kp.ItemArgsHint.REQUIRED,
hit_hint=kp.ItemHitHint.KEEPALL,
)
)
self.set_catalog(catalog)

def on_suggest(self, user_input: str, items_chain: list):
if not items_chain or items_chain[0].category() != kp.ItemCategory.KEYWORD:
return

suggestions = []

if items_chain[0].target() == self.CMD_GH:
suggestions = self.gen_categories()

if len(items_chain) >= 2:

if items_chain[1].target() == self.CMD_GH_REPOS:
suggestions = self.gen_repos_suggestion(user_input)

if len(items_chain) >= 3:
reponame = str(items_chain[2]).replace(self.LABEL_PREFIX, "")
suggestions = self.gen_repos_action_suggestion(reponame)

if items_chain[1].target() == self.CMD_GITHUB_MY_ACCOUNT:
suggestions = self.gen_my_account_sugestions()

self.set_suggestions(suggestions, kp.Match.FUZZY, kp.Sort.NONE)

def on_execute(self, item, action):

target = item.target()
p = ""

if target == self.CMD_GH:
return
elif self.CMD_PREFIX["repos_action"] in target:
p = target.replace(self.CMD_PREFIX["repos_action"], "")
print(p)
elif self.CMD_PREFIX["my_account_action"] in target:
p = target.replace(self.CMD_PREFIX["my_account_action"], "")

if "https://" in p:
url = p
else:
url = f"https://github.com{p}"

kpu.web_browser_command(url=url, execute=True)

def on_activated(self):
pass

def on_deactivated(self):
pass

def on_events(self, flags):
pass

def gen_categories(self) -> list: # -> list[kp.CatalogItem]
catalog = []

repository_cmd = self.create_item(
category=kp.ItemCategory.KEYWORD,
label=self.LABEL_PREFIX + "Repository",
short_desc="Github Repository Search",
target=self.CMD_GH_REPOS,
args_hint=kp.ItemArgsHint.REQUIRED,
hit_hint=kp.ItemHitHint.KEEPALL,
)
catalog.append(repository_cmd)

my_account_cmd = self.create_item(
category=kp.ItemCategory.KEYWORD,
label=self.LABEL_PREFIX + "My Account",
short_desc="Github My Account",
target=self.CMD_GITHUB_MY_ACCOUNT,
args_hint=kp.ItemArgsHint.REQUIRED,
hit_hint=kp.ItemHitHint.KEEPALL,
)
catalog.append(my_account_cmd)

return catalog

def gen_my_account_sugestions(self): # -> list[kp.CatalogItem]

try:
user_id = fetch_user_id(self.token)
except Exception as e:
self.err(e)
return "fetching data from github graphql api has failed."

my_account_pages = [
# ("name", "path")
("dashboard", "/"),
("notifications", "/notifications"),
("issues", "/issues"),
("pulls", "/pulls"),
("settings", "/settings/profile"),
("personal access tokens", "/settings/tokens"),
("profile", f"/{user_id}"),
("repos", f"/{user_id}/?tab=repositories"),
("stars", f"/{user_id}/?tab=stars"),
("gists", f"https://gist.github.com/{user_id}"),
]

return [
self.create_item(
category=kp.ItemCategory.KEYWORD,
label=label,
target=self.CMD_GITHUB_MY_ACCOUNT_ACTION.format(action=path),
short_desc="",
args_hint=kp.ItemArgsHint.FORBIDDEN,
hit_hint=kp.ItemHitHint.NOARGS,
)
for label, path in my_account_pages
]

def gen_repos_suggestion(self, user_input: str) -> list: # -> list[kp.CatalogItem]

try:
repo_names = fetch_repos_by_name(user_input, self.token)
except Exception as e:
self.err(e)
return "fetching data from github graphql api has failed."

suggestions = []
for r in repo_names:
desc = r["description"] if r.get("description") else ""
suggestions.append(
self.create_item(
category=kp.ItemCategory.KEYWORD,
label=self.LABEL_PREFIX + r["nameWithOwner"],
target=self.CMD_GH_REPO_CHOICE.format(reponame=r["nameWithOwner"]),
short_desc=desc,
args_hint=kp.ItemArgsHint.REQUIRED,
hit_hint=kp.ItemHitHint.KEEPALL,
)
)

return suggestions

def gen_repos_action_suggestion(
self, reponame: str, gh_host: str = "github.com"
) -> list: # -> list[kp.CatalogItem]

actions = [
# (action, path, desctiption)
("Open", f"/{reponame}", "Open github repository"),
("issues", f"/{reponame}/issues", ""),
("new issue", f"/{reponame}/issues/new", ""),
("milestones", f"/{reponame}/milestones", ""),
("pulls", f"/{reponame}/pulls", ""),
("projects", f"/{reponame}/projects", ""),
("pulse", f"/{reponame}/pulse", ""),
("releases", f"/{reponame}/releases", ""),
("wiki", f"/{reponame}/wiki", ""),
]

return [
self.create_item(
category=kp.ItemCategory.KEYWORD,
label=self.LABEL_PREFIX + f"{action}: {gh_host}{path}",
target=self.CMD_GH_REPO_ACTION.format(action=path),
short_desc=desc,
args_hint=kp.ItemArgsHint.FORBIDDEN,
hit_hint=kp.ItemHitHint.NOARGS,
)
for action, path, desc in actions
]
52 changes: 52 additions & 0 deletions src/github_api_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import urllib.request
import json


GITHUB_GRAPHQL_API_ENDPOINT = "https://api.github.com/graphql"


def fetch_data_by_query(
query: str, token: str, endpoint: str = GITHUB_GRAPHQL_API_ENDPOINT
) -> list:

headers = {"Authorization": f"bearer {token}"}
post_data = {"query": query}

req = urllib.request.Request(
GITHUB_GRAPHQL_API_ENDPOINT, json.dumps(post_data).encode(), headers
)

res = ""
with urllib.request.urlopen(req) as f:
res = f.read().decode("utf-8")

return json.loads(res)["data"]


def fetch_user_id(token: str, endpoint: str = GITHUB_GRAPHQL_API_ENDPOINT) -> str:

q = "query { viewer { login } }"
data = fetch_data_by_query(q, token, endpoint)

return data["viewer"]["login"]


def fetch_repos_by_name(
name: str, token: str, endpoint: str = GITHUB_GRAPHQL_API_ENDPOINT
) -> list:

q = f"""
query {{
search(query: "{name}", type: REPOSITORY, first: 10) {{
nodes {{
... on Repository {{
nameWithOwner
description
}}
}}
}}
}}
"""
data = fetch_data_by_query(q, token, endpoint)

return data["search"]["nodes"]

0 comments on commit 0806418

Please sign in to comment.