1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
mod comment;
pub mod issue;
mod owner;
mod repo;
mod user;
mod pull_request;
mod review;
pub mod event;
mod sender;
use crate::reg;
use crate::config_env;
use serde::{Deserialize, Serialize};
use anyhow::Result;

pub fn get_work_product(s: &str) -> Option<String> {
  let pat = reg!(r"(?P<item_id>((DE)|(US))\d{4,8}(\s*,\s*((DE)|(US))\d{4,8})*);");
  if let Some(m) = pat.captures(s) {
      if let Some(n) = m.name("item_id") {
          return Some(n.as_str().to_string());
      }
  }
  None
}
#[derive(Debug, Deserialize, Serialize)]
pub struct PostComment<'a> {
    body: &'a str,
}
pub async fn post_issue_comment(repo_name: &str, pr_number: u64, s: &str) -> Result<()> {
  let comment_url = format!(
      "https://github.corpname.com/api/v3/repos/{}/issues/{}/comments",
      repo_name, pr_number
  );

  let client = reqwest::Client::new();
  let data = &PostComment { body: s };

  match client
      .post(comment_url)
      .header(
          "Authorization",
          format!("token {}", config_env::get_github_token()),
      )
      .header("Accept", "application/vnd.github+json")
      .json(data)
      .send()
      .await
  {
      Ok(_) => {
          println!("{:?}", data);
          Ok(())
      }
      Err(e) => Err(anyhow::anyhow!(format!("post comment failed {}", e))),
  }
}


