10

I'm writing a continuous integration step that checks the titles of our pull requests for proper tags and formatting before allowing them to be merged.

To do this, I need to echo the title of the Pull Request when given the PR number. Is there a way to do this simply in command line?

Danny Delott
  • 6,756
  • 3
  • 33
  • 57

5 Answers5

5

building off of @Simon-Warta's answer you can do the following:

git clone https://github.com/user_org/project.git
pushd project
for pr in $(git log --pretty="%s" --merges | grep pull | sed -e "s/.*#\([0-9]\+\).*/\1/g" | sort -rn | uniq); do
  curl https://api.github.com/repos/user_org/project/pulls/${pr} 2>/dev/null | jq '.title';
done
popd

You'll need curl and jq available on your machine but that shouldn't be hard if you have a reasonable linux distro..

Kevin Kreiser
  • 594
  • 5
  • 11
5

With Github's new official CLI (command line interface):

gh pr view 3 --repo OWNER/REPO | head -n 1

where:

  • 3 is the PR number
  • gh pr view [options] displays the title (first line), body, and other information about the PR
  • head -n 1 only keeps the first line of the gh pr view output in order to only get the title

See additional details and options and installation instructions.

Xavier Guihot
  • 54,987
  • 21
  • 291
  • 190
2

For pull requests use

https://api.github.com/repos/randombit/botan/pulls/359

For patches in a pull request, search for Subject: in the .patch url:

https://github.com/randombit/botan/pull/359.patch

Note that you can only do 60 request per hour and IP on the Github API.

Simon Warta
  • 10,850
  • 5
  • 40
  • 78
  • The patch url does contain a subject line with the PR title, however when I edit the title and then look at the patch url again, it still contains the old title. – Danny Delott Jan 04 '16 at 23:28
  • 1
    @DannyDelott, right, because the subject is the commit message(s), not the PR title. PR data is not part of the Git system but Github specific meta data. So you'll nee to take their API if commit messages are not okay for you. – Simon Warta Jan 04 '16 at 23:41
  • that makes a lot of sense. Thanks! – Danny Delott Jan 04 '16 at 23:50
2

Thanks Kevin Kreiser. My version of his solution without jq & only current commits from last tag.

TL;DR

instead

curl https://api.github.com/repos/user_org/project/pulls/${pr} 2>/dev/null | jq '.title';

use

curl https://api.github.com/repos/user_org/project/pulls/${pr} | grep title | cut -d'"' -f4

For CocoaHeadsRu repository example:

git clone https://github.com/cocoaheadsru/application.git  // clone the repository
cd application           // go to the application folder
touch getTitles.sh       // create file getTitles.sh
chmod a+x getTitles.sh   // make the file executable
vi getTitles.sh          // open the file in the vim text editor

add the following code to the getTitles.sh:

for pr in $(git log $(git describe --tags --abbrev=0)..HEAD --pretty="%s" --merges | cut -d' ' -f4 | cut -d'#' -f2 | tr '\n' ' ')
do
    curl https://api.github.com/repos/cocoaheadsru/application/pulls/${pr} | grep title | cut -d'"' -f4
done

save the code and exit the vim

:wq

Now we can create a release notes file using our script:

./getTitles.sh > ReleaseNotes.txt
cat ReleaseNotes.txt

// [qXUdCQay] Changed app build number, deleted MARKETING_VERSION and CU…
// Feature/kn vl5 eaw/add bluetooth always key
// [CQoos82f] Updated Realm, increment app version
// upgraded travis configuration
// Открытие места проведения митапа в 2ГИС
// Автокапитализация и технические улучшения
// Интеграция Travis-CI
// Postpone fetchEvents for main view after view appearing

Description script code:

1

git describe --tags --abbrev=0                  // get the last tag
git log $(git describe --tags --abbrev=0)..HEAD // get all commit from last tag to HEAD
[--//--] --pretty="%s"                          // all commits in one line
[--//--] --merges                               // merged commits only

2

break the string into parts using the space character and get 4th value

ex Merge pull request #347 from antonsergeev88/swipeToPop_crash

[--//--] cut -d' ' -f4      // get '#347'

3

break the string into parts using the '#' character and get 2nd value

ex #347

[--//--] cut -d'#' -f2      // get '347'

4

tr '\n' ' '                 // replaced '/n' with a space character

we get something like 355 354 353 352 350 349 348 347

5

In the loop using github api:

curl https://api.github.com/repos/cocoaheadsru/application/pulls/347

we get all pull requests in json-format. One of them:

{
  "url": "https://api.github.com/repos/cocoaheadsru/application/pulls/347",
  "id": 215781239,
  "number": 347,
  "state": "closed",
  ...
  "title": "Postpone fetchEvents for main view after view appearing",
  ...
  "user": {...},
  "merged_at": "2018-09-30T17:26:47Z",
  "merge_commit_sha": "553d8ea6f189882a22d424d0770785b53ddc4ab4",
  "commits": 1,
  ...
}

6

using grep

[--//--] | grep title

get a line with the word title:

"title": "Postpone fetchEvents for main view after view appearing",

7

using cut:

[--//--] | cut -d'"' -f4

finally we get only title value:

Postpone fetchEvents for main view after view appearing
Community
  • 1
  • 1
A.Kant
  • 2,750
  • 3
  • 19
  • 15
1

If you are using github:

Locate the section for your github remote in the .git/config file. It looks like this:

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = git@github.com:a.git

Now add the line fetch = +refs/pull/*/head:refs/remotes/origin/pr/* to this section. Obviously, change the github url to match your project's URL. It ends up looking like this:

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = git@github.com:a.git
    fetch = +refs/pull/*/head:refs/remotes/origin/pr/*

Now fetch all the pull requests:

$ git fetch origin
From github.com:ja
* [new ref]         refs/pull/1000/head -> origin/pr/1000
* [new ref]         refs/pull/1002/head -> origin/pr/1002
* [new ref]         refs/pull/1004/head -> origin/pr/1004
* [new ref]         refs/pull/1009/head -> origin/pr/1009

To check out a particular pull request:

$ git checkout pr/999
Branch pr/999 set up to track remote branch pr/999 from origin.
Switched to a new branch 'pr/999'
CodeWizard
  • 128,036
  • 21
  • 144
  • 167