Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions .github/labeler.yml

This file was deleted.

144 changes: 144 additions & 0 deletions .github/tests/release-policy.test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const test = require('node:test');
const vm = require('node:vm');

const workflow = fs.readFileSync(
path.join(__dirname, '..', 'workflows', 'workflow.yml'),
'utf8'
);
const scriptMatch = workflow.match(
/ script: \|\r?\n((?: [^\r\n]*(?:\r?\n|$))*)/
);
assert.ok(scriptMatch, 'The release policy script must exist in the workflow.');
const script = scriptMatch[1].replace(/^ {12}/gm, '');

test('no workflow uses pull_request_target and the labeler is removed', () => {
const workflowDirectory = path.join(__dirname, '..', 'workflows');
for (const file of fs.readdirSync(workflowDirectory)) {
if (/\.ya?ml$/i.test(file)) {
const content = fs.readFileSync(path.join(workflowDirectory, file), 'utf8');
assert.doesNotMatch(content, /\bpull_request_target\b/, file);
}
}
assert.equal(fs.existsSync(path.join(workflowDirectory, 'labeler.yml')), false);
assert.equal(fs.existsSync(path.join(__dirname, '..', 'labeler.yml')), false);
});

function mergedPullRequest(overrides = {}) {
return {
merged_at: '2026-10-19T12:00:00Z',
merge_commit_sha: 'trusted-commit',
base: { repo: { id: 123 }, ref: 'main' },
labels: [{ name: 'Release' }],
...overrides
};
}

async function evaluate(pullRequests, { branch = 'main', apiError } = {}) {
const outputs = {};
const endpoint = Symbol('listPullRequestsAssociatedWithCommit');
const context = {
repo: { owner: 'microsoft', repo: 'CollectServiceFabricData' },
sha: 'trusted-commit',
ref: `refs/heads/${branch}`,
payload: { repository: { id: 123 } }
};
const github = {
rest: { repos: { listPullRequestsAssociatedWithCommit: endpoint } },
paginate: async (method, options) => {
assert.equal(method, endpoint);
assert.deepEqual({ ...options }, {
...context.repo,
commit_sha: context.sha,
per_page: 100
});
if (apiError) {
throw apiError;
}
return pullRequests;
}
};
await vm.runInNewContext(`(async () => {\n${script}\n})()`, {
github,
context,
core: {
setOutput: (name, value) => {
outputs[name] = value;
}
}
});
return outputs;
}

test('a Release-labelled merge publishes a stable release', async () => {
assert.deepEqual(await evaluate([mergedPullRequest()]), {
publish: true,
prerelease: false
});
});

test('a Pre-release-labelled merge publishes a prerelease', async () => {
const pr = mergedPullRequest({ labels: [{ name: 'Pre-release' }] });
assert.deepEqual(await evaluate([pr]), { publish: true, prerelease: true });
});

test('Pre-release takes precedence when both labels are present', async () => {
const pr = mergedPullRequest({
labels: [{ name: 'Release' }, { name: 'Pre-release' }]
});
assert.deepEqual(await evaluate([pr]), { publish: true, prerelease: true });
});

test('a fork PR can release only after its merge matches the trusted push', async () => {
const pr = mergedPullRequest({
head: { repo: { id: 456 }, sha: 'untrusted-head' }
});
assert.equal((await evaluate([pr])).publish, true);
pr.merge_commit_sha = 'untrusted-head';
assert.equal((await evaluate([pr])).publish, false);
});

test('master merges are supported', async () => {
const pr = mergedPullRequest({ base: { repo: { id: 123 }, ref: 'master' } });
assert.equal((await evaluate([pr], { branch: 'master' })).publish, true);
});

for (const [name, pullRequests] of [
['direct push', []],
['unlabelled merge', [mergedPullRequest({ labels: [] })]],
['unrelated label', [mergedPullRequest({ labels: [{ name: 'documentation' }] })]],
['case-mismatched release label', [mergedPullRequest({ labels: [{ name: 'release' }] })]],
['open or unmerged PR', [mergedPullRequest({ merged_at: null })]],
['different merge commit', [mergedPullRequest({ merge_commit_sha: 'older-commit' })]],
['different base repository', [mergedPullRequest({ base: { repo: { id: 456 }, ref: 'main' } })]],
['different base branch', [mergedPullRequest({ base: { repo: { id: 123 }, ref: 'master' } })]]
]) {
test(`${name} does not publish`, async () => {
assert.deepEqual(await evaluate(pullRequests), {
publish: false,
prerelease: false
});
});
}

test('only the merged PR matching the pushed tip determines labels', async () => {
const older = mergedPullRequest({ merge_commit_sha: 'older-commit' });
const tip = mergedPullRequest({ labels: [] });
assert.equal((await evaluate([older, tip])).publish, false);
});

test('ambiguous merged PR matches fail explicitly', async () => {
await assert.rejects(
evaluate([mergedPullRequest(), mergedPullRequest()]),
/Multiple merged pull requests/
);
});

