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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
use std::{ffi::OsStr, fs, path};

use keep_a_changelog::{changelog::ChangelogBuilder, ChangeKind, Changelog, Release};
use log::debug;
use url::Url;

use crate::Error;

#[derive(Debug)]
pub struct PrTitle {
    pub title: String,
    pub pr_id: Option<u64>,
    pub pr_url: Option<Url>,
    pub commit_type: Option<String>,
    pub commit_scope: Option<String>,
    pub commit_breaking: bool,
    pub section: Option<ChangeKind>,
    pub entry: String,
}

impl PrTitle {
    pub fn parse(title: &str) -> Result<Self, Error> {
        let re = regex::Regex::new(
            r"^(?P<type>[a-z]+)(?:\((?P<scope>.+)\))?(?P<breaking>!)?: (?P<description>.*)$",
        )?;

        debug!("String to parse: `{}`", title);

        let pr_title = if let Some(captures) = re.captures(title) {
            let commit_type = captures.name("type").map(|m| m.as_str().to_string());
            let commit_scope = captures.name("scope").map(|m| m.as_str().to_string());
            let commit_breaking = captures.name("breaking").is_some();
            let title = captures
                .name("description")
                .map(|m| m.as_str().to_string())
                .unwrap();

            Self {
                title,
                pr_id: None,
                pr_url: None,
                commit_type,
                commit_scope,
                commit_breaking,
                section: None,
                entry: String::new(),
            }
        } else {
            Self {
                title: title.to_string(),
                pr_id: None,
                pr_url: None,
                commit_type: None,
                commit_scope: None,
                commit_breaking: false,
                section: None,
                entry: String::new(),
            }
        };

        debug!("Parsed title: {:?}", pr_title);

        Ok(pr_title)
    }

    pub fn set_pr_id(&mut self, id: u64) {
        self.pr_id = Some(id);
    }

    pub fn set_pr_url(&mut self, url: Url) {
        self.pr_url = Some(url);
    }

    pub fn calculate_section_and_entry(&mut self) {
        let mut section = ChangeKind::Changed;
        let mut entry = self.title.clone();

        debug!("Initial description `{}`", entry);

        if let Some(commit_type) = &self.commit_type {
            match commit_type.as_str() {
                "feat" => section = ChangeKind::Added,
                "fix" => section = ChangeKind::Fixed,
                _ => {
                    section = ChangeKind::Changed;
                    entry = format!("{}-{}", self.commit_type.as_ref().unwrap(), entry);
                }
            }
        }

        debug!("After checking type `{}`", entry);

        if let Some(commit_scope) = &self.commit_scope {
            match commit_scope.as_str() {
                "security" => {
                    section = ChangeKind::Security;
                    entry = format!("Security: {}", self.title);
                }
                "deps" => {
                    section = ChangeKind::Security;
                    entry = format!("Dependencies: {}", self.title);
                }
                "remove" => {
                    section = ChangeKind::Removed;
                    entry = format!("Removed: {}", self.title);
                }
                "deprecate" => {
                    section = ChangeKind::Deprecated;
                    entry = format!("Deprecated: {}", self.title);
                }
                _ => {
                    section = ChangeKind::Changed;
                    let split_description = entry.splitn(2, '-').collect::<Vec<&str>>();
                    entry = format!(
                        "{}({})-{}",
                        split_description[0],
                        self.commit_scope.as_ref().unwrap(),
                        split_description[1]
                    );
                }
            }
        }

        debug!("After checking scope `{}`", entry);

        if self.commit_breaking {
            entry = format!("BREAKING: {}", entry);
        }

        if let Some(id) = self.pr_id {
            entry = format!("{}(pr #{})", entry, id);

            debug!("After checking pr id `{}`", entry);

            if let Some(url) = &self.pr_url {
                let split_description = entry.splitn(2, '(').collect::<Vec<&str>>();
                entry = format!("{}(pr [#{}]({}))", split_description[0], id, url);
            }
        };

        self.section = Some(section);
        self.entry = entry;
    }

    fn section(&self) -> ChangeKind {
        match &self.section {
            Some(kind) => kind.clone(),
            None => ChangeKind::Changed,
        }
    }

    fn entry(&self) -> String {
        if self.entry.as_str() == "" {
            self.title.clone()
        } else {
            self.entry.clone()
        }
    }

