diff --git a/lib/platform/gitea/gitea-helper.spec.ts b/lib/platform/gitea/gitea-helper.spec.ts index e9ed3da0aa708ac93a9e23e2ba9066dcf750c883..59d02905a2e1e1d2103e996a2d2b10aaba48a48a 100644 --- a/lib/platform/gitea/gitea-helper.spec.ts +++ b/lib/platform/gitea/gitea-helper.spec.ts @@ -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'; @@ -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 = { @@ -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`, @@ -795,7 +828,7 @@ describe('platform/gitea/gitea-helper', () => { mockRepo.full_name, mockBranch.name ); - expect(res.worstStatus).toEqual(status); + expect(res.worstStatus).toEqual(expected); } }); }); diff --git a/lib/platform/gitea/gitea-helper.ts b/lib/platform/gitea/gitea-helper.ts index 218010a5cb1c113531dade78e25bcfb9361bbbc1..1b7b6566e1c598ad05bd6c3eb98955829e2de97a 100644 --- a/lib/platform/gitea/gitea-helper.ts +++ b/lib/platform/gitea/gitea-helper.ts @@ -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'; @@ -116,6 +116,7 @@ export interface CommitStatus { context: string; description: string; target_url: string; + created_at: string; } export interface CombinedCommitStatus { @@ -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, @@ -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)); }