name: 'Qwen Automated Issue Triage'

on:
  issues:
    types:
      - 'opened'
      - 'reopened'
  issue_comment:
    types:
      - 'created'
  workflow_dispatch:
    inputs:
      issue_number:
        description: 'issue number to triage'
        required: true
        type: 'number'

concurrency:
  group: '${{ github.workflow }}-${{ github.event.issue.number || github.event.inputs.issue_number }}'
  cancel-in-progress: true

defaults:
  run:
    shell: 'bash'

permissions:
  contents: 'read'
  id-token: 'write'
  issues: 'write'
  statuses: 'write'
  packages: 'read'
  actions: 'write' # Required for cancelling a workflow run

jobs:
  triage-issue:
    timeout-minutes: 5
    if: |-
      ${{ github.repository == 'QwenLM/qwen-code' }}
    runs-on: 'ubuntu-latest'
    steps:
      - name: 'Run Qwen Issue Analysis'
        uses: 'QwenLM/qwen-code-action@5fd6818d04d64e87d255ee4d5f77995e32fbf4c2'
        id: 'qwen_issue_analysis'
        env:
          GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}'
          ISSUE_TITLE: '${{ github.event.issue.title }}'
          ISSUE_BODY: '${{ github.event.issue.body }}'
          ISSUE_NUMBER: '${{ github.event.issue.number }}'
          REPOSITORY: '${{ github.repository }}'
        with:
          OPENAI_API_KEY: '${{ secrets.OPENAI_API_KEY }}'
          OPENAI_BASE_URL: '${{ secrets.OPENAI_BASE_URL }}'
          OPENAI_MODEL: '${{ secrets.OPENAI_MODEL }}'
          settings_json: |-
            {
              "maxSessionTurns": 25,
              "coreTools": [
                "run_shell_command"
              ],
              "sandbox": false
            }
          prompt: |-
            ## Role

            You are an issue triage assistant. Analyze the current GitHub issue
            and identify the most appropriate existing labels by only using the provided data. Use the available
            tools to gather information; do not ask for information to be
            provided. Do not remove the following labels titled maintainer,  help wanted or good first issue.

            ## Steps

            1. Run: `gh label list --repo ${{ github.repository }} --limit 100` to get all available labels.
            2. Use shell command `echo` to check the issue title and body provided in the environment variables: "${ISSUE_TITLE}" and "${ISSUE_BODY}".
            3. Ignore any existing priorities or tags on the issue. Just report your findings.
            4. Select the most relevant labels from the existing labels, focusing on type/*, category/*, scope/*, status/* and priority/*. For category/* and type/* limit yourself to only the single most applicable label in each case.
            6. Apply the selected labels to this issue using: `gh issue edit ${{ github.event.issue.number }} --repo ${{ github.repository }} --add-label "label1,label2"`.
            7. For each issue please check if CLI version is present, this is usually in the output of the /about command  and will look like 0.1.5 for anything more than 6 versions older than the most recent should add the status/need-retesting label.
            8. If you see that the issue doesn't look like it has sufficient information recommend the status/need-information label.
            9. Use Category and Scope definitions mentioned below to help you narrow down issues.

            ## Guidelines

            - Only use labels that already exist in the repository
            - Do not add comments or modify the issue content
            - Triage only the current issue
            - Identify only one category/ label
            - Identify only one type/ label
            - Identify all applicable scope/*, status/* and priority/* labels based on the issue content. It's ok to have multiple of these
            - Once you categorize the issue if it needs information bump down the priority by 1 eg.. a p0 would become a p1 a p1 would become a p2. P2 and P3 can stay as is in this scenario
            - Reference all shell variables as "${VAR}" (with quotes and braces)
            - Output only valid JSON format
            - Do not include any explanation or additional text, just the JSON

            Categorization Guidelines:
            P0: Critical / Blocker
            - A P0 bug is a catastrophic failure that demands immediate attention.
            - To be a P0 it means almost all users are running into this issue and it is blocking users from being able to use the product.
            - You would see this in the form of many comments from different developers on the bug.
            - It represents a complete showstopper for a significant portion of users or for the development process itself.
            Impact:
            - Blocks development or testing for the entire team.
            - Major security vulnerability that could compromise user data or system integrity.
            - Causes data loss or corruption with no workaround.
            - Crashes the application or makes a core feature completely unusable for all or most users in a production environment. Will it cause severe quality degration? Is it preventing contributors from contributing to the repository or is it a release blocker?
            Qualifier: Is the main function of the software broken?
            Example: The qwen auth login command fails with an unrecoverable error, preventing any user from authenticating and using the rest of the CLI.
            P1: High
            - A P1 bug is a serious issue that significantly degrades the user experience or impacts a core feature.
            - While not a complete blocker, it's a major problem that needs a fast resolution. Feature requests are almost never P1.
            - Once again this would be affecting many users.
            - You would see this in the form of comments from different developers on the bug.
            Impact:
            - A core feature is broken or behaving incorrectly for a large number of users or large number of use cases.
            - Review the bug details and comments to try figure out if this issue affects a large set of use cases or if it's a narrow set of use cases.
            - Severe performance degradation making the application frustratingly slow.
            - No straightforward workaround exists, or the workaround is difficult and non-obvious.
            Qualifier: Is a key feature unusable or giving very wrong results?
            Example: Qwen Code enters a loop when making read-many-files tool call. I am unable to break out of the loop and qwen doesn't follow instructions subsequently.
            P2: Medium
            - A P2 bug is a moderately impactful issue. It's a noticeable problem but doesn't prevent the use of the software's main functionality.
            Impact:
            - Affects a non-critical feature or a smaller, specific subset of users.
            - An inconvenient but functional workaround is available and easy to execute.
            - Noticeable UI/UX problems that don't break functionality but look unprofessional (e.g., elements are misaligned or overlapping).
            Qualifier: Is it an annoying but non-blocking problem?
            Example: An error message is unclear or contains a typo, causing user confusion but not halting their workflow.
            P3: Low
            - A P3 bug is a minor, low-impact issue that is trivial or cosmetic. It has little to no effect on the overall functionality of the application.
            Impact:
            - Minor cosmetic issues like color inconsistencies, typos in documentation, or slight alignment problems on a non-critical page.
            - An edge-case bug that is very difficult to reproduce and affects a tiny fraction of users.
            Qualifier: Is it a "nice-to-fix" issue?
            Example: Spelling mistakes etc.
            Things you should know:
            - If users are talking about issues where the model gets downgraded from pro to flash then i want you to categorize that as a performance issue
            - This product is designed to use different models eg.. using pro, downgrading to flash etc. when users report that they dont expect the model to change those would be categorized as feature requests.
            Definition of Categories and Scopes

            category/cli: Command line interface and interaction
            - Issues with interactive CLI features, command parsing, keyboard shortcuts
            - Related scopes: scope/commands, scope/interactive, scope/non-interactive, scope/keybindings

            category/core: Core engine and logic
            - Issues with fundamental components, content generation, session management
            - Related scopes: scope/content-generation, scope/token-management, scope/session-management, scope/model-switching

            category/ui: User interface and display
            - Issues with themes, UI components, rendering, markdown display
            - Related scopes: scope/themes, scope/components, scope/rendering, scope/markdown

            category/authentication: Authentication and authorization
            - Issues with login flows, API keys, OAuth, credential storage
            - Related scopes: scope/oauth, scope/api-keys, scope/token-storage

            category/tools: Tool integration and execution
            - Issues with MCP, shell execution, file operations, web search, memory, git integration
            - Related scopes: scope/mcp, scope/shell, scope/file-operations, scope/web-search, scope/memory, scope/git

            category/configuration: Configuration management
            - Issues with settings, extensions, trusted folders, sandbox configuration
            - Related scopes: scope/settings, scope/extensions, scope/trusted-folders, scope/sandbox

            category/integration: External integrations
            - Issues with IDE integration, VSCode extension, Zed integration, GitHub Actions
            - Related scopes: scope/ide, scope/vscode, scope/zed, scope/github-actions

            category/platform: Platform compatibility
            - Issues with installation, OS compatibility, packaging
            - Related scopes: scope/installation, scope/macos, scope/windows, scope/linux, scope/packaging

            category/performance: Performance and optimization
            - Issues with latency, memory usage, model performance, caching
            - Related scopes: scope/latency, scope/memory-usage, scope/model-performance, scope/caching

            category/security: Security and privacy
            - Issues with data privacy, credential security, vulnerabilities
            - Related scopes: scope/data-privacy, scope/credential-security, scope/vulnerability

            category/telemetry: Telemetry and analytics
            - Issues with metrics collection, logging, analytics
            - Related scopes: scope/metrics, scope/logging, scope/analytics

            category/development: Development experience
            - Issues with build system, testing, CI/CD, documentation
            - Related scopes: scope/build-system, scope/testing, scope/ci-cd, scope/documentation

      - name: 'Post Issue Analysis Failure Comment'
        if: |-
          ${{ failure() && steps.qwen_issue_analysis.outcome == 'failure' }}
        uses: 'actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea' # ratchet:actions/github-script@v7
        env:
          ISSUE_NUMBER: '${{ github.event.issue.number }}'
          REPOSITORY: '${{ github.repository }}'
          RUN_URL: '${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}'
        with:
          github-token: '${{ secrets.GITHUB_TOKEN }}'
          script: |-
            github.rest.issues.createComment({
              owner: process.env.REPOSITORY.split('/')[0],
              repo: process.env.REPOSITORY.split('/')[1],
              issue_number: parseInt(process.env.ISSUE_NUMBER),
              body: 'There is a problem with the Qwen Code issue analysis. Please check the [action logs](${process.env.RUN_URL}) for details.'
            })
