@@ -258,34 +258,149 @@ const processIssueItem = async (
258258 console . log ( `Moved issue #${ issue . number } to "${ TARGET_COLUMN } "` ) ;
259259} ;
260260
261+ const handleLabeledEvent = async (
262+ octokit ,
263+ issue ,
264+ projectData ,
265+ TARGET_COLUMN ,
266+ IGNORED_COLUMNS ,
267+ TARGET_LABELS
268+ ) => {
269+ validateIssue ( issue , TARGET_LABELS ) ;
270+
271+ await processIssueItem (
272+ octokit ,
273+ projectData ,
274+ issue ,
275+ TARGET_COLUMN ,
276+ IGNORED_COLUMNS
277+ ) ;
278+ } ;
279+
280+ const handleUnlabeledEvent = async (
281+ octokit ,
282+ issue ,
283+ projectData ,
284+ DEFAULT_COLUMN ,
285+ IGNORED_COLUMNS ,
286+ TARGET_LABELS
287+ ) => {
288+ const removedLabel = github . context . payload . label . name ;
289+ if ( ! TARGET_LABELS . includes ( removedLabel ) ) {
290+ return ;
291+ }
292+
293+ const hasTargetLabel = issue . labels . some ( ( label ) =>
294+ TARGET_LABELS . includes ( label . name )
295+ ) ;
296+
297+ if ( hasTargetLabel ) {
298+ console . log (
299+ `Issue #${ issue . number } still has a target label. Not moving to default column.`
300+ ) ;
301+ return ;
302+ }
303+
304+ await moveIssueToDefaultColumn (
305+ octokit ,
306+ projectData ,
307+ issue ,
308+ DEFAULT_COLUMN ,
309+ IGNORED_COLUMNS
310+ ) ;
311+ } ;
312+
313+ const moveIssueToDefaultColumn = async (
314+ octokit ,
315+ projectData ,
316+ issue ,
317+ defaultColumn ,
318+ ignoredColumns
319+ ) => {
320+ const statusField = await getStatusField ( octokit , projectData . id ) ;
321+ const defaultStatusOption = getTargetStatusOption ( statusField , defaultColumn ) ;
322+
323+ if ( ! defaultStatusOption ) {
324+ throw new Error ( `Default column "${ defaultColumn } " not found in project` ) ;
325+ }
326+
327+ let issueItemData = await getIssueItemData (
328+ octokit ,
329+ projectData . id ,
330+ issue . node_id
331+ ) ;
332+
333+ if ( ! issueItemData ) {
334+ console . log ( `Issue #${ issue . number } is not in the project. Skipping.` ) ;
335+ return ;
336+ }
337+
338+ const currentStatus = getCurrentStatus ( issueItemData ) ;
339+
340+ if ( ignoredColumns . includes ( currentStatus ) ) {
341+ console . log (
342+ `Issue #${ issue . number } is in an ignored column (${ currentStatus } ). Skipping.`
343+ ) ;
344+ return ;
345+ }
346+
347+ await updateIssueStatus (
348+ octokit ,
349+ projectData . id ,
350+ issueItemData . id ,
351+ statusField . id ,
352+ defaultStatusOption . id
353+ ) ;
354+ console . log ( `Moved issue #${ issue . number } back to "${ defaultColumn } "` ) ;
355+ } ;
356+
261357const run = async ( ) => {
262358 try {
263359 const token = core . getInput ( "github-token" ) ;
264360 const projectUrl = core . getInput ( "project-url" ) ;
265361 const targetLabels = core . getInput ( "target-labels" ) ;
266362 const targetColumn = core . getInput ( "target-column" ) ;
267363 const ignoredColumns = core . getInput ( "ignored-columns" ) ;
364+ const defaultColumn = core . getInput ( "default-column" , { required : false } ) ;
268365
269366 const TARGET_COLUMN = targetColumn . trim ( ) ;
270367 const TARGET_LABELS = parseCommaSeparatedInput ( targetLabels ) ;
271368 const IGNORED_COLUMNS = parseCommaSeparatedInput ( ignoredColumns ) ;
369+ const DEFAULT_COLUMN = defaultColumn ? defaultColumn . trim ( ) : null ;
272370
273371 const octokit = github . getOctokit ( token ) ;
274372 const issue = github . context . payload . issue ;
275-
276- validateIssue ( issue , TARGET_LABELS ) ;
373+ const action = github . context . payload . action ;
277374
278375 const projectData = await getProjectData ( octokit , projectUrl ) ;
279376
280- await processIssueItem (
281- octokit ,
282- projectData ,
283- issue ,
284- TARGET_COLUMN ,
285- IGNORED_COLUMNS
286- ) ;
377+ if ( action === "labeled" ) {
378+ await handleLabeledEvent (
379+ octokit ,
380+ issue ,
381+ projectData ,
382+ TARGET_COLUMN ,
383+ IGNORED_COLUMNS ,
384+ TARGET_LABELS
385+ ) ;
386+ return ;
387+ }
388+
389+ if ( action === "unlabeled" && DEFAULT_COLUMN ) {
390+ await handleUnlabeledEvent (
391+ octokit ,
392+ issue ,
393+ projectData ,
394+ DEFAULT_COLUMN ,
395+ IGNORED_COLUMNS ,
396+ TARGET_LABELS
397+ ) ;
398+ return ;
399+ }
400+
401+ console . log ( `No action taken for ${ action } event.` ) ;
287402 } catch ( error ) {
288- core . setFailed ( `Error moving issue: ${ error . message } ` ) ;
403+ core . setFailed ( `Error processing issue: ${ error . message } ` ) ;
289404 }
290405} ;
291406
0 commit comments