Merge pull request #3570 from github/mbg/repo-props/warn-on-unexpecte… · github/codeql-action@4c356c7 (original) (raw)

Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import * as sinon from "sinon";
4 4 import * as api from "../api-client";
5 5 import { getRunnerLogger } from "../logging";
6 6 import { parseRepositoryNwo } from "../repository";
7 -import { setupTests } from "../testing-utils";
7 +import { RecordingLogger, setupTests } from "../testing-utils";
8 8
9 9 import * as properties from "./properties";
10 10
@@ -197,3 +197,38 @@ test.serial(
197 197 );
198 198 },
199 199 );
200 +
201 +test.serial(
202 +"loadPropertiesFromApi warns if a repository property name starts with the common prefix, but is not recognised by us",
203 +async (t) => {
204 +process.env["GITHUB_EVENT_NAME"] = "push";
205 +const propertyName: string = `${properties.GITHUB_CODEQL_PROPERTY_PREFIX}unknown`;
206 +sinon.stub(api, "getRepositoryProperties").resolves({
207 +headers: {},
208 +status: 200,
209 +url: "",
210 +data: [
211 +{
212 +property_name: propertyName,
213 +value: "true",
214 +},
215 +] satisfies properties.GitHubPropertiesResponse,
216 +});
217 +const logger = new RecordingLogger();
218 +const warningSpy = sinon.spy(logger, "warning");
219 +const mockRepositoryNwo = parseRepositoryNwo("owner/repo");
220 +const response = await properties.loadPropertiesFromApi(
221 +logger,
222 +mockRepositoryNwo,
223 +);
224 +t.deepEqual(response, {});
225 +t.true(warningSpy.calledOnce);
226 +t.assert(
227 +warningSpy.firstCall.args[0]
228 +.toString()
229 +.startsWith(
230 +`Found repository properties ('${propertyName}'), which look like CodeQL Action repository properties`,
231 +),
232 +);
233 +},
234 +);
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
1 +import { isDynamicWorkflow } from "../actions-util";
1 2 import { getRepositoryProperties } from "../api-client";
2 3 import { Logger } from "../logging";
3 4 import { RepositoryNwo } from "../repository";
4 5
6 +/** The common prefix that we expect all of our repository properties to have. */
7 +export const GITHUB_CODEQL_PROPERTY_PREFIX = "github-codeql-";
8 +
5 9 /**
6 10 * Enumerates repository property names that have some meaning to us.
7 11 */
@@ -114,6 +118,8 @@ export async function loadPropertiesFromApi(
114 118 );
115 119
116 120 const properties: RepositoryProperties = {};
121 +const unrecognisedProperties: string[] = [];
122 +
117 123 for (const property of remoteProperties) {
118 124 if (property.property_name === undefined) {
119 125 throw new Error(
@@ -123,6 +129,11 @@ export async function loadPropertiesFromApi(
123 129
124 130 if (isKnownPropertyName(property.property_name)) {
125 131 setProperty(properties, property.property_name, property.value, logger);
132 +} else if (
133 +property.property_name.startsWith(GITHUB_CODEQL_PROPERTY_PREFIX) &&
134 +!isDynamicWorkflow()
135 +) {
136 +unrecognisedProperties.push(property.property_name);
126 137 }
127 138 }
128 139
@@ -139,6 +150,20 @@ export async function loadPropertiesFromApi(
139 150 }
140 151 }
141 152
153 +// Emit a warning if we encountered unrecognised properties that have our prefix.
154 +if (unrecognisedProperties.length > 0) {
155 +const unrecognisedPropertyList = unrecognisedProperties
156 +.map((name) => `'${name}'`)
157 +.join(", ");
158 +
159 +logger.warning(
160 +`Found repository properties (${unrecognisedPropertyList}), ` +
161 +"which look like CodeQL Action repository properties, " +
162 +"but which are not understood by this version of the CodeQL Action. " +
163 +"Do you need to update to a newer version?",
164 +);
165 +}
166 +
142 167 return properties;
143 168 } catch (e) {
144 169 throw new Error(