1
1
import { NextResponse } from "next/server" ;
2
2
import { getGraphQL } from "@/app/api/github" ;
3
3
import { CoverageData } from "@/app/dashboard/coverage/types" ;
4
- import { ABSOLUTE_URLS } from "@/config" ;
4
+ import { ABSOLUTE_URLS , PRODUCT_ID } from "@/config" ;
5
5
import { supabase } from "@/lib/supabase" ;
6
6
7
7
type CreateIssueResponse = {
@@ -13,9 +13,17 @@ type CreateIssueResponse = {
13
13
} ;
14
14
} ;
15
15
16
+ type CreateLabelResponse = {
17
+ createLabel : {
18
+ label : {
19
+ id : string ;
20
+ } ;
21
+ } ;
22
+ } ;
23
+
16
24
export async function POST ( request : Request ) {
17
25
try {
18
- const { selectedCoverages, ownerName, repoName, accessToken, parentNodeId } =
26
+ const { selectedCoverages, ownerName, repoName, accessToken, parentNodeId, hasLabel } =
19
27
await request . json ( ) ;
20
28
21
29
if ( ! selectedCoverages ?. length || ! ownerName || ! repoName || ! accessToken || ! parentNodeId ) {
@@ -31,19 +39,68 @@ export async function POST(request: Request) {
31
39
32
40
const graphqlClient = getGraphQL ( accessToken ) ;
33
41
34
- // Get repository Node ID
35
- const getRepoId = await graphqlClient < { repository : { id : string } } > (
42
+ // Get repository Node ID and label ID if needed
43
+ const getRepoAndLabel = await graphqlClient < {
44
+ repository : {
45
+ id : string ;
46
+ labels ?: {
47
+ nodes : Array < {
48
+ id : string ;
49
+ name : string ;
50
+ } > ;
51
+ } ;
52
+ } ;
53
+ } > (
36
54
`
37
- query getRepoId ($owner: String!, $name: String!) {
55
+ query getRepoAndLabel ($owner: String!, $name: String!, $labelName : String!) {
38
56
repository(owner: $owner, name: $name) {
39
57
id
58
+ labels(first: 1, query: $labelName) {
59
+ nodes {
60
+ id
61
+ name
62
+ }
63
+ }
40
64
}
41
65
}
42
66
` ,
43
- { owner : ownerName , name : repoName }
67
+ { owner : ownerName , name : repoName , labelName : PRODUCT_ID || "" }
44
68
) ;
45
69
46
- const repositoryId = getRepoId . repository . id ;
70
+ const repositoryId = getRepoAndLabel . repository . id ;
71
+ const labels = getRepoAndLabel . repository . labels ?. nodes ;
72
+ console . log ( "labels: " , labels ) ;
73
+ let labelId =
74
+ hasLabel && PRODUCT_ID ? labels ?. find ( ( label ) => label . name === PRODUCT_ID ) ?. id : null ;
75
+
76
+ // If label doesn't exist but is needed, create it
77
+ if ( hasLabel && PRODUCT_ID && ! labelId ) {
78
+ try {
79
+ const createLabelResponse = await graphqlClient < CreateLabelResponse > (
80
+ `
81
+ mutation createLabel($input: CreateLabelInput!) {
82
+ createLabel(input: $input) {
83
+ label {
84
+ id
85
+ }
86
+ }
87
+ }
88
+ ` ,
89
+ {
90
+ input : {
91
+ repositoryId,
92
+ name : PRODUCT_ID ,
93
+ color : "EC4899" , // GitAuto brand color (pink)
94
+ description : "Issues to be handled by GitAuto" ,
95
+ } ,
96
+ }
97
+ ) ;
98
+ labelId = createLabelResponse . createLabel . label . id ;
99
+ } catch ( error ) {
100
+ console . error ( "Failed to create label:" , error ) ;
101
+ // If label creation fails, continue with issue creation
102
+ }
103
+ }
47
104
48
105
const createdIssues = await Promise . all (
49
106
selectedCoverages . map ( async ( coverage : CoverageData ) => {
@@ -91,6 +148,7 @@ View full coverage details in the [Coverage Dashboard](${ABSOLUTE_URLS.GITAUTO.C
91
148
title,
92
149
body,
93
150
...( parentNodeId && { parentIssueId : parentNodeId } ) ,
151
+ ...( labelId && { labelIds : [ labelId ] } ) ,
94
152
} ,
95
153
}
96
154
) ;
0 commit comments