Skip to content

Commit

Permalink
[#93] added presenting repo info on flow page
Browse files Browse the repository at this point in the history
  • Loading branch information
godfryd committed Apr 4, 2021
1 parent 3d874b9 commit 1fcfc30
Show file tree
Hide file tree
Showing 12 changed files with 271 additions and 20 deletions.
17 changes: 14 additions & 3 deletions server/kraken/server/bg/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,13 +609,20 @@ def _check_repo_commits(stage, flow_kind):

# iterate over repos
changes = False
repo_data = {}
repo_data = []
for repo_url, repo_branch in repos:
commits = gitops.get_repo_commits_since(stage.branch_id, prev_run, repo_url, repo_branch)
commits, base_commit = gitops.get_repo_commits_since(stage.branch_id, prev_run, repo_url, repo_branch)

if commits:
changes = True
repo_data[repo_url] = commits
after = commits[0]['id']
else:
after = base_commit
repo_data.append(dict(repo=repo_url,
commits=commits,
before=base_commit,
after=after,
trigger='git-push'))

if not changes:
log.info('no commits since prev check')
Expand All @@ -635,8 +642,10 @@ def trigger_run(self, stage_id, flow_kind=consts.FLOW_KIND_CI, reason=None):
return

# if this stage does not have parent then start new flow
new_flow = False
if stage.schema['parent'] == 'root':
flow = Flow(branch=stage.branch, kind=flow_kind)
new_flow = True

# TODO: other root, sibling stages should be triggered when new flow is started

Expand Down Expand Up @@ -684,6 +693,8 @@ def trigger_run(self, stage_id, flow_kind=consts.FLOW_KIND_CI, reason=None):
return
if repo_data and reason and reason['reason'] == 'repo_interval':
reason = dict(reason='commit to repo')
if repo_data and new_flow:
flow.trigger_data = repo_data

# commit new flow if created and repo changes if detected
db.session.commit()
Expand Down
43 changes: 36 additions & 7 deletions server/kraken/server/gitops.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def get_repo_commits_since(branch_id, prev_run, repo_url, repo_branch):
log.info('base commit: %s', base_commit)
cmd += ' %s..' % base_commit
else:
base_commit = None
log.info('no base commit %s %s', repo_url, prev_run.repo_data)
p = subprocess.run(cmd, shell=True, check=True, cwd=repo_dir, capture_output=True, text=True)
text = p.stdout.strip()
Expand All @@ -109,16 +110,44 @@ def get_repo_commits_since(branch_id, prev_run, repo_url, repo_branch):


# collect commits info
commit = {}
commit = {'author': {}, 'added': [], 'modified': [], 'removed': []}
record_lines = 0
for line in text.splitlines():
field, val = line.split(':', 1)
commit[field] = val
if len(commit) == 5:
commits.append(commit)
line = line.strip()
# if empty line ie. record finished
if not line:
log.info(' %s', commit)
commit = {}
commits.append(commit)
commit = {'author': {}}
record_lines = 0
continue

record_lines += 1
if record_lines <= 5:
field, val = line.split(':', 1)
if field == 'commit':
commit['id'] = val
elif field == 'author':
commit['author']['name'] = val
elif field == 'email':
commit['author']['email'] = val
elif field == 'date':
commit['timestamp'] = val
elif field == 'subject':
commit['message'] = val
else:
raise Exception('unrecognized field %s' % field)
else:
flag, fpath = line.split(' ', 1)
fpath = fpath.strip()
if flag == 'A':
commit['added'].append(fpath)
elif flag == 'D':
commit['deleted'].append(fpath)
else:
commit['modified'].append(fpath)

return commits
return commits, base_commit


def get_schema_from_repo(repo_url, repo_branch, repo_access_token, schema_file): # pylint: disable=unused-argument
Expand Down
4 changes: 3 additions & 1 deletion ui/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ import { AgentsPageComponent } from './agents-page/agents-page.component'
import { DiscoveredPageComponent } from './discovered-page/discovered-page.component'
import { GroupsPageComponent } from './groups-page/groups-page.component'
import { SettingsPageComponent } from './settings-page/settings-page.component'
import { DiagsPageComponent } from './diags-page/diags-page.component'
import { DiagsPageComponent } from './diags-page/diags-page.component';
import { RepoChangesComponent } from './repo-changes/repo-changes.component'

export function cfgFactory() {
const params: ConfigurationParameters = {
Expand Down Expand Up @@ -100,6 +101,7 @@ export function cfgFactory() {
GroupsPageComponent,
SettingsPageComponent,
DiagsPageComponent,
RepoChangesComponent,
],
imports: [
BrowserModule,
Expand Down
65 changes: 59 additions & 6 deletions ui/src/app/flow-results/flow-results.component.html
Original file line number Diff line number Diff line change
@@ -1,23 +1,75 @@
<p-button label="Refresh" icon="pi pi-refresh" (onClick)="refresh()"></p-button>
<div style="display: flex; align-items: center;">
<div style="font-size: 2.5em; font-weight: bold; width: 10em;">
Flow {{ flow ? flow.label : flowId }}
</div>
<p-button label="Refresh" icon="pi pi-refresh" (onClick)="refresh()" styleClass="p-button-sm"></p-button>
</div>

<p-tabView [activeIndex]="activeTabIndex">
<p-tabPanel header="Stages">
<div class="p-grid">
<div class="p-col-6">
<div class="p-col-5">
<div style="display: flex; flex-direction: column; align-items: flex-end;">
<div *ngFor="let n of flatTree" class="level{{n.level - 1}}" style="margin-bottom: 8px; display: flex; align-items: stretch;">
<!--
<svg *ngIf="n.level > 1" height="30" width="25">
<line x1="0" y1="5" x2="20" y2="25" style="stroke:rgb(0,0,0);stroke-width:1" />
</svg>
-->
<app-run-box [run]="n.run" [stage]="n.stage" [flowId]="flowId" (stageRun)="onStageRun($event)" style="flex: 1;">
<app-run-box [run]="n.run"
[stage]="n.stage"
[flowId]="flowId"
(stageRun)="onStageRun($event)"
[selectionEnabled]="true"
[selected]="n.selected"
(onSelect)="changeSelection(n)" style="flex: 1;">
</app-run-box>
</div>
</div>
</div>
<div class="p-col-6">
details
<div class="p-col-7">
<div style="font-size: 2em; margin: -10px 0 0 20px;">
<span *ngIf="selectedNode.run">
Run <b>{{ selectedNode.run.label || (selectedNode.run.id + '.') }}</b> of stage
</span>
<span *ngIf="!selectedNode.run">
Stage
</span>
<b>{{ selectedNode.stage.name }}</b>
</div>
<p-tabView>
<p-tabPanel header="Overview">
<div *ngIf="selectedNode.run">
<div style="font-size: 1.3em; font-weight: bold; margin-bottom: 0.8em;">State</div>
{{ selectedNode.run.state }}
<br>
{{ selectedNode.run.created | localtime }} &rarr; {{ selectedNode.run.started | localtime }} &rarr; {{ selectedNode.run.finished | localtime }}
<br>
{{ selectedNode.run.duration }}
<div style="font-size: 1.3em; font-weight: bold; margin: 1em 0 0.8em 0;">Trigger</div>
Reason: {{ selectedNode.run.reason }}
</div>
<div *ngIf="!selectedNode.run">
Run not started
</div>
</p-tabPanel>

<p-tabPanel header="Arguments" *ngIf="selectedNode.run">
<div *ngFor="let arg of selectedNode.run.args | keyvalue" style="margin-bottom: 10px;">
{{ arg.key }}: {{ arg.value }}
</div>
</p-tabPanel>

<p-tabPanel header="Repo Trigger" *ngIf="selectedNode.run && selectedNode.run.repo_data">
<div *ngFor="let r of selectedNode.run.repo_data">
<app-repo-changes [changes]="r"></app-repo-changes>
</div>
</p-tabPanel>

<p-tabPanel header="Schema Code Dump" *ngIf="selectedNode.run && false">
{{ selectedNode.stage.schema_code }}
</p-tabPanel>
</p-tabView>
</div>
</div>
</p-tabPanel>
Expand Down Expand Up @@ -50,7 +102,8 @@
</div>
</p-tabPanel>

<p-tabPanel header="Repo Changes">
<p-tabPanel header="Repo Trigger" *ngIf="hasFlowCommits(flow)">
<app-repo-changes [changes]="flow.trigger"></app-repo-changes>
</p-tabPanel>

<p-tabPanel header="Artifacts" *ngIf="flow && flow.artifacts && ((flow.artifacts.private && flow.artifacts.private.count > 0) || (flow.artifacts.public && flow.artifacts.public.count > 0) || (flow.artifacts.report && flow.artifacts.report.count > 0))">
Expand Down
40 changes: 39 additions & 1 deletion ui/src/app/flow-results/flow-results.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { MenuItem } from 'primeng/api'
import { MessageService } from 'primeng/api'

import { AuthService } from '../auth.service'
import { humanBytes } from '../utils'
import { datetimeToLocal, humanBytes } from '../utils'
import { ManagementService } from '../backend/api/management.service'
import { ExecutionService } from '../backend/api/execution.service'
import { BreadcrumbsService } from '../breadcrumbs.service'
Expand Down Expand Up @@ -37,6 +37,17 @@ export class FlowResultsComponent implements OnInit, OnDestroy {

refreshTimer: any = null

selectedNode: any = {
stage: {
name: '',
id: null,
},
run: null,
selected: false
}
repoUrl = 'awe'
diffUrl = 'dafsd'

constructor(
private route: ActivatedRoute,
private router: Router,
Expand Down Expand Up @@ -104,6 +115,7 @@ export class FlowResultsComponent implements OnInit, OnDestroy {
data: {
stage: c,
run: this._getRunForStage(c.name),
selected: false,
},
}
if (allParents[c.name] !== undefined) {
Expand All @@ -118,10 +130,16 @@ export class FlowResultsComponent implements OnInit, OnDestroy {

_traverseTree(node, level) {
if (node.data.run || node.data.stage) {
let selected = false
if (this.selectedNode.stage.id === null) {
this.selectedNode = node.data
selected = true
}
this.flatTree.push({
level,
run: node.data.run,
stage: node.data.stage,
selected: selected,
})
}
if (node.children) {
Expand Down Expand Up @@ -324,4 +342,24 @@ export class FlowResultsComponent implements OnInit, OnDestroy {
this.loadingArtifacts = false
})
}

changeSelection(selectedNode) {
for (const node of this.flatTree) {
if (node.stage.id !== selectedNode.stage.id) {
node.selected = false
}
}
selectedNode.selected = true
this.selectedNode = selectedNode
}

hasFlowCommits(flow) {
if (flow &&
flow.trigger &&
(flow.trigger.commits || flow.trigger.pull_request)
) {
return true
}
return false
}
}
31 changes: 31 additions & 0 deletions ui/src/app/repo-changes/repo-changes.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<div>
<p>
<a href="{{ repoUrl }}" target="blank" style="font-size: 1.5em; font-weight: bold;">{{ repoUrl }}</a>
<a href="{{ diffUrl }}" target="blank" style="margin-left: 20px;" *ngIf="diffUrl">diff</a>
</p>

<div *ngIf="changes.commits">
<div *ngFor="let c of changes.commits" style="margin-bottom: 10px;">
<a href="{{ c.url || repoUrl + '/commit/' + c.commit }}" target="blank"><b>{{ c.id.slice(0,8) }}</b></a>
by <a href="mailto:{{ c.author.email }}">{{ c.author.name }}</a>
at {{ c.timestamp | localtime }}<br>
{{ c.message }}
<div style="font-size: 0.8em;">
<span *ngIf="c.modified && c.modified.length">modified {{ c.modified.length }}&nbsp;&nbsp;&nbsp;</span>
<span *ngIf="c.added && c.added.length">added {{ c.added.length }}&nbsp;&nbsp;&nbsp;</span>
<span *ngIf="c.removed && c.removed.length">removed {{ c.removed.length }}</span>
</div>
</div>
</div>

<div *ngIf="changes.pull_request">
Pull Request
<a href="{{ changes.pull_request.html_url }}" target="blank">#{{ changes.pull_request.number }}</a>
by <a href="{{ changes.pull_request.user.html_url }}" target="blank">{{ changes.pull_request.user.login }}</a> `
at {{ changes.pull_request.updated_at | localtime }}<br>
{{ changes.pull_request.title }}<br>
branch: {{ changes.pull_request.head.ref }}<br>
commits: {{ changes.pull_request.commits }}
</div>

</div>
Empty file.
25 changes: 25 additions & 0 deletions ui/src/app/repo-changes/repo-changes.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { RepoChangesComponent } from './repo-changes.component';

describe('RepoChangesComponent', () => {
let component: RepoChangesComponent;
let fixture: ComponentFixture<RepoChangesComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ RepoChangesComponent ]
})
.compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(RepoChangesComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
45 changes: 45 additions & 0 deletions ui/src/app/repo-changes/repo-changes.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Component, Input, OnInit } from '@angular/core';

@Component({
selector: 'app-repo-changes',
templateUrl: './repo-changes.component.html',
styleUrls: ['./repo-changes.component.sass']
})
export class RepoChangesComponent implements OnInit {
@Input() changes: any
@Input() repos: any

repoUrl = ''
diffUrl = ''

constructor() {
}

ngOnInit(): void {
// prepare repo url
this.repoUrl = this.changes.repo
if (this.repoUrl && this.repoUrl.endsWith('.git')) {
this.repoUrl = this.repoUrl.slice(0, -4)
}

// prepare diff url
let startCommit = ''
let lastCommit = ''
if (this.changes.commits) {
startCommit = this.changes.before
lastCommit = this.changes.after
}
if (this.changes.pull_request) {
startCommit = this.changes.pull_request.base.sha
lastCommit = this.changes.after
}
if (this.changes.commits2) {
startCommit = this.changes.commits2[this.changes.commits2.length - 1].commit
lastCommit = this.changes.commits2[0].commit
}
if (startCommit && lastCommit) {
this.diffUrl = `${this.repoUrl}/compare/${startCommit}...${lastCommit}`
}
}

}
Loading

0 comments on commit 1fcfc30

Please sign in to comment.