import { getState, setState } from "windmill-client@1";
type Bitbucket = {
username: string;
password: string;
};
export async function main(
bitbucket: Bitbucket,
workspace: string,
repo: string
) {
const lastChecked: number = (await getState()) || 0;
const response = await fetch(
`https://api.bitbucket.org/2.0/repositories/${workspace}/${repo}/pullrequests`,
{
headers: {
Authorization:
"Basic " +
Buffer.from(bitbucket.username + ":" + bitbucket.password).toString(
"base64"
),
},
}
);
const data = await response.json();
if (!response.ok) {
throw new Error(data.error.message);
}
const newPullRequests = [];
for (const pullRequest of data?.values || []) {
if (new Date(pullRequest.created_on).getTime() > lastChecked) {
newPullRequests.push(pullRequest);
} else {
break;
}
}
if (newPullRequests.length > 0) {
await setState(new Date(newPullRequests[0].created_on).getTime());
}
return newPullRequests;
}
Submitted by hugo697 425 days ago