# All pipeline steps run in their own container, but the working directory (with code) is in a shared
# volume,  which is mounted in all the containers.  So modifying the filesystem (within the working dir)
# is persistent between build steps.

pipeline:
  lint:
    image: offline-twitter/go
    commands:
      - golangci-lint run

  test:
    image: offline-twitter/go
    commands:
      - mkdir pkg/persistence/test_profiles
      - sample_data/init_data.sh
      - go test -bench=. -cover ./...

  integration_test:
    image: offline-twitter/go
    secrets:
      - offline_twatter_passwd
    when:  # At least one
      - path:
        - pkg/scraper/**
        - cmd/**
      - branch: v*.*.*
    commands:
      - cd cmd
      - ./tests.sh

  webserver_test:
    image: offline-twitter/go
    commands:
      - cd cmd
      - ./compile.sh
      - ./tw --profile ../sample_data/profile webserver --addr localhost:1000 &
      - sleep 1
      - curl localhost:1000/cernovich > webserver_test_output.html
      - jobs -p | xargs -I{} kill -- -{}

  version_bump_test:
    image: offline-twitter/go
    when:
      branch: v*.*.*
    secrets:
      - offline_twatter_passwd
    commands:
      - export version=$(echo $CI_COMMIT_BRANCH | grep -Poh "(?<=^v)\d+\.\d+\.\d+")
      - 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
      - which twitter
      - twitter create_profile outdated_profile
      - cd outdated_profile
      - twitter login offline_twatter "$OFFLINE_TWATTER_PASSWD"
      - twitter fetch_user pcmonk
      - twitter --session Offline_Twatter get_user_tweets pcmonk
      - (cd ../cmd && ./compile.sh $version)
      - which ../cmd/tw
      - ../cmd/tw fetch_user pcmonk

  dpkg_build_and_upload:
    when:
      branch: v*.*.*
    image: offline-twitter/deploy
    commands:
      - export version=$(echo $CI_COMMIT_BRANCH | grep -Poh "(?<=^v)\d+\.\d+\.\d+")
      - cd build
      - ./build_dpkg.sh $version
      - scp offline-twitter_$${version}_all.deb aptrepo@apt.playfulpachyderm.com:/apt-repo/test-repo
      - ssh aptrepo@apt.playfulpachyderm.com "cd ~/test-repo && ./update.sh"

  windows_build:
    image: offline-twitter/go-mingw
    when:
      branch: v*.*.*
    commands:
      - cd cmd
      - export version=$(echo $CI_COMMIT_BRANCH | grep -Poh "(?<=^v)\d+\.\d+\.\d+")
      - ./windows-compile.sh $version

  windows_package_and_upload:
    when:
      branch: v*.*.*
    image: offline-twitter/innosetup
    secrets:
      - offline_twatter_id_ecdsa
    commands:
      - export version=$(echo $CI_COMMIT_BRANCH | grep -Poh "(?<=^v)\d+\.\d+\.\d+")
      - iscc.sh /Dversion=$version /Dexe_path=`winepath -w cmd/twitter.exe` build/windows/setup.iss
      - eval $(ssh-agent)
      - echo "$OFFLINE_TWATTER_ID_ECDSA" | ssh-add -
      - scp build/windows/Output/mysetup.exe offline-twitter@offline-twitter.com:/var/www/offline-twitter.com/downloads/offline-twitter-for-windows.exe


depends_on:
  - build-docker
