コード例 #1
0
        public override int HandleImpl()
        {
            using (var context = new GithubWatcherContext())
            {
                var query = context.GithubBindings.Where(p => p.QQ == fromQQ).OrderBy(p => p.GithubUserName);

                if (query.Count() == 0)
                {
                    Reply("您目前尚未绑定任何Github账户,输入“绑定Github账户”以进行绑定!");
                    return(0);
                }

                string message = "您绑定的Github账户有:";
                int    i       = 0;

                foreach (var account in query)
                {
                    i++;
                    message = message + $"\n{i}. " + account.GithubUserName;
                }
                Reply(message);
            }

            return(0);
        }
コード例 #2
0
 public static OAuthConfig GetOAuthConfig()
 {
     using (var context = new GithubWatcherContext()) 
     {
         return context.OAuthConfigs.FirstOrDefault();
     }
 }
コード例 #3
0
        public override int HandleImpl()
        {
            using (var context = new GithubWatcherContext())
            {
                var query = context.RepositorySubscriptions.Where(p => p.GroupQQ == fromGroup).OrderBy(p => p.RepositoryName);

                if (query.Count() == 0)
                {
                    Reply("该群尚未绑定任何仓库,管理员输入“绑定仓库#仓库名称#”以绑定仓库!");
                    return(0);
                }

                string message = "该群绑定的仓库有:";
                int    i       = 0;

                foreach (var subscription in query)
                {
                    i++;
                    message = message + $"\n{i}. " + subscription.RepositoryName;
                }
                Reply(message);
            }

            return(0);
        }
コード例 #4
0
        public override int HandleImpl()
        {
            string          pattern = @"解绑Github账户#(?<account>[\S]+)#";
            MatchCollection matches = Regex.Matches(message, pattern, RegexOptions.IgnoreCase);

            if (matches.Count == 1)
            {
                using (var context = new GithubWatcherContext())
                {
                    string account = "";
                    foreach (Match match in matches)
                    {
                        account = match.Groups["account"].Value;
                    }

                    var query = context.GithubBindings.FirstOrDefault(p => p.QQ == fromQQ && p.GithubUserName == account);
                    if (query == null)
                    {
                        Reply("抱歉,您尚未绑定该Github账户!");
                    }
                    else
                    {
                        context.GithubBindings.Remove(query);   // 删除绑定信息

                        // 删除仓库信息
                        var repositories = context.RepositoryInformations.Where(s => s.GithubUserName == account);
                        foreach (var repository in repositories)
                        {
                            context.RepositoryInformations.Remove(repository);

                            // 如果仓库已订阅,也一并删除
                            var subscription = context.RepositorySubscriptions.FirstOrDefault(s => s.RepositoryName == repository.Repository);
                            if (subscription != null)
                            {
                                var githubConnector = new GithubConnector();
                                githubConnector.DeleteWebhook(query.AccessToken, subscription.WebhookId, repository.Repository);  // 删除webhook

                                context.RepositorySubscriptions.Remove(subscription);
                            }
                        }

                        context.SaveChanges();
                        Reply("您已与Github账户" + account + "取消绑定!");
                    }
                }
            }
            else if (matches.Count == 0)
            {
                Reply("您想要与取消绑定哪个Github账户呢?可以输入“查询Github账户”查看您已绑定的Github账户!然后您可以通过输入“解绑Github账户#账户名称#”与Github账户取消绑定哦!");
            }
            else
            {
                Reply("抱歉,您一次只能够与一个Github账户取消绑定!输入“解绑Github账户#账户名称#”与Github账户取消绑定!");
            }

            return(0);
        }
コード例 #5
0
        public override int HandleImpl()
        {
            // 首先判断是群主或管理员
            var memberType = CQ.Api.GetGroupMemberInfo(Convert.ToInt64(fromGroup), Convert.ToInt64(fromQQ)).MemberType;

            if (!(memberType == QQGroupMemberType.Creator || memberType == QQGroupMemberType.Manage))
            {
                Reply("您不是群主或管理员,没有权限进行仓库操作!");
                return(0);
            }

            string          pattern = @"解绑仓库#(?<repository>[\S]+)#";
            MatchCollection matches = Regex.Matches(message, pattern, RegexOptions.IgnoreCase);

            if (matches.Count == 1)
            {
                using (var context = new GithubWatcherContext())
                {
                    string repository = "";
                    foreach (Match match in matches)
                    {
                        repository = match.Groups["repository"].Value;
                    }

                    var query = context.RepositorySubscriptions.FirstOrDefault(p => p.QQ == fromQQ && p.RepositoryName == repository && p.Type == "群组绑定" && p.GroupQQ == fromGroup);
                    if (query == null)
                    {
                        Reply("抱歉,该群尚未绑定该仓库!");
                    }
                    else
                    {
                        var githubConnector = new GithubConnector();

                        var githubBinding = context.GithubBindings.FirstOrDefault(s => s.QQ == fromQQ);

                        githubConnector.DeleteWebhook(githubBinding.AccessToken, query.WebhookId, repository); // 删除webhook
                        context.RepositorySubscriptions.Remove(query);                                         // 数据库中删除记录
                        context.SaveChanges();
                        Reply("仓库" + repository + "已与该群取消绑定!");
                    }
                }
            }
            else if (matches.Count == 0)
            {
                Reply("您想要与取消绑定哪个仓库呢?可以输入“查询仓库”查看您已绑定的仓库清单!然后您可以通过输入“解绑仓库#仓库名称#”与您不关注的仓库取消绑定哦!");
            }
            else
            {
                Reply("抱歉,您一次只能够与一个仓库取消绑定!输入“解绑仓库#仓库名称#”与您不关注的仓库取消绑定!");
            }

            return(0);
        }
コード例 #6
0
        public override int HandleImpl()
        {
            string          pattern = @"解绑仓库#(?<repository>[\S]+)#";
            MatchCollection matches = Regex.Matches(message, pattern, RegexOptions.IgnoreCase);

            if (matches.Count == 1)
            {
                using (var context = new GithubWatcherContext())
                {
                    string repository = "";
                    foreach (Match match in matches)
                    {
                        repository = match.Groups["repository"].Value;
                    }

                    var query = context.RepositorySubscriptions.FirstOrDefault(p => p.QQ == fromQQ && p.RepositoryName == repository);
                    if (query == null)
                    {
                        Reply("抱歉,您尚未绑定该仓库!");
                    }
                    else
                    {
                        var githubConnector = new GithubConnector();

                        var githubBinding = context.GithubBindings.FirstOrDefault(s => s.QQ == fromQQ);

                        githubConnector.DeleteWebhook(githubBinding.AccessToken, query.WebhookId, repository); // 删除webhook
                        context.RepositorySubscriptions.Remove(query);                                         // 数据库中删除记录
                        context.SaveChanges();
                        Reply("您已与仓库" + repository + "取消绑定!");
                    }
                }
            }
            else if (matches.Count == 0)
            {
                Reply("您想要与取消绑定哪个仓库呢?可以输入“查询仓库”查看您已绑定的仓库清单!然后您可以通过输入“解绑仓库#仓库名称#”与您不关注的仓库取消绑定哦!");
            }
            else
            {
                Reply("抱歉,您一次只能够与一个仓库取消绑定!输入“解绑仓库#仓库名称#”与您不关注的仓库取消绑定!");
            }

            return(0);
        }
