Skip to content

Commit

Permalink
Neuter the precommit hook, fix current dev schema at version 3 (pokep…
Browse files Browse the repository at this point in the history
…ark#239)

- Roll all current schema changes into schema version 2
- Drop FUNDING.yml, no active maintainer was on it
- Switch docker image versioning scheme to include commit hash
  • Loading branch information
jinnatar authored Aug 24, 2021
1 parent 8b4b455 commit 28fd774
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 188 deletions.
12 changes: 0 additions & 12 deletions .github/FUNDING.yml

This file was deleted.

22 changes: 9 additions & 13 deletions .github/workflows/publish-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,15 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v2

- name: Log in to the Container registry
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Get the version
id: get_version
run: echo ::set-output name=version::$(cat VERSION)

- name: Get the release channel
id: get_channel
shell: bash
run: |
if [[ "$GITHUB_REF" == 'refs/heads/main' ]]; then
echo ::set-output name=channel::"latest"
echo ::set-output name=version::main_$(cat VERSION)
echo ::set-output name=version::main_$(cat VERSION)-${GITHUB_SHA::6}
elif [[ "$GITHUB_REF" == "refs/heads/"* ]]; then
echo ::set-output name=channel::${GITHUB_REF/refs\/heads\//}_$(cat VERSION)
echo ::set-output name=version::${GITHUB_REF/refs\/heads\//}-$(cat VERSION)-${GITHUB_SHA::6}
elif [[ "$GITHUB_REF" == "refs/tags/"* ]]; then
echo ::set-output name=channel::${GITHUB_REF/refs\/tags\//}
fi
Expand All @@ -50,6 +39,13 @@ jobs:
type=raw,value=${{ steps.get_channel.outputs.channel }}
type=raw,value=${{ steps.get_channel.outputs.version }}
- name: Log in to the Container registry
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push Docker image
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
with:
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.236.6
3
2 changes: 1 addition & 1 deletion config/config.json.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"VERSION":"2.1.236.6",
"VERSION":"3",
"DB_HOST":"localhost",
"DB_NAME":"your_database_name",
"DB_USER":"your_database_user",
Expand Down
14 changes: 6 additions & 8 deletions core/bot/logic/bot_upgrade_check.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function upgrade_config_version($version)
* Bot upgrade check
* @param $current
* @param $latest
* @return bool: if an upgrade is needed
* @return bool: if a manual upgrade is needed
*/
function bot_upgrade_check($current, $latest)
{
Expand All @@ -24,11 +24,9 @@ function bot_upgrade_check($current, $latest)
$upgrade_files = str_replace(UPGRADE_PATH . '/','', glob(UPGRADE_PATH . '/*.sql'));

// Remove dots from current and latest version for easier comparison.
$nodot_current = str_replace('.', '', $current);
$nodot_latest = str_replace('.', '', $latest);

// Same version?
if($nodot_current == $nodot_latest) {
if($current == $latest) {
// No upgrade needed.
return false;
} else {
Expand All @@ -38,13 +36,13 @@ function bot_upgrade_check($current, $latest)
// Check each sql filename.
foreach ($upgrade_files as $ufile)
{
$nodot_ufile = str_replace('.sql', '', $ufile);
// Skip every older sql file from array.
$nodot_ufile = str_replace('.', '', str_replace('.sql', '', $ufile));
if($nodot_ufile <= $nodot_current) {
if($nodot_ufile <= $current) {
continue;
} else {
if ($config->UPGRADE_SQL_AUTO){
debug_log('PERFORMING AUTO SQL UPGRADE:' . UPGRADE_PATH . '/' . $ufile, '!');
info_log('PERFORMING AUTO SQL UPGRADE:' . UPGRADE_PATH . '/' . $ufile, '!');
require_once('sql_utils.php');
if (run_sql_file(UPGRADE_PATH . '/' . $ufile)) {
upgrade_config_version(basename($ufile, '.sql'));
Expand All @@ -70,7 +68,7 @@ function bot_upgrade_check($current, $latest)
info_log('AUTO UPGRADE FAILED: ' . ROOT_PATH . '/sql/game-master-raid-boss-pokedex.sql!');
}
}
// Upgrade required.
// Signal whether manual action is required or not.
return $require_upgrade;
}
}
Expand Down
49 changes: 14 additions & 35 deletions core/bot/version.php
Original file line number Diff line number Diff line change
@@ -1,75 +1,54 @@
<?php
// Check if version is defined in config.
!empty($config->VERSION) or $config->VERSION = '1.0.0,0';
!empty($config->VERSION) or $config->VERSION = '1';
$current = $config->VERSION;
$nodot_current = str_replace('.', '', $current);

// Get version from VERSION file.
$lfile = ROOT_PATH . '/VERSION';
if(is_file($lfile) && filesize($lfile)) {
$latest = file($lfile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$latest = $latest[0];
} else {
$latest = '1.0.0.0';
$error = 'VERSION file missing, cannot continue without it since we would not know the required DB schema version.';
info_log($error);
error_log($error);
exit();
}
$nodot_latest = str_replace('.', '', $latest);

// Compare versions.
if($nodot_current == $nodot_latest) {
debug_log($current, 'Your bot version:');
if($current == $latest) {
debug_log($current, 'Your bot schema version:');
} else {
// Current version not defined in config!
if($nodot_current == '1000') {
info_log('Failed to determine your bot version!', '!');
if($current == '1') {
info_log('Failed to determine your bot version! Have you removed it from config.json?', '!');

// Tell user bot maintainance is required!
if(!empty($config->MAINTAINER_ID)) {
// Echo data.
$msg = 'ERROR! BOT MAINTAINANCE REQUIRED!' . CR . 'FAILED TO GET YOUR BOT VERSION!' . CR;
$msg = 'ERROR! BOT MAINTAINANCE REQUIRED!' . CR . 'FAILED TO GET YOUR BOT SCHEMA VERSION!' . CR;
$msg .= 'Server: ' . $_SERVER['SERVER_ADDR'] . CR;
$msg .= 'User: ' . $_SERVER['REMOTE_ADDR'] . ' ' . isset($_SERVER['HTTP_X_FORWARDED_FOR']) . CR;
$msg .= 'Your version: ' . $current . CR . 'Latest version: ' . $latest;
sendMessageEcho($config->MAINTAINER_ID, $msg);
} else {
// Write to standard error log.
error_log('ERROR! The config item MAINTAINER_ID is not defined!');
error_log('ERROR! BOT MAINTAINANCE REQUIRED! FAILED TO GET YOUR BOT VERSION! --- Your version: ' . $current . ' --- Latest version: ' . $latest);
error_log('ERROR! BOT MAINTAINANCE REQUIRED! FAILED TO GET YOUR BOT SCHEMA VERSION! --- Your version: ' . $current . ' --- Latest version: ' . $latest);
}
// Exit script.
exit();

// Latest version unavailable!
} else if($nodot_latest == '1000') {
info_log('Failed to determine the latest bot version!', '!');

// Tell user bot maintainance is required!
if(!empty($config->MAINTAINER_ID)) {
// Echo data.
$msg = 'ERROR! BOT MAINTAINANCE REQUIRED!' . CR . 'FAILED TO GET THE LATEST BOT VERSION!' . CR;
$msg .= 'Server: ' . $_SERVER['SERVER_ADDR'] . CR;
$msg .= 'User: ' . $_SERVER['REMOTE_ADDR'] . ' ' . isset($_SERVER['HTTP_X_FORWARDED_FOR']) . CR;
$msg .= 'Your version: ' . $current . CR . 'Latest version: ' . $latest;
sendMessageEcho($config->MAINTAINER_ID, $msg);
} else {
// Write to standard error log.
error_log('ERROR! The config item MAINTAINER_ID is not defined!');
error_log('ERROR! BOT MAINTAINANCE REQUIRED! FAILED TO GET THE LATEST BOT VERSION! --- Your version: ' . $current . ' --- Latest version: ' . $latest);
}
// Exit script.
exit();

// Check for upgrade files.
} else {
// Check for upgrade files.
debug_log('Bot version: ' . $current . ', Latest: ' . $latest);
require_once('db.php');
$upgrade = bot_upgrade_check($current, $latest);

// Upgrade needed?
// Manual upgrade needed?
if($upgrade) {
// Tell user an upgrade is required!
if(!empty($config->MAINTAINER_ID)) {
// Echo data.
sendMessageEcho($config->MAINTAINER_ID, 'ERROR! BOT UPGRADE REQUIRED!' . CR . 'Server: ' . $_SERVER['SERVER_ADDR'] . CR . 'User: ' . $_SERVER['REMOTE_ADDR'] . ' ' . isset($_SERVER['HTTP_X_FORWARDED_FOR']) . CR . 'Your version: ' . $current . CR . 'Latest version: ' . $latest);
sendMessageEcho($config->MAINTAINER_ID, 'BOT SCHEMA NEEDS AN UPGRADE, and and admin to take a manual look at it.' . CR . 'Server: ' . $_SERVER['SERVER_ADDR'] . CR . 'User: ' . $_SERVER['REMOTE_ADDR'] . ' ' . isset($_SERVER['HTTP_X_FORWARDED_FOR']) . CR . 'Your version: ' . $current . CR . 'Latest version: ' . $latest);
} else {
// Write to standard error log.
error_log('ERROR! The config item MAINTAINER_ID is not defined!');
Expand Down
40 changes: 2 additions & 38 deletions core/hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -1,43 +1,7 @@
#!/bin/bash

# Debug
# set -x


regen_version(){
# Old version
OLD_VERSION=$(cat VERSION)
echo 'Current version:' $OLD_VERSION

# Build new version string
MAJOR=$(date +%y | cut -c1)
MINOR=$(date +%y | cut -c2)
DAY=$(date +%j)
COMMIT=$((`git rev-list --count --since=midnight HEAD` + 1))
VERSION=$MAJOR.$MINOR.$DAY.$COMMIT

# Update version file
echo 'Updating project version to:' $VERSION
echo $VERSION > VERSION
# Update sample config VERSION
sed -E -i "s/(\"VERSION\":\")[0-9\.]+/\1$VERSION/" config/config.json.example

# Add version file to commit
echo 'Adding new version to commit'
echo
git add VERSION config/config.json.example

# Interrupt, so user can see output before commit
read -p "Press any key to continue..." </dev/tty
echo
}

# Tell the user we're running pre-commit hooks
echo 'Running pre-commit hooks'

# Run version regen on every commit if VERSION file present
# TODO(artanicus): Is this truly the intention, to run at every commit? And should the update also go to the sample config?
[ -f VERSION ] && regen_version
# We don't currently have any precommit hooks so this one is a no-op.
# Some may still have it configured though so we just do nothing instead of deleting it.

# exit cleanly to let commit continue
exit 0
77 changes: 10 additions & 67 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,6 @@ To keep local data, such as ``pokemon`` table and Pokemon icons directory, up to
Config reference
================


* For default values, see ``config/defaults-config.json``.
* Most values are strings.
* Boolean values should use ``true`` & ``false``\ , not strings.
Expand Down Expand Up @@ -1628,86 +1627,35 @@ Config reference
Development
===========

Releasing a new version
-----------------------

Whenever significant enough changes are made that they should be minted into a new Docker image you'll need to tag the version to trigger an image build.


#. In GitHub after merging a PR, head to the `Releases <https://github.com/pokepark/PokemonRaidBot/releases>`_ page.
#. Create a new release giving it the contents of the VERSION file prepended with a ``v``. i.e. at time of writing, the latest release tag is ``v2.1.191.10``. The ``v`` is mandatory!
#. Add the same tag as the release name, describe what's new. As soon as you submit you can find the image release running in the ``Actions`` tab.

Alternatively from the CLI:

.. code-block::
git tag -f v$(cat VERSION)
git push pokepark v$(cat VERSION)
* The release name becomes the same as the tag name.
* The release description comes from the description of the revision you tagged.

Adding new config values
------------------------


* Any previously undefined config value needs a sane default value in ``config/defaults-config.json``. Anyone updating will get that default!
* If the new config item is something people will likely want to override, add it to ``config/config.json.example``.
* You can access the new config item in code with ``$config->CONFIG_ITEM_NAME`` but if inside a function, remember to specify ``global $config;``
* Don't break backwards compatibility if you can.

Git Hooks
---------

In the needed core repository we provide a folder with git hooks which can be used to automate development processes. Copy them to the ``.git/hooks/`` folder of this bot and make them executable (e.g. ``chmod +x .git/hooks/pre-commit``\ ) to use them, or set your local git config to use them directly:

.. code-block::
git config --local core.hooksPath core/hooks
pre-commit
^^^^^^^^^^

The pre-commit git hook will automatically update the VERSION file and table of contents whenever you do a ``git commit``.

The bot version is automatically generated when using the pre-commit hook according to the following scheme consisting of 4 parts separated by dots:


* Current decade (1 char)
* Current year (1 char)
* Current day of the year (up to 3 chars)
* Number of the commit at the current day of the year (1 or more chars)

To give a little example the bot version ``1.9.256.4`` means:

Schema changes
--------------

* Decade was 20\ **1**\ 0-20\ **1**\ 9
* Year was 201\ **9**
* Day number **256** (from 365 days in 2019) was the 13th September 2019
* There have been **4** commits at that day
Schema changes should be done sparingly and grouped into larger updates that are first built in the ``dev`` branch. Once the branch is merged into ``main``
the schema version is final and immutable and any schema changes need to happen in the next version.

This way it is easy to find out when a bot version was released and how old/new a version is.
* If the schema version has been raised, a matching sql upgrade file must exist, for example ``sql/upgrade/3.sql``
* The ``VERSION`` file contains the schema version required by the code it's checked in with.
* The ``config/config.json`` file is expected to have a config item ``VERSION`` that records the latest schema that the DB has been upgraded to.

The following command is used to create the game-master-raid-boss-pokedex.sql file.

game-master-raid-boss-pokedex.sql
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Pokedex updates
---------------

The following command is used to create the game-master-raid-boss-pokedex.sql file. Normally this file is kept up to date by developers
The following command is used to create the `game-master-raid-boss-pokedex.sql` file. Normally this file is kept up to date by developers
but if no one has updated it yet, you can generate an updated version (and optionally also create a Pull Request.)

.. code-block::
php getDB.php
To create the sql upgrade file, the following one-liner usually works:

.. code-block::
git diff sql/ | egrep '^\+REPLACE' | tr -d + > sql/upgrade/$(cat VERSION).sql
Translations
------------

Expand All @@ -1719,12 +1667,7 @@ translate.py
To help in adding a new translation or improving an existing one the ``lang/`` folder has a tool called ``translate.py``
It will add placeholders for a new language and allow you to incrementally and interatively translate strings. All changes are saved as you go.

Usage
~~~~~

By default:


* Translations are read from and saved directly into language.json but any other file(s) can be specified with ``--input`` and ``--output``
* The current English translation is shown as context. The language can be chosen with ``--from_language``
* Only missing translations are prompted (incremental mode), use ``--noincremental`` or ``--incremental=False`` to prompt every string.
Expand Down
1 change: 0 additions & 1 deletion sql/upgrade/2.0.117.5.sql

This file was deleted.

1 change: 0 additions & 1 deletion sql/upgrade/2.0.222.2.sql

This file was deleted.

7 changes: 0 additions & 7 deletions sql/upgrade/2.0.235.8.sql

This file was deleted.

1 change: 0 additions & 1 deletion sql/upgrade/2.0.340.6.sql

This file was deleted.

1 change: 0 additions & 1 deletion sql/upgrade/2.0.341.2.sql

This file was deleted.

Loading

0 comments on commit 28fd774

Please sign in to comment.