    pub fn update_changelog(
        &mut self,
        log_file: &OsStr,
    ) -> Result<Option<(ChangeKind, String)>, Error> {
        let Some(log_file) = log_file.to_str() else {
            return Err(Error::InvalidPath(log_file.to_owned()));
        };

        self.calculate_section_and_entry();

        log::trace!("Changelog entry:\n\n---\n{}\n---\n\n", self.entry());

        let mut change_log = if path::Path::new(log_file).exists() {
            let file_contents = fs::read_to_string(path::Path::new(log_file))?;
            log::trace!("file contents:\n---\n{}\n---\n\n", file_contents);
            if file_contents.contains(&self.entry) {
                log::trace!("The changelog exists and already contains the entry!");
                return Ok(None);
            } else {
                log::trace!("The changelog exists but does not contain the entry!");
            }
            Changelog::parse_from_file(log_file, None)
                .map_err(|e| Error::KeepAChangelog(e.to_string()))?
        } else {
            log::trace!("The changelog does not exist! Create a default changelog.");
            let mut changelog = ChangelogBuilder::default()
                .build()
                .map_err(|e| Error::KeepAChangelog(e.to_string()))?;
            log::debug!("Changelog: {:#?}", changelog);
            let release = Release::builder()
                .build()
                .map_err(|e| Error::KeepAChangelog(e.to_string()))?;
            changelog.add_release(release);
            log::debug!("Changelog: {:#?}", changelog);

            changelog
                .save_to_file(log_file)
                .map_err(|e| Error::KeepAChangelog(e.to_string()))?;
            changelog
        };

        // Get the unreleased section from the Changelog.
        // If there is no unreleased section create it and add it to the changelog
        let unreleased = if let Some(unreleased) = change_log.get_unreleased_mut() {
            unreleased
        } else {
            let release = Release::builder()
                .build()
                .map_err(|e| Error::KeepAChangelog(e.to_string()))?;
            change_log.add_release(release);
            let unreleased = change_log.get_unreleased_mut().unwrap();
            unreleased
        };

        match self.section() {
            ChangeKind::Added => {
                unreleased.added(self.entry());
            }
            ChangeKind::Fixed => {
                unreleased.fixed(self.entry());
            }
            ChangeKind::Security => {
                unreleased.security(self.entry());
            }
            ChangeKind::Removed => {
                unreleased.removed(self.entry());
            }
            ChangeKind::Deprecated => {
                unreleased.deprecated(self.entry());
            }
            ChangeKind::Changed => {
                unreleased.changed(self.entry());
            }
        }
        change_log
            .save_to_file(log_file)
            .map_err(|e| Error::KeepAChangelog(e.to_string()))?;

        Ok(Some((self.section(), self.entry())))
    }
}

//test module
#[cfg(test)]
mod tests {
    use std::{
        fs::{self, File},
        io::Write,
        path::Path,
    };

    use super::*;
    use log::LevelFilter;
    use log4rs_test_utils::test_logging;
    use rstest::rstest;
    use uuid::Uuid;

    #[test]
    fn test_pr_title_parse() {
        let pr_title = PrTitle::parse("feat: add new feature").unwrap();

        assert_eq!(pr_title.title, "add new feature");
        assert_eq!(pr_title.commit_type, Some("feat".to_string()));
        assert_eq!(pr_title.commit_scope, None);
        assert!(!pr_title.commit_breaking);

        let pr_title = PrTitle::parse("feat(core): add new feature").unwrap();
        assert_eq!(pr_title.title, "add new feature");
        assert_eq!(pr_title.commit_type, Some("feat".to_string()));
        assert_eq!(pr_title.commit_scope, Some("core".to_string()));
        assert!(!pr_title.commit_breaking);

        let pr_title = PrTitle::parse("feat(core)!: add new feature").unwrap();
        assert_eq!(pr_title.title, "add new feature");
        assert_eq!(pr_title.commit_type, Some("feat".to_string()));
        assert_eq!(pr_title.commit_scope, Some("core".to_string()));
        assert!(pr_title.commit_breaking);
    }

    #[test]
    fn test_pr_title_parse_with_breaking_scope() {
        let pr_title = PrTitle::parse("feat(core)!: add new feature").unwrap();
        assert_eq!(pr_title.title, "add new feature");
        assert_eq!(pr_title.commit_type, Some("feat".to_string()));
        assert_eq!(pr_title.commit_scope, Some("core".to_string()));
        assert!(pr_title.commit_breaking);
    }

    #[test]
    fn test_pr_title_parse_with_security_scope() {
        let pr_title = PrTitle::parse("fix(security): fix security vulnerability").unwrap();
        assert_eq!(pr_title.title, "fix security vulnerability");
        assert_eq!(pr_title.commit_type, Some("fix".to_string()));
        assert_eq!(pr_title.commit_scope, Some("security".to_string()));
        assert!(!pr_title.commit_breaking);
    }

    #[test]
    fn test_pr_title_parse_with_deprecate_scope() {
        let pr_title = PrTitle::parse("chore(deprecate): deprecate old feature").unwrap();
        assert_eq!(pr_title.title, "deprecate old feature");
        assert_eq!(pr_title.commit_type, Some("chore".to_string()));
        assert_eq!(pr_title.commit_scope, Some("deprecate".to_string()));
        assert!(!pr_title.commit_breaking);
    }

