name: Monthly Nightly Update

on:
  schedule:
    - cron: "0 0 1 * *"

jobs:
  update:
    name: Update nightly
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac
        with:
          submodules: "recursive"

      - name: Write nightly version
        run: echo $(date +"nightly-%Y-%m"-01) > .github/nightly-version

      - name: Create the commit
        run: |
          git config user.name "GitHub Actions"
          git config user.email "<>"

          git checkout -b $(date +"nightly-%Y-%m")

          git add .github/nightly-version
          git commit -m "Update nightly"
          git push -u origin $(date +"nightly-%Y-%m")

      - name: Pull Request
        uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410
        with:
          script: |
            const { repo, owner } = context.repo;

            const result = await github.rest.pulls.create({
              title: (new Date()).toLocaleString(
                false,
                { month: "long", year: "numeric" }
              ) + " - Rust Nightly Update",
              owner,
              repo,
              head: "nightly-" + (new Date()).toISOString().split("-").splice(0, 2).join("-"),
              base: "develop",
              body: "PR auto-generated by a GitHub workflow."
            });

            github.rest.issues.addLabels({
              owner,
              repo,
              issue_number: result.data.number,
              labels: ["improvement"]
            });
