forked from guacsec/trustify-release-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
80 lines (64 loc) · 2.17 KB
/
main.rs
File metadata and controls
80 lines (64 loc) · 2.17 KB
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
use action::context::GitHubVariables;
use pr::prefix::PRType;
use serde::Deserialize;
use std::fs;
mod error;
#[derive(Debug, Deserialize)]
struct Event {
pub pull_request: PullRequestEvent,
}
#[derive(Debug, Deserialize)]
struct PullRequestEvent {
title: String,
}
fn main() -> error::Result<()> {
let result = verify_pr();
match &result {
Ok(_) => {}
Err(error) => println!("{error}"),
};
result
}
fn verify_pr() -> error::Result<()> {
let gh_context = GitHubVariables::from_env()?;
// Parse the event
let event_file = fs::read_to_string(gh_context.github_event_path)?;
let event: Event = serde_json::from_str(&event_file).map_err(|err| {
crate::error::Error::UnmarshalPullRequest {
file_path: event_file,
err,
}
})?;
// Check the title of the PR
let pr_type = PRType::from_title(&event.pull_request.title)?;
println!("{:?}", pr_type);
Ok(())
}
#[cfg(test)]
mod tests {
use crate::main;
use tempfile::NamedTempFile;
#[test]
fn read_from_file() {
std::env::set_var("CI", "true");
std::env::set_var("GITHUB_ACTIONS", "true");
std::env::set_var("GITHUB_EVENT_NAME", "foo");
let event_that_generates_ok = "{\"pull_request\":{\"title\":\"WIP: :bug: Fix bug\"}}";
let event_that_generates_error =
"{\"pull_request\":{\"title\":\"WIP: [docs] Update documentation\"}}";
//
let temp_file = NamedTempFile::new().expect("Failed to create temp file");
let path = temp_file.path(); // Get the temporary file's path
std::fs::write(path, event_that_generates_ok).expect("Failed to write to temp file");
std::env::set_var("GITHUB_EVENT_PATH", path);
let result = main();
assert!(result.is_ok());
//
let temp_file = NamedTempFile::new().expect("Failed to create temp file");
let path = temp_file.path(); // Get the temporary file's path
std::fs::write(path, event_that_generates_error).expect("Failed to write to temp file");
std::env::set_var("GITHUB_EVENT_PATH", path);
let result = main();
assert!(result.is_err());
}
}