# Automates creation of changelog drafts using Release Drafter
# More Info: https://github.com/jenkinsci/.github/blob/master/.github/release-drafter.adoc
name: Changelog Drafter

on:
  workflow_dispatch:
  push:
    # branches to consider in the event; optional, defaults to all
    branches:
      - master

permissions:
  contents: read

jobs:
  update_draft_release:
    permissions:
      pull-requests: write # to add label to PR (release-drafter/release-drafter)
      contents: write # to create a github release (release-drafter/release-drafter)

    if: github.repository_owner == 'jenkinsci'
    runs-on: ubuntu-latest
    steps:
      # Drafts your next Release notes as Pull Requests are merged into "master"
      - name: Generate GitHub Release Draft
        id: release-drafter
        uses: release-drafter/release-drafter@v6
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      # Generates a YAML changelog file using https://github.com/jenkinsci/jenkins-core-changelog-generator
      # used by Oleg N in open graph generator experiment for now
      - name: Generate YAML changelog draft
        id: jenkins-core-changelog-generator
        uses: jenkinsci/jenkins-core-changelog-generator@master
        env:
          GITHUB_AUTH: github-actions:${{ secrets.GITHUB_TOKEN }}
      - name: Upload Changelog YAML
        uses: actions/upload-artifact@v4
        with:
          name: changelog.yaml
          path: changelog.yaml

  jenkins_io_draft:
    runs-on: ubuntu-latest
    if: github.repository_owner == 'jenkinsci'
    steps:
      - uses: tibdex/github-app-token@v2
        id: generate-token
        with:
          app_id: ${{ secrets.JENKINS_CHANGELOG_UPDATER_APP_ID }}
          installation_retrieval_mode: repository
          installation_retrieval_payload: jenkins-infra/jenkins.io
          private_key: ${{ secrets.JENKINS_CHANGELOG_UPDATER_PRIVATE_KEY }}
          repositories: >-
            ["jenkins.io"]
      - name: Check out
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Publish jenkins.io changelog draft
        env:
          GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
          GITHUB_AUTH: "nobody:${{ steps.generate-token.outputs.token }}"
          GIT_AUTHOR_NAME: jenkins-infra-changelog-generator
          GIT_AUTHOR_EMAIL: <86592549+jenkins-infra-changelog-generator[bot]@users.noreply.github.com>
          GIT_COMMITTER_NAME: jenkins-infra-changelog-generator
          GIT_COMMITTER_EMAIL: <86592549+jenkins-infra-changelog-generator[bot]@users.noreply.github.com>
        run: |
          wget --quiet https://raw.githubusercontent.com/jenkinsci/core-changelog-generator/master/generate-weekly-changelog.sh
          # Create a Python virtual environment for pip install
          # See https://github.com/jenkinsci/core-changelog-generator/issues/37
          python3 -m venv venv
          source venv/bin/activate
          bash generate-weekly-changelog.sh
