Skip to content

Commit

Permalink
fix: gitea status never green (#5776)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
NateScarlet and viceice committed Mar 26, 2020
1 parent 180cedd commit f9fe9fb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
55 changes: 44 additions & 11 deletions lib/platform/gitea/gitea-helper.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { URL } from 'url';
import { PR_STATE_CLOSED } from '../../constants/pull-requests';
import { GotResponse } from '..';
import { partial } from '../../../test/util';
import { PR_STATE_CLOSED } from '../../constants/pull-requests';
import { GiteaGotApi, GiteaGotOptions } from './gitea-got-wrapper';
import * as ght from './gitea-helper';
import { PRSearchParams } from './gitea-helper';
Expand Down Expand Up @@ -103,6 +103,7 @@ describe('platform/gitea/gitea-helper', () => {
context: 'some-context',
description: 'some-description',
target_url: 'https://gitea.renovatebot.com/commit-status',
created_at: '2020-03-25T00:00:00Z',
};

const otherMockCommitStatus: ght.CommitStatus = {
Expand Down Expand Up @@ -766,22 +767,54 @@ describe('platform/gitea/gitea-helper', () => {
});

it('should properly determine worst commit status', async () => {
const statuses: ght.CommitStatusType[] = [
'unknown',
'success',
'pending',
'warning',
'failure',
'error',
const statuses: {
status: ght.CommitStatusType;
created_at: string;
expected: ght.CommitStatusType;
}[] = [
{
status: 'unknown',
created_at: '2020-03-25T01:00:00Z',
expected: 'unknown',
},
{
status: 'pending',
created_at: '2020-03-25T03:00:00Z',
expected: 'pending',
},
{
status: 'warning',
created_at: '2020-03-25T04:00:00Z',
expected: 'warning',
},
{
status: 'failure',
created_at: '2020-03-25T05:00:00Z',
expected: 'failure',
},
{
status: 'success',
created_at: '2020-03-25T02:00:00Z',
expected: 'failure',
},
{
status: 'success',
created_at: '2020-03-25T06:00:00Z',
expected: 'success',
},
];

const commitStatuses: ght.CommitStatus[] = [
{ ...mockCommitStatus, status: 'unknown' },
];

for (const status of statuses) {
for (const { status, created_at, expected } of statuses) {
// Add current status ot list of commit statuses, then mock the API to return the whole list
commitStatuses.push({ ...mockCommitStatus, status });
commitStatuses.push({
...mockCommitStatus,
status,
created_at,
});
mockAPI<ght.CommitStatus[]>(
{
urlPattern: `/api/v1/repos/${mockRepo.full_name}/commits/${mockBranch.name}/statuses`,
Expand All @@ -795,7 +828,7 @@ describe('platform/gitea/gitea-helper', () => {
mockRepo.full_name,
mockBranch.name
);
expect(res.worstStatus).toEqual(status);
expect(res.worstStatus).toEqual(expected);
}
});
});
Expand Down
20 changes: 17 additions & 3 deletions lib/platform/gitea/gitea-helper.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { URLSearchParams } from 'url';
import { api, GiteaGotOptions } from './gitea-got-wrapper';
import { GotResponse } from '../common';
import { PR_STATE_CLOSED } from '../../constants/pull-requests';
import { BranchStatus } from '../../types';
import { GotResponse } from '../common';
import { api, GiteaGotOptions } from './gitea-got-wrapper';

export type PRState = 'open' | 'closed' | 'all';
export type IssueState = 'open' | 'closed' | 'all';
Expand Down Expand Up @@ -116,6 +116,7 @@ export interface CommitStatus {
context: string;
description: string;
target_url: string;
created_at: string;
}

export interface CombinedCommitStatus {
Expand Down Expand Up @@ -496,6 +497,19 @@ export const renovateToGiteaStatusMapping: Record<
red: 'failure',
};

function filterStatus(data: CommitStatus[]): CommitStatus[] {
const ret: Record<string, CommitStatus> = {};
for (const i of data) {
if (
!ret[i.context] ||
new Date(ret[i.context].created_at) < new Date(i.created_at)
) {
ret[i.context] = i;
}
}
return Object.values(ret);
}

export async function getCombinedCommitStatus(
repoPath: string,
branchName: string,
Expand All @@ -508,7 +522,7 @@ export async function getCombinedCommitStatus(
});

let worstState = 0;
for (const cs of res.body) {
for (const cs of filterStatus(res.body)) {
worstState = Math.max(worstState, commitStatusStates.indexOf(cs.status));
}

Expand Down

0 comments on commit f9fe9fb

Please sign in to comment.