Skip to content

Commit d1b9859

Browse files
committed
Add binary for simplified bot comment training
This allows us to use the same infrastructure we use for PR metadata validation, but in a more controlled setting for learning what a bot comment is. The comment it leaves is fairly artificial, but should be easy for people to understand and act on.
1 parent 1ce32a3 commit d1b9859

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/// This binary exists to be a lightweight teaching version of the pr-metadata-validator.
2+
/// Its purpose is to train trainees in the idea that bots will comment on their PRs, and they should follow their advice.
3+
/// It is installed in https://github.com/CodeYourFuture/github_issues_prs_practice as a GitHub Action.
4+
use std::process::exit;
5+
6+
use const_format::concatcp;
7+
use trainee_tracker::{
8+
octocrab::octocrab_for_token,
9+
pr_comments::{PullRequest, close_existing_comments, leave_tagged_comment},
10+
};
11+
12+
#[tokio::main]
13+
async fn main() {
14+
let Ok([_argv0, pr_url]) = <[_; _]>::try_from(std::env::args().collect::<Vec<_>>()) else {
15+
eprintln!("Expected one arg - PR URL");
16+
exit(1);
17+
};
18+
let pr_metadata = PullRequest::from_html_url(&pr_url).expect("Failed to parse PR URL");
19+
let github_token =
20+
std::env::var("GH_TOKEN").expect("GH_TOKEN wasn't set - must be set to a GitHub API token");
21+
let octocrab = octocrab_for_token(github_token.to_owned()).expect("Failed to get octocrab");
22+
23+
let pr_from_rest = octocrab
24+
.pulls(&pr_metadata.org, &pr_metadata.repo)
25+
.get(pr_metadata.number)
26+
.await
27+
.expect("Failed to get PR");
28+
if pr_from_rest
29+
.body
30+
.unwrap_or_default()
31+
.ends_with(EXPECTED_SUFFIX)
32+
{
33+
let result = close_existing_comments(&octocrab, &pr_metadata, TAG).await;
34+
if let Err(err) = result {
35+
eprintln!("Failed to close existing comments: {:?}", err);
36+
}
37+
} else {
38+
leave_tagged_comment(&octocrab, &pr_metadata, &[TAG], COMMENT_TO_LEAVE.to_owned())
39+
.await
40+
.expect("Failed to leave comment");
41+
}
42+
}
43+
44+
const EXPECTED_SUFFIX: &str = "I agree to follow the code of conduct for this organisation.";
45+
46+
const TAG: &str = "dummy-code-of-conduct-validator";
47+
48+
const COMMENT_TO_LEAVE: &str = concatcp!(
49+
COMMENT_TO_LEAVE_PREFIX,
50+
EXPECTED_SUFFIX,
51+
COMMENT_TO_LEAVE_SUFFIX
52+
);
53+
54+
const COMMENT_TO_LEAVE_PREFIX: &str = r#"This is a comment from a bot.
55+
56+
You should read it, make sure you understand it, and take the action it suggests.
57+
58+
If you don't understand the action it suggests, ask a volunteer or another trainee for help.
59+
60+
## ⚠️ Problem detected
61+
62+
In this repository, all pull request descriptions must end with the sentence:
63+
64+
> "#;
65+
66+
const COMMENT_TO_LEAVE_SUFFIX: &str = r#"
67+
68+
Your pull request description does not currently end with this sentence.
69+
70+
Please edit your pull request description to add this sentence at the end.
71+
72+
If you are successful in doing this, this comment will get automatically hidden within about a minute.
73+
"#;

0 commit comments

Comments
 (0)