Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable command-triggered hot reloading of the plugins #84

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
44 changes: 44 additions & 0 deletions src/command/refresh.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function commander(c::Client, m::Message, ::Val{:refresh})
@assert startswith(m.content, COMMAND_PREFIX * "refresh")
hasprivilege(m.user, Val(:refresh)) || return
parts = split(m.content[length(COMMAND_PREFIX)+1:end])
idx = findfirst(x->startswith(x,"test"), parts)
if idx !== nothing
tester = parts[idx][5:end]
if startswith(tester, "pr")
id = parts[idx+1]
tryparse(Int, id) === nothing && return nothing
branch = "branch_"*id
run(`git fetch origin pull/$id/head:$branch`)
elseif startswith(tester, "branch")
branch = parts[idx+1]
else
reply(c, m, "test$tester is an unknown argument")
return nothing
end
run(`git switch $branch`)
end
if any(==("needsrestart"), parts)
touch("SHUTDOWN")
else
#Revise.revise()
@eval HoJBot includejlfiles("command/")
@eval HoJBot includejlfiles("handler/")
end
return nothing
end

function help_commander(c::Client, m::Message, ::Val{:refresh})
reply(c, m, """
How to use the `refresh` command:
```
refresh help
refresh [cmd arg] [needsrestart]
```
`cmd` can be either of `testpr` and `testbranch`. Arg has a different meaning correspondingly:
testpr: arg is of the format `id` where id is the pull request id as on github (creates a local branch: "branch_\$id")
testbranch: arg is of the format `branch` where branch is the (remote) branch to be switched to

when `needsrestart` is passed, the bot will restart once the corresponding operation has been completed. Otherwise it'll just reinclude the files in "commands/" and "handlers/"
""")
end
28 changes: 28 additions & 0 deletions src/rights.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
abstract type CollectionOfRights end

struct BotAdmin <: CollectionOfRights end
struct OrdinaryUser <: CollectionOfRights end
abstract type GranularRight <: CollectionOfRights end


struct Functionality{symb} end

allowedto(user::User, func::Val{T}) where T = allowedto(user, Functionality{T})

function allowedto(user::User, func::Functionality)
return allows(rights(user), requirements(func))
end

function allows(role::CollectionOfRights, right::GranularRight)::Bool
return role isa BotAdmin
end

function allows(role::GranularRight, right::GranularRight)::Bool
return role==right
end

function requirement(func::Functionality)::AbstractRight end

function rights(u::User)::AbstractVector{CollectionOfRights}
u.id in [198170782285692928, 268802670553202699, 223188378235961347] ? BotAdmin() : OrdinaryUser()
end