github / issue-metrics

Gather metrics on issues/prs/discussions such as time to first response, count of issues opened, closed, etc.

Home Page:https://github.blog/2023-07-19-metrics-for-issues-pull-requests-and-discussions/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Filtering by project raising `query is invalid` error

daniel-petitfils opened this issue · comments

I am trying to set up a Github action to generate a bi-weekly report on open and closed issues within a specific project. I have a working file for all issues inside a repository, but cannot seem to find a way to run a project query with issue-metrics. The query that I am sending to the docker container is working in a local sandbox with github3.py.

I blanked the name of the org in the following extracts.

sprint_metrics.yml workflow file:

name: Sprint issue metrics
on:
  workflow_dispatch:
env:
  PROJECT: 7
  ORG: NAME_OF_THE_ORG

permissions:
  issues: write
  pull-requests: read

jobs:
  build:
    name: issue metrics
    runs-on: ubuntu-latest
    steps:
    - name: Get dates for last week
      shell: bash
      run: |
        # Calculate the first day of the previous week - to be run on Mondays
        first_day=$(date -d "today -2 week" +%Y-%m-%d)

        # Calculate the last day of the previous week
        last_day=$(date -d "today" +%Y-%m-%d)

        #Set an environment variable with the date range
        echo "last_sprint=$first_day..$last_day" >> "$GITHUB_ENV"

        #Set an environment variable with the date range
        echo "today=$last_day" >> "$GITHUB_ENV"

    - name: Run issue-metrics tool
      uses: github/issue-metrics@v2
      env:
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        SEARCH_QUERY: 'org:${{ env.ORG }} project:${{ env.ORG }}/${{ env.PROJECT }} is:issue created:${{ env.last_sprint }}'

    - name: Save data to md file
      shell: bash
      run: |
        sed -i 's/# Issue Metrics/# Created Issues/g' ./issue_metrics.md
        cat ./issue_metrics.md >> ./report.md

    - name: Run issue-metrics tool
      uses: github/issue-metrics@v2
      env:
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        SEARCH_QUERY: 'org:${{ env.ORG }} project:${{ env.ORG }}/${{ env.PROJECT }} is:issue closed:${{ env.last_sprint }}'

    - name: Save data to md file
      shell: bash
      run: |
        sed -i 's/# Issue Metrics/# Closed Issues/g' ./issue_metrics.md
        cat ./issue_metrics.md >> ./report.md

    - name: Create issue for metrics
      uses: peter-evans/create-issue-from-file@v4
      with:
        title: Sprint ending ${{ env.today }} - Issue metrics report
        token: ${{ secrets.GITHUB_TOKEN }}
        content-filepath: ./report.md
        labels: report

Log of job failure

Run github/issue-metrics@v2
  env:
    PROJECT: 7
    ORG: NAME_OF_THE_ORG
    last_sprint: 2024-02-08..2024-02-22
    today: 2024-02-22
    GH_TOKEN: ***
    SEARCH_QUERY: org:NAME_OF_THE_ORG project:NAME_OF_THE_ORG/7 is:issue created:2024-02-08..2024-02-22
/usr/bin/docker run --name ghcriogithubissue_metricsv2_75014b --label 5ca9ce --workdir /github/workspace --rm -e "PROJECT" -e "ORG" -e "last_sprint" -e "today" -e "GH_TOKEN" -e "SEARCH_QUERY" -e "HOME" -e "GITHUB_JOB" -e "GITHUB_REF" -e "GITHUB_SHA" -e "GITHUB_REPOSITORY" -e "GITHUB_REPOSITORY_OWNER" -e "GITHUB_REPOSITORY_OWNER_ID" -e "GITHUB_RUN_ID" -e "GITHUB_RUN_NUMBER" -e "GITHUB_RETENTION_DAYS" -e "GITHUB_RUN_ATTEMPT" -e "GITHUB_REPOSITORY_ID" -e "GITHUB_ACTOR_ID" -e "GITHUB_ACTOR" -e "GITHUB_TRIGGERING_ACTOR" -e "GITHUB_WORKFLOW" -e "GITHUB_HEAD_REF" -e "GITHUB_BASE_REF" -e "GITHUB_EVENT_NAME" -e "GITHUB_SERVER_URL" -e "GITHUB_API_URL" -e "GITHUB_GRAPHQL_URL" -e "GITHUB_REF_NAME" -e "GITHUB_REF_PROTECTED" -e "GITHUB_REF_TYPE" -e "GITHUB_WORKFLOW_REF" -e "GITHUB_WORKFLOW_SHA" -e "GITHUB_WORKSPACE" -e "GITHUB_ACTION" -e "GITHUB_EVENT_PATH" -e "GITHUB_ACTION_REPOSITORY" -e "GITHUB_ACTION_REF" -e "GITHUB_PATH" -e "GITHUB_ENV" -e "GITHUB_STEP_SUMMARY" -e "GITHUB_STATE" -e "GITHUB_OUTPUT" -e "RUNNER_OS" -e "RUNNER_ARCH" -e "RUNNER_NAME" -e "RUNNER_ENVIRONMENT" -e "RUNNER_TOOL_CACHE" -e "RUNNER_TEMP" -e "RUNNER_WORKSPACE" -e "ACTIONS_RUNTIME_URL" -e "ACTIONS_RUNTIME_TOKEN" -e "ACTIONS_CACHE_URL" -e "ACTIONS_RESULTS_URL" -e GITHUB_ACTIONS=true -e CI=true -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/work/_temp/_runner_file_commands":"/github/file_commands" -v "/home/runner/work/test-aws-action/test-aws-action":"/github/workspace" ghcr.io/github/issue_metrics:v2
Starting issue-metrics search...
Searching for issues...
The search query is invalid; Check the search query.

And the following code is working in a python sandbox, with the search query copied and pasted directly from the job log. It returns a list of issues as per the query. I also tested that query directly at https://github.com/search, and it works there too.

import github3

github_connection = github3.login(token="REDACTED")
search_query = "org:NAME_OF_THE_ORG project:NAME_OF_THE_ORG/7 is:issue created:2024-02-08..2024-02-22"
print("Searching for issues...")
issues_iterator = github_connection.search_issues(search_query, per_page=100)

# Print the issue titles
issues = []
try:
    for issue in issues_iterator:
        print(issue.title)  # type: ignore
        issues.append(issue)
except github3.exceptions.ForbiddenError:
    print(
        "You do not have permission to view this repository; Check you API Token."
    )
    sys.exit(1)
except github3.exceptions.NotFoundError:
    print("The repository could not be found; Check the repository owner and name.")
    sys.exit(1)
except github3.exceptions.ConnectionError:
    print(
        "There was a connection error; Check your internet connection or API Token."
    )
    sys.exit(1)
except github3.exceptions.AuthenticationFailed:
    print("Authentication failed; Check your API Token.")
    sys.exit(1)
except github3.exceptions.UnprocessableEntity:
    print("The search query is invalid; Check the search query.")
    sys.exit(1)

Is there some special character escaping required to go from environment variables to python? Removing the project filtering does make the query successful again so I am wondering if the slash is transferred correctly from the environment (where the log shows the query is correct) to python.

👋 Hi there! Sorry it took me so long to respond to this. 😓

I think the crux of the issue you are experiencing here is that you are using the default token GITHUB_TOKEN in the action step where you run github/issue-metrics@v2. The error messages you are receiving are noting that your permissions with that token are not sufficient to get the search data you are requesting. You should pursue setting up an API Token, store that in your repository secrets (of the repository that you are running this action in), and then see if you get the same results. Does that help?

Thanks, that worked