Skip to content

Create PR for submodule #297

Create PR for submodule

Create PR for submodule #297

name: 'Create PR for submodule'
on:
workflow_dispatch:
inputs:
submodule:
description: 'Submodule to create a PR for'
required: true
type: choice
options:
- 'gw2auth.com-api'
- 'gw2auth.com-canary'
- 'gw2auth.com-cdk'
- 'gw2auth.com-ui'
- 'opentelemetry-lambda'
submodule_ref:
description: 'Ref of the submodule to create a PR for'
required: true
type: string
submodule_branch:
description: 'Branch of the submodule'
required: false
type: string
target_override:
description: 'Target branch override'
required: false
type: string
jobs:
prepare_matrix:
name: 'Prepare matrix'
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.featureset_match.outputs.result }}
steps:
- name: 'Checkout'
uses: actions/checkout@v4
with:
sparse-checkout: |
featuresets.js
sparse-checkout-cone-mode: false
- name: 'Featureset match'
id: 'featureset_match'
uses: actions/github-script@v7
env:
SUBMODULE: ${{ inputs.submodule }}
SUBMODULE_REF: ${{ inputs.submodule_ref }}
SUBMODULE_BRANCH: ${{ inputs.submodule_branch }}
TARGET_OVERRIDE: ${{ inputs.target_override }}
with:
script: |
const submodule = process.env.SUBMODULE;
const submodule_ref = process.env.SUBMODULE_REF;
const submodule_branch = process.env.SUBMODULE_BRANCH;
const target_override = process.env.TARGET_OVERRIDE;
const featuresets = require('./featuresets.js');
const matrix = {include: []};
if (target_override && target_override.length > 0) {
matrix.include.push({target: target_override});
} else {
for (const [feature_branch, v] of Object.entries(featuresets)) {
const allowed_sources = v[submodule];
if (allowed_sources) {
for (const pattern of allowed_sources) {
if (new RegExp(pattern).test(submodule_ref) || new RegExp(pattern).test(submodule_branch)) {
matrix.include.push({target: feature_branch});
break;
}
}
}
}
}
console.info(`matrix: ${JSON.stringify(matrix)}`);
return matrix;
create_pr_branch:
name: 'Prepare PR branch'
runs-on: ubuntu-latest
needs:
- prepare_matrix
strategy:
matrix: ${{ fromJSON(needs.prepare_matrix.outputs.matrix) }}
permissions:
contents: write
steps:
- name: 'Checkout'
uses: actions/checkout@v4
with:
ref: 'beta'
ssh-key: ${{ secrets.CDK_SSH_KEY }}
submodules: true
fetch-depth: 0
- name: 'Checkout target branch'
run: 'git checkout ${{ matrix.target }} || git checkout -b ${{ matrix.target }}'
- name: 'Push target branch'
run: 'git push "https://${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" ${{ matrix.target }}'
- name: 'Checkout PR branch (1)'
run: 'git checkout -b "pr/${{ matrix.target }}/${{ inputs.submodule }}" && git reset --hard ${{ matrix.target }}'
- name: 'Update submodule ref (1)'
working-directory: ${{ inputs.submodule }}
run: 'git fetch origin "${{ inputs.submodule_ref }}" && git checkout "${{ inputs.submodule_ref }}"'
- name: 'Commit changes (1)'
run: |
git config --local user.name "${{ github.actor }}"
git config --local user.email "${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com"
git add ${{ inputs.submodule }}
git commit -m "update ${{ inputs.submodule }} to ${{ inputs.submodule_ref }}" || echo "ignore commit"
- name: 'Push PR branch (1)'
run: 'git push --force "https://${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" "pr/${{ matrix.target }}/${{ inputs.submodule }}"'
- name: 'Checkout PR branch (2)'
run: 'git checkout ${{ matrix.target }} && (git checkout "pr/${{ matrix.target }}/all" || git checkout -b "pr/${{ matrix.target }}/all")'
- name: 'Update submodule ref (2)'
working-directory: ${{ inputs.submodule }}
run: 'git fetch origin "${{ inputs.submodule_ref }}" && git checkout "${{ inputs.submodule_ref }}"'
- name: 'Commit changes (2)'
run: |
git config --local user.name "${{ github.actor }}"
git config --local user.email "${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com"
git add ${{ inputs.submodule }}
git commit -m "update ${{ inputs.submodule }} to ${{ inputs.submodule_ref }}" || echo "ignore commit"
- name: 'Push PR branch (2)'
run: 'git push --force "https://${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" "pr/${{ matrix.target }}/all"'
create_pr:
name: 'Create PR'
runs-on: ubuntu-latest
needs:
- prepare_matrix
- create_pr_branch
strategy:
matrix: ${{ fromJSON(needs.prepare_matrix.outputs.matrix) }}
permissions:
pull-requests: write
steps:
- name: 'Create PR'
uses: actions/github-script@v7
with:
script: |
const { owner, repo } = context.repo;
const base = '${{ matrix.target }}';
const heads = [
'pr/${{ matrix.target }}/${{ inputs.submodule }}',
'pr/${{ matrix.target }}/all',
];
for (const head of heads) {
const pulls = await github.rest.pulls.list({
owner: owner,
repo: repo,
head: `${owner}:${head}`,
base: base,
state: 'open',
});
if (pulls.data.length < 1) {
try {
await github.rest.pulls.create({
title: `[CI] Merge ${head} into ${base}`,
owner: owner,
repo: repo,
head: head,
base: base,
body: [
'This PR is auto-generated by',
'[actions/github-script](https://github.com/actions/github-script)',
].join('\n'),
});
} catch (e) {
console.warn(e);
}
} else {
const existingPR = pulls.data[0];
await github.rest.issues.createComment({
owner: owner,
repo: repo,
issue_number: existingPR.number,
body: [
`Updated by Job ${context.job}`,
].join('\n'),
});
}
}