Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Use Node.js
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: '16.x' # You might need to adjust this value to your own version
node-version: "20.x" # You might need to adjust this value to your own version

- uses: pnpm/action-setup@v4
with:
version: 9.4.0
- name: Build
id: build
env:
GITHUBAUTHENTICATIONTOKEN: ${{ secrets.GITHUBAUTHENTICATIONTOKEN }}
run: |
npm i && npm run build
pnpm i && pnpm run build
mkdir ${{ env.PLUGIN_NAME }}
cp README.md package.json icon.png ${{ env.PLUGIN_NAME }}
mv dist ${{ env.PLUGIN_NAME }}
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ Born out of frustration with github's issue handling, this plugin allows you to
- Open plugins
- Click the gear icon in the logseq github plugin
- Click open settings
- Customize the options
- Customize the options

## Usage
1. Enter `/Fetch Github Issues` into any position in Logseq
2. The query will be carried out as defined in the SearchQuery field in Settings
- Example :`repo:sawhney17/logseq-github-plugin is:issue state:open`
141 changes: 78 additions & 63 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,32 @@ import '@logseq/libs';
import { SettingSchemaDesc } from '@logseq/libs/dist/LSPlugin.user';
// import 'octokit'
import { Octokit, App } from 'octokit'
let githubToken = process.env.GITHUBAUTHENTICATIONTOKEN
const octokit = new Octokit({ auth: `${githubToken}` });
//Inputs 5 numbered blocks when called
let octokit

var blockArray

const pageName = "Github"
let settings: SettingSchemaDesc[] = [
{
key: "API Key",
key: "GHIssuesAccessToken",
type: "string",
title: "Enter github personal access token",
description: "Enter your personal access token here",
default: "user:sawhney17"
description: "Enter your personal access token here (optional for public repos)",
default: "YOURTOKEN"
},
{
key: "SearchQuery",
type: "string",
title: "Enter github search query",
description: "Enter your desired search query here",
default: "user:sawhney17"
description: "Enter your desired search query here (see [searching-issues on Github](https://docs.github.com/en/search-github/searching-on-github/searching-issues-and-pull-requests))",
default: "repo:USERNAME/REPOSITORY state:open is:issue"
},
{
key: "TargetPage",
type: "string",
title: "Enter target page",
description: "Enter your desired page, to where the blocks will be inserted",
default: "Github Issues"
},
{
key: "Block1InsertionTemplate",
key: "ReferenceTemplate",
type: "string",
title: "Insertion template for block 1",
description: "Enter your desired template for the parent block, created by default for every return value of the query ",
default: "TODO [Title](URL)"
default: "TODO [[{Title}]]"
},
{
key: "Block2InsertionTemplate",
key: "ContentTemplate",
type: "string",
title: "Insertion template for block 2",
description: "Enter your desired template for the child block, created by default for every return value of the query ",
Expand All @@ -48,75 +36,102 @@ let settings: SettingSchemaDesc[] = [
]
logseq.useSettingsSchema(settings)

function updateSettings() {
blockArray = logseq.settings.blockTracker
}

function syncSettings() {
logseq.updateSettings({blockTracker: blockArray })
}
async function fetchGithubIssues(e) {
console.log('hi')
console.log()
logseq.App.showMsg('Fetching Issues...')
octokit.request('GET /search/issues', {
q: `${logseq.settings.SearchQuery}`,
q: `${logseq.settings!.SearchQuery}`,
}).then((response) => { insertBlocks(response) })
}


function applyTemplate(response, inputString) {
var finalString = inputString
console.log("Repo")

finalString = finalString.replaceAll(/{Title}/gi, response.title)
finalString = finalString.replaceAll(/{URL}/gi, response.html_url)
finalString = finalString.replaceAll(/{Body}/gi, response.body)
console.log(response.repository_url.split("repo/"[1]))
// finalString = finalString.replaceAll(/{Repo}/gi, response)

return finalString
}

function makeEmptyPage(pageName) {
return logseq.Editor.createPage(
pageName,
{},
{
redirect: false,
createFirstBlock: false,
journal: false,
},
)
}

async function insertBlocks(response) {
updateSettings()
console.log(response.data.items)
// Get current block position
const currentBlock = await logseq.Editor.getCurrentBlock()

for (const dataPoint in response.data.items) {
console.log(response.data.items[dataPoint].id)
syncSettings()
if (blockArray.indexOf(response.data.items[dataPoint].id) == -1) {
blockArray.push(response.data.items[dataPoint].id)
syncSettings()

console.log(dataPoint)
let page = await logseq.Editor.createPage(logseq.settings.TargetPage, {}, { redirect: true })

let block1Text = applyTemplate(response.data.items[dataPoint], logseq.settings.Block1InsertionTemplate)
// console.log(block1Text)
console.log(block1Text)
let block1 = await logseq.Editor.insertBlock(page.name, block1Text, { isPageBlock: true })
logseq.Editor.updateBlock(block1.uuid, `${block1.content}\nid:: ${block1.uuid}\ncollapsed:: true`)
if (logseq.settings.Block2InsertionTemplate != "") {
let block2Text = applyTemplate(response.data.items[dataPoint], logseq.settings.Block2InsertionTemplate)
let block2 = await logseq.Editor.insertBlock(block1.uuid, `${block2Text}`, { sibling: false })
// Define page name
const pageName = response.data.items[dataPoint].title
console.log(pageName)
// Create page if not existent
let issuePage = await logseq.Editor.getPage(pageName)
if (!issuePage) {
issuePage = await makeEmptyPage(pageName)
}

// Append content to page
if (logseq.settings!.ContentTemplate != "") {
let contentText = applyTemplate(
response.data.items[dataPoint],
logseq.settings!.ContentTemplate
)

// Check whether Block is already on first level of page
const issuePageTree = await logseq.Editor.getPageBlocksTree(issuePage!?.uuid)
let foundContentText = false
console.log(issuePageTree.toString())
console.log(`len: ${issuePageTree.length}`)
for (const i in issuePageTree) {
console.log(`block: ${issuePageTree[i]}`)
if (issuePageTree[i].content == contentText) {
foundContentText = true
break
}
}

if (!foundContentText) {
console.log(`Added ${contentText}`)
const contentBlock = await logseq.Editor.appendBlockInPage(
pageName,
contentText,
)
}
}

// Insert reference
let referenceText = applyTemplate(
response.data.items[dataPoint],
logseq.settings!.ReferenceTemplate
)

const referenceBlock = await logseq.Editor.insertBlock(
currentBlock!.uuid,
referenceText,
)
}
logseq.App.pushState("page", {
name: logseq.settings.TargetPage,
})
}



const main = async () => {
console.log('plugin loaded');
logseq.onSettingsChanged(updateSettings)
logseq.Editor.registerSlashCommand('Github Sync', async (e) => {
let githubToken = logseq.settings!.GHIssuesAccessToken

octokit = new Octokit({ auth: `${githubToken}` });
logseq.Editor.registerSlashCommand('Fetch Github Issues', async (e) => {
fetchGithubIssues(e)
}
)
if (logseq.settings.blockTracker == undefined) {
logseq.updateSettings({ blockTracker: [] })
console.log(logseq.settings.blockTracker)
}
}

logseq.ready(main).catch(console.error);
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"devDependencies": {
"buffer": "^6.0.3",
"parcel": "^2.0.0"
"parcel": "^2.0.0",
"process": "^0.11.10"
}
}
Loading