Browse Source

Add Multi-Repo support

master
parent
commit
783b4f8c2e
No known key found for this signature in database GPG Key ID: DA34C790D267C164
4 changed files with 30 additions and 13 deletions
  1. +5
    -1
      README.md
  2. +21
    -8
      src/TildesAutolinkerBot.cr
  3. +2
    -2
      src/issue.cr
  4. +2
    -2
      src/mr.cr

+ 5
- 1
README.md View File

@@ -8,7 +8,11 @@ Add a .env file containing
```
discord_token = your-bot-token
gl_token = your-personal-access-token
gl_project = your-project-id
defaultrepo = your-project-id

additionalrepo = another-project-id
yetanotherrepo = project-id-thing
seeabove = la-id-du-project
```
After that, plain old `shards build` and run.



+ 21
- 8
src/TildesAutolinkerBot.cr View File

@@ -4,20 +4,22 @@ require "discordcr"
require "./issue"
require "./mr"

Dotenv.load!
client = Discord::Client.new(token: "Bot #{ENV["discord_token"]}")
DOTENV = Dotenv.load!
client = Discord::Client.new(token: "Bot #{DOTENV["discord_token"]}")

bot_id = client.get_current_user.id
bot_name = client.get_current_user.username

issue_regex = /#([0-9]+)/
mr_regex = /!([0-9]+)/
issue_regex = /([\w\/]*)#([0-9]+)/
mr_regex = /([\w\/]*)!([0-9]+)/
help_regex = Regex.new("<@!?#{bot_id}> help")

help_string = <<-STR
Accepted formats:
`#1234` Link Tildes Issue.
`!1234` Link Tildes Merge Request.
Prefix with a repo name to retrieve items from it instead of the default one.
Enabled Repositories: `#{DOTENV.keys.reject("discord_token").reject("gl_token")}`

Hacked together by <@344166495317655562> — [Source](https://git.dingenskirchen.systems/deing/TildesAutolinkerBot) — `@#{bot_name} help` to see this message
STR
@@ -36,10 +38,15 @@ client.on_message_create do |message|
issues = message.content.scan issue_regex
issues.each do |issue|
begin
output += get_issue(issue[1]).formatify + "\n"
repo = if issue[1].size == 0
DOTENV["defaultrepo"]
else
DOTENV[issue[1]]
end
output += get_issue(repo, issue[2]).formatify + "\n"
rescue e
pp e
output += "##{issue[1]} `#{e}`\n"
output += "##{issue[2]} `#{e}`\n"
end
end

@@ -47,11 +54,17 @@ client.on_message_create do |message|
merges.each do |mr|
# Avoid reacting to pings with ! in them
next if mr[2].size > 5
pp mr[0]
begin
output += get_mr(mr[1]).formatify + "\n"
repo = if mr[1].size == 0
DOTENV["defaultrepo"]
else
DOTENV[mr[1]]
end
output += get_mr(repo, mr[2]).formatify + "\n"
rescue e
pp e
output += "!#{mr[1]} `#{e}`\n"
output += "!#{mr[2]} `#{e}`\n"
end
end



+ 2
- 2
src/issue.cr View File

@@ -20,8 +20,8 @@ struct Issue
end
end

def get_issue(issue_id)
resp = HTTP::Client.get("https://gitlab.com/api/v4/projects/#{ENV["gl_project"]}/issues/#{issue_id}", HTTP::Headers{"PRIVATE-TOKEN" => ENV["gl_token"]})
def get_issue(repo_id, issue_id)
resp = HTTP::Client.get("https://gitlab.com/api/v4/projects/#{repo_id}/issues/#{issue_id}", HTTP::Headers{"PRIVATE-TOKEN" => ENV["gl_token"]})
raise "Negative API Response: #{resp.body}" unless resp.success?
Issue.from_json(resp.body)
end

+ 2
- 2
src/mr.cr View File

@@ -20,8 +20,8 @@ struct MergeRequest
end
end

def get_mr(issue_id)
resp = HTTP::Client.get("https://gitlab.com/api/v4/projects/#{ENV["gl_project"]}/merge_requests/#{issue_id}", HTTP::Headers{"PRIVATE-TOKEN" => ENV["gl_token"]})
def get_mr(repo_id, issue_id)
resp = HTTP::Client.get("https://gitlab.com/api/v4/projects/#{repo_id}/merge_requests/#{issue_id}", HTTP::Headers{"PRIVATE-TOKEN" => ENV["gl_token"]})
raise "Negative API Response: #{resp.body}" unless resp.success?
MergeRequest.from_json(resp.body)
end

Loading…
Cancel
Save