diff --git a/ardumont/sync-mirror-to-github.sh b/ardumont/sync-mirror-to-github.sh index 673bc89..bb0c7a0 100755 --- a/ardumont/sync-mirror-to-github.sh +++ b/ardumont/sync-mirror-to-github.sh @@ -1,104 +1,113 @@ #!/bin/bash -GHB_API_TOKEN_FILE=$HOME/.config/swh/github-token +GITHUB_API_TOKEN_FILE=$HOME/.config/swh/github-token FORGE_API_TOKEN_FILE=$HOME/.config/swh/forge-token -[ ! -f "$GHB_API_TOKEN_FILE" ] && cat << EOF && exit 1 -Install the github api's token in $GHB_API_TOKEN_FILE. -It must have the scope public_repo. -You must be associated to https://github.com/softwareheritage organization. -Once the installation is done, you can trigger this script again. +[ ! -f "$GITHUB_API_TOKEN_FILE" ] && cat << EOF && exit 1 + +Install one personal github token in $GITHUB_API_TOKEN_FILE with scope +public_repo (https://github.com/settings/tokens). + +You must be associated to https://github.com/softwareheritage +organization. Once the installation is done, you can trigger this +script again. EOF [ ! -f "$FORGE_API_TOKEN_FILE" ] && cat << EOF && exit 1 -Install the github api's token in $FORGE_API_TOKEN_FILE. +Install the forge's token in $FORGE_API_TOKEN_FILE +(https://forge.softwareheritage.org/settings/user//page/apitokens/). + Once the installation is done, you can trigger this script again. EOF [[ ( $# -lt 4 ) ]] && cat << EOF && exit 1 Use: $0 FORGE_REPO_CALLSIGN Repository's callsign (we will deduce the phid from it) FORGE_REPO_NAME Repository's name (we will use it for github name) FORGE_REPO_URL Repository's forge url FORGE_REPO_DESCRIPTION Repository's forge description +CREDENTIAL_KEY_ID Credential key id to use (default to 2 which is K2) Example: -sync-mirror-to-github.sh swh-indexer https://forge.softwareheritage.org/source/swh-indexer.git "Software Heritage content (as in "blob") indexers" +sync-mirror-to-github.sh swh-indexer https://forge.softwareheritage.org/source/swh-indexer.git "Software Heritage content (as in "blob") indexers" 3 EOF FORGE_REPO_CALLSIGN=$1 FORGE_REPO_NAME=$2 FORGE_REPO_URL=$3 FORGE_REPO_DESCRIPTION=$4 -TRYOUT=$5 +CREDENTIAL_KEY_ID=${5-"2"} # default to K2 #### Retrieve phabricator information FORGE_API_TOKEN=$(cat $FORGE_API_TOKEN_FILE) -# Retrieve the repository's phid -FORGE_REPO_PHID=$(curl -d api.token=$FORGE_API_TOKEN \ - -d "constraints[callsigns][0]=$FORGE_REPO_CALLSIGN" \ - https://forge.softwareheritage.org/api/diffusion.repository.search 2>/dev/null \ - jq '.result.data[].phid') +DATA_REPO=$(curl -d api.token=$FORGE_API_TOKEN \ + -d "constraints[callsigns][0]=$FORGE_REPO_CALLSIGN" \ + https://forge.softwareheritage.org/api/diffusion.repository.search 2>/dev/null) -echo $FORGE_REPO_PHID +# Retrieve the repository's id +FORGE_REPO_ID=$(echo $DATA_REPO | jq '.result.data[].id' | sed -e 's/"//gi') -#### Create the repository in github +# Retrieve the repository's phid +FORGE_REPO_PHID=$(echo $DATA_REPO | jq '.result.data[].phid' | sed -e 's/"//gi') -GHB_USER_TOKEN=$(cat $GHB_API_TOKEN_FILE) +#### Create the repository in github -if [ ! -z "$TRYOUT"]; then +GITHUB_USER_TOKEN=$(cat $GITHUB_API_TOKEN_FILE) - TMP_FILE=$(mktemp) - cat < $TMP_FILE +TMP_FILE=$(mktemp) +cat < $TMP_FILE { "name": "$FORGE_REPO_NAME", "description": "$FORGE_REPO_DESCRIPTION", "homepage": "$FORGE_REPO_URL", "private": false, "has_issues": false, "has_wiki": false, "has_downloads": true } EOF - # Create the repository in github - curl -H "Authorization: token $GHB_USER_TOKEN" \ - -X POST \ - -d @${TMP_FILE} \ - https://api.github.com/v3/orgs/SoftwareHeritage/repos +# Create the repository in github +curl -H "Authorization: token $GITHUB_USER_TOKEN" \ + -X POST \ + -d @${TMP_FILE} \ + https://api.github.com/orgs/SoftwareHeritage/repos - # clean up - [ -f $TMP_FILE ] && rm $TMP_FILE - -fi +clean up +[ -f $TMP_FILE ] && rm $TMP_FILE #### phabricator -GITHUB_REPOSITORY_URI=git@github.com:SoftwareHeritage/$FORGE_REPO_NAME.git - -K2_PHID=$(curl -d "api.token=$FORGE_API_TOKEN" \ - -d 'ids[0]=2' \ - https://forge.softwareheritage.org/api/passphrase.query \ - 2>/dev/null \ - | jq '.result.data[].phid') - -echo $K2_PHID - - -if [ ! -z "$TRYOUT" ]; then - curl https://forge.softwareheritage.org/api/diffusion.uri.edit \ - -d api.token=$FORGE_API_TOKEN \ - -d transactions[0][type]=repository \ - -d transactions[0][value]=$FORGE_REPO_PHID \ - -d transactions[1][type]=uri \ - -d transactions[1][value]=$GITHUB_REPOSITORY_URI \ - -d transactions[2][type]=io \ - -d transactions[2][value]=mirror \ - -d transactions[3][type]=display \ - -d transactions[3][value]=never \ - -d transactions[4][type]=credential \ - -d transactions[4][value]=$K2_PHID -fi +GITHUB_REPO_URI=git@github.com:SoftwareHeritage/$FORGE_REPO_NAME.git + +KEY_PHID=$(curl -d "api.token=$FORGE_API_TOKEN" \ + -d "ids[0]=$CREDENTIAL_KEY_ID" \ + https://forge.softwareheritage.org/api/passphrase.query \ + | jq '.result.data[].phid' | sed -e 's/"//gi') + +# Create the uri to associate with the repository with the following options: +# - github uri +# - i/o: Mirror" in the ui is 'i/o': read in the api +# - display: hidden +# - credentials: set the credential to the wanted credential (id passed as input) +curl https://forge.softwareheritage.org/api/diffusion.uri.edit \ + -d api.token=$FORGE_API_TOKEN \ + -d transactions[0][type]=repository \ + -d transactions[0][value]=$FORGE_REPO_PHID \ + -d transactions[1][type]=uri \ + -d transactions[1][value]=$GITHUB_REPO_URI \ + -d transactions[2][type]=io \ + -d transactions[2][value]=mirror \ + -d transactions[3][type]=display \ + -d transactions[3][value]=never \ + -d transactions[4][type]=credential \ + -d transactions[4][value]=$KEY_PHID \ + -d transactions[5][type]=disable \ + -d transactions[5][value]=false | jq + +cat <