コード例 #1
0
        public override int HandleImpl()
        {
            GithubConnector githubConnector = new GithubConnector();

            Reply("请点击下方链接以绑定Github账户\n" + githubConnector.Authorize(fromQQ));
            return(0);
        }
コード例 #2
0
        public override object ExecuteAction(BaseActionItem actionItem)
        {
            GithubConnector connector = (GithubConnector)this.RootNode.NodeConnector;

            switch (actionItem.Action)
            {
            case NodeActions.Open:
                OpenActionResult oar = new OpenActionResult();
                oar.Source = connector.GetSource(this.Url);
                var els = this.Url.Split('.');
                oar.Language = els[els.Length - 1];
                return(oar);

            case NodeActions.Save:

                return(connector.GetSource(this.Url));

            case NodeActions.Copy:
                if (actionItem.Name == "Copy link")
                {
                    return(this._file.html_url);
                }
                else
                {
                    return(null);
                }


            default:
                return(null);
            }
        }
コード例 #3
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);
        }
コード例 #4
0
 public GithubWatcherController(
     IJsonSerialiser jsonSerialiser,
     IRequestValidator requestValidator,
     GithubConnector githubConnector
     )
 {
     this.jsonSerialiser   = jsonSerialiser;
     this.requestValidator = requestValidator;
     this.githubConnector  = githubConnector;
 }
コード例 #5
0
        public override object GetDefaultSource()
        {
            GithubConnector  connector = (GithubConnector)this.RootNode.NodeConnector;
            OpenActionResult oar       = new OpenActionResult();

            oar.Source = connector.GetSource(this.Url);
            var els = this.Url.Split('.');

            oar.Language = els[els.Length - 1];
            return(oar);
        }
コード例 #6
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);
        }
コード例 #7
0
        /// <summary>
        /// Add a github user to a repository
        /// </summary>
        /// <param name="repoName">repositoy name</param>
        /// <param name="username">github username</param>
        /// <returns></returns>
        public AddMemberToRepo AddMemberToRepoRepo(string repoName, string username)
        {
            ///repos/{org-name}/{repo-name}/collaborators/{username}
            string  orgName = Settings.GetSetting("hackathon.OrganizationName");
            string  apiUrl  = Settings.GetSetting("hackathon.GithubAddUserToRepoUrl").Replace("{org-name}", orgName).Replace("{repo-name}", repoName).Replace("{username}", username);
            dynamic body    = new JObject();

            body.permission = "admin";

            AddMemberToRepo response = GithubConnector.ExecuteRequest <AddMemberToRepo>(HttpMethod.Put, apiUrl, body, "201 Created");

            return(response);
        }
コード例 #8
0
        /// <summary>
        /// Create team inside Github Organization
        /// </summary>
        /// <param name="teamName">Your team name</param>
        /// <param name="teamDescription">your team description</param>
        /// <returns></returns>
        public CreateOrganizationTeamResponse CreateOrganizationTeam(string teamName, string teamDescription)
        {
            string  orgName = Settings.GetSetting("hackathon.OrganizationName");
            string  apiUrl  = Settings.GetSetting("hackathon.GithubCreateOrganizationTeamUrl").Replace("{org-name}", orgName);
            dynamic body    = new JObject();

            body.name        = teamName;
            body.description = teamDescription;
            body.permission  = "admin";
            body.privacy     = "closed";

            CreateOrganizationTeamResponse response = GithubConnector.ExecuteRequest <CreateOrganizationTeamResponse>(HttpMethod.Post, apiUrl, body, "201 Created");

            return(response);
        }
コード例 #9
0
        /// <summary>
        /// Create a github reporsitory and assign it to a team from the organization
        /// </summary>
        /// <param name="teamId">team ID, this is stored in sitecore team item</param>
        /// <param name="repoName">repositoy name</param>
        /// <returns></returns>
        public CreateTeamRepo CreateOrganizationTeamRepo(string teamId, string repoName)
        {
            ///orgs/{org-name}/repos
            string  orgName = Settings.GetSetting("hackathon.OrganizationName");
            string  apiUrl  = Settings.GetSetting("hackathon.GithubCreateOrganizationTeamRepoUrl").Replace("{org-name}", orgName);
            dynamic body    = new JObject();

            body.name       = repoName;
            body.team_id    = teamId;
            body.Private    = false;
            body.visibility = "public";


            CreateTeamRepo response = GithubConnector.ExecuteRequest <CreateTeamRepo>(HttpMethod.Post, apiUrl, body, "201 Created");

            return(response);
        }
コード例 #10
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);
        }
コード例 #11
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);
        }
コード例 #12
0
 public GithubWatcherController()
 {
     jsonSerialiser   = new JsonSerialiser();
     requestValidator = new RequestValidator();
     githubConnector  = new GithubConnector();
 }