Skip to content

Commit b569b68

Browse files
committed
add user-agent header; adjust auth haeader value
adjust tests accordingly
1 parent 5835363 commit b569b68

File tree

6 files changed

+22
-12
lines changed

6 files changed

+22
-12
lines changed

cpp-linter/src/rest_api/github/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ impl RestApiClient for GithubApiClient {
100100
"Accept",
101101
HeaderValue::from_str("application/vnd.github.raw+json")?,
102102
);
103-
// headers.insert("User-Agent", USER_AGENT.parse().unwrap());
104103
if let Ok(token) = env::var("GITHUB_TOKEN") {
105-
let mut val = HeaderValue::from_str(token.as_str())?;
104+
log::debug!("Using auth token from GITHUB_TOKEN environment variable");
105+
let mut val = HeaderValue::from_str(format!("token {token}").as_str())?;
106106
val.set_sensitive(true);
107107
headers.insert(AUTHORIZATION, val);
108108
}

cpp-linter/src/rest_api/github/specific_api.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515
clang_tools::{clang_format::summarize_style, ReviewComments},
1616
cli::FeedbackInput,
1717
common_fs::FileObj,
18-
rest_api::{RestApiRateLimitHeaders, COMMENT_MARKER},
18+
rest_api::{RestApiRateLimitHeaders, COMMENT_MARKER, USER_AGENT},
1919
};
2020

2121
use super::{
@@ -58,6 +58,7 @@ impl GithubApiClient {
5858
Ok(GithubApiClient {
5959
client: Client::builder()
6060
.default_headers(Self::make_headers()?)
61+
.user_agent(USER_AGENT)
6162
.build()
6263
.with_context(|| "Failed to create a session client for REST API calls")?,
6364
pull_request,

cpp-linter/src/rest_api/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ use crate::cli::FeedbackInput;
2222
use crate::common_fs::{FileFilter, FileObj};
2323

2424
pub static COMMENT_MARKER: &str = "<!-- cpp linter action -->\n";
25-
pub static USER_OUTREACH: &str = "\n\nHave any feedback or feature suggestions? [Share it here.](https://github.com/cpp-linter/cpp-linter-action/issues)";
25+
pub static USER_OUTREACH: &str = concat!(
26+
"\n\nHave any feedback or feature suggestions? [Share it here.]",
27+
"(https://github.com/cpp-linter/cpp-linter-action/issues)"
28+
);
29+
pub static USER_AGENT: &str = concat!("cpp-linter/", env!("CARGO_PKG_VERSION"));
2630

2731
/// A structure to contain the different forms of headers that
2832
/// describe a REST API's rate limit status.

cpp-linter/tests/comments.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ async fn setup(lib_root: &Path, test_params: &TestParams) {
9494
server
9595
.mock("GET", format!("/repos/{REPO}/{diff_end_point}").as_str())
9696
.match_header("Accept", "application/vnd.github.diff")
97-
.match_header("Authorization", TOKEN)
97+
.match_header("Authorization", format!("token {TOKEN}").as_str())
9898
.with_body_from_file(format!("{asset_path}patch.diff"))
9999
.with_header(REMAINING_RATE_LIMIT_HEADER, "50")
100100
.with_header(RESET_RATE_LIMIT_HEADER, reset_timestamp.as_str())
@@ -109,7 +109,7 @@ async fn setup(lib_root: &Path, test_params: &TestParams) {
109109
format!("/repos/{REPO}/commits/{SHA}/comments").as_str(),
110110
)
111111
.match_header("Accept", "application/vnd.github.raw+json")
112-
.match_header("Authorization", TOKEN)
112+
.match_header("Authorization", format!("token {TOKEN}").as_str())
113113
.match_body(Matcher::Any)
114114
.match_query(Matcher::UrlEncoded("page".to_string(), "1".to_string()))
115115
.with_body_from_file(format!("{asset_path}push_comments_{SHA}.json"))
@@ -134,7 +134,7 @@ async fn setup(lib_root: &Path, test_params: &TestParams) {
134134
server
135135
.mock("GET", pr_endpoint.as_str())
136136
.match_header("Accept", "application/vnd.github.raw+json")
137-
.match_header("Authorization", TOKEN)
137+
.match_header("Authorization", format!("token {TOKEN}").as_str())
138138
.match_body(Matcher::Any)
139139
.match_query(Matcher::UrlEncoded("page".to_string(), pg.to_string()))
140140
.with_body_from_file(format!("{asset_path}pr_comments_pg{pg}.json"))
@@ -153,6 +153,7 @@ async fn setup(lib_root: &Path, test_params: &TestParams) {
153153
server
154154
.mock("DELETE", comment_url.as_str())
155155
.match_body(Matcher::Any)
156+
.match_header("Authorization", format!("token {TOKEN}").as_str())
156157
.with_status(if test_params.fail_dismissal { 403 } else { 200 })
157158
.with_header(REMAINING_RATE_LIMIT_HEADER, "50")
158159
.with_header(RESET_RATE_LIMIT_HEADER, reset_timestamp.as_str())
@@ -177,6 +178,7 @@ async fn setup(lib_root: &Path, test_params: &TestParams) {
177178
server
178179
.mock("PATCH", comment_url.as_str())
179180
.match_body(new_comment_match.clone())
181+
.match_header("Authorization", format!("token {TOKEN}").as_str())
180182
.with_status(if test_params.fail_posting { 403 } else { 200 })
181183
.with_header(REMAINING_RATE_LIMIT_HEADER, "50")
182184
.with_header(RESET_RATE_LIMIT_HEADER, reset_timestamp.as_str())
@@ -200,6 +202,7 @@ async fn setup(lib_root: &Path, test_params: &TestParams) {
200202
.as_str(),
201203
)
202204
.match_body(new_comment_match)
205+
.match_header("Authorization", format!("token {TOKEN}").as_str())
203206
.with_header(REMAINING_RATE_LIMIT_HEADER, "50")
204207
.with_header(RESET_RATE_LIMIT_HEADER, reset_timestamp.as_str())
205208
.with_status(if test_params.fail_posting { 403 } else { 200 })

cpp-linter/tests/paginated_changed_files.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ async fn get_paginated_changes(lib_root: &Path, event_type: EventType) {
7373
server
7474
.mock("GET", diff_end_point.as_str())
7575
.match_header("Accept", "application/vnd.github.diff")
76-
.match_header("Authorization", TOKEN)
76+
.match_header("Authorization", format!("token {TOKEN}").as_str())
7777
.with_header(REMAINING_RATE_LIMIT_HEADER, "50")
7878
.with_header(RESET_RATE_LIMIT_HEADER, reset_timestamp.as_str())
7979
.with_status(403)
@@ -94,7 +94,7 @@ async fn get_paginated_changes(lib_root: &Path, event_type: EventType) {
9494
server
9595
.mock("GET", pg_end_point.as_str())
9696
.match_header("Accept", "application/vnd.github.raw+json")
97-
.match_header("Authorization", TOKEN)
97+
.match_header("Authorization", format!("token {TOKEN}").as_str())
9898
.match_query(Matcher::UrlEncoded("page".to_string(), pg.to_string()))
9999
.with_header(REMAINING_RATE_LIMIT_HEADER, "50")
100100
.with_header(RESET_RATE_LIMIT_HEADER, reset_timestamp.as_str())

cpp-linter/tests/reviews.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ async fn setup(lib_root: &Path, test_params: &TestParams) {
8484
server
8585
.mock("GET", pr_endpoint.as_str())
8686
.match_header("Accept", "application/vnd.github.diff")
87-
.match_header("Authorization", TOKEN)
87+
.match_header("Authorization", format!("token {TOKEN}").as_str())
8888
.with_body_from_file(format!("{asset_path}pr_{PR}.diff"))
8989
.with_header(REMAINING_RATE_LIMIT_HEADER, "50")
9090
.with_header(RESET_RATE_LIMIT_HEADER, reset_timestamp.as_str())
@@ -94,7 +94,7 @@ async fn setup(lib_root: &Path, test_params: &TestParams) {
9494
server
9595
.mock("GET", pr_endpoint.as_str())
9696
.match_header("Accept", "application/vnd.github.raw+json")
97-
.match_header("Authorization", TOKEN)
97+
.match_header("Authorization", format!("token {TOKEN}").as_str())
9898
.with_body(
9999
json!({"state": test_params.pr_state, "draft": test_params.pr_draft}).to_string(),
100100
)
@@ -109,7 +109,7 @@ async fn setup(lib_root: &Path, test_params: &TestParams) {
109109
server
110110
.mock("GET", reviews_endpoint.as_str())
111111
.match_header("Accept", "application/vnd.github.raw+json")
112-
.match_header("Authorization", TOKEN)
112+
.match_header("Authorization", format!("token {TOKEN}").as_str())
113113
.match_body(Matcher::Any)
114114
.match_query(Matcher::UrlEncoded("page".to_string(), "1".to_string()))
115115
.with_body_from_file(format!("{asset_path}pr_reviews.json"))
@@ -127,6 +127,7 @@ async fn setup(lib_root: &Path, test_params: &TestParams) {
127127
server
128128
.mock("PUT", format!("{reviews_endpoint}/1807607546").as_str())
129129
.match_body(r#"{"event":"DISMISS","message":"outdated suggestion"}"#)
130+
.match_header("Authorization", format!("token {TOKEN}").as_str())
130131
.with_header(REMAINING_RATE_LIMIT_HEADER, "50")
131132
.with_header(RESET_RATE_LIMIT_HEADER, reset_timestamp.as_str())
132133
.with_status(if test_params.fail_dismissal { 403 } else { 200 })
@@ -170,6 +171,7 @@ async fn setup(lib_root: &Path, test_params: &TestParams) {
170171
server
171172
.mock("POST", reviews_endpoint.as_str())
172173
.match_body(Matcher::Regex(expected_review_payload))
174+
.match_header("Authorization", format!("token {TOKEN}").as_str())
173175
.with_header(REMAINING_RATE_LIMIT_HEADER, "50")
174176
.with_header(RESET_RATE_LIMIT_HEADER, reset_timestamp.as_str())
175177
.with_status(if test_params.fail_posting { 403 } else { 200 })

0 commit comments

Comments
 (0)