Skip to content

Commit b41b1f0

Browse files
22367rhRobin Hilton
andauthored
fix(github-actions): use default branch of repository for latest builds where branch unspecified instead of assuming master (#4636)
* fix(github-actions): use repo default branch instead of assuming master Signed-off-by: Robin Hilton <[email protected]> * expand to include feedback from PR and also update Alpha UI to remove default of master Signed-off-by: Robin Hilton <[email protected]> * remove superfluous lines that are unused Signed-off-by: Robin Hilton <[email protected]> * attempt to resolve CI errors Signed-off-by: Robin Hilton <[email protected]> * remove superfluous lines that are unused Signed-off-by: Robin Hilton <[email protected]> * remove superfluous lines that are unused Signed-off-by: Robin Hilton <[email protected]> * Fix linting issue Signed-off-by: Robin Hilton <[email protected]> * force run prettier over the file Signed-off-by: Robin Hilton <[email protected]> * Fix CI issue around report-alpha.api.md Signed-off-by: Robin Hilton <[email protected]> * Fix CI issue around report-alpha.api.md Signed-off-by: Robin Hilton <[email protected]> * update test suite to match new ui expectation Signed-off-by: Robin Hilton <[email protected]> --------- Signed-off-by: Robin Hilton <[email protected]> Signed-off-by: Robin Hilton <[email protected]> Co-authored-by: Robin Hilton <[email protected]>
1 parent 5eecb19 commit b41b1f0

File tree

6 files changed

+78
-12
lines changed

6 files changed

+78
-12
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@backstage-community/plugin-github-actions': patch
3+
---
4+
5+
Change default branch to be that of the target repository instead of assuming it is using master.

workspaces/github-actions/plugins/github-actions/report-alpha.api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const _default: FrontendPlugin<
4545
]: ExtensionDefinition<{
4646
config: {
4747
props: {
48-
branch: string;
48+
branch?: string | undefined;
4949
};
5050
} & {
5151
filter: EntityPredicate | undefined;
@@ -104,7 +104,7 @@ const _default: FrontendPlugin<
104104
[x: `entity-card:${string}/latest-workflow-run`]: ExtensionDefinition<{
105105
config: {
106106
props: {
107-
branch: string;
107+
branch?: string | undefined;
108108
};
109109
} & {
110110
filter: EntityPredicate | undefined;
@@ -164,7 +164,7 @@ const _default: FrontendPlugin<
164164
config: {
165165
props: {
166166
dense: boolean;
167-
branch: string;
167+
branch?: string | undefined;
168168
limit?: number | undefined;
169169
};
170170
} & {

workspaces/github-actions/plugins/github-actions/src/alpha/entityCards.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ describe('Entity card extensions', () => {
7676

7777
await waitFor(
7878
() => {
79-
expect(screen.getByText('Last master build')).toBeInTheDocument();
79+
expect(screen.getByText('Last main build')).toBeInTheDocument();
8080
},
8181
{ timeout: 5000 },
8282
);
@@ -95,7 +95,7 @@ describe('Entity card extensions', () => {
9595

9696
await waitFor(
9797
() => {
98-
expect(screen.getByText('Last master build')).toBeInTheDocument();
98+
expect(screen.getByText('Recent main builds')).toBeInTheDocument();
9999
},
100100
{ timeout: 5000 },
101101
);

workspaces/github-actions/plugins/github-actions/src/alpha/entityCards.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const entityLatestGithubActionRunCard =
3939
props: z =>
4040
z
4141
.object({
42-
branch: z.string().default('master'),
42+
branch: z.string().optional(),
4343
})
4444
.default({}),
4545
},
@@ -66,7 +66,7 @@ export const entityLatestGithubActionsForBranchCard =
6666
props: z =>
6767
z
6868
.object({
69-
branch: z.string().default('master'),
69+
branch: z.string().optional(),
7070
})
7171
.default({}),
7272
},
@@ -93,7 +93,7 @@ export const entityRecentGithubActionsRunsCard =
9393
props: z =>
9494
z
9595
.object({
96-
branch: z.string().default('master'),
96+
branch: z.string().optional(),
9797
dense: z.boolean().default(false),
9898
limit: z.number().default(5).optional(),
9999
})

workspaces/github-actions/plugins/github-actions/src/components/Cards/Cards.tsx

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
StructuredMetadataTable,
3333
} from '@backstage/core-components';
3434
import { getHostnameFromEntity } from '../getHostnameFromEntity';
35+
import { useDefaultBranch } from '../useDefaultBranch';
3536
import Box from '@material-ui/core/Box';
3637

3738
const useStyles = makeStyles({
@@ -45,7 +46,7 @@ const WidgetContent = (props: {
4546
error?: Error;
4647
loading?: boolean;
4748
lastRun: WorkflowRun;
48-
branch: string;
49+
branch?: string;
4950
}) => {
5051
const { error, loading, lastRun, branch } = props;
5152
const classes = useStyles();
@@ -81,13 +82,19 @@ export const LatestWorkflowRunCard = (props: {
8182
branch?: string;
8283
variant?: InfoCardVariants;
8384
}) => {
84-
const { branch = 'master', variant } = props;
85+
const { variant } = props;
8586
const { entity } = useEntity();
8687
const errorApi = useApi(errorApiRef);
8788
const hostname = getHostnameFromEntity(entity);
8889
const [owner, repo] = (
8990
entity?.metadata.annotations?.[GITHUB_ACTIONS_ANNOTATION] ?? '/'
9091
).split('/');
92+
const defaultBranch = useDefaultBranch({
93+
hostname,
94+
owner,
95+
repo,
96+
}).branch;
97+
const branch = props.branch ?? defaultBranch;
9198
const [{ runs, loading, error }] = useWorkflowRuns({
9299
hostname,
93100
owner,
@@ -119,11 +126,21 @@ export const LatestWorkflowsForBranchCard = (props: {
119126
branch?: string;
120127
variant?: InfoCardVariants;
121128
}) => {
122-
const { branch = 'master', variant } = props;
129+
const { variant } = props;
123130
const { entity } = useEntity();
131+
const hostname = getHostnameFromEntity(entity);
132+
const [owner, repo] = (
133+
entity?.metadata.annotations?.[GITHUB_ACTIONS_ANNOTATION] ?? '/'
134+
).split('/');
135+
const defaultBranch = useDefaultBranch({
136+
hostname,
137+
owner,
138+
repo,
139+
}).branch;
140+
const branch = props.branch ?? defaultBranch;
124141

125142
return (
126-
<InfoCard title={`Last ${branch} build`} variant={variant}>
143+
<InfoCard title={`Recent ${branch} builds`} variant={variant}>
127144
<WorkflowRunsTable branch={branch} entity={entity} />
128145
</InfoCard>
129146
);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2020 The Backstage Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import useAsyncRetry from 'react-use/esm/useAsyncRetry';
17+
import { githubActionsApiRef } from '../api/GithubActionsApi';
18+
import { useApi } from '@backstage/core-plugin-api';
19+
20+
export function useDefaultBranch({
21+
hostname,
22+
owner,
23+
repo,
24+
}: {
25+
hostname?: string;
26+
owner: string;
27+
repo: string;
28+
}) {
29+
const api = useApi(githubActionsApiRef);
30+
31+
const { value, loading, error } = useAsyncRetry<string>(async () => {
32+
return api.getDefaultBranch({
33+
hostname,
34+
owner,
35+
repo,
36+
});
37+
}, [api]);
38+
39+
return {
40+
branch: value,
41+
loading,
42+
error,
43+
};
44+
}

0 commit comments

Comments
 (0)