/// <summary> /// リポジトリ照会 /// </summary> /// <param name="channelId"></param> /// <param name="responseUrl"></param> /// <param name="teamId"></param> /// <param name="func"></param> /// <returns></returns> public static async Task GetRepository(string channelId, string responseUrl, string teamId, Func <string, Task> func) { // PartitionKeyがチャンネルIDのEntityを取得するクエリ var query = new TableQuery <ChannelIdEntity>() .Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, channelId)); // クエリ実行 var entityList = StorageOperation.GetTableIfNotExistsCreate("channel").ExecuteQuery(query); // クエリ実行結果で要素がひとつでもあるかどうか var channelIdEntities = entityList.ToList(); if (channelIdEntities.Any()) { await func(channelIdEntities.First().Repository); } else { var model = new PostMessageModel() { Channel = channelId, Text = "登録リポジトリが存在しません。", Response_type = "ephemeral" }; var slackApi = new SlackApi(); await slackApi.ExecutePostApiAsJson(model, responseUrl, teamId); } }
/// <summary> /// リポジトリ登録 /// </summary> /// <param name="data"></param> /// <returns></returns> public async Task SetRepository(SlackRequest.Active <RepositoryData> data) { // 保存するトークンを入れたentityを作成 var entity = new ChannelIdEntity(data.Channel.Id, data.Channel.Name, data.Submission.UserName + "/" + data.Submission.Repository); // Entityがなければ挿入、あれば更新する var insertResult = EntityOperationChannelId.InsertOrUpdateEntityResult(entity, "channel"); string text; // 結果があるかどうか if (insertResult != null) { text = "リポジトリの登録に成功しました。" + Environment.NewLine + "https://github.com/" + data.Submission.UserName + "/" + data.Submission.Repository; } else { text = "リポジトリの登録に失敗しました"; } var model = new PostMessageModel() { Channel = data.Channel.Id, Text = text, Response_type = "in_channel", Unfurl_links = true }; await SlackApi.ExecutePostApiAsJson(model, "https://slack.com/api/chat.postMessage", data.Team.Id); }
public async Task LoginGitHub(HttpRequestMessage request) { var content = await request.Content.ReadAsStringAsync(); var data = HttpUtility.ParseQueryString(content); // アクセストークン保存の為のユーザデータを格納 _userData = data; // =========================== // GitHub OauthURL取得 // =========================== var csrf = Membership.GeneratePassword(20, 0); var oauthRequest = new OauthLoginRequest(ConfigurationManager.AppSettings["client_id"]) { Scopes = { "repo", "user" }, State = csrf }; var url = _githubClient.Oauth.GetGitHubLoginUrl(oauthRequest).ToString(); // ============================== // Oauth用リダイレクトボタン作成 // ============================== var model = new PostMessageModel() { Channel = data["channel_id"], Text = "GitHubへログインしてください", Response_type = "ephemeral", Attachments = new List <Attachment>() { new Attachment() { Fallback = "The GitHub Oauth URL", Actions = new List <Action>() { new Action() { Type = "button", Name = "github_oauth_url", Text = "ログイン", Url = url, Style = "primary" } } } } }; await SlackApi.ExecutePostApiAsJson(model, data["response_url"], data["team_id"]); }
/// <summary> /// エラーハンドラー /// </summary> /// <param name="data"></param> /// <param name="cancel"></param> /// <returns></returns> public static async Task Handler(NameValueCollection data, Canceler cancel) { try { //なにかしらの処理 await cancel(); } //操作キャンセル時の処理 catch (OperationCanceledException) { } //ユーザ情報不正時の処理 catch (AuthorizationException) { var model = new PostMessageModel() { Channel = data["channel_id"], Text = "ユーザ情報が不正、もしくは存在しません。", Response_type = "ephemeral" }; await SlackApi.ExecutePostApiAsJson(model, data["response_url"], data["team_id"]); } //それ以外の例外処理 catch (Exception) { var model = new PostMessageModel() { Channel = data["channel_id"], Text = "予期せぬエラーが発生しました。", Response_type = "ephemeral" }; await SlackApi.ExecutePostApiAsJson(model, data["response_url"], data["team_id"]); } }
/// <summary> /// Issue作成 /// </summary> /// <param name="data"></param> /// <returns></returns> public async Task CreateIssue(SlackRequest.Active <IssueData> data) { // ============================= // Issueオブジェクト作成 // ============================= var newIssue = new NewIssue(data.Submission.Title) { Body = data.Submission.Body }; newIssue.Labels.Add(data.Submission.Label); // ============================= // GitHubアクセストークンの設定 // ============================= GitHubApi.SetCredential(data.User.Id); // ============================= // 登録リポジトリ取得 // ============================= await GetRepository(data.Channel.Id, data.Response_url, data.Team.Id, async repository => { // GitHub認証エラーハンドリング await AuthorizationExceptionHandler(data.Channel.Id, data.Response_url, data.Team.Id, async() => { // ============================= // Issue作成 // ============================= var issue = await GitHubApi.Client.Issue.Create(repository.Split('/')[0], repository.Split('/')[1], newIssue); var model = new PostMessageModel() { Channel = data.Channel.Id, Text = "Issueが登録されました" + Environment.NewLine + "https://github.com/" + repository + "/issues/" + issue.Number, Response_type = "in_channel", Unfurl_links = true }; await SlackApi.ExecutePostApiAsJson(model, data.Response_url, data.Team.Id); }); }); }
public async Task Operation(HttpRequestMessage request) { // =========================== // リクエストの取得・整形 // =========================== NameValueCollection data = await GetBody(request); // 引数の値 var method = data["text"]; // =========================== // 引数の値で処理を分ける // =========================== switch (method) { // =========================== // リポジトリ登録 // =========================== case "set": { // ==================================== // リポジトリ登録用ダイアログモデル作成 // ==================================== var model = CreateDialogModelForSetRespotory(data["trigger_id"]); // ============================= // ダイアログAPI実行 // ============================= await SlackApi.ExecutePostApiAsJson(model, "https://slack.com/api/dialog.open", data["team_id"]); break; } // =========================== // 登録リポジトリ照会 // =========================== case "get": { await GetRepository(data["channel_id"], data["response_url"], data["team_id"], async repository => { var model = new PostMessageModel() { Channel = data["channel_id"], Text = "登録リポジトリのURLを照会します" + Environment.NewLine + "https://github.com/" + repository, Response_type = "ephemeral" }; await SlackApi.ExecutePostApiAsJson(model, data["response_url"], data["team_id"]); }); break; } // =========================== // それ以外 // =========================== default: { var model = new PostMessageModel() { Channel = data["channel_id"], Text = "引数を入力してください", Response_type = "ephemeral" }; await SlackApi.ExecutePostApiAsJson(model, data["response_url"], data["team_id"]); break; } } }
public async Task Create(HttpRequestMessage request) { // =========================== // リクエストの取得・整形 // =========================== var data = await GetBody(request); // 引数の値 var method = data["text"]; // =========================== // 引数の値で処理を分ける // =========================== switch (method) { // =========================== // issue登録 // =========================== case "create": { // ============================= // GitHubアクセストークンの設定 // ============================= GitHubApi.SetCredential(data["user_id"]); // ============================= // 登録リポジトリ照会 // ============================= await GetRepository(data["channel_id"], data["response_url"], data["team_id"], async repository => { List <string> labelNameList = null; // GitHub認証エラーハンドリング await AuthorizationExceptionHandler(data["channel_id"], data["response_url"], data["team_id"], async() => { // クライアントを用いてリポジトリ名からIssueのラベルを取得 var labelList = await GitHubApi.Client.Issue.Labels.GetAllForRepository(repository.Split('/')[0], repository.Split('/')[1]); // ラベル変数リストを文字列リストに変換 labelNameList = labelList.ToList().ConvertAll(x => x.Name); }); // =============================== // Issue作成用ダイアログモデル作成 // =============================== var model = CreateDialogModelForCreateIssue(data["trigger_id"], labelNameList); // ============================= // ダイアログAPI実行 // ============================= await SlackApi.ExecutePostApiAsJson(model, "https://slack.com/api/dialog.open", data["team_id"]); }); break; } case "export": { List <Issue> issueList = null; // ============================= // 登録リポジトリ照会 // ============================= await GetRepository(data["channel_id"], data["response_url"], data["team_id"], async repository => { // GitHub認証エラーハンドリング await AuthorizationExceptionHandler(data["channel_id"], data["response_url"], data["team_id"], async() => { var issueROList = await GitHubApi.Client.Issue.GetAllForRepository( repository.Split('/')[0], repository.Split('/')[1]); issueList = issueROList.ToList(); }); }); var issueText = string.Empty; foreach (var issue in issueList) { var assigneesString = issue.Assignees.Any() ? string.Join(" ", issue.Assignees.Select(x => x.Login)) : ""; issueText += $"```\n***issueID***\n{issue.Number}\n***アサイン者***\n{assigneesString}\n***タイトル***\n{issue.Title}\n***本文***\n{issue.Body}\n***登録者?***{issue.User.Login}\n***状態***\n{issue.State.StringValue}\n```"; } var model = new PostMessageModel() { Channel = data["channel_id"], Text = issueText, Response_type = "ephemeral" }; await SlackApi.ExecutePostApiAsJson(model, data["response_url"], data["team_id"]); break; } // =========================== // それ以外 // =========================== default: { var model = new PostMessageModel() { Channel = data["channel_id"], Text = "引数を入力してください", Response_type = "ephemeral" }; await SlackApi.ExecutePostApiAsJson(model, data["response_url"], data["team_id"]); break; } } #endregion }