test('GitHub API failures fail explicitly', async () => {
await assert.rejects(
evaluate([], { apiError: new Error('GitHub API unavailable') }),
/GitHub API unavailable/
);
});
16 changes: 0 additions & 16 deletions .github/workflows/labeler.yml

This file was deleted.

84 changes: 63 additions & 21 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,47 @@ env:
artifacts_path: ${{ github.workspace }}\\src\\bin\\artifacts
release_path: ${{ github.workspace }}\\src\\bin\\Release
release_config: ${{ github.workspace }}\\configurationFiles\\collectsfdata.options.json
github_owner: ${{ github.event.repository.owner.name }}

on:
pull_request_target:
types: [closed, opened]
pull_request:
types: [opened, synchronize, reopened]
paths-ignore:
- "docs/**"
- "configurationFiles/**"
- "kusto/**"
branches:
- master
- main
push:
paths-ignore:
- "docs/**"
- "configurationFiles/**"
- "kusto/**"
branches:
- master
- main

permissions: {}

jobs:
build:
permissions:
contents: read
runs-on: windows-latest
timeout-minutes: 30
steps:
- name: checkout
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
with:
persist-credentials: false

- name: Validate release policy
shell: cmd
run: node --test .github\tests\release-policy.test.cjs

- name: environment
shell: cmd
run: |
set
dotnet --info
dotnet nuget locals all --clear

Expand All @@ -48,20 +65,18 @@ jobs:
- name: dotnet build / pack
shell: cmd
run: |
set
dotnet build %project_root%\%project_name%\%project_name%.csproj -v detailed -c Release
if errorlevel 1 exit /b %errorlevel%
dir

- name: build output
shell: cmd
run: |
set
dir /s

- name: copy artifacts
shell: powershell
run: |
[environment]::GetEnvironmentVariables()
write-host "copy "$($env:release_path)" "$($env:artifacts_path)" -recurse -force"
copy "$($env:release_path)" "$($env:artifacts_path)" -recurse -force
write-host "copy "$($env:release_config)" "$($env:artifacts_path)" -recurse -force"
Expand All @@ -77,23 +92,51 @@ jobs:
with:
name: Release
path: ${{ env.artifacts_path }}
if-no-files-found: error

release_metadata:
if: github.event_name == 'push'
runs-on: windows-latest
timeout-minutes: 5
permissions:
contents: read
pull-requests: read
outputs:
publish: ${{ steps.release_policy.outputs.publish }}
prerelease: ${{ steps.release_policy.outputs.prerelease }}
steps:
- name: Check merged pull request release labels
id: release_policy
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
with:
script: |
const pullRequests = await github.paginate(
github.rest.repos.listPullRequestsAssociatedWithCommit,
{ ...context.repo, commit_sha: context.sha, per_page: 100 }
);
const branch = context.ref.replace(/^refs\/heads\//, '');
const mergedPullRequests = pullRequests.filter(pr =>
pr.merged_at &&
pr.merge_commit_sha === context.sha &&
pr.base.repo.id === context.payload.repository.id &&
pr.base.ref === branch
);
if (mergedPullRequests.length > 1) {
throw new Error('Multiple merged pull requests match the pushed commit.');
}
const labels = mergedPullRequests[0]?.labels.map(label => label.name) ?? [];
core.setOutput('publish', labels.includes('Release') || labels.includes('Pre-release'));
core.setOutput('prerelease', labels.includes('Pre-release'));

release:
if: success() && github.event.pull_request.merged == true && github.event.action == 'closed' && (contains(github.event.pull_request.labels.*.name, 'Release') || contains(github.event.pull_request.labels.*.name, 'Pre-release'))
needs: build
if: github.event_name == 'push' && needs.release_metadata.outputs.publish == 'true'
needs: [build, release_metadata]
runs-on: windows-latest
timeout-minutes: 10
permissions:
contents: write
packages: write

steps:
- name: checkout
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0

- name: environment
shell: cmd
run: set

- name: download artifact
uses: actions/download-artifact@65a9edc5881444af0b9093a5e628f2fe47ea3b2e # v4.1.7
with:
Expand Down Expand Up @@ -130,8 +173,6 @@ jobs:
echo "file_version=v$fileVersion" >> $env:GITHUB_OUTPUT
write-host "dir $env:release_path -recurse"
dir $env:release_path -recurse
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# https://github.com/marketplace/actions/gh-release
- name: create release
Expand All @@ -142,8 +183,9 @@ jobs:
**/${{ env.project_name }}.zip
**/*${{ env.project_name }}*.nupkg
tag_name: ${{ steps.prepare_release_asset.outputs.file_version }}
target_commitish: ${{ github.sha }}
name: ${{ env.project_name }}
draft: false
token: ${{ secrets.GITHUB_TOKEN }}
prerelease: ${{ contains(github.event.pull_request.labels.*.name, 'Pre-release') }}
token: ${{ github.token }}
prerelease: ${{ needs.release_metadata.outputs.prerelease == 'true' }}
generate_release_notes: true
Loading
Loading