Skip to content

Commit 5b60ab8

Browse files
committed
fix GithubApiClient init for non-PR events
1 parent ae33a6d commit 5b60ab8

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

cpp-linter-lib/src/rest_api/github_api.rs

+20-16
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ impl Default for GithubApiClient {
5656

5757
impl GithubApiClient {
5858
pub fn new() -> Self {
59-
GithubApiClient {
60-
client: Client::new(),
61-
pull_request: {
62-
if let Ok(event_payload_path) = env::var("GITHUB_EVENT_PATH") {
59+
let event_name = env::var("GITHUB_EVENT_NAME").unwrap_or(String::from("unknown"));
60+
let pull_request = {
61+
match event_name.as_str() {
62+
"pull_request" => {
63+
let event_payload_path = env::var("GITHUB_EVENT_PATH")
64+
.expect("GITHUB_EVENT_NAME is set to 'pull_request', but GITHUB_EVENT_PATH is not set");
6365
let file_buf = &mut String::new();
6466
OpenOptions::new()
6567
.read(true)
@@ -72,21 +74,23 @@ impl GithubApiClient {
7274
)
7375
.unwrap();
7476
json["number"].as_i64()
75-
} else {
76-
None
7777
}
78-
},
79-
event_name: env::var("GITHUB_EVENT_NAME").unwrap_or(String::from("default")),
78+
_ => None,
79+
}
80+
};
81+
82+
GithubApiClient {
83+
client: Client::new(),
84+
pull_request,
85+
event_name,
8086
api_url: env::var("GITHUB_API_URL").unwrap_or(String::from("https://api.github.com")),
81-
repo: if let Ok(val) = env::var("GITHUB_REPOSITORY") {
82-
Some(val)
83-
} else {
84-
None
87+
repo: match env::var("GITHUB_REPOSITORY") {
88+
Ok(val) => Some(val),
89+
Err(_) => None,
8590
},
86-
sha: if let Ok(val) = env::var("GITHUB_SHA") {
87-
Some(val)
88-
} else {
89-
None
91+
sha: match env::var("GITHUB_SHA") {
92+
Ok(val) => Some(val),
93+
Err(_) => None,
9094
},
9195
debug_enabled: match env::var("ACTIONS_STEP_DEBUG") {
9296
Ok(val) => val == "true",

0 commit comments

Comments
 (0)