コード例 #7
0
        public override int HandleImpl()
        {
            // 首先判断是群主或管理员
            var memberType = CQ.Api.GetGroupMemberInfo(Convert.ToInt64(fromGroup), Convert.ToInt64(fromQQ)).MemberType;

            if (!(memberType == QQGroupMemberType.Creator || memberType == QQGroupMemberType.Manage))
            {
                Reply("您不是群主或管理员,没有权限进行仓库操作!");
                return(0);
            }

            string          pattern = @"绑定仓库#(?<repository>[\S]+)#";
            MatchCollection matches = Regex.Matches(message, pattern, RegexOptions.IgnoreCase);

            try
            {
                // 输入合法,正则匹配到一个仓库名
                if (matches.Count == 1)
                {
                    using (var context = new GithubWatcherContext())
                    {
                        string repository = "";
                        foreach (Match match in matches)
                        {
                            repository = match.Groups["repository"].Value;
                        }

                        // 确认具有权限绑定的仓库
                        var authrizedRepositories = from p in context.GithubBindings
                                                    join q in context.RepositoryInformations
                                                    on p.GithubUserName equals q.GithubUserName
                                                    where p.QQ == fromQQ
                                                    select new { q.Repository, p.GithubUserName };

                        var authrizedRepo = authrizedRepositories.FirstOrDefault(s => s.Repository == repository);
                        if (authrizedRepo == null)
                        {
                            Reply("您没有权限绑定该仓库或该仓库不存在,请检查您输入的仓库信息。管理员在进行群组仓库绑定前,请先通过私聊向机器人进行Github账户绑定!");
                            return(0);
                        }

                        // 用户尝试绑定不属于自己的仓库
                        if (!authrizedRepo.Repository.StartsWith(authrizedRepo.GithubUserName))
                        {
                            Reply("为避免冲突,暂不允许您绑定此仓库!");
                            return(0);
                        }

                        var subscription =
                            context.RepositorySubscriptions.FirstOrDefault(s => s.RepositoryName == repository);
                        if (subscription == null) //确保表中不存在此项记录
                        {
                            // 先尝试添加webhook
                            var bindingInfo = context.GithubBindings.FirstOrDefault(s =>
                                                                                    s.QQ == fromQQ && s.GithubUserName == authrizedRepo.GithubUserName);

                            if (bindingInfo == null || bindingInfo.AccessToken == null)
                            {
                                Reply("抱歉,无法为该群绑定该仓库。");
                                return(0);
                            }

                            GithubConnector githubConnector = new GithubConnector();
                            var             webhookInfo     = githubConnector.CreateWebhook(bindingInfo.AccessToken, bindingInfo.GithubUserName, repository);

                            if (webhookInfo == null)
                            {
                                Reply("绑定失败,未能完成Webhook绑定!");
                                return(0);
                            }

                            RepositorySubscription newSubscription = new RepositorySubscription();
                            newSubscription.QQ             = fromQQ;
                            newSubscription.RepositoryName = repository;
                            newSubscription.WebhookId      = webhookInfo.Id;
                            newSubscription.WebhookName    = webhookInfo.Name;
                            newSubscription.Type           = "群组绑定";
                            newSubscription.GroupQQ        = fromGroup;

                            context.RepositorySubscriptions.Add(newSubscription);
                            context.SaveChanges();

                            Reply("仓库" + repository + "已与该群完成绑定!");
                        }
                        else
                        {
                            if (subscription.QQ == fromQQ)
                            {
                                Reply("该仓库已绑定至您的QQ,目前暂不支持同时进行私人绑定和群组绑定!");
                            }
                            else
                            {
                                Reply("抱歉,此仓库已被其他用户绑定。");
                            }
                        }
                    }
                }
                else if (matches.Count == 0)
                {
                    Reply("绑定Github仓库输入错误,请输入“绑定仓库#仓库名称#”以绑定仓库!");
                }
                else
                {
                    Reply("无法同时绑定多个仓库,请输入“绑定仓库#仓库名称#”以绑定仓库!");
                }
            }
            catch (Exception e)
            {
                Reply("绑定错误:" + e.Message);

                if (e.Message.Contains("远程服务器返回错误: (422) Unprocessable Entity"))
                {
                    Reply("这可能是由于该仓库中已存在相同Webhook造成的,请手动删除后重试!");
                }
                else if (e.Message.Contains("基础连接已经关闭: 发送时发生错误"))
                {
                    Reply("您的访问过于频繁,请稍后再试!");
                }

                return(0);
            }

            return(0);
        }
