Skip to content

Update helpbot to use a new Discord library and more #21

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

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
40 changes: 18 additions & 22 deletions autograder.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
package helpbot

import (
"context"
"time"
"crypto/tls"
"net/http"

agpb "github.com/autograde/quickfeed/ag"
"google.golang.org/grpc"
"github.com/bufbuild/connect-go"
"github.com/quickfeed/quickfeed/qf/qfconnect"
"github.com/quickfeed/quickfeed/web/interceptor"
"google.golang.org/grpc/metadata"
)

type Autograder struct {
cc *grpc.ClientConn
agpb.AutograderServiceClient
type QuickFeed struct {
qf qfconnect.QuickFeedServiceClient
md metadata.MD
}

func (s *Autograder) Close() {
s.cc.Close()
}

func NewAutograder(authToken string) (*Autograder, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
cc, err := grpc.DialContext(ctx, ":9090", grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
return nil, err
func NewQuickFeed(authToken string) (*QuickFeed, error) {
client := http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
ag := agpb.NewAutograderServiceClient(cc)
return &Autograder{
cc: cc,
AutograderServiceClient: ag,
md: metadata.New(map[string]string{"cookie": authToken}),
qf := qfconnect.NewQuickFeedServiceClient(&client, "https://uis.itest.run", connect.WithInterceptors(
interceptor.NewTokenAuthClientInterceptor(authToken),
))
return &QuickFeed{
qf: qf,
md: metadata.New(map[string]string{"Authorization": authToken}),
}, nil
}
27 changes: 0 additions & 27 deletions cmd/helpbot/config.go
Original file line number Diff line number Diff line change
@@ -1,28 +1 @@
package main

import (
"github.com/Raytar/helpbot"
"github.com/spf13/viper"
)

var cfg []helpbot.Config

func initConfig(cfgFile string) (err error) {
// command line

// env
viper.SetEnvPrefix(botName)
viper.AutomaticEnv()

// config file
viper.SetConfigName(cfgFile)
viper.SetConfigType("toml")
viper.AddConfigPath(".")
err = viper.ReadInConfig()
if err != nil {
return
}

err = viper.UnmarshalKey("instances", &cfg)
return
}
39 changes: 29 additions & 10 deletions cmd/helpbot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,24 @@ var log = &logrus.Logger{
Level: logrus.InfoLevel,
}

var (
ag *helpbot.Autograder
cfgFile string
)
var ag *helpbot.QuickFeed

func main() {
var cfgFile string
flag.StringVar(&cfgFile, "config", ".helpbotrc", "Path to configuration file")
flag.Parse()

err := initConfig(cfgFile)
if err != nil {
log.Fatalln("Failed to read config:", err)
log.Fatalln("Failed to init config:", err)
}

if viper.GetBool("autograder") {
authToken := os.Getenv("QUICKFEED_AUTH_TOKEN")
if viper.GetBool("quickfeed") {
authToken := viper.GetString("auth-token")
if authToken == "" {
log.Fatalln("QUICKFEED_AUTH_TOKEN is not set")
}
ag, err = helpbot.NewAutograder(authToken)
ag, err = helpbot.NewQuickFeed(authToken)
if err != nil {
log.Fatalln("Failed to init autograder:", err)
}
Expand All @@ -53,7 +51,7 @@ func main() {

var bots []*helpbot.HelpBot

for _, c := range cfg {
for _, c := range cfg {
bot, err := helpbot.New(c, log, ag)
if err != nil {
log.Fatalf("Failed to initialize bot: %v", err)
Expand All @@ -77,6 +75,27 @@ func main() {
b.Disconnect()
}
cancel()
ag.Close()
}
}

var cfg []helpbot.Config

func initConfig(cfgFile string) (err error) {
// command line

// env
viper.SetEnvPrefix(botName)
viper.AutomaticEnv()

// config file
viper.SetConfigName(cfgFile)
viper.SetConfigType("toml")
viper.AddConfigPath(".")
err = viper.ReadInConfig()
if err != nil {
return
}

err = viper.UnmarshalKey("instances", &cfg)
return
}
Loading