    #[test]
    fn test_pr_title_parse_without_scope() {
        let pr_title = PrTitle::parse("docs: update documentation").unwrap();
        assert_eq!(pr_title.title, "update documentation");
        assert_eq!(pr_title.commit_type, Some("docs".to_string()));
        assert_eq!(pr_title.commit_scope, None);
        assert!(!pr_title.commit_breaking);
    }

    #[rstest]
    #[case(
        "feat: add new feature",
        Some(5),
        Some("https://github.com/jerus-org/pcu/pull/5"),
        ChangeKind::Added,
        "add new feature(pr [#5](https://github.com/jerus-org/pcu/pull/5))"
    )]
    #[case(
        "feat: add new feature",
        Some(5),
        None,
        ChangeKind::Added,
        "add new feature(pr #5)"
    )]
    #[case(
        "feat: add new feature",
        None,
        Some("https://github.com/jerus-org/pcu/pull/5"),
        ChangeKind::Added,
        "add new feature"
    )]
    #[case(
        "feat: add new feature",
        None,
        None,
        ChangeKind::Added,
        "add new feature"
    )]
    #[case(
        "fix: fix an existing feature",
        None,
        None,
        ChangeKind::Fixed,
        "fix an existing feature"
    )]
    #[case(
        "test: update tests",
        None,
        None,
        ChangeKind::Changed,
        "test-update tests"
    )]
    #[case(
        "fix(security): Fix security vulnerability",
        None,
        None,
        ChangeKind::Security,
        "Security: Fix security vulnerability"
    )]
    #[case(
        "chore(deps): Update dependencies",
        None,
        None,
        ChangeKind::Security,
        "Dependencies: Update dependencies"
    )]
    #[case(
        "refactor(remove): Remove unused code",
        None,
        None,
        ChangeKind::Removed,
        "Removed: Remove unused code"
    )]
    #[case(
        "docs(deprecate): Deprecate old API",
        None,
        None,
        ChangeKind::Deprecated,
        "Deprecated: Deprecate old API"
    )]
    #[case(
        "ci(other-scope): Update CI configuration",
        None,
        None,
        ChangeKind::Changed,
        "ci(other-scope)-Update CI configuration"
    )]
    #[case(
        "test!: Update test cases",
        None,
        None,
        ChangeKind::Changed,
        "BREAKING: test-Update test cases"
    )]
    fn test_calculate_kind_and_description(
        #[case] title: &str,
        #[case] pr_id: Option<u64>,
        #[case] pr_url: Option<&str>,
        #[case] expected_kind: ChangeKind,
        #[case] expected_desciption: &str,
    ) -> Result<()> {
        test_logging::init_logging_once_for(vec![], LevelFilter::Debug, None);

        let mut pr_title = PrTitle::parse(title).unwrap();
        if let Some(id) = pr_id {
            pr_title.set_pr_id(id);
        }
        if let Some(url) = pr_url {
            let url = Url::parse(url)?;
            pr_title.set_pr_url(url);
        }
        pr_title.calculate_section_and_entry();
        assert_eq!(expected_kind, pr_title.section());
        assert_eq!(expected_desciption, pr_title.entry);

        Ok(())
    }

    use color_eyre::Result;

    #[rstest]
    fn test_update_change_log_added() -> Result<()> {
        test_logging::init_logging_once_for(vec![], LevelFilter::Debug, None);

        let initial_content = fs::read_to_string("tests/data/initial_changelog.md")?;
        let expected_content = fs::read_to_string("tests/data/expected_changelog.md")?;

        let temp_dir_string = format!("tests/tmp/test-{}", Uuid::new_v4());
        let temp_dir = Path::new(&temp_dir_string);
        fs::create_dir_all(temp_dir)?;

        let file_name = temp_dir.join("CHANGELOG.md");
        debug!("filename : {:?}", file_name);

        let mut file = File::create(&file_name)?;
        file.write_all(initial_content.as_bytes())?;

        let mut pr_title = PrTitle {
            title: "add new feature".to_string(),
            pr_id: Some(5),
            pr_url: Some(Url::parse("https://github.com/jerus-org/pcu/pull/5")?),
            commit_type: Some("feat".to_string()),
            commit_scope: None,
            commit_breaking: false,
            section: Some(ChangeKind::Added),
            entry: "add new feature".to_string(),
        };

        let file_name = &file_name.into_os_string();
        pr_title.update_changelog(file_name)?;

        let actual_content = fs::read_to_string(file_name)?;

        assert_eq!(actual_content, expected_content);

        // tidy up the test environment
        std::fs::remove_dir_all(temp_dir)?;

        Ok(())
    }
}