Skip to content

Instantly share code, notes, and snippets.

@schmidtw
Created August 9, 2022 02:55
Show Gist options
  • Save schmidtw/aff64a190561e6b19a79ed43fb4fe995 to your computer and use it in GitHub Desktop.
Save schmidtw/aff64a190561e6b19a79ed43fb4fe995 to your computer and use it in GitHub Desktop.
A tool for importing issues/PRs from github repos into a github project.
#!/bin/bash
# Copyright 2022 Weston Schmidt <weston_schmidt@alumni.purdue.edu>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Version 1 - it worked enough to save me a bunch of busywork.
###########################
# How to use this script. #
###########################
# ./gh_project_importer.sh project_org 5 file
#
# project_org: This is the github organization the project exists in.
#
# 5: This is whatever number of the project board you want
# to import into is.
#
# file: The file that contains a list of org and repos (one per line)
#
# Example file:
#
# /-- start of the line. Don't put spaces before the org or after the repo.
# v
# |schmidtw goschtalt
# |xmidt-org gokeepachangelog
#
#
# Notes:
# - It's safe to run the script multiple times. If an item exists on the
# board github just says it's there & moves on.
# - You're going to have to do some work to make this work for over 100
# issues, because I didn't have to do it today.
# - This has been tested against an organization based project. A user
# based project may work, but is untested.
#
target_org=$1
target_proj=$2
list=$3
project=`gh api graphql -f query='
query FindProject($org: String!, $num: Int!) {
organization(login: $org){
projectV2(number: $num) {
id
}
}
}' -f org="${target_org}" -F num="${target_proj}" \
-q '.data.organization.projectV2.id'`
echo "Target project: ${target_org}/${target_proj} --> ${project}"
################################################################################
# The main loop iterator that goes through the list of orgs and repos
################################################################################
while read -r line; do
# Split the input file into org and repo based on whitespace.
read org repo <<< ${line}
echo "Processing: '${org}/${repo}'"
echo "--------------------------------------------"
all=`gh api graphql -f query='
query OpenThings($repo: String!, $org: String!) {
repository(name: $repo, owner: $org) {
issues(states: OPEN, first: 100) {
totalCount
nodes {
url
}
}
pullRequests(states: OPEN, first: 100) {
totalCount
nodes {
url
}
}
}
}' -f repo="${repo}" -f org="${org}"`
issueCount=$(echo ${all} | jq '.data.repository.issues.totalCount')
prCount=$(echo ${all} | jq '.data.repository.pullRequests.totalCount')
echo -n "Issue count: ${issueCount}"
if [ "100" -lt "${issueCount}" ]; then
echo -n " [Too many, only 100 are supported]"
fi
echo ""
echo -n "Pull Request count: ${prCount}"
if [ "100" -lt "${prCount}" ]; then
echo -n " [Too many, only 100 are supported]"
fi
echo ""
# Create the issues/PRs as drafts because otherwise you can't import them
# into GH Enterprise projects.
#
# Here's where I figured this trick out at:
# https://github.com/actions/add-to-project/blob/c9e794be1305e5a9248ae56fee824459b1c1a375/src/add-to-project.ts#L142
# Import the issues first
urls=($(echo ${all} | jq -r '.data.repository.issues.nodes | .[] | .url'))
echo "Importing Issues"
for url in "${urls[@]}"
do
echo "Importing Issue: ${url}"
gh api graphql -f query='
mutation Update($project: ID!, $title: String!) {
addProjectV2DraftIssue(input: {projectId: $project, title: $title}) {
projectItem {
id
}
}
}' -f project="${project}" -f title="${url}"
done
# Import the PRs next
urls=($(echo ${all} | jq -r '.data.repository.pullRequests.nodes | .[] | .url'))
echo "Importing PR"
for url in "${urls[@]}"
do
echo "Importing PR: ${url}"
gh api graphql -f query='
mutation Update($project: ID!, $title: String!) {
addProjectV2DraftIssue(input: {projectId: $project, title: $title}) {
projectItem {
id
}
}
}' -f project="${project}" -f title="${url}"
done
################################################################################
# The end of the large loop.
################################################################################
done < ${list}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment