name: Release

on:
    workflow_dispatch:
    release:
        types: [ released ]

jobs:
    remove_prerelease_label:
        name: Remove pre-release label from issues
        runs-on: ubuntu-latest
        steps:
            -   name: Remove label
                id: tags
                uses: actions/github-script@v6
                env:
                    LABEL_NAME: pre-release
                with:
                    script: |
                        const { data: issues } = await github.request('GET /repos/{owner}/{repo}/issues', {
                            owner: context.repo.owner,
                            repo: context.repo.repo,
                            labels: process.env.LABEL_NAME,
                            state: 'all'
                        });
                
                        if (issues.length === 0) {
                            core.notice(`No issues with label "${ process.env.LABEL_NAME }" found.`);
                            return;
                        }

                        const issueIds = issues.map(({ node_id }) => node_id);
                        const labelId = issues[0].labels.find(({ name }) => name === process.env.LABEL_NAME).node_id;

                        core.info(`Found ${ issueIds.length } issues with label "${ process.env.LABEL_NAME }"`);

                        for (const issueId of issueIds) {
                            core.info(`Removing label for issue with node_id ${ issueId }...`);

                            await github.graphql(`
                                mutation($input: RemoveLabelsFromLabelableInput!) {
                                    removeLabelsFromLabelable(input: $input) {
                                        clientMutationId
                                    }
                                }
                                `, {
                                input: {
                                    labelIds: [ labelId ],
                                    labelableId: issueId
                                },
                            });

                            core.info('Done.\n');
                        }


    label_released_issues:
        name: Label released issues
        uses: './.github/workflows/label-issues-in-release.yml'
        with:
            tag: ${{ github.ref_name }}
            label_name: ${{ github.ref_name }}
            label_description: Release ${{ github.ref_name }}
        secrets: inherit

    update_project_cards:
        name: Update project tasks statuses
        needs: label_released_issues
        uses: './.github/workflows/update-project-cards.yml'
        with:
            project_name: ${{ vars.PROJECT_NAME }}
            field_name: Status
            field_value: Released
            issues: ${{ needs.label_released_issues.outputs.issues }}
        secrets: inherit

    publish_image:
        name: Publish Docker image
        uses: './.github/workflows/publish-image.yml'
        secrets: inherit

    upload_source_maps:
        name: Upload source maps to Sentry
        needs: publish_image
        uses: './.github/workflows/upload-source-maps.yml'
        secrets: inherit
