Skip to content

Commit afda4ec

Browse files
committed
basic code for issues
1 parent e4882cc commit afda4ec

File tree

1 file changed

+59
-42
lines changed

1 file changed

+59
-42
lines changed

mdbook-goals/src/rfc.rs

Lines changed: 59 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -101,29 +101,13 @@ pub fn generate_issues(repository: &str, path: &Path, dry_run: bool) -> anyhow::
101101

102102
let goal_documents = goal::goals_in_dir(path)?;
103103
let teams_with_asks = teams_with_asks(&goal_documents);
104-
// let issues: Vec<_> = goal_documents
105-
// .iter()
106-
// .map(|goal_document| {
107-
// let title = format!("Goal: {}", goal_document.title);
108-
// let owners = goal_document.metadata.owner_usernames();
109-
// let body = goal_document.description.clone();
110-
// let teams = goal_document
111-
// .team_asks
112-
// .iter()
113-
// .flat_map(|ask| &ask.teams)
114-
// .copied()
115-
// .collect::<BTreeSet<&TeamName>>();
116-
117-
// GithubIssue {
118-
// title,
119-
// owners,
120-
// body,
121-
// teams,
122-
// }
123-
// })
124-
// .collect();
125-
126104
let mut actions = initialize_labels(repository, &teams_with_asks)?;
105+
actions.extend(initialize_issues(repository, &goal_documents)?);
106+
107+
if actions.is_empty() {
108+
eprintln!("No actions to be executed.");
109+
return Ok(());
110+
}
127111

128112
eprintln!("Actions to be executed:");
129113
for action in &actions {
@@ -149,11 +133,11 @@ pub struct GithubIssue {
149133

150134
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
151135
enum GithubAction {
152-
CreateLabel { name: String, color: String },
136+
CreateLabel { label: GhLabel },
153137
CreateIssue { issue: GithubIssue },
154138
}
155139

156-
#[derive(Debug, Serialize, Deserialize)]
140+
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
157141
struct GhLabel {
158142
name: String,
159143
color: String,
@@ -176,36 +160,65 @@ fn list_labels(repository: &str) -> anyhow::Result<Vec<GhLabel>> {
176160

177161
/// Initializes the required `T-<team>` labels on the repository.
178162
/// Warns if the labels are found with wrong color.
179-
pub fn initialize_labels(
163+
fn initialize_labels(
180164
repository: &str,
181165
teams_with_asks: &BTreeSet<&TeamName>,
182166
) -> anyhow::Result<BTreeSet<GithubAction>> {
183167
const TEAM_LABEL_COLOR: &str = "bfd4f2";
184168

185-
let existing_labels = list_labels(repository)?;
186-
187-
Ok(teams_with_asks
169+
let mut desired_labels: BTreeSet<_> = teams_with_asks
188170
.iter()
189-
.flat_map(|team| {
171+
.map(|team| {
190172
let label_name = team.gh_label();
191173

192-
if let Some(existing_label) = existing_labels
193-
.iter()
194-
.find(|label| label.name == label_name)
195-
{
196-
if existing_label.color == TEAM_LABEL_COLOR {
197-
return None;
198-
}
199-
}
200-
201-
Some(GithubAction::CreateLabel {
174+
GhLabel {
202175
name: label_name,
203176
color: TEAM_LABEL_COLOR.to_string(),
204-
})
177+
}
205178
})
179+
.collect();
180+
181+
for existing_label in list_labels(repository)? {
182+
desired_labels.remove(&existing_label);
183+
}
184+
185+
Ok(desired_labels
186+
.into_iter()
187+
.map(|label| GithubAction::CreateLabel { label })
206188
.collect())
207189
}
208190

191+
/// Initializes the required `T-<team>` labels on the repository.
192+
/// Warns if the labels are found with wrong color.
193+
fn initialize_issues(
194+
repository: &str,
195+
document: &[GoalDocument],
196+
) -> anyhow::Result<BTreeSet<GithubAction>> {
197+
// let issues: Vec<_> = goal_documents
198+
// .iter()
199+
// .map(|goal_document| {
200+
// let title = format!("Goal: {}", goal_document.title);
201+
// let owners = goal_document.metadata.owner_usernames();
202+
// let body = goal_document.description.clone();
203+
// let teams = goal_document
204+
// .team_asks
205+
// .iter()
206+
// .flat_map(|ask| &ask.teams)
207+
// .copied()
208+
// .collect::<BTreeSet<&TeamName>>();
209+
210+
// GithubIssue {
211+
// title,
212+
// owners,
213+
// body,
214+
// teams,
215+
// }
216+
// })
217+
// .collect();
218+
219+
Ok(None.into_iter().collect())
220+
}
221+
209222
fn teams_with_asks(goal_documents: &[GoalDocument]) -> BTreeSet<&'static TeamName> {
210223
goal_documents
211224
.iter()
@@ -218,7 +231,9 @@ fn teams_with_asks(goal_documents: &[GoalDocument]) -> BTreeSet<&'static TeamNam
218231
impl Display for GithubAction {
219232
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
220233
match self {
221-
GithubAction::CreateLabel { name, color } => {
234+
GithubAction::CreateLabel {
235+
label: GhLabel { name, color },
236+
} => {
222237
write!(f, "create label `{}` with color `{}`", name, color)
223238
}
224239
GithubAction::CreateIssue { issue } => {
@@ -231,7 +246,9 @@ impl Display for GithubAction {
231246
impl GithubAction {
232247
pub fn execute(self, repository: &str) -> anyhow::Result<()> {
233248
match self {
234-
GithubAction::CreateLabel { name, color } => {
249+
GithubAction::CreateLabel {
250+
label: GhLabel { name, color },
251+
} => {
235252
let output = Command::new("gh")
236253
.arg("-R")
237254
.arg(repository)

0 commit comments

Comments
 (0)