kind: pipeline
type: docker
name: api

steps:
  - name: "Sync upstream to local"
    image: nixery.dev/shell/git/openssh
    environment:
      GITEA_SSH_KEY:
        from_secret: gitea_ssh_key
      HOME: /root
    commands:
      # This is a hack because nixery containers contain no passwd file, which openssh wants to read
      - echo "root:x:0:0:System administrator:/root:/run/current-system/sw/bin/bash" >> /etc/passwd
      - mkdir -p ~/.ssh
      - echo $$GITEA_SSH_KEY > ~/.ssh/id_rsa
      - cat ~/.ssh/id_rsa
      - echo "[git.b12f.io]:2222 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJ4uaREL7acSSCNAX+voDYl1Kj7JipP62fR5x1UyGP9u" >> ~/.ssh/known_hosts
      - echo "Host git.b12f.io" >> ~/.ssh/config
      - echo "  Port 2222" >> ~/.ssh/config
      - chmod -R 600 ~/.ssh
      - git clone -b devos git@git.b12f.io:pub-solar/os
      - cd os
      - git remote add devos https://github.com/divnix/devos.git
      - git fetch --all
      - git merge -X theirs devos/core
      - git push origin devos

  - name: "Sync core with upstream"
    image: nixery.dev/shell/git/openssh
    when:
      status:
        - success
    depends_on:
      - "Sync upstream to local"
    environment:
      GITEA_SSH_KEY:
        from_secret: gitea_ssh_key
      HOME: /root
    commands:
      # This is a hack because nixery containers contain no passwd file, which openssh wants to read
      - echo "root:x:0:0:System administrator:/root:/run/current-system/sw/bin/bash" >> /etc/passwd
      - mkdir -p ~/.ssh
      - echo $$GITEA_SSH_KEY > ~/.ssh/id_rsa
      - echo "[git.b12f.io]:2222 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJ4uaREL7acSSCNAX+voDYl1Kj7JipP62fR5x1UyGP9u" >> ~/.ssh/known_hosts
      - echo "Host git.b12f.io" >> ~/.ssh/config
      - echo "  Port 2222" >> ~/.ssh/config
      - chmod -R 600 ~/.ssh
      - git clone -b core git@git.b12f.io:pub-solar/os
      - cd os
      - git fetch
      - git merge devos
      - git push origin core

  - name: "Notify success"
    image: plugins/matrix
    when:
      status:
        - success
    depends_on:
      - "Sync core with upstream"
    settings:
      homeserver: https://element.pub.solar
      roomid: operations:pub.solar
      username:
        from_secret: matrix_username
      password:
        from_secret: matrix_password
      template: Upstreaming devos into core succeeded.

  - name: "Open pull request for failed merge"
    image: nixery.dev/shell/tea
    when:
      status:
        - failure
    depends_on:
      - "Sync core with upstream"
    environment:
      TEA_CONFIG:
        from_secret: tea_config
    commands:
      - mkdir -p ~/.config/tea
      - echo $$TEA_CONFIG > ~/.config/tea/config.yml
      - tea pulls create --base core --head devos

  - name: Notify failure
    image: plugins/matrix
    when:
      status:
        - success
    depends_on:
      - "Open pull request for failed merge"
    settings:
      homeserver: https://element.pub.solar
      roomid: operations:pub.solar
      username:
        from_secret: matrix_username
      password:
        from_secret: matrix_password
      template: Upstreaming devos into core failed. A pull request has been created https://git.b12f.io/pub-solar/os/pulls