コード例 #8
0
        public IHttpActionResult Get(string code, string state)
        {
            bool   checkResult = false;
            string fromQQ      = Encryption.AesDecrypt(state);
            var    tokenModel  = githubConnector.AccessToken(code, ref checkResult); // 获取Access Token

            if (tokenModel != null)
            {
                if (!tokenModel.scope.Contains("repo") || tokenModel.scope == null)    // 用户手动更改了权限,向用户返回权限不足信息
                {
                    CQ.Api.SendPrivateMessage(Convert.ToInt64(fromQQ), "抱歉,您申请的权限不足,绑定失败!");
                    return(BadRequest("权限不足"));
                }

                // 调用Github API获取用户数据
                try
                {
                    GithubUserInfo userInfo = githubConnector.GetUserInfo(tokenModel.access_token);                      // 用户信息
                    List <GithubRepositoryInfo> repositories = githubConnector.GetRepositories(tokenModel.access_token); // 授权用户的所有仓库信息

                    using (var context = new GithubWatcherContext())
                    {
                        var user = context.GithubBindings.FirstOrDefault(s => s.GithubUserName == userInfo.Login);

                        // 如果不存在,则往数据库中添加信息
                        if (user == null)
                        {
                            GithubBinding newBinding = new GithubBinding();
                            newBinding.QQ             = fromQQ;
                            newBinding.GithubUserName = userInfo.Login;
                            newBinding.AccessToken    = tokenModel.access_token;

                            context.GithubBindings.Add(newBinding);

                            CQ.Api.SendPrivateMessage(Convert.ToInt64(fromQQ), "绑定Github账户" + userInfo.Login + "成功!");
                        }
                        else if (user.QQ == fromQQ)
                        {
                            if (user.AccessToken == tokenModel.access_token)
                            {
                                CQ.Api.SendPrivateMessage(Convert.ToInt64(fromQQ), "您已经绑定过该Github账户!");
                            }
                            else
                            {
                                // 更新accessToken
                                user.AccessToken = tokenModel.access_token;
                                CQ.Api.SendPrivateMessage(Convert.ToInt64(fromQQ), "您已经绑定过该Github账户,已为您刷新Access Token,请尽快完成仓库绑定操作。");
                            }
                        }
                        else
                        {
                            CQ.Api.SendPrivateMessage(Convert.ToInt64(fromQQ), "抱歉,该Github账户已被其他用户绑定!");
                        }


                        foreach (var repository in repositories)
                        {
                            var query = context.RepositoryInformations.FirstOrDefault(s => s.GithubUserName == userInfo.Login && s.Repository == repository.FullName);

                            // 如果不存在,则往数据库中添加信息
                            if (query == null)
                            {
                                RepositoryInformation newRepositoryInfo = new RepositoryInformation();
                                newRepositoryInfo.GithubUserName = userInfo.Login;
                                newRepositoryInfo.Repository     = repository.FullName;

                                context.RepositoryInformations.Add(newRepositoryInfo);
                            }
                        }

                        context.SaveChanges();
                        return(Ok("绑定成功!"));
                    }
                }
                catch (Exception e)
                {
                    if (e.Message.Contains("基础连接已经关闭: 发送时发生错误"))
                    {
                        CQ.Api.SendPrivateMessage(Convert.ToInt64(fromQQ), "您的访问过于频繁,请稍后再试!");
                    }
                    else
                    {
                        CQ.Api.SendPrivateMessage(Convert.ToInt64(fromQQ), "错误:" + e.Message + "请联系管理员QQ:2426837192!");
                    }
                    return(BadRequest(e.Message));
                }
            }

            return(BadRequest("获取Access Token失败!"));
        }
コード例 #9
0
        // POST: api/GithubWatcher
        public IHttpActionResult Post()
        {
            /* We need the raw body to validate the request
             * by computing a HMAC hash from it based upon
             * the secret key. We then manually deserialise
             * it for validation and further manipulation.
             */

            // Body
            string body = Request.Content.ReadAsStringAsync().Result;

            string eventType, signature, delivery = "";

            // Head
            if (Request.Headers.TryGetValues("X-GitHub-Event", out var eventTypeHeader))
            {
                eventType = eventTypeHeader.FirstOrDefault();
            }
            else
            {
                return(BadRequest("Request中应附有事件类型信息!"));
            }

            if (Request.Headers.TryGetValues("X-Hub-Signature", out var signatureHeader))
            {
                signature = signatureHeader.FirstOrDefault();
            }

            if (Request.Headers.TryGetValues("X-GitHub-Delivery", out var deliveryHeader))
            {
                delivery = deliveryHeader.FirstOrDefault();
            }
            else
            {
                return(BadRequest("Request中应附有GUID!"));
            }

            //bool isValidRequest = this.requestValidator.IsValidRequest(signature, "312725802", body);

            //if (!isValidRequest) {
            //    return this.CreateUnauthorisedResult();
            //}

            if (!IsSupportEvent(eventType))
            {
                return(BadRequest("不支持的事件类型!"));
            }

            Payload       payload          = this.jsonSerialiser.Deserialise <Payload>(body); // 将body反序列化
            PayloadRecord newPayloadRecord = GenerateRecord(payload, eventType, delivery);    // 生成一条Payload Record

            using (var context = new GithubWatcherContext())
            {
                var payloadRecord = context.PayloadRecords.SingleOrDefault(s => s.DeliveryID == newPayloadRecord.DeliveryID);
                if (payloadRecord == null)      //确保表中不存在此项记录
                {
                    context.PayloadRecords.Add(newPayloadRecord);
                    context.SaveChanges();
                }
                else
                {
                    return(BadRequest("此记录已发送,不可重复发送!"));
                }

                string msg = GenerateMessage(payload, eventType);

                var user = context.RepositorySubscriptions.FirstOrDefault(s => s.RepositoryName == payload.Repository.FullName);

                if (user != null)
                {
                    if (user.Type == "私人绑定")
                    {
                        CQ.Api.SendPrivateMessage(Convert.ToInt64(user.QQ), msg);
                    }
                    else if (user.Type == "群组绑定")
                    {
                        CQ.Api.SendGroupMessage(Convert.ToInt64(user.GroupQQ), msg);
                    }
                }

                return(Ok(msg));
            }
        }