n4n5/commands/gh/
lib.rs

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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//!
//! To see all subcommands, run:
//! ```shell
//! n4n5 gh
//! ```
//!
use std::{fs::write, path::PathBuf, process::Command};

use clap::{arg, ArgMatches, Command as ClapCommand};
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::{
    cli::{input_path, CliCommand},
    commands::gh::types::GhProject,
    config::Config,
    config_path,
};

use super::types::{GhPageInfo, GhResponse};

/// Github configuration
#[derive(Deserialize, Serialize, Default)]
pub struct Gh {
    /// Path to the movies file
    pub username: Option<String>,

    /// Path to the pulls file
    pub file_pulls: Option<String>,

    /// Path to the projects file
    pub file_projects: Option<String>,
}

impl CliCommand for Gh {
    fn get_subcommand() -> ClapCommand {
        ClapCommand::new("gh")
            .about("Github cli wrap")
            .subcommand(
                ClapCommand::new("pulls").about("save pulls").arg(
                    arg!(
                        -j --json ... "print as json"
                    )
                    .required(false),
                ),
            )
            .subcommand(
                ClapCommand::new("projects").about("save projects").arg(
                    arg!(
                        -j --json ... "print as json"
                    )
                    .required(false),
                ),
            )
            .arg_required_else_help(true)
    }

    fn invoke(config: &mut Config, args_matches: &ArgMatches) {
        if let Some(matches) = args_matches.subcommand_matches("pulls") {
            Gh::save_pulls(config, Some(matches));
        } else if let Some(matches) = args_matches.subcommand_matches("projects") {
            Gh::save_projects(config, Some(matches));
        }
    }
}

/// Project type
enum ProjectType {
    /// Gists
    Gists,
    /// Repos
    Repos,
}

impl Gh {
    /// Sync the github data
    pub fn sync_github(config: &mut Config, matches: Option<&ArgMatches>) {
        println!("Syncing github data");
        Gh::save_pulls(config, matches);
        Gh::save_projects(config, matches);
    }

    /// Save the pulls to the specified file
    fn save_pulls(config: &mut Config, _matches: Option<&ArgMatches>) {
        let pulls_path = config_path!(config, gh, Gh, file_pulls, "pulls file");
        println!("Saving pulls to {}", pulls_path.display());
        let mut response_data = GhPageInfo {
            has_next_page: true,
            ..Default::default()
        };
        let mut all_pulls = Vec::new();
        while response_data.has_next_page {
            let add = match response_data.end_cursor.trim().is_empty() {
                true => "".to_string(),
                false => format!(", after: \"{}\"", response_data.end_cursor),
            };
            let command = "gh api graphql -F owner='Its-Just-Nans' -f query='
            query($owner: String!) {
              user(login: $owner) {
                pullRequests(first: 100) {
                    edges {
                        node {
                        id
                        number
                        title
                        url
                        state
                        createdAt
                        baseRepository {
                            url
                            name
                            description
                            owner {
                            login
                            }
                            languages(first: 1) {
                                nodes {
                                    name
                                    color
                                }
                            }
                        }
                        }
                    }
                    pageInfo {
                        endCursor
                        startCursor
                        hasNextPage
                        hasPreviousPage
                    }
                }
              }
            }'"
            .replace("100)", format!("100{})", add).as_str());
            if config.debug > 0 {
                println!("Running command:");
                println!("{}", command);
            }
            let output = Command::new("sh")
                .arg("-c")
                .arg(command)
                .output()
                .expect("failed to execute process");
            let output = String::from_utf8_lossy(&output.stdout).to_string();
            if config.debug > 1 {
                println!("Output:");
                println!("{}", output);
            }
            let output = serde_json::from_str::<GhResponse>(&output)
                .expect("Unable to parse json from gh command");
            println!(
                "Received {} pulls requests",
                output.data.user.pull_requests.edges.len()
            );
            all_pulls.extend(output.data.user.pull_requests.edges);
            response_data = output.data.user.pull_requests.page_info;
        }
        let formatter = serde_json::ser::PrettyFormatter::with_indent(b"    ");
        let mut buf = Vec::new();
        let mut ser = serde_json::Serializer::with_formatter(&mut buf, formatter);
        all_pulls.serialize(&mut ser).unwrap();
        write(&pulls_path, buf).expect("Unable to write to file");
        println!(
            "Saving {} pulls to {}",
            all_pulls.len(),
            pulls_path.display()
        );
    }

    /// Fetch projects with gh cli
    fn fetch_projects(project_type: ProjectType, debug: u8) -> Vec<GhProject> {
        let mut response_data = GhPageInfo {
            has_next_page: true,
            ..Default::default()
        };
        let fetch_type = match project_type {
            ProjectType::Gists => "gists",
            ProjectType::Repos => "repositories",
        };
        let repo_arg = match project_type {
            ProjectType::Gists => "",
            ProjectType::Repos => "isFork: false, ownerAffiliations: [OWNER]",
        };
        let repo_data = match project_type {
            ProjectType::Gists => "",
            ProjectType::Repos => {
                "primaryLanguage {
                                name
                                color
                            }
                            homepageUrl"
            }
        };
        let mut all_projects = Vec::new();
        while response_data.has_next_page {
            let add = match response_data.end_cursor.trim().is_empty() {
                true => "".to_string(),
                false => format!(", after: \"{}\", ", response_data.end_cursor),
            };
            let command = "gh api graphql -F owner='Its-Just-Nans' -f query='
            query( $owner: String!){
                user(login: $owner) {
                    TYPE(first: 100, ADD REPO_ARG, privacy: PUBLIC) {
                        pageInfo {
                            hasNextPage
                            endCursor
                            startCursor
                        }
                        nodes {
                            url
                            name
                            REPO_DATA
                            description
                            stargazerCount
                        }
                    }
                }
            }'"
            .replace("TYPE", fetch_type)
            .replace("ADD", &add)
            .replace("REPO_ARG", repo_arg)
            .replace("REPO_DATA", repo_data);
            if debug > 1 {
                println!("Running command:");
                println!("{}", command);
            }
            let output = Command::new("sh")
                .arg("-c")
                .arg(command)
                .output()
                .expect("failed to execute process");
            let output = String::from_utf8_lossy(&output.stdout).to_string();
            if debug > 2 {
                println!("Output:");
                println!("{}", output);
            }
            let output = serde_json::from_str::<Value>(&output)
                .expect("Unable to parse json from gh command");
            match output {
                Value::Object(map) => {
                    if let Some(Value::Object(data)) = map.get("data") {
                        if let Some(Value::Object(user)) = data.get("user") {
                            if let Some(Value::Object(projects)) = user.get(fetch_type) {
                                if let Some(nodes) = projects.get("nodes") {
                                    let nodes: Vec<GhProject> = serde_json::from_value(
                                        nodes.clone(),
                                    )
                                    .unwrap_or_else(|err| {
                                        panic!("Unable to parse nodes: {}\n{}", err, nodes)
                                    });
                                    if debug > 0 {
                                        println!("Received {} {}", nodes.len(), fetch_type);
                                    }
                                    all_projects.extend(nodes);
                                }
                                response_data = serde_json::from_value(
                                    projects.get("pageInfo").unwrap().clone(),
                                )
                                .unwrap();
                            }
                        }
                    }
                }
                _ => {
                    println!("Unable to parse json from gh command");
                }
            }
        }
        all_projects
    }

    /// Save the projects to the specified file
    fn save_projects(config: &mut Config, matches: Option<&ArgMatches>) {
        let is_json = match matches {
            Some(matches) => !matches!(
                matches.get_one::<u8>("json").expect("Counts are defaulted"),
                0
            ),
            None => false,
        };
        let projects_path = config_path!(config, gh, Gh, file_projects, "projects file");
        if !is_json {
            println!("Saving projects to {}", projects_path.display());
        }
        let debug_level = match is_json {
            true => 0,
            false => config.debug + 1,
        };
        let mut repos = Gh::fetch_projects(ProjectType::Repos, debug_level);
        repos.sort_by(|a, b| a.name.cmp(&b.name));
        let mut gists = Gh::fetch_projects(ProjectType::Gists, debug_level);
        gists.sort_by(|a, b| a.name.cmp(&b.name));
        if !is_json {
            println!(
                "Saving {} repos and {} gists to {}",
                repos.len(),
                gists.len(),
                projects_path.display()
            );
        }
        let formatter = serde_json::ser::PrettyFormatter::with_indent(b"    ");
        let mut buf = Vec::new();
        let mut ser = serde_json::Serializer::with_formatter(&mut buf, formatter);
        repos.append(&mut gists);
        repos.serialize(&mut ser).unwrap();
        if is_json {
            println!("{}", String::from_utf8_lossy(&buf));
        }
        write(&projects_path, buf).expect("Unable to write to file");
    }
}