コード例 #1
0
ファイル: reply_test.go プロジェクト: orktes/captainhub
func ExampleString() {
	c, err := dial()
	if err != nil {
		panic(err)
	}
	defer c.Close()

	c.Do("SET", "hello", "world")
	s, err := redis.String(c.Do("GET", "hello"))
	fmt.Printf("%#v\n", s)
	// Output:
	// "world"
}
コード例 #2
0
// This example implements ZPOP as described at
// http://redis.io/topics/transactions using WATCH/MULTI/EXEC and scripting.
func Example_zpop() {
	c, err := dial()
	if err != nil {
		fmt.Println(err)
		return
	}
	defer c.Close()

	// Add test data using a pipeline.

	for i, member := range []string{"red", "blue", "green"} {
		c.Send("ZADD", "zset", i, member)
	}
	if _, err := c.Do(""); err != nil {
		fmt.Println(err)
		return
	}

	// Pop using WATCH/MULTI/EXEC

	v, err := zpop(c, "zset")
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(v)

	// Pop using a script.

	v, err = redis.String(zpopScript.Do(c, "zset"))
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(v)

	// Output:
	// red
	// blue
}
コード例 #3
0
ファイル: conn_test.go プロジェクト: orktes/captainhub
func TestDialURL(t *testing.T) {
	for _, d := range dialErrors {
		_, err := redis.DialURL(d.rawurl)
		if err == nil || !strings.Contains(err.Error(), d.expectedError) {
			t.Errorf("DialURL did not return expected error (expected %v to contain %s)", err, d.expectedError)
		}
	}

	checkPort := func(network, address string) (net.Conn, error) {
		if address != "localhost:6379" {
			t.Errorf("DialURL did not set port to 6379 by default (got %v)", address)
		}
		return net.Dial(network, address)
	}
	c, err := redis.DialURL("redis://localhost", redis.DialNetDial(checkPort))
	if err != nil {
		t.Error("dial error:", err)
	}
	c.Close()

	checkHost := func(network, address string) (net.Conn, error) {
		if address != "localhost:6379" {
			t.Errorf("DialURL did not set host to localhost by default (got %v)", address)
		}
		return net.Dial(network, address)
	}
	c, err = redis.DialURL("redis://:6379", redis.DialNetDial(checkHost))
	if err != nil {
		t.Error("dial error:", err)
	}
	c.Close()

	// Check that the database is set correctly
	c1, err := redis.DialURL("redis://:6379/8")
	defer c1.Close()
	if err != nil {
		t.Error("Dial error:", err)
	}
	dbSize, _ := redis.Int(c1.Do("DBSIZE"))
	if dbSize > 0 {
		t.Fatal("DB 8 has existing keys; aborting test to avoid overwriting data")
	}
	c1.Do("SET", "var", "val")

	c2, err := redis.Dial("tcp", ":6379")
	defer c2.Close()
	if err != nil {
		t.Error("dial error:", err)
	}
	_, err = c2.Do("SELECT", "8")
	if err != nil {
		t.Error(err)
	}
	got, err := redis.String(c2.Do("GET", "var"))
	if err != nil {
		t.Error(err)
	}
	if got != "val" {
		t.Error("DialURL did not correctly set the db.")
	}
	_, err = c2.Do("DEL", "var")
}
コード例 #4
0
ファイル: main.go プロジェクト: orktes/captainhub
// Handler
func payload(c *echo.Context) error {

	var owner string
	var repo string

	r := c.Request()

	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return err
	}

	if hookSecret != "" {
		sig := r.Header.Get("X-Hub-Signature")
		if sig == "" {
			return errMissingSig
		}

		mac := hmac.New(sha1.New, []byte(hookSecret))
		mac.Write(body)
		expectedMAC := mac.Sum(nil)
		expectedSig := "sha1=" + hex.EncodeToString(expectedMAC)
		if !hmac.Equal([]byte(expectedSig), []byte(sig)) {
			return errInvalidSig
		}
	}

	eventType := r.Header.Get("X-Github-Event")

	data := map[string]interface{}{}
	if err := json.Unmarshal(body, &data); err != nil {
		fmt.Printf("%s", err.Error())
		return err
	}

	repository := data["repository"]

	repoMap, ok := repository.(map[string]interface{})
	if !ok {
		fmt.Printf("Repository missing")
		return nil
	}

	repo = repoMap["name"].(string)

	ownerMap, ok := repoMap["owner"].(map[string]interface{})
	if !ok {
		fmt.Printf("Owner missing")
		return nil
	}

	if _, ok := ownerMap["login"]; ok {
		owner = ownerMap["login"].(string)
	} else if _, ok := ownerMap["name"]; ok {
		owner = ownerMap["name"].(string)
	}

	fmt.Printf("Event: %s, Owner: %s, Repo %s\n", eventType, owner, repo)

	cfg, err := getCaptainConfig(owner, repo)

	fmt.Printf("%s/%s config: %+v\n", owner, repo, cfg)

	if err != nil {
		return err
	}

	for _, plugin := range cfg.Plugins {
		pluginData, err := getCaptainPlugin(owner, repo, plugin.Name)

		if err != nil {
			// TODO set info about this
			fmt.Printf("%s", err.Error())
			continue
		}

		vm := otto.New()
		vm.Run(`
			var global = {};
			_moduleCache = {};
			function require(moduleName) {
				if (!_moduleCache[moduleName]) {
					_require(moduleName);
				}

				return _moduleCache[moduleName];
			}
		`)
		vm.Set("eventType", eventType)
		vm.Set("eventData", data)
		vm.Set("config", plugin.Config)
		vm.Set("matchFilePath", matchFilePath)
		vm.Set("_require", func(call otto.FunctionCall) otto.Value {
			moduleFilename, err := call.Argument(0).ToString()
			if err != nil {
				panic(err)
			}

			content, err := getCaptainPlugin(owner, repo, moduleFilename)
			if err != nil {
				panic(err)
			}
			script := `
				(function (_moduleCache, global) {
				  var module = {exports: {}};
					(function (require, module, exports, undefined) {
						` + string(content) + `
					})(require, module, module.exports);
					_moduleCache["` + moduleFilename + `"] = module.exports;
				})(_moduleCache, global);
			`
			_, err = vm.Run(script)
			if err != nil {
				fmt.Printf("Error evaluating %s %s %s\n", moduleFilename, script, err.Error())
			}

			return otto.Value{}
		})

		vm.Set("print", func(call otto.FunctionCall) otto.Value {
			text, err := call.Argument(0).ToString()
			if err != nil {
				panic(err)
			}

			println(text)
			return otto.Value{}
		})

		vm.Set("getPullRequestFileContent", func(call otto.FunctionCall) otto.Value {
			prNumber, err := call.Argument(0).ToInteger()
			if err != nil {
				panic(err)
			}

			fileName, err := call.Argument(1).ToString()
			if err != nil {
				panic(err)
			}

			content, err := readPullRequestFileContent(owner, repo, int(prNumber), fileName)
			if err != nil {
				panic(err)
			}

			ottoValue, err := otto.ToValue(string(content))
			if err != nil {
				panic(err)
			}

			return ottoValue
		})

		vm.Set("getPullRequestDetails", func(call otto.FunctionCall) otto.Value {
			prNumber, err := call.Argument(0).ToInteger()
			if err != nil {
				panic(err)
			}

			pr, err := getPullRequestDetails(owner, repo, int(prNumber))

			data, err := json.Marshal(pr)
			if err != nil {
				panic(err)
			}

			val, err := vm.Run(fmt.Sprintf("(%s)", string(data)))
			if err != nil {
				panic(err)
			}

			return val
		})

		vm.Set("getPullRequestFiles", func(call otto.FunctionCall) otto.Value {
			prNumber, err := call.Argument(0).ToInteger()
			if err != nil {
				panic(err)
			}

			files, err := listPullRequestFiles(owner, repo, int(prNumber))
			if err != nil {
				panic(err)
			}

			data, err := json.Marshal(files)
			if err != nil {
				panic(err)
			}

			val, err := vm.Run(string(data))
			if err != nil {
				panic(err)
			}

			return val
		})

		vm.Set("createPullRequestComment", func(call otto.FunctionCall) otto.Value {
			prNumber, err := call.Argument(0).ToInteger()
			if err != nil {
				panic(err)
			}

			body := call.Argument(1).String()
			if err != nil {
				panic(err)
			}

			err = createPullRequestComment(owner, repo, int(prNumber), body)
			if err != nil {
				panic(err)
			}

			return otto.Value{}
		})

		vm.Set("createIssueComment", func(call otto.FunctionCall) otto.Value {
			prNumber, err := call.Argument(0).ToInteger()
			if err != nil {
				panic(err)
			}

			body := call.Argument(1).String()
			if err != nil {
				panic(err)
			}

			err = createIssueComment(owner, repo, int(prNumber), body)
			if err != nil {
				panic(err)
			}

			return otto.Value{}
		})

		vm.Set("createStatus", func(call otto.FunctionCall) otto.Value {
			sha, err := call.Argument(0).ToString()
			if err != nil {
				panic(err)
			}

			state, err := call.Argument(1).ToString()
			if err != nil {
				panic(err)
			}

			targetURL, err := call.Argument(2).ToString()
			if err != nil {
				panic(err)
			}

			description, err := call.Argument(3).ToString()
			if err != nil {
				panic(err)
			}

			context, err := call.Argument(4).ToString()
			if err != nil {
				panic(err)
			}

			err = createStatus(owner, repo, sha, state, targetURL, description, context)
			if err != nil {
				panic(err)
			}

			return otto.Value{}
		})

		vm.Set("saveData", func(call otto.FunctionCall) otto.Value {
			redisClient := redisPool.Get()
			defer redisClient.Close()

			key := call.Argument(0).String()
			data := call.Argument(1).String()

			storeKey := fmt.Sprintf("%s/%s/%s", owner, repo, key)

			_, err := redisClient.Do("SET", storeKey, data)
			if err != nil {
				panic(err)
			}

			return otto.Value{}
		})

		vm.Set("loadData", func(call otto.FunctionCall) otto.Value {
			redisClient := redisPool.Get()
			defer redisClient.Close()

			key := call.Argument(0).String()
			storeKey := fmt.Sprintf("%s/%s/%s", owner, repo, key)

			s, err := redis.String(redisClient.Do("GET", storeKey))
			if err != nil {
				panic(err)
			}

			ottoValue, err := otto.ToValue(s)
			if err != nil {
				panic(err)
			}

			return ottoValue
		})

		_, err = vm.Run(`
			var module = {exports: {}};
				(function (require, module, exports, global, undefined) {
					` + string(pluginData) + `
				})(require, module, module.exports, global);
			`)
		if err != nil {
			fmt.Printf("plugin eval error: %s %s\n", err.Error(), pluginData)
			return err
		}
	}

	return nil
}