Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github API: Get pull request for specific release tag

Is it possible to get a list of pull requests (or just the numbers) related to a release tag?


I have been looking at the Github API documentation all day and have tried different things but i can't see how i could make this work.

I can't see that the pull request information is available when i get a commit via the API, even if the pull request id & link is available here for example: https://github.com/octokit/octokit.rb/commit/1d82792d7d16457206418850a3ed0a0230defc81 (see the #962 link next to "master" in the top left)

like image 945
Kenny Lindahl Avatar asked Oct 17 '22 23:10

Kenny Lindahl


2 Answers

You can extract the commits between your tag & the previous one and search issues (of type pull request) with each of those commit SHA using search API :

  • use Get tags to extract the list of tags for your repo
  • extract the specific tag (release tag name) you are interested to & the previous tag (release tag if existing)
  • get all commits between these 2 tags using compare
  • search issues of type pull request with the specific SHA. In this case, you can perform a query(*) with all the commits sha concatenating SHA:<commit sha> to the query

(*)Note that the search query is limited to 256 characters so you'll have to split those search API call

Example using bash, curl & jq :

#!/bin/bash

current_tag="v4.8.0"
name_with_owner="octokit/octokit.rb"
access_token="YOUR_ACCESS_TOKEN"

tags=$(curl -s -H "Authorization: Token $access_token" \
            "https://api.github.com/repos/$name_with_owner/tags")
key=$(jq -r --arg current_tag $current_tag 'to_entries | .[] | select(.value.name == $current_tag) | .key' <<< "$tags")
previous_tag=$(jq -r --arg index $((key+1)) '.[$index | tonumber].name' <<< "$tags")

echo "compare between $previous_tag & $current_tag"

commits=$(curl -s -H "Authorization: Token $access_token" \
               "https://api.github.com/repos/$name_with_owner/compare/$previous_tag...$current_tag" | \
               jq -r '.commits[].sha')


# you can have a query of maximum of 256 character so we only process 17 sha for each request
count=0
max_per_request=17
while read sha; do
    if [ $count == 0 ]; then
        query="repo:$name_with_owner%20type:pr"
    fi
    query="$query%20SHA:%20${sha:0:7}"
    count=$((count+1))
    if ! (($count % $max_per_request)); then
        echo "https://api.github.com/search/issues?q=$query"
        curl -s -H "Authorization: Token $access_token" \
            "https://api.github.com/search/issues?q=$query" | jq -r '.items[].html_url'
        count=0
    fi
done <<< "$commits"
like image 58
Bertrand Martel Avatar answered Oct 21 '22 02:10

Bertrand Martel


I did it in Ruby:

  • Using octokit gem
  • Only looking for commits with the prefix fix or feat
  • Purpose:

The code looks like this:

require 'octokit'
Client = Octokit::Client.new(access_token: YOUR_GITHUB_TOKEN)

def getCommitsForTag(repo, tagName)
  previousTag = getPreviousTag repo, tagName
  (Client.compare repo, previousTag.name, tagName).commits
end

def getPreviousTag(repo, tagName)
  tags = Client.tags repo
  tags.each_with_index { |tag, index|
    if tag.name == tagName
      return tags[index+1]
    end
  }
end

def filterCommitsByPrefix(commits, commitPrefixArray)
  filteredArray = []
  commits.each { |commit|
    commitPrefixArray.each { |commitPrefix|
      if commit.commit.message.start_with?(commitPrefix)
        filteredArray.push(commit)
      end
    }
  }
  filteredArray
end

def getPullRequestsByCommits(commits)
  query = "SHA:"
  commits.each { |commit| 
    query += "#{commit.sha},"
  }
  Client.search_issues query
end


def getPullRequestsForTag(repo, tag)
  commitsForTag = getCommitsForTag repo, tag
  fixCommits = filterCommitsByPrefix commitsForTag, ['fix']
  featCommits = filterCommitsByPrefix commitsForTag, ['feat']
  {
    fixes: getPullRequestsByCommits(fixCommits).items,
    features: getPullRequestsByCommits(featCommits).items
  }
end

#Execute it like this:
pullRequestsForTag = getPullRequestsForTag 'octokit/octokit.rb', 'v4.8.0'

puts "Fix pull requests:"
puts pullRequestsForTag[:fixes]
puts "Feat pull requests:"
puts pullRequestsForTag[:features]
like image 20
Kenny Lindahl Avatar answered Oct 21 '22 03:10

Kenny Lindahl