#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn extract_us() {
        assert_eq!(get_work_product("DE123455; hihkdfd"), Some("DE123455".to_string()));
    }


    #[test]
    fn test_parse() {
        let pay_load = r#"{
            "action": "created",
            "issue": {
              "url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/issues/30",
              "repository_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts",
              "labels_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/issues/30/labels{/name}",
              "comments_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/issues/30/comments",
              "events_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/issues/30/events",
              "html_url": "https://github.corpname.com/evanl00/Scripts/issues/30",
              "id": 275014,
              "node_id": "dd=",
              "number": 30,
              "title": "test rabot",
              "user": {
                "login": "evanl00",
                "id": 869,
                "node_id": "fdfdd==",
                "avatar_url": "https://github.corpname.com/avatars/u/869?",
                "gravatar_id": "",
                "url": "https://github.corpname.com/api/v3/users/evanl00",
                "html_url": "https://github.corpname.com/evanl00",
                "followers_url": "https://github.corpname.com/api/v3/users/evanl00/followers",
                "following_url": "https://github.corpname.com/api/v3/users/evanl00/following{/other_user}",
                "gists_url": "https://github.corpname.com/api/v3/users/evanl00/gists{/gist_id}",
                "starred_url": "https://github.corpname.com/api/v3/users/evanl00/starred{/owner}{/repo}",
                "subscriptions_url": "https://github.corpname.com/api/v3/users/evanl00/subscriptions",
                "organizations_url": "https://github.corpname.com/api/v3/users/evanl00/orgs",
                "repos_url": "https://github.corpname.com/api/v3/users/evanl00/repos",
                "events_url": "https://github.corpname.com/api/v3/users/evanl00/events{/privacy}",
                "received_events_url": "https://github.corpname.com/api/v3/users/evanl00/received_events",
                "type": "User",
                "site_admin": false,
                "ldap_dn": "CN=Lu\\, Evan,OU=Users Hangzhou,OU=China,OU=TPC,OU=CorpName Enterprise,DC=corp,DC=corpname,DC=com"
               },
              "labels": [],
              "state": "open",
              "locked": false,
              "assignee": null,
              "assignees": [],
              "milestone": null,
              "comments": 3,
              "created_at": "2022-02-05T01:49:32Z",
              "updated_at": "2022-07-21T07:36:07Z",
              "closed_at": null,
              "author_association": "OWNER",
              "active_lock_reason": null,
              "body": "hi @evanl00 ",
              "performed_via_github_app": null
             },
            "comment": {
              "url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/issues/comments/517747",
              "html_url": "https://github.corpname.com/evanl00/Scripts/issues/30#issuecomment-517747",
              "issue_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/issues/30",
              "id": 517747,
              "node_id": "fdfdd==",
              "user": {
                "login": "evanl00",
                "id": 869,
                "node_id": "fdfdd==",
                "avatar_url": "https://github.corpname.com/avatars/u/869?",
                "gravatar_id": "",
                "url": "https://github.corpname.com/api/v3/users/evanl00",
                "html_url": "https://github.corpname.com/evanl00",
                "followers_url": "https://github.corpname.com/api/v3/users/evanl00/followers",
                "following_url": "https://github.corpname.com/api/v3/users/evanl00/following{/other_user}",
                "gists_url": "https://github.corpname.com/api/v3/users/evanl00/gists{/gist_id}",
                "starred_url": "https://github.corpname.com/api/v3/users/evanl00/starred{/owner}{/repo}",
                "subscriptions_url": "https://github.corpname.com/api/v3/users/evanl00/subscriptions",
                "organizations_url": "https://github.corpname.com/api/v3/users/evanl00/orgs",
                "repos_url": "https://github.corpname.com/api/v3/users/evanl00/repos",
                "events_url": "https://github.corpname.com/api/v3/users/evanl00/events{/privacy}",
                "received_events_url": "https://github.corpname.com/api/v3/users/evanl00/received_events",
                "type": "User",
                "site_admin": false,
                "ldap_dn": "CN=Lu\\, Evan,OU=Users Hangzhou,OU=China,OU=TPC,OU=CorpName Enterprise,DC=corp,DC=corpname,DC=com"
               },
              "created_at": "2022-07-21T07:36:07Z",
              "updated_at": "2022-07-21T07:36:07Z",
              "author_association": "OWNER",
              "body": "Hi",
              "performed_via_github_app": null
             },
            "repository": {
              "id": 5093,
              "node_id": "dd",
              "name": "Scripts",
              "full_name": "evanl00/Scripts",
              "private": false,
              "owner": {
                "login": "evanl00",
                "id": 869,
                "node_id": "fdfdd==",
                "avatar_url": "https://github.corpname.com/avatars/u/869?",
                "gravatar_id": "",
                "url": "https://github.corpname.com/api/v3/users/evanl00",
                "html_url": "https://github.corpname.com/evanl00",
                "followers_url": "https://github.corpname.com/api/v3/users/evanl00/followers",
                "following_url": "https://github.corpname.com/api/v3/users/evanl00/following{/other_user}",
                "gists_url": "https://github.corpname.com/api/v3/users/evanl00/gists{/gist_id}",
                "starred_url": "https://github.corpname.com/api/v3/users/evanl00/starred{/owner}{/repo}",
                "subscriptions_url": "https://github.corpname.com/api/v3/users/evanl00/subscriptions",
                "organizations_url": "https://github.corpname.com/api/v3/users/evanl00/orgs",
                "repos_url": "https://github.corpname.com/api/v3/users/evanl00/repos",
                "events_url": "https://github.corpname.com/api/v3/users/evanl00/events{/privacy}",
                "received_events_url": "https://github.corpname.com/api/v3/users/evanl00/received_events",
                "type": "User",
                "site_admin": false,
                "ldap_dn": "CN=Lu\\, Evan,OU=Users Hangzhou,OU=China,OU=TPC,OU=CorpName Enterprise,DC=corp,DC=corpname,DC=com"
               },
              "html_url": "https://github.corpname.com/evanl00/Scripts",
              "description": "Scripts to help debug easier",
              "fork": false,
              "url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts",
              "forks_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/forks",
              "keys_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/keys{/key_id}",
              "collaborators_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/collaborators{/collaborator}",
              "teams_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/teams",
              "hooks_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/hooks",
              "issue_events_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/issues/events{/number}",
              "events_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/events",
              "assignees_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/assignees{/user}",
              "branches_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/branches{/branch}",
              "tags_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/tags",
              "blobs_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/git/blobs{/sha}",
              "git_tags_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/git/tags{/sha}",
              "git_refs_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/git/refs{/sha}",
              "trees_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/git/trees{/sha}",
              "statuses_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/statuses/{sha}",
              "languages_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/languages",
              "stargazers_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/stargazers",
              "contributors_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/contributors",
              "subscribers_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/subscribers",
              "subscription_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/subscription",
              "commits_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/commits{/sha}",
              "git_commits_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/git/commits{/sha}",
              "comments_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/comments{/number}",
              "issue_comment_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/issues/comments{/number}",
              "contents_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/contents/{+path}",
              "compare_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/compare/{base}...{head}",
              "merges_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/merges",
              "archive_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/{archive_format}{/ref}",
              "downloads_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/downloads",
              "issues_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/issues{/number}",
              "pulls_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/pulls{/number}",
              "milestones_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/milestones{/number}",
              "notifications_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/notifications{?since,all,participating}",
              "labels_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/labels{/name}",
              "releases_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/releases{/id}",
              "deployments_url": "https://github.corpname.com/api/v3/repos/evanl00/Scripts/deployments",
              "created_at": "2019-03-29T13:55:11Z",
              "updated_at": "2022-05-07T09:11:11Z",
              "pushed_at": "2022-05-09T09:43:06Z",
              "git_url": "git://github.corpname.com/evanl00/Scripts.git",
              "ssh_url": "git@github.corpname.com:evanl00/Scripts.git",
              "clone_url": "https://github.corpname.com/evanl00/Scripts.git",
              "svn_url": "https://github.corpname.com/evanl00/Scripts",
              "homepage": "",
              "size": 108015,
              "stargazers_count": 1,
              "watchers_count": 1,
              "language": "C",
              "has_issues": true,
              "has_projects": true,
              "has_downloads": true,
              "has_wiki": true,
              "has_pages": false,
              "forks_count": 1,
              "mirror_url": null,
              "archived": false,
              "disabled": false,
              "open_issues_count": 1,
              "license": null,
              "forks": 1,
              "open_issues": 1,
              "watchers": 1,
              "default_branch": "master"
             },
            "enterprise": {
              "id": 1,
              "slug": "corpname-inc",
              "name": "CorpName, Inc.",
              "node_id": "dddd",
              "avatar_url": "https://github.corpname.com/avatars/b/1?",
              "description": null,
              "website_url": null,
              "html_url": "https://github.corpname.com/enterprises/corpname-inc",
              "created_at": "2018-11-04T04:17:23Z",
              "updated_at": "2021-07-20T17:41:34Z"
             },
            "sender": {
              "login": "evanl00",
              "id": 869,
              "node_id": "fdfdd==",
              "avatar_url": "https://github.corpname.com/avatars/u/869?",
              "gravatar_id": "",
              "url": "https://github.corpname.com/api/v3/users/evanl00",
              "html_url": "https://github.corpname.com/evanl00",
              "followers_url": "https://github.corpname.com/api/v3/users/evanl00/followers",
              "following_url": "https://github.corpname.com/api/v3/users/evanl00/following{/other_user}",
              "gists_url": "https://github.corpname.com/api/v3/users/evanl00/gists{/gist_id}",
              "starred_url": "https://github.corpname.com/api/v3/users/evanl00/starred{/owner}{/repo}",
              "subscriptions_url": "https://github.corpname.com/api/v3/users/evanl00/subscriptions",
              "organizations_url": "https://github.corpname.com/api/v3/users/evanl00/orgs",
              "repos_url": "https://github.corpname.com/api/v3/users/evanl00/repos",
              "events_url": "https://github.corpname.com/api/v3/users/evanl00/events{/privacy}",
              "received_events_url": "https://github.corpname.com/api/v3/users/evanl00/received_events",
              "type": "User",
              "site_admin": false,
              "ldap_dn": "CN=Lu\\, Evan,OU=Users Hangzhou,OU=China,OU=TPC,OU=CorpName Enterprise,DC=corp,DC=corpname,DC=com"
             }
           }"#;
        use super::*;
        let event = event::GithubEvent::new(pay_load).unwrap();
        assert_eq!("Hi", event.get_code().unwrap());
        assert_eq!("Lu, Evan", event.get_user_name());
    }
}