Skip to content

Commit

Permalink
feat(controller): Create GitHub check for taskRuns (#146)
Browse files Browse the repository at this point in the history
* feat(controller): Create GitHub chceks for taskRuns

Signed-off-by: SamoKopecky <skopecky@redhat.com>

* fix(peribolos-run): Use the correct repo remote

Signed-off-by: SamoKopecky <skopecky@redhat.com>
  • Loading branch information
SamoKopecky authored Jun 30, 2022
1 parent 39a84ac commit 24b25e9
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 46 deletions.
32 changes: 30 additions & 2 deletions manifests/base/tasks/run.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ spec:
default: ".github"
- name: SECRET_NAME
type: string
- name: CHECK_RUN_ID
type: string
steps:
- name: apply-peribolos
image: toolbox
Expand All @@ -22,17 +24,43 @@ spec:
secretKeyRef:
name: $(params.SECRET_NAME)
key: orgName
- name: GITHUB_TOKEN
valueFrom:
secretKeyRef:
name: $(params.SECRET_NAME)
key: token
script: |
#!/usr/bin/bash
echo "Updating check run to in-progress..."
curl -X PATCH \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
https://api.github.com/repos/$ORG_NAME/.github/check-runs/$(params.CHECK_RUN_ID) \
-d '{"status":"in_progress"}'
echo "Cloning repository..."
# Clone repository
git clone https://github.com/$ORG_NAME/$(params.REPO_NAME)
git clone https://x-access-token:$GITHUB_TOKEN@github.com/$ORG_NAME/$(params.REPO_NAME)
cd $(params.REPO_NAME)
# Run Peribolos on the repository
echo "Running peribolos..."
echo "Running peribolos on commit $(git rev-parse --short HEAD)..."
peribolos --config-path peribolos.yaml --github-token-path /mnt/secret/token --fix-org --fix-repos --fix-team-members --fix-teams --fix-team-repos --confirm
if [ $? -eq 0 ]; then
CONCLUSION="success"
else
CONCLUSION="failure"
fi
echo "Updating check run to completed with $CONCLUSION..."
curl -X PATCH \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
https://api.github.com/repos/$ORG_NAME/.github/check-runs/$(params.CHECK_RUN_ID) \
-d "{\"status\":\"completed\",\"conclusion\":\"$CONCLUSION\"}"
volumes:
- name: github-token
secret:
Expand Down
143 changes: 99 additions & 44 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,6 @@ import {
useApi,
} from '@operate-first/probot-kubernetes';

const generateTaskPayload = (name: string, context: any) => ({
apiVersion: 'tekton.dev/v1beta1',
kind: 'TaskRun',
metadata: {
generateName: name + '-',
},
spec: {
taskRef: {
name,
},
params: [
{
name: 'SECRET_NAME',
value: getTokenSecretName(context),
},
],
},
});

export default (
app: Probot,
{
Expand Down Expand Up @@ -86,6 +67,47 @@ export default (
.inc();
};

const createTaskRun = (
name: string,
context: any,
extraParams: Array<Record<string, unknown>> = []
) => {
const params = [
{
name: 'SECRET_NAME',
value: getTokenSecretName(context),
},
...extraParams,
];
const taskRunpayload = {
apiVersion: 'tekton.dev/v1beta1',
kind: 'TaskRun',
metadata: {
generateName: name + '-',
},
spec: {
taskRef: {
name,
},
params: params,
},
};

wrapOperationWithMetrics(
useApi(APIS.CustomObjectsApi).createNamespacedCustomObject(
'tekton.dev',
'v1beta1',
getNamespace(),
'taskruns',
taskRunpayload
),
{
install: context.payload.installation.id,
method: name,
}
);
};

app.onAny((context: any) => {
// On any event inc() the counter
numberOfActionsTotal
Expand Down Expand Up @@ -124,19 +146,7 @@ export default (
});

// Trigger dump-config taskrun
wrapOperationWithMetrics(
useApi(APIS.CustomObjectsApi).createNamespacedCustomObject(
'tekton.dev',
'v1beta1',
getNamespace(),
'taskruns',
generateTaskPayload('peribolos-dump-config', context)
),
{
install: context.payload.installation.id,
method: 'scheduleDumpConfig',
}
);
createTaskRun('peribolos-dump-config', context);
});

app.on('push', async (context: any) => {
Expand Down Expand Up @@ -165,20 +175,65 @@ export default (
method: 'updateSecret',
});

// I think for now we can use the taskName Prefix and edit it when we
// are adding additional information to the check
const checkResponse = await context.octokit.checks.create({
owner: context.payload.organization.login,
repo: '.github',
name: 'peribolos-run',
head_sha: context.payload.after,
status: 'queued',
});

// Trigger taskrun to apply config changes to org
wrapOperationWithMetrics(
useApi(APIS.CustomObjectsApi).createNamespacedCustomObject(
'tekton.dev',
'v1beta1',
getNamespace(),
'taskruns',
generateTaskPayload('peribolos-run', context)
),
createTaskRun('peribolos-run', context, [
{
install: context.payload.installation.id,
method: 'schedulePushTask',
}
);
name: 'CHECK_RUN_ID',
value: checkResponse.data.id.toString(),
},
]);
});

app.on('check_run.rerequested', async (context: any) => {
// In the future a check is needed if this is the peribolos-run check
const checkCommit = context.payload.check_run.head_sha;
const comRepo = await context.octokit.repos.get({
owner: context.payload.organization.login,
repo: '.github',
});
const defaultBranch = await context.octokit.repos.getBranch({
owner: context.payload.organization.login,
repo: '.github',
branch: comRepo.data.default_branch,
});
const headCommit = defaultBranch.data.commit.sha;

if (checkCommit !== headCommit) {
// When adding aditional information to the created check, update
// the body here too with the reason the check was skipped
context.octokit.checks.update({
owner: context.payload.organization.login,
repo: '.github',
check_run_id: context.payload.check_run.id,
status: 'completed',
conclusion: 'skipped',
});
return;
}

context.octokit.checks.update({
owner: context.payload.organization.login,
repo: '.github',
check_run_id: context.payload.check_run.id,
status: 'queued',
});

createTaskRun('peribolos-run', context, [
{
name: 'CHECK_RUN_ID',
value: context.payload.check_run.id.toString(),
},
]);
});

app.on('installation.deleted', async (context: any) => {
Expand Down

0 comments on commit 24b25e9

Please sign in to comment.