This repository has been archived by the owner on Jun 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from opq-osc/beta
Beta
- Loading branch information
Showing
5 changed files
with
342 additions
and
39 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
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,167 @@ | ||
package githubManager | ||
|
||
import ( | ||
"OPQBot-QQGroupManager/Config" | ||
"errors" | ||
"fmt" | ||
"github.com/go-playground/webhooks/v6/github" | ||
"github.com/kataras/iris/v12" | ||
"github.com/mcoo/OPQBot" | ||
"github.com/mcoo/requests" | ||
"log" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
type Manager struct { | ||
github map[string]Config.Repo | ||
githubLock sync.RWMutex | ||
b *OPQBot.BotManager | ||
} | ||
|
||
func (m *Manager) DelRepo(repo string, groupId int64) error { | ||
m.githubLock.Lock() | ||
defer m.githubLock.Unlock() | ||
if v, ok := m.github[repo]; ok { | ||
for i, v1 := range v.Groups { | ||
if v1 == groupId { | ||
v.Groups = append(v.Groups[:i], v.Groups[i+1:]...) | ||
m.github[repo] = v | ||
if len(v.Groups) == 0 { | ||
delete(m.github, repo) | ||
} | ||
break | ||
} | ||
} | ||
m.Save() | ||
return nil | ||
|
||
} else { | ||
return errors.New("订阅不存在!无法删除 ") | ||
} | ||
} | ||
func (m *Manager) AddRepo(repo string, Secret string, groupId int64) error { | ||
m.githubLock.Lock() | ||
defer m.githubLock.Unlock() | ||
|
||
if v, ok := m.github[repo]; ok { | ||
if v.Secret != Secret { | ||
v.Secret = Secret | ||
hook, _ := github.New(github.Options.Secret(Secret)) | ||
v.WebHook = hook | ||
} | ||
for _, v1 := range v.Groups { | ||
if v1 == groupId { | ||
m.Save() | ||
return errors.New("已经订阅过了") | ||
} | ||
} | ||
v.Groups = append(v.Groups, groupId) | ||
m.github[repo] = v | ||
} else { | ||
if Secret == "" { | ||
return errors.New("需要Secret, 请直接私聊我发送Secret") | ||
} | ||
hook, _ := github.New(github.Options.Secret(Secret)) | ||
m.github[repo] = Config.Repo{ | ||
WebHook: hook, | ||
Groups: []int64{groupId}, | ||
Secret: Secret, | ||
} | ||
} | ||
err := m.Save() | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func NewManager(app *iris.Application, bot *OPQBot.BotManager) Manager { | ||
Config.Lock.RLock() | ||
defer Config.Lock.RUnlock() | ||
g := map[string]Config.Repo{} | ||
for k, v := range Config.CoreConfig.GithubSub { | ||
hook, _ := github.New(github.Options.Secret(v.Secret)) | ||
v.WebHook = hook | ||
g[k] = v | ||
} | ||
m := Manager{github: g, githubLock: sync.RWMutex{}, b: bot} | ||
app.Any("github/webhook/{root:path}", func(ctx iris.Context) { | ||
h, err := m.GetRepo(ctx.Params().GetString("root")) | ||
if err != nil { | ||
ctx.StatusCode(404) | ||
return | ||
} | ||
payload, err := h.WebHook.Parse((*ctx).Request(), github.PushEvent, github.PingEvent, github.ReleaseEvent, github.PullRequestEvent) | ||
if err != nil { | ||
log.Println(err) | ||
if err == github.ErrEventNotFound { | ||
ctx.StatusCode(404) | ||
return | ||
} | ||
if err == github.ErrHMACVerificationFailed { | ||
ctx.StatusCode(502) | ||
return | ||
} | ||
} | ||
log.Println(payload) | ||
switch v := payload.(type) { | ||
case github.PingPayload: | ||
log.Println(v) | ||
case github.PushPayload: | ||
var commitString []string | ||
for _, v1 := range v.Commits { | ||
commitString = append(commitString, fmt.Sprintf("[%s] %s", v1.Timestamp, v1.Message)) | ||
} | ||
r, _ := requests.Get(v.Sender.AvatarURL) | ||
for _, v1 := range h.Groups { | ||
m.b.SendGroupPicMsg(v1, fmt.Sprintf("%s\nCommit:\n%s", v.Repository.FullName, strings.Join(commitString, "\n")), r.Content()) | ||
} | ||
case github.ReleasePayload: | ||
r, _ := requests.Get(v.Sender.AvatarURL) | ||
for _, v1 := range h.Groups { | ||
m.b.SendGroupPicMsg(v1, fmt.Sprintf("%s\n发布了:\n%s", v.Repository.FullName, v.Release.TagName), r.Content()) | ||
|
||
} | ||
case github.PullRequestPayload: | ||
for _, v1 := range h.Groups { | ||
m.b.SendGroupTextMsg(v1, fmt.Sprintf("%s\nPR: %s", v.Repository.FullName, v.PullRequest.Body)) | ||
} | ||
|
||
} | ||
}) | ||
return m | ||
} | ||
func (m *Manager) GetGroupSubList(groupId int64) (r map[string]Config.Repo) { | ||
m.githubLock.RLock() | ||
defer m.githubLock.RUnlock() | ||
r = map[string]Config.Repo{} | ||
for k, v := range m.github { | ||
for _, v1 := range v.Groups { | ||
if v1 == groupId { | ||
r[k] = v | ||
} | ||
} | ||
} | ||
return | ||
} | ||
|
||
func (m *Manager) GetRepo(repo string) (r Config.Repo, err error) { | ||
m.githubLock.RLock() | ||
defer m.githubLock.RUnlock() | ||
var ok bool | ||
if r, ok = m.github[repo]; ok { | ||
return | ||
} else { | ||
err = errors.New("没有订阅该Repo") | ||
return | ||
} | ||
|
||
} | ||
|
||
func (m *Manager) Save() error { | ||
Config.Lock.Lock() | ||
defer Config.Lock.Unlock() | ||
Config.CoreConfig.GithubSub = m.github | ||
return Config.Save() | ||
} |
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
Oops, something went wrong.