Skip to content

Commit

Permalink
Merge branch 'brownfield-notice-workflow-run' into test-brownfield-no…
Browse files Browse the repository at this point in the history
…tice-2
  • Loading branch information
mikeharder authored Nov 15, 2024
2 parents 08c62a6 + 911bf99 commit 2499b66
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 188 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
name: Upload KeyValue Artifact
description: Uploads an empty artifact with the specified key and value encoded in the name
name: Add Label Artifact
description: Uploads an empty artifact named `label-${name}=${value}`, that's consumed by update-labels.yaml

inputs:
key:
description: Key
name:
description: Name
required: true
value:
description: Value
description: Value ("true" or "false")
required: true

runs:
using: composite

steps:
# When possible, it's best to upload an empty file with all information in the artifact name, so consumers
# can query the information without needing to download files to disk.
- name: Create empty file to upload artifact
run: "> $RUNNER_TEMP/empty.txt"
shell: bash
Expand All @@ -25,7 +23,7 @@ runs:
# https://github.com/actions/toolkit/blob/main/packages/artifact/src/internal/upload/path-and-artifact-name-validation.ts
- uses: actions/upload-artifact@v4
with:
name: ${{ inputs.key }}=${{ inputs.value }}
name: label-${{ inputs.name }}=${{ inputs.value }}
path: ${{ runner.temp }}/empty.txt
if-no-files-found: error
overwrite: true
54 changes: 0 additions & 54 deletions .github/actions/get-keyvalue-artifacts/action.js

This file was deleted.

33 changes: 0 additions & 33 deletions .github/actions/get-keyvalue-artifacts/action.yaml

This file was deleted.

53 changes: 0 additions & 53 deletions .github/actions/set-label/action.js

This file was deleted.

102 changes: 102 additions & 0 deletions .github/actions/update-labels/action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// @ts-check

const { extractInputs } = require("../context");

/**
* @param {import('github-script').AsyncFunctionArguments} AsyncFunctionArguments
*/
module.exports = async ({ github, context, core }) => {
let owner = process.env.OWNER;
let repo = process.env.REPO;
let issue_number = parseInt(process.env.ISSUE_NUMBER || "");
let run_id = parseInt(process.env.RUN_ID || "");

if (!owner || !repo || !(issue_number || run_id)) {
let inputs = await extractInputs(github, context, core);
owner = owner || inputs.owner;
repo = repo || inputs.repo;
issue_number = issue_number || inputs.issue_number;
run_id = run_id || inputs.run_id;
}

/** @type {string[]} */
let artifactNames = [];

if (run_id) {
// List artifacts from a single run_id
core.info(`listWorkflowRunArtifacts(${owner}, ${repo}, ${run_id})`);
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: owner,
repo: repo,
run_id: run_id,
});

artifactNames = artifacts.data.artifacts.map((a) => a.name);
} else {
// TODO: List all artifacts of issue_number
}

core.info(`artifactNames: ${JSON.stringify(artifactNames)}`);

/** @type {string[]} */
const labelsToAdd = [];

/** @type {string[]} */
const labelsToRemove = [];

for (const artifactName of artifactNames) {
// If artifactName has format "label-name=true|false", add or remove the label
// Else, if artifactName has format "label-name=other-string", throw an error
// Else, if artifactName does not start with "label-", ignore it
const firstEquals = artifactName.indexOf("=");
if (firstEquals !== -1) {
const key = artifactName.substring(0, firstEquals);
const value = artifactName.substring(firstEquals + 1);

if (key.startsWith("label-")) {
const name = key.substring("label-".length);
if (value === "true") {
labelsToAdd.push(name);
} else if (value === "false") {
labelsToRemove.push(name);
} else {
throw new Error(
`Invalid value for label '${name}': ${value}. Expected "true" or "false".`,
);
}
}
}
}

core.info(`labelsToAdd: ${JSON.stringify(labelsToAdd)}`);
core.info(`labelsToRemove: ${JSON.stringify(labelsToRemove)}`);

if (labelsToAdd.length > 0) {
await github.rest.issues.addLabels({
owner: owner,
repo: repo,
issue_number: issue_number,
labels: labelsToAdd,
});
}

if (labelsToRemove.length > 0) {
// Must loop over labelsToRemove ourselves, since GitHub doesn't expose a REST API to remove in bulk.
for (const name in labelsToRemove) {
try {
await github.rest.issues.removeLabel({
owner: owner,
repo: repo,
issue_number: issue_number,
name: name,
});
} catch (error) {
if (error.status === 404) {
core.info(`Ignoring error: ${error.status} - ${error.message}`);
} else {
throw error;
}
}
}
}
};
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
name: Set Label
name: Update Labels
description: Adds or removes label to set state matching value

# If any inputs are not set, we will attempt to extract them from the event context
inputs:
name:
description: Name of the label
required: true
value:
description: Whether to add or remove the label
required: true
owner:
description: The account owner of the repository. The name is not case sensitive.
required: false
repo:
description: The name of the repository without the .git extension. The name is not case sensitive.
required: false
issue_number:
description: The number that identifies the issue.
description: Updates labels from all completed workflows associated with head commit of issue.
required: false
run_id:
description: Updates labels from a single completed workflow.
required: false

runs:
Expand All @@ -26,12 +23,11 @@ runs:
- name: Set Label
uses: actions/github-script@v7
env:
NAME: ${{ inputs.name }}
VALUE: ${{ inputs.value }}
OWNER: ${{ inputs.owner }}
REPO: ${{ inputs.repo }}
ISSUE_NUMBER: ${{ inputs.issue_number }}
RUN_ID: ${{ inputs.run_id }}
with:
script: |
const action = require('./.github/actions/set-label/action.js')
const action = require('./.github/actions/update-labels/action.js')
await action({ github, context, core });
15 changes: 4 additions & 11 deletions .github/workflows/typespec-requirement.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ on: pull_request
jobs:
TypeSpec-Requirement:
name: TypeSpec Requirement
strategy:
fail-fast: false
matrix:
spec-type: [data-plane, resource-manager]

runs-on: ubuntu-latest

Expand All @@ -21,20 +17,17 @@ jobs:
- name: Setup Node and run `npm ci`
uses: ./.github/actions/setup-node-npm-ci

# Outputs
# - spec-lifecycle?: "brownfield"
- run: |
eng/scripts/TypeSpec-Requirement.ps1 `
-BaseCommitish HEAD^ `
-TargetCommitish HEAD `
-SpecType ${{ matrix.spec-type }}
id: tsr-ps1
shell: pwsh
- if: ${{ steps.tsr-ps1.outputs.spec-lifecycle }}
uses: ./.github/actions/upload-keyvalue-artifact
# Always add label artifact, even if "brownfield=false", to ensure label is removed when necessary
- uses: ./.github/actions/add-label-artifact
name: Upload artifact with results
with:
key: spec-lifecycle-${{ matrix.spec-type }}
value: ${{ steps.tsr-ps1.outputs.spec-lifecycle }}
name: "brownfield"
value: "${{ steps.tsr-ps1.outputs.brownfield == 'true' }}"

Loading

0 comments on commit 2499b66

Please sign in to comment.