Skip to content

Improve slope movement #171

Improve slope movement

Improve slope movement #171

# This workflow makes a naive attempt at enforcing a convention that
# PRs should generally always and only be labeled with the release label if it merges the development branch into the production branch.
#
# The workflow requires that the following repository variables have been created:
# - `REPOSITORY_NAME` - The name of the repository
# - `PRODUCTION_BRANCH_NAME` - The name of the repository's production branch (e.g. `main`)
# - `DEVELOPMENT_BRANCH_NAME` - The name of the repository's development branch (e.g. `dev`)
# - `RELEASE_LABEL_NAME` - The name of the release label used in the repository (e.g. `new release`)
# and the following organization variables:
# - `ORGANIZATION_NAME` - The name of the organization in the project URL (https://github.com/orgs/hackerspace-ntnu/projects/1)
name: Label release pull requests
on:
pull_request:
# `edited` will trigger if the PR changes its base branch
types: [ opened, edited ]
jobs:
manage_release_label_of_pull_request:
# Do a rough check - which will weed out most PRs - before doing more detailed checks in the steps below
if: ${{ github.base_ref == vars.PRODUCTION_BRANCH_NAME && github.head_ref == vars.DEVELOPMENT_BRANCH_NAME
|| contains(github.event.pull_request.labels.*.name, vars.RELEASE_LABEL_NAME) }}
runs-on: ubuntu-latest
steps:
- name: Generate token
id: generate_token
uses: tibdex/github-app-token@3beb63f4bd073e61482598c45c71c1019b59b73a # v2.1.0
with:
app_id: ${{ secrets.HACKERSPACE_BOT_APP_ID }}
private_key: ${{ secrets.HACKERSPACE_BOT_APP_PEM }}
- name: Get label data
env:
GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }}
ORGANIZATION: ${{ vars.ORGANIZATION_NAME }}
REPOSITORY_NAME: ${{ vars.REPOSITORY_NAME }}
RELEASE_LABEL_NAME: ${{ vars.RELEASE_LABEL_NAME }}
run: |
gh api graphql -f query='
query ($org: String!, $repoName: String!) {
organization(login: $org) {
repository(name: $repoName) {
labels(first: 100) {
nodes {
id
name
}
}
}
}
}' -f org="$ORGANIZATION" -f repoName="$REPOSITORY_NAME" > repo_data.json
# The ID of this repository's release label
echo "LABEL_ID=$(jq --arg LABEL_NAME "$RELEASE_LABEL_NAME" -r '.data.organization.repository.labels.nodes[] | select(.name==$LABEL_NAME) | .id' repo_data.json)" >> "$GITHUB_ENV"
- name: Label release pull request
# Add the release label if the PR merges the development branch into the production branch,
# and it doesn't already have the release label
if: ${{ github.base_ref == vars.PRODUCTION_BRANCH_NAME && github.head_ref == vars.DEVELOPMENT_BRANCH_NAME
&& !contains(github.event.pull_request.labels.*.name, vars.RELEASE_LABEL_NAME) }}
env:
GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }}
CONTENT_ID: ${{ github.event.pull_request.node_id }}
run: |
gh api graphql -f query='
mutation ($content: ID!, $label: ID!) {
addLabelsToLabelable(input: {labelableId: $content, labelIds: [$label]}) {
labelable {
labels {
totalCount
}
}
}
}' -f content="$CONTENT_ID" -f label="$LABEL_ID" --silent
- name: Remove label from non-release pull request
# Remove the release label if the PR is not for merging the development branch into the production branch
if: ${{ (github.base_ref != vars.PRODUCTION_BRANCH_NAME || github.head_ref != vars.DEVELOPMENT_BRANCH_NAME)
&& contains(github.event.pull_request.labels.*.name, vars.RELEASE_LABEL_NAME) }}
env:
GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }}
CONTENT_ID: ${{ github.event.pull_request.node_id }}
run: |
gh api graphql -f query='
mutation ($content: ID!, $label: ID!) {
removeLabelsFromLabelable(input: {labelableId: $content, labelIds: [$label]}) {
labelable {
labels {
totalCount
}
}
}
}' -f content="$CONTENT_ID" -f label="$LABEL_ID" --silent