bashgitcurl

Filter the pull request in GitHub


I am trying to filter the Pull Requests in Github If particular PR is there we don't need to create PR for that I have included below code but its saying error line 61: syntax error: unexpected end of file

Usual PR name should be like = Checking drift in the resource: $RESOURCE

echo "Checking if a pull request for '$RESOURCE' already exists"
        existing_pr=$(curl -s -H "Authorization: Bearer $(pat-token)" \
        https://github.mytab.com/api/v3/repos/rep/rep-infra/pulls?state=open&head=drift-rep-${RESOURCE} \
         | jq '.[] | select(.title | test("Checking drift in the resource: '"$RESOURCE"'"))')
        if [ -n "$existing_pr" ]; then
        echo "Pull request already exists for '$RESOURCE'. Skipping PR creation."
        else
        echo "Creating Pull Request now"

Is any wrong with logic ?


Solution

  • Possibly fatal bugs:

    1. You appear to be missing a fi after an if.
    2. You appear to have some & in an unquoted string.
    3. It seems to me that "$RESOURCE" in the jq command is not going to appear in double quotes to jq.

    Other considerations:

    1. jq has an --exit-status flag that is probably more suitable than [ -n "$existing_pr" ].
    2. Consider passing RESOURCE into jq as a variable.
    3. Are you sure you don't want to use gh for this?

    Perhaps:

    echo "Checking if a pull request for '$RESOURCE' already exists"
    gh_token=$(pat-token)
    url="https://github.mytab.com/api/v3/repos/rep/rep-infra/pulls?state=open&head=drift-rep-${RESOURCE}"
    jq='.[] |
        select(.title |
               test("Checking drift in the resource: $resource"))'
    json=$(curl -sfH "Authorization: Bearer $gh_token" "$url")
    if jq --exit-status --arg resource "$RESOURCE" "$jq"; then
        echo "Pull request already exists for '$RESOURCE'. Skipping PR creation."
    else
        echo 'Creating Pull Request now'
    fi