Skip to content

Instantly share code, notes, and snippets.

@YukariChiba
Last active April 27, 2023 18:48
Show Gist options
  • Select an option

  • Save YukariChiba/f8bd5108d86341fd694410f55f50f3e6 to your computer and use it in GitHub Desktop.

Select an option

Save YukariChiba/f8bd5108d86341fd694410f55f50f3e6 to your computer and use it in GitHub Desktop.
eweOS Update Checker
#!/bin/bash
AUTHOR="eweOS Auto Package Update <[email protected]>"
GH_TOKEN="GITHUB_TOKEN"
PKG_NAME=$1
if [ -z "$PKG_NAME" ]; then
echo "Error: no package name provided."
exit 0
fi
tput bold
echo "Checking for $PKG_NAME"
tput sgr0
EXISTPR=$(curl --header "Authorization: Bearer $GH_TOKEN" --header "X-GitHub-Api-Version: 2022-11-28" -s "https://api.github.com/repos/eweOS/packages/pulls?base=$PKG_NAME" | grep "[AutoUpdate]")
if [ -n "$EXISTPR" ]; then
echo "Error: pull request exists."
exit 0
fi
PKG_RESULT=$(archversion report $PKG_NAME -n)
if [ -z "$PKG_RESULT" ]; then
echo "Already updated."
exit 0
fi
PKGVER_OLD=$(echo $PKG_RESULT | cut -d ' ' -f 6)
PKGVER_NEW=$(echo $PKG_RESULT | cut -d ' ' -f 3)
if [[ -z "$PKGVER_OLD" || -z "$PKGVER_NEW" ]]; then
echo "Error: version check failed."
exit 0
fi
GITCHECK=$(curl -s -L https://github.com/eweOS/packages/raw/$PKG_NAME/PKGBUILD | grep "^pkgver=$PKGVER_NEW")
if [[ -n "$GITCHECK" ]]; then
echo "Error: PKGBUILD in git already updated."
exit 0
fi
echo "Update $PKG_NAME from $PKGVER_OLD to $PKGVER_NEW"
cd ~/packages
git switch $PKG_NAME
PKGVER_FUNC=$(cat PKGBUILD | grep '^pkgver=' | grep '\$')
if [[ -n "$PKGVER_FUNC" ]]; then
echo "Error: package version contains argument."
exit 0
fi
sed -i "s|$PKGVER_OLD|$PKGVER_NEW|g" PKGBUILD
function testbuild() {
trap cleanup INT
set -o pipefail
makepkg -s --noconfirm 2>&1 | tee /tmp/testbuild.log
TESTBUILD=$?
set +o pipefail
if [[ "$TESTBUILD" -eq 0 ]]; then
echo "Test build finished."
else
ISSUECHECK=$(curl --header "Authorization: Bearer $GH_TOKEN" --header "X-GitHub-Api-Version: 2022-11-28" -s -L https://api.github.com/repos/eweOS/bugs/issues?labels=AutoUpdate | grep "$PKG_NAME")
if [[ -n "$ISSUECHECK" ]]; then
echo "Error: PKGBUILD in git already updated."
cleanup
exit 1
fi
LASTROWS=$(tail -n 50 /tmp/testbuild.log)
curl -L -s \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GH_TOKEN"\
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/eweOS/bugs/issues \
--data-binary @- << EOF
{
"title": "[AutoUpdate][$PKG_NAME] Failure upgrading to $PKGVER_NEW",
"body": "**Action:**\\nUpgrade version from $PKGVER_OLD to $PKGVER_NEW.\\n**Last 50 rows of logs**:\\n\`\`\`\\n$(awk -v ORS='\\n' '1' /tmp/testbuild.log)\\n\`\`\`"
}
EOF
echo "Error: test build failed."
git restore PKGBUILD
cleanup
exit 1
fi
}
function cleanup() {
set +o pipefail
echo "SIGINT detected, revert changes..."
git clean -fd && git reset --hard
}
updpkgsums.sh
testbuild
git add PKGBUILD
git commit -m "Upgrade version to $PKGVER_NEW" --author="$AUTHOR"
git push update HEAD -f
git clean -fdq
sleep 1
curl -L -s \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GH_TOKEN"\
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/eweOS/packages/pulls \
-d "{\"title\":\"[AutoUpdate] Upgrade $PKG_NAME to $PKGVER_NEW\",\"body\":\"Update $PKG_NAME from $PKGVER_OLD to $PKGVER_NEW\",\"head\":\"YukariChiba:$PKG_NAME\",\"base\":\"$PKG_NAME\"}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment