@@ -101,29 +101,13 @@ pub fn generate_issues(repository: &str, path: &Path, dry_run: bool) -> anyhow::
101
101
102
102
let goal_documents = goal:: goals_in_dir ( path) ?;
103
103
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
-
126
104
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
+ }
127
111
128
112
eprintln ! ( "Actions to be executed:" ) ;
129
113
for action in & actions {
@@ -149,11 +133,11 @@ pub struct GithubIssue {
149
133
150
134
#[ derive( Debug , PartialEq , Eq , PartialOrd , Ord ) ]
151
135
enum GithubAction {
152
- CreateLabel { name : String , color : String } ,
136
+ CreateLabel { label : GhLabel } ,
153
137
CreateIssue { issue : GithubIssue } ,
154
138
}
155
139
156
- #[ derive( Debug , Serialize , Deserialize ) ]
140
+ #[ derive( Debug , Serialize , Deserialize , PartialEq , Eq , PartialOrd , Ord ) ]
157
141
struct GhLabel {
158
142
name : String ,
159
143
color : String ,
@@ -176,36 +160,65 @@ fn list_labels(repository: &str) -> anyhow::Result<Vec<GhLabel>> {
176
160
177
161
/// Initializes the required `T-<team>` labels on the repository.
178
162
/// Warns if the labels are found with wrong color.
179
- pub fn initialize_labels (
163
+ fn initialize_labels (
180
164
repository : & str ,
181
165
teams_with_asks : & BTreeSet < & TeamName > ,
182
166
) -> anyhow:: Result < BTreeSet < GithubAction > > {
183
167
const TEAM_LABEL_COLOR : & str = "bfd4f2" ;
184
168
185
- let existing_labels = list_labels ( repository) ?;
186
-
187
- Ok ( teams_with_asks
169
+ let mut desired_labels: BTreeSet < _ > = teams_with_asks
188
170
. iter ( )
189
- . flat_map ( |team| {
171
+ . map ( |team| {
190
172
let label_name = team. gh_label ( ) ;
191
173
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 {
202
175
name : label_name,
203
176
color : TEAM_LABEL_COLOR . to_string ( ) ,
204
- } )
177
+ }
205
178
} )
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 } )
206
188
. collect ( ) )
207
189
}
208
190
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
+
209
222
fn teams_with_asks ( goal_documents : & [ GoalDocument ] ) -> BTreeSet < & ' static TeamName > {
210
223
goal_documents
211
224
. iter ( )
@@ -218,7 +231,9 @@ fn teams_with_asks(goal_documents: &[GoalDocument]) -> BTreeSet<&'static TeamNam
218
231
impl Display for GithubAction {
219
232
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
220
233
match self {
221
- GithubAction :: CreateLabel { name, color } => {
234
+ GithubAction :: CreateLabel {
235
+ label : GhLabel { name, color } ,
236
+ } => {
222
237
write ! ( f, "create label `{}` with color `{}`" , name, color)
223
238
}
224
239
GithubAction :: CreateIssue { issue } => {
@@ -231,7 +246,9 @@ impl Display for GithubAction {
231
246
impl GithubAction {
232
247
pub fn execute ( self , repository : & str ) -> anyhow:: Result < ( ) > {
233
248
match self {
234
- GithubAction :: CreateLabel { name, color } => {
249
+ GithubAction :: CreateLabel {
250
+ label : GhLabel { name, color } ,
251
+ } => {
235
252
let output = Command :: new ( "gh" )
236
253
. arg ( "-R" )
237
254
. arg ( repository)
0 commit comments