Action

Linear Issue with Sub-Issues

Posted by tom.foutz, Last update 5 months ago

Create Issue in Linear with Sub-Issues

By Thomas Foutz
2024-08-13

Steps

  • script

    var credential = Credential.create("Linear.app", "Insert Linear.app token generated on Linear.app account page.");
    
    credential.addTextField("apptoken", "App Token");
    credential.addTextField("teamid", "Team ID");
    credential.authorize();
    
    var appToken = credential.getValue("apptoken");
    var teamId = credential.getValue("teamid");
    
    if (draft.content.length == 0) {
        alert("Draft is blank");
        context.cancel("Draft was blank");
    } else {
        var lines = draft.content.split("\n");
    
        // The first line is the title for the main issue
        let mainIssueTitle = lines[0].trim();
        let mainIssueDescription = "";  // You can customize how you want to handle the description, e.g., combining the other lines, etc.
    
        // Prepare GraphQL query for creating the main issue
        var graphqlQuery = `
        mutation IssueCreate($title: String!, $teamId: String!, $description: String) {
            issueCreate(input: { title: $title, description: $description, teamId: $teamId }) {
                success
                issue {
                    identifier
                    id
                    url
                }
            }
        }`;
    
        var variables = {
            "title": mainIssueTitle,
            "description": mainIssueDescription,
            "teamId": teamId
        };
    
        // Create main issue
        var http = HTTP.create();
        var response = http.request({
            "url": "https://api.linear.app/graphql",
            "method": "POST",
            "encoding": "json",
            "data": {
                "query": graphqlQuery,
                "variables": variables
            },
            "headers": {
                "Authorization": appToken,
                "Content-Type": "application/json"
            }
        });
    
        if (response.statusCode != 200 && response.statusCode != 202) {
            context.fail("Failed to create main issue in Linear.app: " + response.statusCode);
        } else {
            console.log("Main issue created successfully: " + response.responseText);
    
            // Parse the response to get the main issue ID
            var jsonResponse = JSON.parse(response.responseText);
            var mainIssueId = jsonResponse.data.issueCreate.issue.id;
    
            // Create sub-issues
            for (var i = 1; i < lines.length; i++) {
                let subIssueTitle = lines[i].trim();
                if (subIssueTitle.length > 0) {
                    var subIssueVariables = {
                        "title": subIssueTitle,
                        "description": "",
                        "teamId": teamId,
                        "parentId": mainIssueId
                    };
    
                    var subIssueQuery = `
                    mutation IssueCreate($title: String!, $teamId: String!, $description: String, $parentId: String) {
                        issueCreate(input: { title: $title, description: $description, teamId: $teamId, parentId: $parentId }) {
                            success
                            issue {
                                identifier
                                url
                            }
                        }
                    }`;
    
                    var subIssueResponse = http.request({
                        "url": "https://api.linear.app/graphql",
                        "method": "POST",
                        "encoding": "json",
                        "data": {
                            "query": subIssueQuery,
                            "variables": subIssueVariables
                        },
                        "headers": {
                            "Authorization": appToken,
                            "Content-Type": "application/json"
                        }
                    });
    
                    if (subIssueResponse.statusCode != 200 && subIssueResponse.statusCode != 202) {
                        console.log("Failed to create sub-issue: " + subIssueResponse.statusCode);
                    } else {
                        console.log("Sub-issue created successfully: " + subIssueResponse.responseText);
                    }
                }
            }
        }
    }
    

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.