Skip to content

Commit

Permalink
Add pagination support
Browse files Browse the repository at this point in the history
  • Loading branch information
Yikun committed Oct 13, 2020
1 parent 1461b0d commit 6cb3a46
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ inputs:
dst_token:
description: "The app token which is used to create repo in destination hub."
required: true
src_token:
description: "The app token which is used to list the src repo to avoid rate limit."
required: false
dst:
description: "Destination name. Such as `gitee/kunpengcompute`."
required: true
Expand Down Expand Up @@ -47,6 +50,7 @@ runs:
args:
- ${{ inputs.dst_key }}
- ${{ inputs.dst_token }}
- ${{ inputs.src_token }}
- ${{ inputs.src }}
- ${{ inputs.dst }}
- ${{ inputs.account_type }}
Expand Down
45 changes: 44 additions & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ echo "${INPUT_DST_KEY}" > /root/.ssh/id_rsa
chmod 600 /root/.ssh/id_rsa

DST_TOKEN="${INPUT_DST_TOKEN}"
SRC_TOKEN="${INPUT_SRC_TOKEN}"

SRC_HUB="${INPUT_SRC}"
DST_HUB="${INPUT_DST}"
Expand Down Expand Up @@ -68,8 +69,50 @@ else
err_exit "Unknown src args, the `src` should be `[github|gittee]/account`"
fi

function get_all_repo_names
{
if [[ "$SRC_TOKEN" == "" ]]; then
if [[ "$SRC_TYPE" == "github" ]]; then
total=`curl -sI "$SRC_REPO_LIST_API?page=1&per_page=100" | sed -nr 's/^[lL]ink:.*page=([0-9]+)&per_page=100.*/\1/p'`
elif [[ "$SRC_TYPE" == "gitee" ]]; then
total=`curl -sI "$SRC_REPO_LIST_API?page=1&per_page=100" |grep total_page: |cut -d ' ' -f2 |tr -d '\r'`
fi
else
if [[ "$SRC_TYPE" == "github" ]]; then
total=`curl -H "Authorization: token $SRC_TOKEN" -sI "$SRC_REPO_LIST_API?page=1&per_page=100" | sed -nr 's/^[lL]ink:.*page=([0-9]+)&per_page=50.*/\1/p'`
elif [[ "$SRC_TYPE" == "gitee" ]]; then
total=`curl -H "Content-Type: application/json;charset=UTF-8" -sI "$SRC_REPO_LIST_API?access_token=$SRC_TOKEN&page=1&per_page=100" | grep total_page: |cut -d ' ' -f2 | tr -d '\r'`
fi
fi

# use pagination?
if [ -z "$total" ]; then
# no - this result has only one page
total=1
fi

p=1
while [ "$p" -le "$total" ]; do
if [[ "$SRC_TOKEN" == "" ]]; then
if [[ "$SRC_TYPE" == "github" ]]; then
x=`curl -s "$SRC_REPO_LIST_API?page=$p&per_page=100" | jq '.[] | .name' | sed 's/"//g'`
elif [[ "$SRC_TYPE" == "gitee" ]]; then
x=``
fi
else
if [[ "$SRC_TYPE" == "github" ]]; then
x=`curl -H "Authorization: token $SRC_TOKEN" -s "$SRC_REPO_LIST_API?page=$p&per_page=100" | jq '.[] | .name' | sed 's/"//g'`
elif [[ "$SRC_TYPE" == "gitee" ]]; then
x=`curl -H "Content-Type: application/json;charset=UTF-8" -s "$SRC_REPO_LIST_API?access_token=$SRC_TOKEN&page=$p&per_page=100" | jq '.[] | .name' | sed 's/"//g'`
fi
fi
echo $x
p=$(($p + 1))
done
}

if [[ -z $STATIC_LIST ]]; then
SRC_REPOS=`curl $SRC_REPO_LIST_API | jq '.[] | .name' | sed 's/"//g'`
SRC_REPOS=`get_all_repo_names`
else
SRC_REPOS=`echo $STATIC_LIST | tr ',' ' '`
fi
Expand Down

0 comments on commit 6cb3a46

Please sign in to comment.