Skip to content

Commit da9d377

Browse files
authored
Merge pull request #2112 from Urgau/assign-custom-message-without-auto-assign
Be less strict for `[assign]` custom welcome message without auto-assign
2 parents 0012786 + 828578f commit da9d377

File tree

3 files changed

+65
-30
lines changed

3 files changed

+65
-30
lines changed

src/config.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub(crate) struct AssignReviewPrefsConfig {}
9898
#[serde(deny_unknown_fields)]
9999
pub(crate) struct AssignCustomWelcomeMessages {
100100
/// Welcome message with reviewer automaticaly chosen (`{assignee}`)
101-
pub(crate) welcome_message: String,
101+
pub(crate) welcome_message: Option<String>,
102102
/// Welcome message without a reviewer automaticaly chosen
103103
pub(crate) welcome_message_no_reviewer: String,
104104
}
@@ -777,7 +777,7 @@ mod tests {
777777
],
778778
},
779779
custom_welcome_messages: Some(AssignCustomWelcomeMessages {
780-
welcome_message: "Welcome message, assigning {assignee}!".to_string(),
780+
welcome_message: Some("Welcome message, assigning {assignee}!".to_string()),
781781
welcome_message_no_reviewer:
782782
"Welcome message for when no reviewer could be found!".to_string()
783783
}),
@@ -829,4 +829,20 @@ mod tests {
829829
Some(AssignReviewPrefsConfig {})
830830
));
831831
}
832+
833+
#[test]
834+
fn assign_custom_welcome_messaga() {
835+
let config = r#"
836+
[assign.custom_welcome_messages]
837+
welcome-message-no-reviewer = "welcome message!"
838+
"#;
839+
let config = toml::from_str::<Config>(&config).unwrap();
840+
assert_eq!(
841+
config.assign.and_then(|c| c.custom_welcome_messages),
842+
Some(AssignCustomWelcomeMessages {
843+
welcome_message: None,
844+
welcome_message_no_reviewer: "welcome message!".to_string(),
845+
})
846+
);
847+
}
832848
}

src/handlers/assign.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,19 @@ pub(super) async fn handle_input(
150150
let mut welcome = match &assignee {
151151
Some(assignee) => custom_welcome_messages
152152
.welcome_message
153-
.trim()
154-
.replace("{assignee}", &assignee.name),
155-
None => custom_welcome_messages
156-
.welcome_message_no_reviewer
157-
.trim()
158-
.to_string(),
153+
.as_ref()
154+
.map(|wm| wm.trim().replace("{assignee}", &assignee.name)),
155+
None => Some(
156+
custom_welcome_messages
157+
.welcome_message_no_reviewer
158+
.trim()
159+
.to_string(),
160+
),
159161
};
160162

161-
if let Some(contrib) = &config.contributing_url {
163+
if let Some(ref mut welcome) = welcome
164+
&& let Some(contrib) = &config.contributing_url
165+
{
162166
if matches!(
163167
event.issue.author_association,
164168
AuthorAssociation::FirstTimer | AuthorAssociation::FirstTimeContributor
@@ -167,7 +171,7 @@ pub(super) async fn handle_input(
167171
welcome.push_str(&messages::contribution_message(contrib, &ctx.username));
168172
}
169173
}
170-
Some(welcome)
174+
welcome
171175
} else {
172176
// No welcome is posted if they used `r?` in the opening body.
173177
None

src/handlers/check_commits/validate_config.rs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,44 @@ pub(super) async fn validate_config(
3333
let triagebot_content = triagebot_content.unwrap_or_default();
3434
let triagebot_content = String::from_utf8_lossy(&*triagebot_content);
3535

36-
let Err(e) = toml::from_str::<crate::handlers::Config>(&triagebot_content) else {
37-
return Ok(None);
38-
};
36+
match toml::from_str::<crate::handlers::Config>(&triagebot_content) {
37+
Err(e) => {
38+
let position = match e.span() {
39+
// toml sometimes gives bad spans, see https://github.com/toml-rs/toml/issues/589
40+
Some(span) if span != (0..0) => {
41+
let (line, col) = translate_position(&triagebot_content, span.start);
42+
let url = format!(
43+
"https://github.com/{}/blob/{}/{CONFIG_FILE_NAME}#L{line}",
44+
repo.full_name, pr_source.sha
45+
);
46+
format!(" at position [{line}:{col}]({url})",)
47+
}
48+
Some(_) | None => String::new(),
49+
};
3950

40-
let position = match e.span() {
41-
// toml sometimes gives bad spans, see https://github.com/toml-rs/toml/issues/589
42-
Some(span) if span != (0..0) => {
43-
let (line, col) = translate_position(&triagebot_content, span.start);
44-
let url = format!(
45-
"https://github.com/{}/blob/{}/{CONFIG_FILE_NAME}#L{line}",
46-
repo.full_name, pr_source.sha
47-
);
48-
format!(" at position [{line}:{col}]({url})",)
51+
Ok(Some(format!(
52+
"Invalid `triagebot.toml`{position}:\n\
53+
`````\n\
54+
{e}\n\
55+
`````",
56+
)))
4957
}
50-
Some(_) | None => String::new(),
51-
};
58+
Ok(config) => {
59+
// Error if `[assign.owners]` is not empty (ie auto-assign) and the custom welcome message for assignee isn't set.
60+
if let Some(assign) = config.assign
61+
&& !assign.owners.is_empty()
62+
&& let Some(custom_welcome_messages) = &assign.custom_welcome_messages
63+
&& custom_welcome_messages.welcome_message.is_none()
64+
{
65+
return Ok(Some(
66+
"Invalid `triagebot.toml`:\n\
67+
`[assign.owners]` is populated but `[assign.custom_welcome_messages.welcome-message]` is not set!".to_string()
68+
));
69+
}
5270

53-
Ok(Some(format!(
54-
"Invalid `triagebot.toml`{position}:\n\
55-
`````\n\
56-
{e}\n\
57-
`````",
58-
)))
71+
Ok(None)
72+
}
73+
}
5974
}
6075

6176
/// Helper to translate a toml span to a `(line_no, col_no)` (1-based).

0 commit comments

Comments
 (0)