image: ubuntu/focal

secrets:
  - 31c0c342-c396-4190-9637-1c3cedd705a5 # SSH key
  - 4e6bb736-559c-4b3e-ba04-331f5808cacc # aptrepo SSH key (to upload the build result to apt.playfulpachyderm.com)
  - 8803f94a-8b8b-4966-aac7-4b8e24c328ce # Gitlab access token
  - c84e3f2c-7c97-4cef-859e-8b13de32a7be # ~/.ssh/known_hosts (gitlab.com)

sources:
  - git@gitlab.com:playfulpachyderm/twitter_offline_engine.git

packages:
  - wget
  - build-essential
  - sqlite3

tasks:
  - source_code_checkout_kludge: |
      SECONDS=0

      cd twitter_offline_engine
      basename $GITLAB_REF | xargs git checkout

      duration=$SECONDS
      echo "Task completed in $(($duration / 60))m$(($duration % 60))s."

  - install_go: |
      SECONDS=0

      wget https://golang.org/dl/go1.16.4.linux-amd64.tar.gz
      sudo tar -C /usr/local -xzf go1.16.4.linux-amd64.tar.gz
      sudo ln -s /usr/local/go/bin/go /usr/bin/go

      duration=$SECONDS
      echo "Task completed in $(($duration / 60))m$(($duration % 60))s."

  - test_terminal_utils: |
      SECONDS=0

      cd twitter_offline_engine/terminal_utils

      go get .
      go test -bench=. -cover

      duration=$SECONDS
      echo "Task completed in $(($duration / 60))m$(($duration % 60))s."

  - test_scraper: |
      SECONDS=0

      cd twitter_offline_engine/scraper

      go get .
      go test -bench=. -cover

      duration=$SECONDS
      echo "Task completed in $(($duration / 60))m$(($duration % 60))s."

  - test_persistence: |
      SECONDS=0

      cd twitter_offline_engine/persistence

      go get .
      mkdir test_profiles/
      go test -bench=. -cover

      duration=$SECONDS
      echo "Task completed in $(($duration / 60))m$(($duration % 60))s."

  - install_golangci-lint: |
      SECONDS=0

      curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sudo sh -s -- -b /usr/local/go/bin v1.50.0
      sudo ln /usr/local/go/bin/golangci-lint /usr/local/bin/golangci-lint

      duration=$SECONDS
      echo "Task completed in $(($duration / 60))m$(($duration % 60))s."

  - lint: |
      SECONDS=0

      cd twitter_offline_engine
      golangci-lint run

      duration=$SECONDS
      echo "Task completed in $(($duration / 60))m$(($duration % 60))s."

  - integration_test: |
      SECONDS=0

      cd twitter_offline_engine/cmd
      ./tests.sh

      duration=$SECONDS
      echo "Task completed in $(($duration / 60))m$(($duration % 60))s."

  - version_bump_test: |
      SECONDS=0

      cd twitter_offline_engine/cmd

      version=$(git branch --show-current | grep -Poh "(?<=^release-)\d+\.\d+\.\d+") || true
      if [[ -z "$version" ]]
      then
          # Not on a release branch; just exit this task
          complete-build
      fi

      sudo apt install -y curl gnupg
      curl https://apt.playfulpachyderm.com/KEY.gpg | sudo apt-key add -
      echo "deb https://apt.playfulpachyderm.com/ ./" | sudo tee /etc/apt/sources.list.d/offline_twitter.list
      sudo apt update

      sudo apt install -y offline-twitter-engine  # Latest released version
      which twitter  # Ensure that it got installed

      twitter create_profile outdated_profile
      cd outdated_profile

      # Create some sample data
      twitter fetch_user michaelmalice
      twitter get_user_tweets michaelmalice

      # Compile the binary with the version number
      cd .. && ./compile.sh $version
      cd outdated_profile

      # Now test the upgrade using the newer version
      which ../tw
      ../tw fetch_user michaelmalice  # Any operation other than `create_profile` should trigger the upgrade

      duration=$SECONDS
      echo "Task completed in $(($duration / 60))m$(($duration % 60))s."

  - dpkg_build_and_upload: |
      SECONDS=0

      cd twitter_offline_engine/build
      version=$(git branch --show-current | grep -Poh "(?<=^release-)\d+\.\d+\.\d+") || true

      if [[ -z "$version" ]]
      then
          # Not on a release branch; just exit this task
          exit
      fi

      ./build_dpkg.sh $version

      echo "|1|wHeqDXntszAtw63JZ2a8lRFNkzo=|i4RPdW3eWWGZFcRx6ttH66pryWw= ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBFOHLdwogAIaEhVmUCgKMyHQpWsdBH7k29vMs9mrPoAhD9xdtmil0/wLniyBkflqOAPF/XXvsqYb6+lgGYRN40U=" >> ~/.ssh/known_hosts
      echo "|1|CMeJL187AvCdC7uMVPZPP8JtmgY=|24sUol3rn1+7sGBbXkhOSNO/5Q4= ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBFOHLdwogAIaEhVmUCgKMyHQpWsdBH7k29vMs9mrPoAhD9xdtmil0/wLniyBkflqOAPF/XXvsqYb6+lgGYRN40U=" >> ~/.ssh/known_hosts

      scp -i ~/.ssh/4e6bb736-559c-4b3e-ba04-331f5808cacc offline-twitter-engine_${version}_all.deb aptrepo@apt.playfulpachyderm.com:/apt-repo/test-repo
      ssh -i ~/.ssh/4e6bb736-559c-4b3e-ba04-331f5808cacc aptrepo@apt.playfulpachyderm.com "cd ~/test-repo && ./update.sh"

      duration=$SECONDS
      echo "Task completed in $(($duration / 60))m$(($duration % 60))s."
