Last active
February 7, 2020 10:47
-
-
Save 3nids/0cf399297888ea8ebd0e64169c9fbbc2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e | |
# This script will use GraphQL Github API to get pull requests on master branch, that has been merged and matching the defined labels (edit the query) | |
# It will return the last 100 PRs. If totalCount is bigger, you'll need to use pagination in the query | |
# The results can then be filtered by dates | |
# You need a GH_TOKEN as env variable | |
FROM_DATE="2019-10-25T00:00:00Z" | |
TO_DATE="2020-02-21T00:00:00Z" | |
# GNU prefix command for mac os support (${GP}sed, gsplit) | |
GP= | |
if [[ "$OSTYPE" =~ darwin* ]]; then | |
GP=g | |
fi | |
END_CURSOR="" | |
while [[ true ]]; do | |
QUERY='query { | |
repository(name: "qgis", owner: "qgis") { | |
pullRequests(baseRefName: "master", orderBy: {field: UPDATED_AT, direction: DESC}, states: MERGED, first: 100 XXAFTERXX) { | |
edges { | |
node { | |
author { | |
login | |
} | |
bodyHTML | |
title | |
url | |
mergedAt | |
commits(first: 100) { | |
edges { | |
node { | |
commit { | |
message | |
} | |
} | |
} | |
} | |
} | |
} | |
totalCount | |
pageInfo { | |
endCursor | |
hasNextPage | |
} | |
} | |
} | |
}' | |
# make it oneliner and escape double quotes | |
QUERY=$(${GP}sed 's/"/\\"/g' <<< "$(echo ${QUERY})") | |
if [[ -n ${END_CURSOR} ]]; then | |
# use pagination | |
QUERY=$(${GP}sed "s/XXAFTERXX/, after: \\\\\"${END_CURSOR}\\\\\"/" <<< ${QUERY}) | |
else | |
QUERY=$(${GP}sed "s/XXAFTERXX//" <<< ${QUERY}) | |
fi | |
# get the response | |
echo "Fetching data from Github ... " | |
JSON_DATA=$(curl -s -H 'Content-Type: application/json' \ | |
-H "Authorization: bearer ${GH_TOKEN}" \ | |
-X POST -d "{ \"query\": \"$QUERY\"}" https://api.github.com/graphql | jq '.') | |
if [[ -n ${END_CURSOR} ]]; then | |
# merge the data | |
echo "${JSON_DATA}" | jq '.data.repository.pullRequests.edges[]' >> .data.json | |
else | |
# write the data | |
echo "${JSON_DATA}" | jq '.data.repository.pullRequests.edges[]' > .data.json | |
fi | |
END_CURSOR=$(echo ${JSON_DATA} | jq '.data.repository.pullRequests.pageInfo.endCursor' | ${GP}sed 's/"//g') | |
HAS_NEXT_PAGE=$(echo ${JSON_DATA} | jq '.data.repository.pullRequests.pageInfo.hasNextPage') | |
TOO_OLD=$(echo ${JSON_DATA} | jq ".data.repository.pullRequests.edges | last | .node.mergedAt < \"${FROM_DATE}\"") | |
if [[ ${TOO_OLD} == true ]]; then | |
break | |
fi | |
if [[ ${HAS_NEXT_PAGE} == false ]]; then | |
break | |
fi | |
done | |
# filter by merge date and [FEATURE] in commit message or PR title | |
cat .data.json \ | |
| jq -s ".[] | select(.node.mergedAt > \"${FROM_DATE}\") | select( .node.mergedAt < \"${TO_DATE}\" )" \ | |
| jq -s '.[] | select( (.node.title | test("\\[FEATURE\\]")) or (select(.node.commits.edges[] | select(.node.commit.message | test("\\[FEATURE\\]")))) )' \ | |
| jq -s 'map(del(.node.commits))' > changelog.json | |
echo "Found $(cat changelog.json | jq -s '.[] | length') entries for the changelog." | |
echo "Data written to changelog.json" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment